Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d746021

Browse files
committed
Add construct_array_builtin, deconstruct_array_builtin
There were many calls to construct_array() and deconstruct_array() for built-in types, for example, when dealing with system catalog columns. These all hardcoded the type attributes necessary to pass to these functions. To simplify this a bit, add construct_array_builtin(), deconstruct_array_builtin() as wrappers that centralize this hardcoded knowledge. This simplifies many call sites and reduces the amount of hardcoded stuff that is spread around. Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://www.postgresql.org/message-id/flat/2914356f-9e5f-8c59-2995-5997fc48bcba%40enterprisedb.com
1 parent 7c2d6f8 commit d746021

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+284
-298
lines changed

contrib/hstore/hstore_gin.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ gin_extract_hstore_query(PG_FUNCTION_ARGS)
118118
j;
119119
text *item;
120120

121-
deconstruct_array(query,
122-
TEXTOID, -1, false, TYPALIGN_INT,
123-
&key_datums, &key_nulls, &key_count);
121+
deconstruct_array_builtin(query, TEXTOID, &key_datums, &key_nulls, &key_count);
124122

125123
entries = (Datum *) palloc(sizeof(Datum) * key_count);
126124

contrib/hstore/hstore_gist.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,7 @@ ghstore_consistent(PG_FUNCTION_ARGS)
560560
int key_count;
561561
int i;
562562

563-
deconstruct_array(query,
564-
TEXTOID, -1, false, TYPALIGN_INT,
565-
&key_datums, &key_nulls, &key_count);
563+
deconstruct_array_builtin(query, TEXTOID, &key_datums, &key_nulls, &key_count);
566564

567565
for (i = 0; res && i < key_count; ++i)
568566
{
@@ -583,9 +581,7 @@ ghstore_consistent(PG_FUNCTION_ARGS)
583581
int key_count;
584582
int i;
585583

586-
deconstruct_array(query,
587-
TEXTOID, -1, false, TYPALIGN_INT,
588-
&key_datums, &key_nulls, &key_count);
584+
deconstruct_array_builtin(query, TEXTOID, &key_datums, &key_nulls, &key_count);
589585

590586
res = false;
591587

contrib/hstore/hstore_io.c

+3-9
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,7 @@ hstore_from_arrays(PG_FUNCTION_ARGS)
567567
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
568568
errmsg("wrong number of array subscripts")));
569569

570-
deconstruct_array(key_array,
571-
TEXTOID, -1, false, TYPALIGN_INT,
572-
&key_datums, &key_nulls, &key_count);
570+
deconstruct_array_builtin(key_array, TEXTOID, &key_datums, &key_nulls, &key_count);
573571

574572
/* see discussion in hstoreArrayToPairs() */
575573
if (key_count > MaxAllocSize / sizeof(Pairs))
@@ -606,9 +604,7 @@ hstore_from_arrays(PG_FUNCTION_ARGS)
606604
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
607605
errmsg("arrays must have same bounds")));
608606

609-
deconstruct_array(value_array,
610-
TEXTOID, -1, false, TYPALIGN_INT,
611-
&value_datums, &value_nulls, &value_count);
607+
deconstruct_array_builtin(value_array, TEXTOID, &value_datums, &value_nulls, &value_count);
612608

613609
Assert(key_count == value_count);
614610
}
@@ -696,9 +692,7 @@ hstore_from_array(PG_FUNCTION_ARGS)
696692
errmsg("wrong number of array subscripts")));
697693
}
698694

699-
deconstruct_array(in_array,
700-
TEXTOID, -1, false, TYPALIGN_INT,
701-
&in_datums, &in_nulls, &in_count);
695+
deconstruct_array_builtin(in_array, TEXTOID, &in_datums, &in_nulls, &in_count);
702696

703697
count = in_count / 2;
704698

contrib/hstore/hstore_op.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ hstoreArrayToPairs(ArrayType *a, int *npairs)
8080
int i,
8181
j;
8282

83-
deconstruct_array(a,
84-
TEXTOID, -1, false, TYPALIGN_INT,
85-
&key_datums, &key_nulls, &key_count);
83+
deconstruct_array_builtin(a, TEXTOID, &key_datums, &key_nulls, &key_count);
8684

