Skip to content

Commit b1484a3

Browse files
committed
Let table AM insertion methods control index insertion
Previously, the executor did index insert unconditionally after calling table AM interface methods tuple_insert() and multi_insert(). This commit introduces the new parameter insert_indexes for these two methods. Setting '*insert_indexes' to true saves the current logic. Setting it to false indicates that table AM cares about index inserts itself and doesn't want the caller to do that. Discussion: https://postgr.es/m/CAPpHfdurb9ycV8udYqM%3Do0sPS66PJ4RCBM1g-bBpvzUfogY0EA%40mail.gmail.com Reviewed-by: Pavel Borisov, Matthias van de Meent, Mark Dilger
1 parent c95c25f commit b1484a3

File tree

12 files changed

+60
-24
lines changed

12 files changed

+60
-24
lines changed

src/backend/access/heap/heapam.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,8 @@ heap_multi_insert_pages(HeapTuple *heaptuples, int done, int ntuples, Size saveF
20882088
*/
20892089
void
20902090
heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples,
2091-
CommandId cid, int options, BulkInsertState bistate)
2091+
CommandId cid, int options, BulkInsertState bistate,
2092+
bool *insert_indexes)
20922093
{
20932094
TransactionId xid = GetCurrentTransactionId();
20942095
HeapTuple *heaptuples;
@@ -2437,6 +2438,7 @@ heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples,
24372438
slots[i]->tts_tid = heaptuples[i]->t_self;
24382439

24392440
pgstat_count_heap_insert(relation, ntuples);
2441+
*insert_indexes = true;
24402442
}
24412443

24422444
/*

src/backend/access/heap/heapam_handler.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ heapam_tuple_satisfies_snapshot(Relation rel, TupleTableSlot *slot,
245245

246246
static TupleTableSlot *
247247
heapam_tuple_insert(Relation relation, TupleTableSlot *slot, CommandId cid,
248-
int options, BulkInsertState bistate)
248+
int options, BulkInsertState bistate, bool *insert_indexes)
249249
{
250250
bool shouldFree = true;
251251
HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
@@ -261,6 +261,8 @@ heapam_tuple_insert(Relation relation, TupleTableSlot *slot, CommandId cid,
261261
if (shouldFree)
262262
pfree(tuple);
263263

264+
*insert_indexes = true;
265+
264266
return slot;
265267
}
266268

src/backend/access/table/tableam.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,11 @@ table_tuple_get_latest_tid(TableScanDesc scan, ItemPointer tid)
273273
* default command ID and not allowing access to the speedup options.
274274
*/
275275
void
276-
simple_table_tuple_insert(Relation rel, TupleTableSlot *slot)
276+
simple_table_tuple_insert(Relation rel, TupleTableSlot *slot,
277+
bool *insert_indexes)
277278
{
278-
table_tuple_insert(rel, slot, GetCurrentCommandId(true), 0, NULL);
279+
table_tuple_insert(rel, slot, GetCurrentCommandId(true), 0, NULL,
280+
insert_indexes);
279281
}
280282

281283
/*

src/backend/catalog/indexing.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,14 @@ void
273273
CatalogTuplesMultiInsertWithInfo(Relation heapRel, TupleTableSlot **slot,
274274
int ntuples, CatalogIndexState indstate)
275275
{
276+
bool insertIndexes;
277+
276278
/* Nothing to do */
277279
if (ntuples <= 0)
278280
return;
279281

280282
heap_multi_insert(heapRel, slot, ntuples,
281-
GetCurrentCommandId(true), 0, NULL);
283+
GetCurrentCommandId(true), 0, NULL, &insertIndexes);
282284

283285
/*
284286
* There is no equivalent to heap_multi_insert for the catalog indexes, so

src/backend/commands/copyfrom.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ CopyMultiInsertBufferFlush(CopyMultiInsertInfo *miinfo,
397397
bool line_buf_valid = cstate->line_buf_valid;
398398
uint64 save_cur_lineno = cstate->cur_lineno;
399399
MemoryContext oldcontext;
400+
bool insertIndexes;
400401

401402
Assert(buffer->bistate != NULL);
402403

@@ -416,7 +417,8 @@ CopyMultiInsertBufferFlush(CopyMultiInsertInfo *miinfo,
416417
nused,
417418
mycid,
418419
ti_options,
419-
buffer->bistate);
420+
buffer->bistate,
421+
&insertIndexes);
420422
MemoryContextSwitchTo(oldcontext);
421423

422424
for (i = 0; i < nused; i++)
@@ -425,7 +427,7 @@ CopyMultiInsertBufferFlush(CopyMultiInsertInfo *miinfo,
425427
* If there are any indexes, update them for all the inserted
426428
* tuples, and run AFTER ROW INSERT triggers.
427429
*/
428-
if (resultRelInfo->ri_NumIndices > 0)
430+
if (insertIndexes && resultRelInfo->ri_NumIndices > 0)
429431
{
430432
List *recheckIndexes;
431433

@@ -1265,11 +1267,14 @@ CopyFrom(CopyFromState cstate)
12651267
}
12661268
else
12671269
{
1270+
bool insertIndexes;
1271+
12681272
/* OK, store the tuple and create index entries for it */
12691273
table_tuple_insert(resultRelInfo->ri_RelationDesc,
1270-
myslot, mycid, ti_options, bistate);
1274+
myslot, mycid, ti_options, bistate,
1275+
&insertIndexes);
12711276

