Skip to content

Commit 3a0d473

Browse files
committed
Use wrappers of PG_DETOAST_DATUM_PACKED() more.
This makes almost all core code follow the policy introduced in the previous commit. Specific decisions: - Text search support functions with char* and length arguments, such as prsstart and lexize, may receive unaligned strings. I doubt maintainers of non-core text search code will notice. - Use plain VARDATA() on values detoasted or synthesized earlier in the same function. Use VARDATA_ANY() on varlenas sourced outside the function, even if they happen to always have four-byte headers. As an exception, retain the universal practice of using VARDATA() on return values of SendFunctionCall(). - Retain PG_GETARG_BYTEA_P() in pageinspect. (Page images are too large for a one-byte header, so this misses no optimization.) Sites that do not call get_page_from_raw() typically need the four-byte alignment. - For now, do not change btree_gist. Its use of four-byte headers in memory is partly entangled with storage of 4-byte headers inside GBT_VARKEY, on disk. - For now, do not change gtrgm_consistent() or gtrgm_distance(). They incorporate the varlena header into a cache, and there are multiple credible implementation strategies to consider.
1 parent 9d7726c commit 3a0d473

Some content is hidden

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

59 files changed

+521
-529
lines changed

contrib/adminpack/adminpack.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ pg_file_write(PG_FUNCTION_ARGS)
124124

125125
requireSuperuser();
126126

127-
filename = convert_and_check_filename(PG_GETARG_TEXT_P(0), false);
128-
data = PG_GETARG_TEXT_P(1);
127+
filename = convert_and_check_filename(PG_GETARG_TEXT_PP(0), false);
128+
data = PG_GETARG_TEXT_PP(1);
129129

130130
if (!PG_GETARG_BOOL(2))
131131
{
@@ -147,8 +147,8 @@ pg_file_write(PG_FUNCTION_ARGS)
147147
errmsg("could not open file \"%s\" for writing: %m",
148148
filename)));
149149

150-
count = fwrite(VARDATA(data), 1, VARSIZE(data) - VARHDRSZ, f);
151-
if (count != VARSIZE(data) - VARHDRSZ || FreeFile(f))
150+
count = fwrite(VARDATA_ANY(data), 1, VARSIZE_ANY_EXHDR(data), f);
151+
if (count != VARSIZE_ANY_EXHDR(data) || FreeFile(f))
152152
ereport(ERROR,
153153
(errcode_for_file_access(),
154154
errmsg("could not write file \"%s\": %m", filename)));
@@ -170,12 +170,12 @@ pg_file_rename(PG_FUNCTION_ARGS)
170170
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
171171
PG_RETURN_NULL();
172172

173-
fn1 = convert_and_check_filename(PG_GETARG_TEXT_P(0), false);
174-
fn2 = convert_and_check_filename(PG_GETARG_TEXT_P(1), false);
173+
fn1 = convert_and_check_filename(PG_GETARG_TEXT_PP(0), false);
174+
fn2 = convert_and_check_filename(PG_GETARG_TEXT_PP(1), false);
175175
if (PG_ARGISNULL(2))
176176
fn3 = 0;
177177
else
178-
fn3 = convert_and_check_filename(PG_GETARG_TEXT_P(2), false);
178+
fn3 = convert_and_check_filename(PG_GETARG_TEXT_PP(2), false);
179179