8785
if (key_count == 0)
8886
{
@@ -582,9 +580,7 @@ hstore_slice_to_array(PG_FUNCTION_ARGS)
582580
int key_count;
583581
int i;
584582

585-
deconstruct_array(key_array,
586-
TEXTOID, -1, false, TYPALIGN_INT,
587-
&key_datums, &key_nulls, &key_count);
583+
deconstruct_array_builtin(key_array, TEXTOID, &key_datums, &key_nulls, &key_count);
588584

589585
if (key_count == 0)
590586
{
@@ -719,8 +715,7 @@ hstore_akeys(PG_FUNCTION_ARGS)
719715
d[i] = PointerGetDatum(t);
720716
}
721717

722-
a = construct_array(d, count,
723-
TEXTOID, -1, false, TYPALIGN_INT);
718+
a = construct_array_builtin(d, count, TEXTOID);
724719

725720
PG_RETURN_POINTER(a);
726721
}

contrib/pageinspect/btreefuncs.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,7 @@ bt_page_print_tuples(struct user_args *uargs)
425425
tids_datum = (Datum *) palloc(nposting * sizeof(Datum));
426426
for (int i = 0; i < nposting; i++)
427427
tids_datum[i] = ItemPointerGetDatum(&tids[i]);
428-
values[j++] = PointerGetDatum(construct_array(tids_datum,
429-
nposting,
430-
TIDOID,
431-
sizeof(ItemPointerData),
432-
false, TYPALIGN_SHORT));
428+
values[j++] = PointerGetDatum(construct_array_builtin(tids_datum, nposting, TIDOID));
433429
pfree(tids_datum);
434430
}
435431
else

contrib/pageinspect/ginfuncs.c

+2-8
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,7 @@ gin_page_opaque_info(PG_FUNCTION_ARGS)
166166

167167
values[0] = Int64GetDatum(opaq->rightlink);
168168
values[1] = Int32GetDatum(opaq->maxoff);
169-
values[2] = PointerGetDatum(construct_array(flags, nflags,
170-
TEXTOID,
171-
-1, false, TYPALIGN_INT));
169+
values[2] = PointerGetDatum(construct_array_builtin(flags, nflags, TEXTOID));
172170

173171
/* Build and return the result tuple. */
174172
resultTuple = heap_form_tuple(tupdesc, values, nulls);
@@ -273,11 +271,7 @@ gin_leafpage_items(PG_FUNCTION_ARGS)
273271
tids_datum = (Datum *) palloc(ndecoded * sizeof(Datum));
274272
for (i = 0; i < ndecoded; i++)
275273
tids_datum[i] = ItemPointerGetDatum(&tids[i]);
276-
values[2] = PointerGetDatum(construct_array(tids_datum,
277-
ndecoded,
278-
TIDOID,
279-
sizeof(ItemPointerData),
280-
false, TYPALIGN_SHORT));
274+
values[2] = PointerGetDatum(construct_array_builtin(tids_datum, ndecoded, TIDOID));
281275
pfree(tids_datum);
282276
pfree(tids);
283277

contrib/pageinspect/gistfuncs.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ gist_page_opaque_info(PG_FUNCTION_ARGS)
104104
values[0] = LSNGetDatum(PageGetLSN(page));
105105
values[1] = LSNGetDatum(GistPageGetNSN(page));
106106
values[2] = Int64GetDatum(opaq->rightlink);
107-
values[3] = PointerGetDatum(construct_array(flags, nflags,
108-
TEXTOID,
109-
-1, false, TYPALIGN_INT));
107+
values[3] = PointerGetDatum(construct_array_builtin(flags, nflags, TEXTOID));
110108

111109
/* Build and return the result tuple. */
112110
resultTuple = heap_form_tuple(tupdesc, values, nulls);

contrib/pageinspect/hashfuncs.c

+2-12
Original file line numberDiff line numberDiff line change
@@ -564,21 +564,11 @@ hash_metapage_info(PG_FUNCTION_ARGS)
564564

565565
for (i = 0; i < HASH_MAX_SPLITPOINTS; i++)
566566
spares[i] = Int64GetDatum((int64) metad->hashm_spares[i]);
567-
values[j++] = PointerGetDatum(construct_array(spares,
568-
HASH_MAX_SPLITPOINTS,
569-
INT8OID,
570-
sizeof(int64),
571-
FLOAT8PASSBYVAL,
572-
TYPALIGN_DOUBLE));
567+
values[j++] = PointerGetDatum(construct_array_builtin(spares, HASH_MAX_SPLITPOINTS, INT8OID));
573568