1272-
if (resultRelInfo->ri_NumIndices > 0)
1277+
if (insertIndexes && resultRelInfo->ri_NumIndices > 0)
12731278
recheckIndexes = ExecInsertIndexTuples(resultRelInfo,
12741279
myslot,
12751280
estate,

src/backend/commands/createas.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ static bool
578578
intorel_receive(TupleTableSlot *slot, DestReceiver *self)
579579
{
580580
DR_intorel *myState = (DR_intorel *) self;
581+
bool insertIndexes;
581582

582583
/* Nothing to insert if WITH NO DATA is specified. */
583584
if (!myState->into->skipData)
@@ -594,7 +595,8 @@ intorel_receive(TupleTableSlot *slot, DestReceiver *self)
594595
slot,
595596
myState->output_cid,
596597
myState->ti_options,
597-
myState->bistate);
598+
myState->bistate,
599+
&insertIndexes);
598600
}
599601

600602
/* We know this is a newly created relation, so there are no indexes */

src/backend/commands/matview.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ static bool
476476
transientrel_receive(TupleTableSlot *slot, DestReceiver *self)
477477
{
478478
DR_transientrel *myState = (DR_transientrel *) self;
479+
bool insertIndexes;
479480

480481
/*
481482
* Note that the input slot might not be of the type of the target
@@ -490,7 +491,8 @@ transientrel_receive(TupleTableSlot *slot, DestReceiver *self)
490491
slot,
491492
myState->output_cid,
492493
myState->ti_options,
493-
myState->bistate);
494+
myState->bistate,
495+
&insertIndexes);
494496

495497
/* We know this is a newly created relation, so there are no indexes */
496498

src/backend/commands/tablecmds.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6360,8 +6360,12 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
63606360

63616361
/* Write the tuple out to the new relation */
63626362
if (newrel)
6363+
{
6364+
bool insertIndexes;
6365+
63636366
table_tuple_insert(newrel, insertslot, mycid,
6364-
ti_options, bistate);
6367+
ti_options, bistate, &insertIndexes);
6368+
}
63656369

63666370
ResetExprContext(econtext);
63676371

src/backend/executor/execReplication.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ ExecSimpleRelationInsert(ResultRelInfo *resultRelInfo,
509509
if (!skip_tuple)
510510
{
511511
List *recheckIndexes = NIL;
512+
bool insertIndexes;
512513

513514
/* Compute stored generated columns */
514515
if (rel->rd_att->constr &&
@@ -523,9 +524,10 @@ ExecSimpleRelationInsert(ResultRelInfo *resultRelInfo,
523524
ExecPartitionCheck(resultRelInfo, slot, estate, true);
524525

525526
/* OK, store the tuple and create index entries for it */
526-
simple_table_tuple_insert(resultRelInfo->ri_RelationDesc, slot);
527+
simple_table_tuple_insert(resultRelInfo->ri_RelationDesc, slot,
528+
&insertIndexes);
527529

528-
if (resultRelInfo->ri_NumIndices > 0)
530+
if (insertIndexes && resultRelInfo->ri_NumIndices > 0)
529531
recheckIndexes = ExecInsertIndexTuples(resultRelInfo,
530532
slot, estate, false, false,
531533
NULL, NIL, false);

src/backend/executor/nodeModifyTable.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,13 +1135,15 @@ ExecInsert(ModifyTableContext *context,
11351135
}
11361136
else
11371137
{
1138+
bool insertIndexes;
1139+
11381140
/* insert the tuple normally */
11391141
slot = table_tuple_insert(resultRelationDesc, slot,
11401142
estate->es_output_cid,
1141-
0, NULL);
1143+
0, NULL, &insertIndexes);
11421144

11431145
/* insert index entries for tuple */
1144-
if (resultRelInfo->ri_NumIndices > 0)
1146+
if (insertIndexes && resultRelInfo->ri_NumIndices > 0)
11451147
recheckIndexes = ExecInsertIndexTuples(resultRelInfo,
11461148
slot, estate, false,
11471149
false, NULL, NIL,

0 commit comments

Comments
 (0)