Skip to content

Commit ea268cd

Browse files
committed
Add macros to make AllocSetContextCreate() calls simpler and safer.
I found that half a dozen (nearly 5%) of our AllocSetContextCreate calls had typos in the context-sizing parameters. While none of these led to especially significant problems, they did create minor inefficiencies, and it's now clear that expecting people to copy-and-paste those calls accurately is not a great idea. Let's reduce the risk of future errors by introducing single macros that encapsulate the common use-cases. Three such macros are enough to cover all but two special-purpose contexts; those two calls can be left as-is, I think. While this patch doesn't in itself improve matters for third-party extensions, it doesn't break anything for them either, and they can gradually adopt the simplified notation over time. In passing, change TopMemoryContext to use the default allocation parameters. Formerly it could only be extended 8K at a time. That was probably reasonable when this code was written; but nowadays we create many more contexts than we did then, so that it's not unusual to have a couple hundred K in TopMemoryContext, even without considering various dubious code that sticks other things there. There seems no good reason not to let it use growing blocks like most other contexts. Back-patch to 9.6, mostly because that's still close enough to HEAD that it's easy to do so, and keeping the branches in sync can be expected to avoid some future back-patching pain. The bugs fixed by these changes don't seem to be significant enough to justify fixing them further back. Discussion: <21072.1472321324@sss.pgh.pa.us>
1 parent 26fa446 commit ea268cd

Some content is hidden

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

99 files changed

+206
-522
lines changed

contrib/bloom/blinsert.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ blbuild(Relation heap, Relation index, IndexInfo *indexInfo)
130130
initBloomState(&buildstate.blstate, index);
131131
buildstate.tmpCtx = AllocSetContextCreate(CurrentMemoryContext,
132132
"Bloom build temporary context",
133-
ALLOCSET_DEFAULT_MINSIZE,
134-
ALLOCSET_DEFAULT_INITSIZE,
135-
ALLOCSET_DEFAULT_MAXSIZE);
133+
ALLOCSET_DEFAULT_SIZES);
136134
initCachedPage(&buildstate);
137135

138136
/* Do the heap scan */
@@ -204,9 +202,7 @@ blinsert(Relation index, Datum *values, bool *isnull,
204202

205203
insertCtx = AllocSetContextCreate(CurrentMemoryContext,
206204
"Bloom insert temporary context",
207-
ALLOCSET_DEFAULT_MINSIZE,
208-
ALLOCSET_DEFAULT_INITSIZE,
209-
ALLOCSET_DEFAULT_MAXSIZE);
205+
ALLOCSET_DEFAULT_SIZES);
210206

211207
oldCtx = MemoryContextSwitchTo(insertCtx);
212208

contrib/dblink/dblink.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,7 @@ materializeQueryResult(FunctionCallInfo fcinfo,
980980
/* Create short-lived memory context for data conversions */
981981
sinfo.tmpcontext = AllocSetContextCreate(CurrentMemoryContext,
982982
"dblink temporary context",
983-
ALLOCSET_DEFAULT_MINSIZE,
984-
ALLOCSET_DEFAULT_INITSIZE,
985-
ALLOCSET_DEFAULT_MAXSIZE);
983+
ALLOCSET_DEFAULT_SIZES);
986984

987985
/* execute query, collecting any tuples into the tuplestore */
988986
res = storeQueryResult(&sinfo, conn, sql);

contrib/file_fdw/file_fdw.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,9 +1061,7 @@ file_acquire_sample_rows(Relation onerel, int elevel,
10611061
*/
10621062
tupcontext = AllocSetContextCreate(CurrentMemoryContext,
10631063
"file_fdw temporary context",
1064-
ALLOCSET_DEFAULT_MINSIZE,
1065-
ALLOCSET_DEFAULT_INITSIZE,
1066-
ALLOCSET_DEFAULT_MAXSIZE);
1064+
ALLOCSET_DEFAULT_SIZES);
10671065

10681066
/* Prepare for sampling rows */
10691067
reservoir_init_selection_state(&rstate, targrows);

