Thanks to visit codestin.com
Credit goes to github.com

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,

src/include/access/heapam.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ extern void heap_insert(Relation relation, HeapTuple tup, CommandId cid,
282282
int options, BulkInsertState bistate);
283283
extern void heap_multi_insert(Relation relation, struct TupleTableSlot **slots,
284284
int ntuples, CommandId cid, int options,
285-
BulkInsertState bistate);
285+
BulkInsertState bistate, bool *insert_indexes);
286286
extern TM_Result heap_delete(Relation relation, ItemPointer tid,
287287
CommandId cid, Snapshot crosscheck, int options,
288288
struct TM_FailureData *tmfd, bool changingPart,

src/include/access/tableam.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,8 @@ typedef struct TableAmRoutine
512512
/* see table_tuple_insert() for reference about parameters */
513513
TupleTableSlot *(*tuple_insert) (Relation rel, TupleTableSlot *slot,
514514
CommandId cid, int options,
515-
struct BulkInsertStateData *bistate);
515+
struct BulkInsertStateData *bistate,
516+
bool *insert_indexes);
516517

517518
/* see table_tuple_insert_speculative() for reference about parameters */
518519
void (*tuple_insert_speculative) (Relation rel,
@@ -530,7 +531,8 @@ typedef struct TableAmRoutine
530531

531532
/* see table_multi_insert() for reference about parameters */
532533
void (*multi_insert) (Relation rel, TupleTableSlot **slots, int nslots,
533-
CommandId cid, int options, struct BulkInsertStateData *bistate);
534+
CommandId cid, int options, struct BulkInsertStateData *bistate,
535+
bool *insert_indexes);
534536

535537
/* see table_tuple_delete() for reference about parameters */
536538
TM_Result (*tuple_delete) (Relation rel,
@@ -1384,6 +1386,12 @@ table_index_delete_tuples(Relation rel, TM_IndexDeleteOp *delstate)
13841386
* behavior) is also just passed through to RelationGetBufferForTuple. If
13851387
* `bistate` is provided, table_finish_bulk_insert() needs to be called.
13861388
*
1389+
* The table AM's implementation of tuple_insert should set `*insert_indexes`
1390+
* to true if it expects the caller to insert the relevant index tuples
1391+
* (as heap table AM does). It should set `*insert_indexes` to false if
1392+
* it cares about index inserts itself and doesn't want the caller to do
1393+
* index inserts.
1394+
*
13871395
* Returns the slot containing the inserted tuple, which may differ from the
13881396
* given slot. For instance, the source slot may be VirtualTupleTableSlot, but
13891397
* the result slot may correspond to the table AM. On return the slot's
@@ -1393,10 +1401,11 @@ table_index_delete_tuples(Relation rel, TM_IndexDeleteOp *delstate)
13931401
*/
13941402
static inline TupleTableSlot *
13951403
table_tuple_insert(Relation rel, TupleTableSlot *slot, CommandId cid,
1396-
int options, struct BulkInsertStateData *bistate)
1404+
int options, struct BulkInsertStateData *bistate,
1405+
bool *insert_indexes)
13971406
{
13981407
return rel->rd_tableam->tuple_insert(rel, slot, cid, options,
1399-
bistate);
1408+
bistate, insert_indexes);
14001409
}
14011410

14021411
/*
@@ -1448,10 +1457,11 @@ table_tuple_complete_speculative(Relation rel, TupleTableSlot *slot,
14481457
*/
14491458
static inline void
14501459
table_multi_insert(Relation rel, TupleTableSlot **slots, int nslots,
1451-
CommandId cid, int options, struct BulkInsertStateData *bistate)
1460+
CommandId cid, int options, struct BulkInsertStateData *bistate,
1461+
bool *insert_indexes)
14521462
{
14531463
rel->rd_tableam->multi_insert(rel, slots, nslots,
1454-
cid, options, bistate);
1464+
cid, options, bistate, insert_indexes);
14551465
}
14561466

14571467
/*
@@ -2096,7 +2106,8 @@ table_scan_sample_next_tuple(TableScanDesc scan,
20962106
* ----------------------------------------------------------------------------
20972107
*/
20982108

2099-
extern void simple_table_tuple_insert(Relation rel, TupleTableSlot *slot);
2109+
extern void simple_table_tuple_insert(Relation rel, TupleTableSlot *slot,
2110+
bool *insert_indexes);
21002111
extern void simple_table_tuple_delete(Relation rel, ItemPointer tid,
21012112
Snapshot snapshot,
21022113
TupleTableSlot *oldSlot);

0 commit comments

Comments
 (0)