180180
if (access(fn1, W_OK) < 0)
181181
{
@@ -254,7 +254,7 @@ pg_file_unlink(PG_FUNCTION_ARGS)
254254

255255
requireSuperuser();
256256

257-
filename = convert_and_check_filename(PG_GETARG_TEXT_P(0), false);
257+
filename = convert_and_check_filename(PG_GETARG_TEXT_PP(0), false);
258258

259259
if (access(filename, W_OK) < 0)
260260
{

contrib/dblink/dblink.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,7 @@ dblink_get_pkey(PG_FUNCTION_ARGS)
15021502
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
15031503

15041504
/* open target relation */
1505-
rel = get_rel_from_relname(PG_GETARG_TEXT_P(0), AccessShareLock, ACL_SELECT);
1505+
rel = get_rel_from_relname(PG_GETARG_TEXT_PP(0), AccessShareLock, ACL_SELECT);
15061506

15071507
/* get the array of attnums */
15081508
results = get_pkey_attnames(rel, &numatts);
@@ -1603,7 +1603,7 @@ PG_FUNCTION_INFO_V1(dblink_build_sql_insert);
16031603
Datum
16041604
dblink_build_sql_insert(PG_FUNCTION_ARGS)
16051605
{
1606-
text *relname_text = PG_GETARG_TEXT_P(0);
1606+
text *relname_text = PG_GETARG_TEXT_PP(0);
16071607
int2vector *pkattnums_arg = (int2vector *) PG_GETARG_POINTER(1);
16081608
int32 pknumatts_arg = PG_GETARG_INT32(2);
16091609
ArrayType *src_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
@@ -1694,7 +1694,7 @@ PG_FUNCTION_INFO_V1(dblink_build_sql_delete);
16941694
Datum
16951695
dblink_build_sql_delete(PG_FUNCTION_ARGS)
16961696
{
1697-
text *relname_text = PG_GETARG_TEXT_P(0);
1697+
text *relname_text = PG_GETARG_TEXT_PP(0);
16981698
int2vector *pkattnums_arg = (int2vector *) PG_GETARG_POINTER(1);
16991699
int32 pknumatts_arg = PG_GETARG_INT32(2);
17001700
ArrayType *tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
@@ -1771,7 +1771,7 @@ PG_FUNCTION_INFO_V1(dblink_build_sql_update);
17711771
Datum
17721772
dblink_build_sql_update(PG_FUNCTION_ARGS)
17731773
{
1774-
text *relname_text = PG_GETARG_TEXT_P(0);
1774+
text *relname_text = PG_GETARG_TEXT_PP(0);
17751775
int2vector *pkattnums_arg = (int2vector *) PG_GETARG_POINTER(1);
17761776
int32 pknumatts_arg = PG_GETARG_INT32(2);
17771777
ArrayType *src_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
@@ -2338,7 +2338,7 @@ quote_ident_cstr(char *rawstr)
23382338
char *result;
23392339

23402340
rawstr_text = cstring_to_text(rawstr);
2341-
result_text = DatumGetTextP(DirectFunctionCall1(quote_ident,
2341+
result_text = DatumGetTextPP(DirectFunctionCall1(quote_ident,
23422342
PointerGetDatum(rawstr_text)));
23432343
result = text_to_cstring(result_text);
23442344

contrib/fuzzystrmatch/dmetaphone.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ dmetaphone(PG_FUNCTION_ARGS)
139139
if (PG_ARGISNULL(0))
140140
PG_RETURN_NULL();
141141
#endif
142-
arg = PG_GETARG_TEXT_P(0);
142+
arg = PG_GETARG_TEXT_PP(0);
143143
aptr = text_to_cstring(arg);
144144

145145
DoubleMetaphone(aptr, codes);
@@ -168,7 +168,7 @@ dmetaphone_alt(PG_FUNCTION_ARGS)
168168
if (PG_ARGISNULL(0))
169169
PG_RETURN_NULL();
170170
#endif
171-
arg = PG_GETARG_TEXT_P(0);
171+
arg = PG_GETARG_TEXT_PP(0);
172172
aptr = text_to_cstring(arg);
173173

174174
DoubleMetaphone(aptr, codes);

contrib/fuzzystrmatch/fuzzystrmatch.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ soundex(PG_FUNCTION_ARGS)
736736
char outstr[SOUNDEX_LEN + 1];
737737
char *arg;
738738

739-
arg = text_to_cstring(PG_GETARG_TEXT_P(0));
739+
arg = text_to_cstring(PG_GETARG_TEXT_PP(0));
740740

741741
_soundex(arg, outstr);
742742

@@ -802,8 +802,8 @@ difference(PG_FUNCTION_ARGS)
802802
int i,
803803
result;
804804

805-
_soundex(text_to_cstring(PG_GETARG_TEXT_P(0)), sndx1);
806-
_soundex(text_to_cstring(PG_GETARG_TEXT_P(1)), sndx2);
805+
_soundex(text_to_cstring(PG_GETARG_TEXT_PP(0)), sndx1);
806+
_soundex(text_to_cstring(PG_GETARG_TEXT_PP(1)), sndx2);
807807

808808
result = 0;
809809
for (i = 0; i < SOUNDEX_LEN; i++)

contrib/intarray/_int_op.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ Datum
200200
sort(PG_FUNCTION_ARGS)
201201
{
202202
ArrayType *a = PG_GETARG_ARRAYTYPE_P_COPY(0);
203-
text *dirstr = (fcinfo->nargs == 2) ? PG_GETARG_TEXT_P(1) : NULL;
204-
int32 dc = (dirstr) ? VARSIZE(dirstr) - VARHDRSZ : 0;
205-
char *d = (dirstr) ? VARDATA(dirstr) : NULL;
203+
text *dirstr = (fcinfo->nargs == 2) ? PG_GETARG_TEXT_PP(1) : NULL;
204+
int32 dc = (dirstr) ? VARSIZE_ANY_EXHDR(dirstr) : 0;
205+
char *d = (dirstr) ? VARDATA_ANY(dirstr) : NULL;
206206
int dir = -1;
207207

208208
CHECKARRVALID(a);

contrib/pageinspect/btreefuncs.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ GetBTPageStatistics(BlockNumber blkno, Buffer buffer, BTPageStat *stat)
159159
Datum
160160
bt_page_stats(PG_FUNCTION_ARGS)
161161
{
162-
text *relname = PG_GETARG_TEXT_P(0);
162+
text *relname = PG_GETARG_TEXT_PP(0);
163163
uint32 blkno = PG_GETARG_UINT32(1);
164164
Buffer buffer;
165165
Relation rel;
@@ -256,7 +256,7 @@ struct user_args
256256
Datum
257257
bt_page_items(PG_FUNCTION_ARGS)
258258
{
259-
text *relname = PG_GETARG_TEXT_P(0);
259+
text *relname = PG_GETARG_TEXT_PP(0);
260260
uint32 blkno = PG_GETARG_UINT32(1);
261261
Datum result;
262262
char *values[6];
@@ -408,7 +408,7 @@ bt_page_items(PG_FUNCTION_ARGS)
408408
Datum
409409
bt_metap(PG_FUNCTION_ARGS)
410410
{
411-
text *relname = PG_GETARG_TEXT_P(0);
411+
text *relname = PG_GETARG_TEXT_PP(0);
412412
Datum result;
413413
Relation rel;
414414
RangeVar *relrv;

contrib/pageinspect/rawpage.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ PG_FUNCTION_INFO_V1(get_raw_page);
4545
Datum
4646
get_raw_page(PG_FUNCTION_ARGS)
4747
{
48-
text *relname = PG_GETARG_TEXT_P(0);
48+
text *relname = PG_GETARG_TEXT_PP(0);
4949
uint32 blkno = PG_GETARG_UINT32(1);
5050
bytea *raw_page;
5151

@@ -74,8 +74,8 @@ PG_FUNCTION_INFO_V1(get_raw_page_fork);
7474
Datum
7575
get_raw_page_fork(PG_FUNCTION_ARGS)
7676
{
77-
text *relname = PG_GETARG_TEXT_P(0);
78-
text *forkname = PG_GETARG_TEXT_P(1);
77+
text *relname = PG_GETARG_TEXT_PP(0);
78+
text *forkname = PG_GETARG_TEXT_PP(1);
7979
uint32 blkno = PG_GETARG_UINT32(2);
8080
bytea *raw_page;
8181
ForkNumber forknum;
@@ -184,7 +184,7 @@ get_page_from_raw(bytea *raw_page)
184184
Page page;
185185
int raw_page_size;
186186

187-
raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
187+
raw_page_size = VARSIZE_ANY_EXHDR(raw_page);
188188

189189
if (raw_page_size != BLCKSZ)
190190
ereport(ERROR,
@@ -195,7 +195,7 @@ get_page_from_raw(bytea *raw_page)
195195

196196
page = palloc(raw_page_size);
197197

198-
memcpy(page, VARDATA(raw_page), raw_page_size);
198+
memcpy(page, VARDATA_ANY(raw_page), raw_page_size);
199199

200200
return page;
201201
}

contrib/pg_prewarm/pg_prewarm.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pg_prewarm(PG_FUNCTION_ARGS)
7979
ereport(ERROR,
8080
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
8181
(errmsg("prewarm type cannot be null"))));
82-
type = PG_GETARG_TEXT_P(1);
82+
type = PG_GETARG_TEXT_PP(1);
8383
ttype = text_to_cstring(type);
8484
if (strcmp(ttype, "prefetch") == 0)
8585
ptype = PREWARM_PREFETCH;
@@ -99,7 +99,7 @@ pg_prewarm(PG_FUNCTION_ARGS)
9999
ereport(ERROR,
100100
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
101101
(errmsg("relation fork cannot be null"))));
102-
forkName = PG_GETARG_TEXT_P(2);
102+
forkName = PG_GETARG_TEXT_PP(2);
103103
forkString = text_to_cstring(forkName);
104104
forkNumber = forkname_to_number(forkString);
105105

contrib/pg_trgm/trgm_gin.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ gin_extract_trgm(PG_FUNCTION_ARGS)
3535
Datum
3636
gin_extract_value_trgm(PG_FUNCTION_ARGS)
3737
{
38-
text *val = (text *) PG_GETARG_TEXT_P(0);
38+
text *val = (text *) PG_GETARG_TEXT_PP(0);
3939
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
4040
Datum *entries = NULL;
4141
TRGM *trg;
4242
int32 trglen;
4343

4444
*nentries = 0;
4545

46-
trg = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
46+
trg = generate_trgm(VARDATA_ANY(val), VARSIZE_ANY_EXHDR(val));
4747
trglen = ARRNELEM(trg);
4848

4949
if (trglen > 0)
@@ -70,7 +70,7 @@ gin_extract_value_trgm(PG_FUNCTION_ARGS)
7070
Datum
7171
gin_extract_query_trgm(PG_FUNCTION_ARGS)
7272
{
73-
text *val = (text *) PG_GETARG_TEXT_P(0);
73+
text *val = (text *) PG_GETARG_TEXT_PP(0);
7474
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
7575
StrategyNumber strategy = PG_GETARG_UINT16(2);
7676

@@ -90,7 +90,7 @@ gin_extract_query_trgm(PG_FUNCTION_ARGS)
9090
{
9191
case SimilarityStrategyNumber:
9292
case WordSimilarityStrategyNumber:
93-
trg = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
93+
trg = generate_trgm(VARDATA_ANY(val), VARSIZE_ANY_EXHDR(val));
9494
break;
9595
case ILikeStrategyNumber:
9696
#ifndef IGNORECASE
@@ -103,7 +103,8 @@ gin_extract_query_trgm(PG_FUNCTION_ARGS)
103103
* For wildcard search we extract all the trigrams that every
104104
* potentially-matching string must include.
105105
*/
106-
trg = generate_wildcard_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
106+
trg = generate_wildcard_trgm(VARDATA_ANY(val),
107+
VARSIZE_ANY_EXHDR(val));
107108
break;
108109
case RegExpICaseStrategyNumber:
109110
#ifndef IGNORECASE
@@ -170,7 +171,7 @@ gin_trgm_consistent(PG_FUNCTION_ARGS)
170171
bool *check = (bool *) PG_GETARG_POINTER(0);
171172
StrategyNumber strategy = PG_GETARG_UINT16(1);
172173

173-
/* text *query = PG_GETARG_TEXT_P(2); */
174+
/* text *query = PG_GETARG_TEXT_PP(2); */
174175
int32 nkeys = PG_GETARG_INT32(3);
175176
Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
176177
bool *recheck = (bool *) PG_GETARG_POINTER(5);
@@ -268,7 +269,7 @@ gin_trgm_triconsistent(PG_FUNCTION_ARGS)
268269
GinTernaryValue *check = (GinTernaryValue *) PG_GETARG_POINTER(0);
269270
StrategyNumber strategy = PG_GETARG_UINT16(1);
270271

271-
/* text *query = PG_GETARG_TEXT_P(2); */
272+
/* text *query = PG_GETARG_TEXT_PP(2); */
272273
int32 nkeys = PG_GETARG_INT32(3);
273274
Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
274275
GinTernaryValue res = GIN_MAYBE;

contrib/pg_trgm/trgm_gist.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ gtrgm_compress(PG_FUNCTION_ARGS)
100100
if (entry->leafkey)
101101
{ /* trgm */
102102
TRGM *res;
103-
text *val = DatumGetTextP(entry->key);
103+
text *val = DatumGetTextPP(entry->key);
104104

105-
res = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
105+
res = generate_trgm(VARDATA_ANY(val), VARSIZE_ANY_EXHDR(val));
106106
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
107107
gistentryinit(*retval, PointerGetDatum(res),
108108
entry->rel, entry->page,
@@ -142,7 +142,7 @@ gtrgm_decompress(PG_FUNCTION_ARGS)
142142
GISTENTRY *retval;
143143
text *key;
144144

145-
key = DatumGetTextP(entry->key);
145+
key = DatumGetTextPP(entry->key);
146146

147147
if (key != (text *) DatumGetPointer(entry->key))
148148
{
@@ -200,11 +200,12 @@ gtrgm_consistent(PG_FUNCTION_ARGS)
200200
* depends on strategy.
201201
*
202202
* The cached structure is a single palloc chunk containing the
203-
* gtrgm_consistent_cache header, then the input query (starting at a
204-
* MAXALIGN boundary), then the TRGM value (also starting at a MAXALIGN
205-
* boundary). However we don't try to include the regex graph (if any) in
206-
* that struct. (XXX currently, this approach can leak regex graphs
207-
* across index rescans. Not clear if that's worth fixing.)
203+
* gtrgm_consistent_cache header, then the input query (4-byte length
204+
* word, uncompressed, starting at a MAXALIGN boundary), then the TRGM
205+
* value (also starting at a MAXALIGN boundary). However we don't try to
206+
* include the regex graph (if any) in that struct. (XXX currently, this
207+
* approach can leak regex graphs across index rescans. Not clear if
208+
* that's worth fixing.)
208209
*/
209210
cache = (gtrgm_consistent_cache *) fcinfo->flinfo->fn_extra;
210211
if (cache == NULL ||

contrib/pg_trgm/trgm_op.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -878,14 +878,14 @@ trgm2int(trgm *ptr)
878878
Datum
879879
show_trgm(PG_FUNCTION_ARGS)
880880
{
881-
text *in = PG_GETARG_TEXT_P(0);
881+
text *in = PG_GETARG_TEXT_PP(0);
882882
TRGM *trg;
883883
Datum *d;
884884
ArrayType *a;
885885
trgm *ptr;
886886
int i;
887887

888-
trg = generate_trgm(VARDATA(in), VARSIZE(in) - VARHDRSZ);
888+
trg = generate_trgm(VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
889889
d = (Datum *) palloc(sizeof(Datum) * (1 + ARRNELEM(trg)));
890890

891891
for (i = 0, ptr = GETARR(trg); i < ARRNELEM(trg); i++, ptr++)
@@ -1053,14 +1053,14 @@ trgm_presence_map(TRGM *query, TRGM *key)
10531053
Datum
10541054
similarity(PG_FUNCTION_ARGS)
10551055
{
1056-
text *in1 = PG_GETARG_TEXT_P(0);
1057-
text *in2 = PG_GETARG_TEXT_P(1);
1056+
text *in1 = PG_GETARG_TEXT_PP(0);
1057+
text *in2 = PG_GETARG_TEXT_PP(1);
10581058
TRGM *trg1,
10591059
*trg2;
10601060
float4 res;
10611061

1062-
trg1 = generate_trgm(VARDATA(in1), VARSIZE(in1) - VARHDRSZ);
1063-
trg2 = generate_trgm(VARDATA(in2), VARSIZE(in2) - VARHDRSZ);
1062+
trg1 = generate_trgm(VARDATA_ANY(in1), VARSIZE_ANY_EXHDR(in1));
1063+
trg2 = generate_trgm(VARDATA_ANY(in2), VARSIZE_ANY_EXHDR(in2));
10641064

10651065
res = cnt_sml(trg1, trg2, false);
10661066

0 commit comments

Comments
 (0)