contrib/pg_trgm/trgm_regexp.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,7 @@ createTrgmNFA(text *text_re, Oid collation,
529529
*/
530530
tmpcontext = AllocSetContextCreate(CurrentMemoryContext,
531531
"createTrgmNFA temporary context",
532-
ALLOCSET_DEFAULT_MINSIZE,
533-
ALLOCSET_DEFAULT_INITSIZE,
534-
ALLOCSET_DEFAULT_MAXSIZE);
532+
ALLOCSET_DEFAULT_SIZES);
535533
oldcontext = MemoryContextSwitchTo(tmpcontext);
536534

537535
/*

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,14 +1315,10 @@ postgresBeginForeignScan(ForeignScanState *node, int eflags)
13151315
/* Create contexts for batches of tuples and per-tuple temp workspace. */
13161316
fsstate->batch_cxt = AllocSetContextCreate(estate->es_query_cxt,
13171317
"postgres_fdw tuple data",
1318-
ALLOCSET_DEFAULT_MINSIZE,
1319-
ALLOCSET_DEFAULT_INITSIZE,
1320-
ALLOCSET_DEFAULT_MAXSIZE);
1318+
ALLOCSET_DEFAULT_SIZES);
13211319
fsstate->temp_cxt = AllocSetContextCreate(estate->es_query_cxt,
13221320
"postgres_fdw temporary data",
1323-
ALLOCSET_SMALL_MINSIZE,
1324-
ALLOCSET_SMALL_INITSIZE,
1325-
ALLOCSET_SMALL_MAXSIZE);
1321+
ALLOCSET_SMALL_SIZES);
13261322

13271323
/*
13281324
* Get info we'll need for converting data fetched from the foreign server
@@ -1695,9 +1691,7 @@ postgresBeginForeignModify(ModifyTableState *mtstate,
16951691
/* Create context for per-tuple temp workspace. */
16961692
fmstate->temp_cxt = AllocSetContextCreate(estate->es_query_cxt,
16971693
"postgres_fdw temporary data",
1698-
ALLOCSET_SMALL_MINSIZE,
1699-
ALLOCSET_SMALL_INITSIZE,
1700-
ALLOCSET_SMALL_MAXSIZE);
1694+
ALLOCSET_SMALL_SIZES);
17011695

17021696
/* Prepare for input conversion of RETURNING results. */
17031697
if (fmstate->has_returning)
@@ -2294,9 +2288,7 @@ postgresBeginDirectModify(ForeignScanState *node, int eflags)
22942288
/* Create context for per-tuple temp workspace. */
22952289
dmstate->temp_cxt = AllocSetContextCreate(estate->es_query_cxt,
22962290
"postgres_fdw temporary data",
2297-
ALLOCSET_SMALL_MINSIZE,
2298-
ALLOCSET_SMALL_INITSIZE,
2299-
ALLOCSET_SMALL_MAXSIZE);
2291+
ALLOCSET_SMALL_SIZES);
23002292

23012293
/* Prepare for input conversion of RETURNING results. */
23022294
if (dmstate->has_returning)
@@ -3481,9 +3473,7 @@ postgresAcquireSampleRowsFunc(Relation relation, int elevel,
34813473
astate.anl_cxt = CurrentMemoryContext;
34823474
astate.temp_cxt = AllocSetContextCreate(CurrentMemoryContext,
34833475
"postgres_fdw temporary data",
3484-
ALLOCSET_SMALL_MINSIZE,
3485-
ALLOCSET_SMALL_INITSIZE,
3486-
ALLOCSET_SMALL_MAXSIZE);
3476+
ALLOCSET_SMALL_SIZES);
34873477

34883478
/*
34893479
* Get the connection to use. We do the remote access as the table's

contrib/sepgsql/uavc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,11 @@ sepgsql_avc_init(void)
498498
int rc;
499499

500500
/*
501-
* All the avc stuff shall be allocated on avc_mem_cxt
501+
* All the avc stuff shall be allocated in avc_mem_cxt
502502
*/
503503
avc_mem_cxt = AllocSetContextCreate(TopMemoryContext,
504504
"userspace access vector cache",
505-
ALLOCSET_DEFAULT_MINSIZE,
506-
ALLOCSET_DEFAULT_INITSIZE,
507-
ALLOCSET_DEFAULT_MAXSIZE);
505+
ALLOCSET_DEFAULT_SIZES);
508506
memset(avc_slots, 0, sizeof(avc_slots));
509507
avc_num_caches = 0;
510508
avc_lru_hint = 0;