574569
for (i = 0; i < HASH_MAX_BITMAPS; i++)
575570
mapp[i] = Int64GetDatum((int64) metad->hashm_mapp[i]);
576-
values[j++] = PointerGetDatum(construct_array(mapp,
577-
HASH_MAX_BITMAPS,
578-
INT8OID,
579-
sizeof(int64),
580-
FLOAT8PASSBYVAL,
581-
TYPALIGN_DOUBLE));
571+
values[j++] = PointerGetDatum(construct_array_builtin(mapp, HASH_MAX_BITMAPS, INT8OID));
582572

583573
tuple = heap_form_tuple(tupleDesc, values, nulls);
584574

contrib/pageinspect/heapfuncs.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ heap_tuple_infomask_flags(PG_FUNCTION_ARGS)
590590

591591
/* build value */
592592
Assert(cnt <= bitcnt);
593-
a = construct_array(flags, cnt, TEXTOID, -1, false, TYPALIGN_INT);
593+
a = construct_array_builtin(flags, cnt, TEXTOID);
594594
values[0] = PointerGetDatum(a);
595595

596596
/*
@@ -612,7 +612,7 @@ heap_tuple_infomask_flags(PG_FUNCTION_ARGS)
612612
if (cnt == 0)
613613
a = construct_empty_array(TEXTOID);
614614
else
615-
a = construct_array(flags, cnt, TEXTOID, -1, false, TYPALIGN_INT);
615+
a = construct_array_builtin(flags, cnt, TEXTOID);
616616
pfree(flags);
617617
values[1] = PointerGetDatum(a);
618618

contrib/pg_trgm/trgm_op.c

+1-6
Original file line numberDiff line numberDiff line change
@@ -977,12 +977,7 @@ show_trgm(PG_FUNCTION_ARGS)
977977
d[i] = PointerGetDatum(item);
978978
}
979979

980-
a = construct_array(d,
981-
ARRNELEM(trg),
982-
TEXTOID,
983-
-1,
984-
false,
985-
TYPALIGN_INT);
980+
a = construct_array_builtin(d, ARRNELEM(trg), TEXTOID);
986981

987982
for (i = 0; i < ARRNELEM(trg); i++)
988983
pfree(DatumGetPointer(d[i]));

contrib/pgcrypto/pgp-pgsql.c

+2-7
Original file line numberDiff line numberDiff line change
@@ -774,13 +774,8 @@ parse_key_value_arrays(ArrayType *key_array, ArrayType *val_array,
774774
if (nkdims == 0)
775775
return 0;
776776

777-
deconstruct_array(key_array,
778-
TEXTOID, -1, false, TYPALIGN_INT,
779-
&key_datums, &key_nulls, &key_count);
780-
781-
deconstruct_array(val_array,
782-
TEXTOID, -1, false, TYPALIGN_INT,
783-
&val_datums, &val_nulls, &val_count);
777+
deconstruct_array_builtin(key_array, TEXTOID, &key_datums, &key_nulls, &key_count);
778+
deconstruct_array_builtin(val_array, TEXTOID, &val_datums, &val_nulls, &val_count);
784779

785780
if (key_count != val_count)
786781
ereport(ERROR,

src/backend/access/common/reloptions.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -1177,8 +1177,7 @@ transformRelOptions(Datum oldOptions, List *defList, const char *namspace,
11771177
int noldoptions;
11781178
int i;
11791179

1180-
deconstruct_array(array, TEXTOID, -1, false, TYPALIGN_INT,
1181-
&oldoptions, NULL, &noldoptions);
1180+
deconstruct_array_builtin(array, TEXTOID, &oldoptions, NULL, &noldoptions);
11821181

11831182
for (i = 0; i < noldoptions; i++)
11841183
{
@@ -1345,8 +1344,7 @@ untransformRelOptions(Datum options)
13451344

13461345
array = DatumGetArrayTypeP(options);
13471346

1348-
deconstruct_array(array, TEXTOID, -1, false, TYPALIGN_INT,
1349-
&optiondatums, NULL, &noptions);
1347+
deconstruct_array_builtin(array, TEXTOID, &optiondatums, NULL, &noptions);
13501348

13511349
for (i = 0; i < noptions; i++)
13521350
{
@@ -1436,8 +1434,7 @@ parseRelOptionsInternal(Datum options, bool validate,
14361434
int noptions;
14371435
int i;
14381436

1439-
deconstruct_array(array, TEXTOID, -1, false, TYPALIGN_INT,
1440-
&optiondatums, NULL, &noptions);
1437+
deconstruct_array_builtin(array, TEXTOID, &optiondatums, NULL, &noptions);
14411438

14421439
for (i = 0; i < noptions; i++)
14431440
{

src/backend/catalog/objectaddress.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -2099,8 +2099,7 @@ textarray_to_strvaluelist(ArrayType *arr)
20992099
List *list = NIL;
21002100
int i;
21012101

2102-
deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
2103-
&elems, &nulls, &nelems);
2102+
deconstruct_array_builtin(arr, TEXTOID, &elems, &nulls, &nelems);
21042103

21052104
for (i = 0; i < nelems; i++)
21062105
{
@@ -2156,8 +2155,7 @@ pg_get_object_address(PG_FUNCTION_ARGS)
21562155
bool *nulls;
21572156
int nelems;
21582157

2159-
deconstruct_array(namearr, TEXTOID, -1, false, TYPALIGN_INT,
2160-
&elems, &nulls, &nelems);
2158+
deconstruct_array_builtin(namearr, TEXTOID, &elems, &nulls, &nelems);
21612159
if (nelems != 1)
21622160
ereport(ERROR,
21632161
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@@ -2174,8 +2172,7 @@ pg_get_object_address(PG_FUNCTION_ARGS)
21742172
bool *nulls;
21752173
int nelems;
21762174

2177-
deconstruct_array(namearr, TEXTOID, -1, false, TYPALIGN_INT,
2178-
&elems, &nulls, &nelems);
2175+
deconstruct_array_builtin(namearr, TEXTOID, &elems, &nulls, &nelems);
21792176
if (nelems != 1)
21802177
ereport(ERROR,
21812178
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@@ -2213,8 +2210,7 @@ pg_get_object_address(PG_FUNCTION_ARGS)
22132210
int nelems;
22142211
int i;
22152212

2216-
deconstruct_array(argsarr, TEXTOID, -1, false, TYPALIGN_INT,
2217-
&elems, &nulls, &nelems);
2213+
deconstruct_array_builtin(argsarr, TEXTOID, &elems, &nulls, &nelems);
22182214

22192215
args = NIL;
22202216
for (i = 0; i < nelems; i++)

src/backend/catalog/pg_constraint.c

+7-14
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ CreateConstraintEntry(const char *constraintName,
112112
conkey = (Datum *) palloc(constraintNKeys * sizeof(Datum));
113113
for (i = 0; i < constraintNKeys; i++)
114114
conkey[i] = Int16GetDatum(constraintKey[i]);
115-
conkeyArray = construct_array(conkey, constraintNKeys,
116-
INT2OID, 2, true, TYPALIGN_SHORT);
115+
conkeyArray = construct_array_builtin(conkey, constraintNKeys, INT2OID);
117116
}
118117
else
119118
conkeyArray = NULL;
@@ -125,27 +124,22 @@ CreateConstraintEntry(const char *constraintName,
125124
fkdatums = (Datum *) palloc(foreignNKeys * sizeof(Datum));
126125
for (i = 0; i < foreignNKeys; i++)
127126
fkdatums[i] = Int16GetDatum(foreignKey[i]);
128-
confkeyArray = construct_array(fkdatums, foreignNKeys,
129-
INT2OID, 2, true, TYPALIGN_SHORT);
127+
confkeyArray = construct_array_builtin(fkdatums, foreignNKeys, INT2OID);
130128
for (i = 0; i < foreignNKeys; i++)
131129
fkdatums[i] = ObjectIdGetDatum(pfEqOp[i]);
132-
conpfeqopArray = construct_array(fkdatums, foreignNKeys,
133-
OIDOID, sizeof(Oid), true, TYPALIGN_INT);
130+
conpfeqopArray = construct_array_builtin(fkdatums, foreignNKeys, OIDOID);
134131
for (i = 0; i < foreignNKeys; i++)
135132
fkdatums[i] = ObjectIdGetDatum(ppEqOp[i]);
136-
conppeqopArray = construct_array(fkdatums, foreignNKeys,
137-
OIDOID, sizeof(Oid), true, TYPALIGN_INT);
133+
conppeqopArray = construct_array_builtin(fkdatums, foreignNKeys, OIDOID);
138134
for (i = 0; i < foreignNKeys; i++)
139135
fkdatums[i] = ObjectIdGetDatum(ffEqOp[i]);
140-
conffeqopArray = construct_array(fkdatums, foreignNKeys,
141-
OIDOID, sizeof(Oid), true, TYPALIGN_INT);
136+
conffeqopArray = construct_array_builtin(fkdatums, foreignNKeys, OIDOID);
142137

143138
if (numFkDeleteSetCols > 0)
144139
{
145140
for (i = 0; i < numFkDeleteSetCols; i++)
146141
fkdatums[i] = Int16GetDatum(fkDeleteSetCols[i]);
147-
confdelsetcolsArray = construct_array(fkdatums, numFkDeleteSetCols,
148-
INT2OID, 2, true, TYPALIGN_SHORT);
142+
confdelsetcolsArray = construct_array_builtin(fkdatums, numFkDeleteSetCols, INT2OID);
149143
}
150144
else
151145
confdelsetcolsArray = NULL;
@@ -166,8 +160,7 @@ CreateConstraintEntry(const char *constraintName,
166160
opdatums = (Datum *) palloc(constraintNKeys * sizeof(Datum));
167161
for (i = 0; i < constraintNKeys; i++)
168162
opdatums[i] = ObjectIdGetDatum(exclOp[i]);
169-
conexclopArray = construct_array(opdatums, constraintNKeys,
170-
OIDOID, sizeof(Oid), true, TYPALIGN_INT);
163+
conexclopArray = construct_array_builtin(opdatums, constraintNKeys, OIDOID);
171164
}
172165
else
173166
conexclopArray = NULL;

src/backend/catalog/pg_proc.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1193,10 +1193,7 @@ oid_array_to_list(Datum datum)
11931193
int i;
11941194
List *result = NIL;
11951195

1196-
deconstruct_array(array,
1197-
OIDOID,
1198-
sizeof(Oid), true, TYPALIGN_INT,
1199-
&values, NULL, &nelems);
1196+
deconstruct_array_builtin(array, OIDOID, &values, NULL, &nelems);
12001197
for (i = 0; i < nelems; i++)
12011198
result = lappend_oid(result, values[i]);
12021199
return result;

src/backend/catalog/pg_subscription.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,7 @@ textarray_to_stringlist(ArrayType *textarray)
260260
i;
261261
List *res = NIL;
262262

263-
deconstruct_array(textarray,
264-
TEXTOID, -1, false, TYPALIGN_INT,
265-
&elems, NULL, &nelems);
263+
deconstruct_array_builtin(textarray, TEXTOID, &elems, NULL, &nelems);
266264

267265
if (nelems == 0)
268266
return NIL;

src/backend/commands/analyze.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1688,10 +1688,7 @@ update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats)
16881688

16891689
for (n = 0; n < nnum; n++)
16901690
numdatums[n] = Float4GetDatum(stats->stanumbers[k][n]);
1691-
/* XXX knows more than it should about type float4: */
1692-
arry = construct_array(numdatums, nnum,
1693-
FLOAT4OID,
1694-
sizeof(float4), true, TYPALIGN_INT);
1691+
arry = construct_array_builtin(numdatums, nnum, FLOAT4OID);
16951692
values[i++] = PointerGetDatum(arry); /* stanumbersN */
16961693
}
16971694
else

src/backend/commands/event_trigger.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ filter_list_to_array(List *filterlist)
351351
pfree(result);
352352
}
353353

354-
return PointerGetDatum(construct_array(data, l, TEXTOID,
355-
-1, false, TYPALIGN_INT));
354+
return PointerGetDatum(construct_array_builtin(data, l, TEXTOID));
356355
}
357356

358357
/*

0 commit comments

Comments
 (0)