contrib/test_decoding/test_decoding.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ pg_decode_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
102102
data = palloc0(sizeof(TestDecodingData));
103103
data->context = AllocSetContextCreate(ctx->context,
104104
"text conversion context",
105-
ALLOCSET_DEFAULT_MINSIZE,
106-
ALLOCSET_DEFAULT_INITSIZE,
107-
ALLOCSET_DEFAULT_MAXSIZE);
105+
ALLOCSET_DEFAULT_SIZES);
108106
data->include_xids = true;
109107
data->include_timestamp = false;
110108
data->skip_empty_xacts = false;

src/backend/access/brin/brin.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,7 @@ brininsert(Relation idxRel, Datum *values, bool *nulls,
165165
bdesc = brin_build_desc(idxRel);
166166
tupcxt = AllocSetContextCreate(CurrentMemoryContext,
167167
"brininsert cxt",
168-
ALLOCSET_DEFAULT_MINSIZE,
169-
ALLOCSET_DEFAULT_INITSIZE,
170-
ALLOCSET_DEFAULT_MAXSIZE);
168+
ALLOCSET_DEFAULT_SIZES);
171169
oldcxt = MemoryContextSwitchTo(tupcxt);
172170
}
173171

@@ -347,9 +345,7 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
347345
*/
348346
perRangeCxt = AllocSetContextCreate(CurrentMemoryContext,
349347
"bringetbitmap cxt",
350-
ALLOCSET_DEFAULT_MINSIZE,
351-
ALLOCSET_DEFAULT_INITSIZE,
352-
ALLOCSET_DEFAULT_MAXSIZE);
348+
ALLOCSET_DEFAULT_SIZES);
353349
oldcxt = MemoryContextSwitchTo(perRangeCxt);
354350

355351
/*
@@ -856,9 +852,7 @@ brin_build_desc(Relation rel)
856852

857853
cxt = AllocSetContextCreate(CurrentMemoryContext,
858854
"brin desc cxt",
859-
ALLOCSET_SMALL_INITSIZE,
860-
ALLOCSET_SMALL_MINSIZE,
861-
ALLOCSET_SMALL_MAXSIZE);
855+
ALLOCSET_SMALL_SIZES);
862856
oldcxt = MemoryContextSwitchTo(cxt);
863857
tupdesc = RelationGetDescr(rel);
864858

@@ -1169,9 +1163,7 @@ union_tuples(BrinDesc *bdesc, BrinMemTuple *a, BrinTuple *b)
11691163
/* Use our own memory context to avoid retail pfree */
11701164
cxt = AllocSetContextCreate(CurrentMemoryContext,
11711165
"brin union",
1172-
ALLOCSET_DEFAULT_MINSIZE,
1173-
ALLOCSET_DEFAULT_INITSIZE,
1174-
ALLOCSET_DEFAULT_MAXSIZE);
1166+
ALLOCSET_DEFAULT_SIZES);
11751167
oldcxt = MemoryContextSwitchTo(cxt);
11761168
db = brin_deform_tuple(bdesc, b);
11771169
MemoryContextSwitchTo(oldcxt);

src/backend/access/brin/brin_tuple.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,7 @@ brin_new_memtuple(BrinDesc *brdesc)
367367

368368
dtup->bt_context = AllocSetContextCreate(CurrentMemoryContext,
369369
"brin dtuple",
370-
ALLOCSET_DEFAULT_MINSIZE,
371-
ALLOCSET_DEFAULT_INITSIZE,
372-
ALLOCSET_DEFAULT_MAXSIZE);
370+
ALLOCSET_DEFAULT_SIZES);
373371
return dtup;
374372
}
375373

src/backend/access/common/printtup.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
135135
*/
136136
myState->tmpcontext = AllocSetContextCreate(CurrentMemoryContext,
137137
"printtup",
138-
ALLOCSET_DEFAULT_MINSIZE,
139-
ALLOCSET_DEFAULT_INITSIZE,
140-
ALLOCSET_DEFAULT_MAXSIZE);
138+
ALLOCSET_DEFAULT_SIZES);
141139

142140
if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
143141
{

0 commit comments

Comments
 (0)