PostgreSQL Source Code  git master
execIndexing.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/relscan.h"
#include "access/tableam.h"
#include "access/xact.h"
#include "catalog/index.h"
#include "executor/executor.h"
#include "nodes/nodeFuncs.h"
#include "storage/lmgr.h"
#include "utils/snapmgr.h"
Include dependency graph for execIndexing.c:

Go to the source code of this file.

Enumerations

enum  CEOUC_WAIT_MODE { CEOUC_WAIT , CEOUC_NOWAIT , CEOUC_LIVELOCK_PREVENTING_WAIT }
 

Functions

static bool check_exclusion_or_unique_constraint (Relation heap, Relation index, IndexInfo *indexInfo, ItemPointer tupleid, const Datum *values, const bool *isnull, EState *estate, bool newIndex, CEOUC_WAIT_MODE waitMode, bool violationOK, ItemPointer conflictTid)
 
static bool index_recheck_constraint (Relation index, const Oid *constr_procs, const Datum *existing_values, const bool *existing_isnull, const Datum *new_values)
 
static bool index_unchanged_by_update (ResultRelInfo *resultRelInfo, EState *estate, IndexInfo *indexInfo, Relation indexRelation)
 
static bool index_expression_changed_walker (Node *node, Bitmapset *allUpdatedCols)
 
void ExecOpenIndices (ResultRelInfo *resultRelInfo, bool speculative)
 
void ExecCloseIndices (ResultRelInfo *resultRelInfo)
 
ListExecInsertIndexTuples (ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, bool update, bool noDupErr, bool *specConflict, List *arbiterIndexes, bool onlySummarizing)
 
bool ExecCheckIndexConstraints (ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, ItemPointer conflictTid, ItemPointer tupleid, List *arbiterIndexes)
 
void check_exclusion_constraint (Relation heap, Relation index, IndexInfo *indexInfo, ItemPointer tupleid, const Datum *values, const bool *isnull, EState *estate, bool newIndex)
 

Enumeration Type Documentation

◆ CEOUC_WAIT_MODE

Enumerator
CEOUC_WAIT 
CEOUC_NOWAIT 
CEOUC_LIVELOCK_PREVENTING_WAIT 

Definition at line 120 of file execIndexing.c.

121 {
122  CEOUC_WAIT,
123  CEOUC_NOWAIT,
CEOUC_WAIT_MODE
Definition: execIndexing.c:121
@ CEOUC_NOWAIT
Definition: execIndexing.c:123
@ CEOUC_WAIT
Definition: execIndexing.c:122
@ CEOUC_LIVELOCK_PREVENTING_WAIT
Definition: execIndexing.c:124

Function Documentation

◆ check_exclusion_constraint()

void check_exclusion_constraint ( Relation  heap,
Relation  index,
IndexInfo indexInfo,
ItemPointer  tupleid,
const Datum values,
const bool isnull,
EState estate,
bool  newIndex 
)

Definition at line 920 of file execIndexing.c.

925 {
926  (void) check_exclusion_or_unique_constraint(heap, index, indexInfo, tupleid,
927  values, isnull,
928  estate, newIndex,
929  CEOUC_WAIT, false, NULL);
930 }
static Datum values[MAXATTR]
Definition: bootstrap.c:150
static bool check_exclusion_or_unique_constraint(Relation heap, Relation index, IndexInfo *indexInfo, ItemPointer tupleid, const Datum *values, const bool *isnull, EState *estate, bool newIndex, CEOUC_WAIT_MODE waitMode, bool violationOK, ItemPointer conflictTid)
Definition: execIndexing.c:694
Definition: type.h:95

References CEOUC_WAIT, check_exclusion_or_unique_constraint(), and values.

Referenced by IndexCheckExclusion().

◆ check_exclusion_or_unique_constraint()

static bool check_exclusion_or_unique_constraint ( Relation  heap,
Relation  index,
IndexInfo indexInfo,
ItemPointer  tupleid,
const Datum values,
const bool isnull,
EState estate,
bool  newIndex,
CEOUC_WAIT_MODE  waitMode,
bool  violationOK,
ItemPointer  conflictTid 
)
static

Definition at line 694 of file execIndexing.c.

702 {
703  Oid *constr_procs;
704  uint16 *constr_strats;
705  Oid *index_collations = index->rd_indcollation;
706  int indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index);
707  IndexScanDesc index_scan;
708  ScanKeyData scankeys[INDEX_MAX_KEYS];
709  SnapshotData DirtySnapshot;
710  int i;
711  bool conflict;
712  bool found_self;
713  ExprContext *econtext;
714  TupleTableSlot *existing_slot;
715  TupleTableSlot *save_scantuple;
716 
717  if (indexInfo->ii_ExclusionOps)
718  {
719  constr_procs = indexInfo->ii_ExclusionProcs;
720  constr_strats = indexInfo->ii_ExclusionStrats;
721  }
722  else
723  {
724  constr_procs = indexInfo->ii_UniqueProcs;
725  constr_strats = indexInfo->ii_UniqueStrats;
726  }
727 
728  /*
729  * If any of the input values are NULL, and the index uses the default
730  * nulls-are-distinct mode, the constraint check is assumed to pass (i.e.,
731  * we assume the operators are strict). Otherwise, we interpret the
732  * constraint as specifying IS NULL for each column whose input value is
733  * NULL.
734  */
735  if (!indexInfo->ii_NullsNotDistinct)
736  {
737  for (i = 0; i < indnkeyatts; i++)
738  {
739  if (isnull[i])
740  return true;
741  }
742  }
743 
744  /*
745  * Search the tuples that are in the index for any violations, including
746  * tuples that aren't visible yet.
747  */
748  InitDirtySnapshot(DirtySnapshot);
749 
750  for (i = 0; i < indnkeyatts; i++)
751  {
752  ScanKeyEntryInitialize(&scankeys[i],
753  isnull[i] ? SK_ISNULL | SK_SEARCHNULL : 0,
754  i + 1,
755  constr_strats[i],
756  InvalidOid,
757  index_collations[i],
758  constr_procs[i],
759  values[i]);
760  }
761 
762  /*
763  * Need a TupleTableSlot to put existing tuples in.
764  *
765  * To use FormIndexDatum, we have to make the econtext's scantuple point
766  * to this slot. Be sure to save and restore caller's value for
767  * scantuple.
768  */
769  existing_slot = table_slot_create(heap, NULL);
770 
771  econtext = GetPerTupleExprContext(estate);
772  save_scantuple = econtext->ecxt_scantuple;
773  econtext->ecxt_scantuple = existing_slot;
774 
775  /*
776  * May have to restart scan from this point if a potential conflict is
777  * found.
778  */
779 retry:
780  conflict = false;
781  found_self = false;
782  index_scan = index_beginscan(heap, index, &DirtySnapshot, indnkeyatts, 0);
783  index_rescan(index_scan, scankeys, indnkeyatts, NULL, 0);
784 
785  while (index_getnext_slot(index_scan, ForwardScanDirection, existing_slot))
786  {
787  TransactionId xwait;
788  XLTW_Oper reason_wait;
789  Datum existing_values[INDEX_MAX_KEYS];
790  bool existing_isnull[INDEX_MAX_KEYS];
791  char *error_new;
792  char *error_existing;
793 
794  /*
795  * Ignore the entry for the tuple we're trying to check.
796  */
797  if (ItemPointerIsValid(tupleid) &&
798  ItemPointerEquals(tupleid, &existing_slot->tts_tid))
799  {
800  if (found_self) /* should not happen */
801  elog(ERROR, "found self tuple multiple times in index \"%s\"",
803  found_self = true;
804  continue;
805  }
806 
807  /*
808  * Extract the index column values and isnull flags from the existing
809  * tuple.
810  */
811  FormIndexDatum(indexInfo, existing_slot, estate,
812  existing_values, existing_isnull);
813 
814  /* If lossy indexscan, must recheck the condition */
815  if (index_scan->xs_recheck)
816  {
818  constr_procs,
819  existing_values,
820  existing_isnull,
821  values))
822  continue; /* tuple doesn't actually match, so no
823  * conflict */
824  }
825 
826  /*
827  * At this point we have either a conflict or a potential conflict.
828  *
829  * If an in-progress transaction is affecting the visibility of this
830  * tuple, we need to wait for it to complete and then recheck (unless
831  * the caller requested not to). For simplicity we do rechecking by
832  * just restarting the whole scan --- this case probably doesn't
833  * happen often enough to be worth trying harder, and anyway we don't
834  * want to hold any index internal locks while waiting.
835  */
836  xwait = TransactionIdIsValid(DirtySnapshot.xmin) ?
837  DirtySnapshot.xmin : DirtySnapshot.xmax;
838 
839  if (TransactionIdIsValid(xwait) &&
840  (waitMode == CEOUC_WAIT ||
841  (waitMode == CEOUC_LIVELOCK_PREVENTING_WAIT &&
842  DirtySnapshot.speculativeToken &&
844  {
845  reason_wait = indexInfo->ii_ExclusionOps ?
847  index_endscan(index_scan);
848  if (DirtySnapshot.speculativeToken)
849  SpeculativeInsertionWait(DirtySnapshot.xmin,
850  DirtySnapshot.speculativeToken);
851  else
852  XactLockTableWait(xwait, heap,
853  &existing_slot->tts_tid, reason_wait);
854  goto retry;
855  }
856 
857  /*
858  * We have a definite conflict (or a potential one, but the caller
859  * didn't want to wait). Return it to caller, or report it.
860  */
861  if (violationOK)
862  {
863  conflict = true;
864  if (conflictTid)
865  *conflictTid = existing_slot->tts_tid;
866  break;
867  }
868 
869  error_new = BuildIndexValueDescription(index, values, isnull);
870  error_existing = BuildIndexValueDescription(index, existing_values,
871  existing_isnull);
872  if (newIndex)
873  ereport(ERROR,
874  (errcode(ERRCODE_EXCLUSION_VIOLATION),
875  errmsg("could not create exclusion constraint \"%s\"",
877  error_new && error_existing ?
878  errdetail("Key %s conflicts with key %s.",
879  error_new, error_existing) :
880  errdetail("Key conflicts exist."),
881  errtableconstraint(heap,
883  else
884  ereport(ERROR,
885  (errcode(ERRCODE_EXCLUSION_VIOLATION),
886  errmsg("conflicting key value violates exclusion constraint \"%s\"",
888  error_new && error_existing ?
889  errdetail("Key %s conflicts with existing key %s.",
890  error_new, error_existing) :
891  errdetail("Key conflicts with existing key."),
892  errtableconstraint(heap,
894  }
895 
896  index_endscan(index_scan);
897 
898  /*
899  * Ordinarily, at this point the search should have found the originally
900  * inserted tuple (if any), unless we exited the loop early because of
901  * conflict. However, it is possible to define exclusion constraints for
902  * which that wouldn't be true --- for instance, if the operator is <>. So
903  * we no longer complain if found_self is still false.
904  */
905 
906  econtext->ecxt_scantuple = save_scantuple;
907 
908  ExecDropSingleTupleTableSlot(existing_slot);
909 
910  return !conflict;
911 }
unsigned short uint16
Definition: c.h:505
uint32 TransactionId
Definition: c.h:652
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
static bool index_recheck_constraint(Relation index, const Oid *constr_procs, const Datum *existing_values, const bool *existing_isnull, const Datum *new_values)
Definition: execIndexing.c:937
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1341
#define GetPerTupleExprContext(estate)
Definition: executor.h:561
char * BuildIndexValueDescription(Relation indexRelation, const Datum *values, const bool *isnull)
Definition: genam.c:175
void FormIndexDatum(IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
Definition: index.c:2703
bool index_getnext_slot(IndexScanDesc scan, ScanDirection direction, TupleTableSlot *slot)
Definition: indexam.c:673
IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys)
Definition: indexam.c:256
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:378
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:352
int i
Definition: isn.c:73
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:35
static bool ItemPointerIsValid(const ItemPointerData *pointer)
Definition: itemptr.h:83
void XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid, XLTW_Oper oper)
Definition: lmgr.c:657
void SpeculativeInsertionWait(TransactionId xid, uint32 token)
Definition: lmgr.c:814
XLTW_Oper
Definition: lmgr.h:25
@ XLTW_InsertIndex
Definition: lmgr.h:31
@ XLTW_RecheckExclusionConstr
Definition: lmgr.h:34
#define INDEX_MAX_KEYS
uintptr_t Datum
Definition: postgres.h:64
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition: rel.h:524
int errtableconstraint(Relation rel, const char *conname)
Definition: relcache.c:6006
void ScanKeyEntryInitialize(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, RegProcedure procedure, Datum argument)
Definition: scankey.c:32
@ ForwardScanDirection
Definition: sdir.h:28
#define SK_SEARCHNULL
Definition: skey.h:121
#define SK_ISNULL
Definition: skey.h:115
#define InitDirtySnapshot(snapshotdata)
Definition: snapmgr.h:40
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:257
uint16 * ii_ExclusionStrats
Definition: execnodes.h:195
Oid * ii_ExclusionOps
Definition: execnodes.h:193
bool ii_NullsNotDistinct
Definition: execnodes.h:200
uint16 * ii_UniqueStrats
Definition: execnodes.h:198
Oid * ii_ExclusionProcs
Definition: execnodes.h:194
Oid * ii_UniqueProcs
Definition: execnodes.h:197
TransactionId xmin
Definition: snapshot.h:157
TransactionId xmax
Definition: snapshot.h:158
uint32 speculativeToken
Definition: snapshot.h:193
ItemPointerData tts_tid
Definition: tuptable.h:129
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:91
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
#define TransactionIdIsValid(xid)
Definition: transam.h:41
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:453

References BuildIndexValueDescription(), CEOUC_LIVELOCK_PREVENTING_WAIT, CEOUC_WAIT, ExprContext::ecxt_scantuple, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, errtableconstraint(), ExecDropSingleTupleTableSlot(), FormIndexDatum(), ForwardScanDirection, GetCurrentTransactionId(), GetPerTupleExprContext, i, IndexInfo::ii_ExclusionOps, IndexInfo::ii_ExclusionProcs, IndexInfo::ii_ExclusionStrats, IndexInfo::ii_NullsNotDistinct, IndexInfo::ii_UniqueProcs, IndexInfo::ii_UniqueStrats, index_beginscan(), index_endscan(), index_getnext_slot(), INDEX_MAX_KEYS, index_recheck_constraint(), index_rescan(), IndexRelationGetNumberOfKeyAttributes, InitDirtySnapshot, InvalidOid, ItemPointerEquals(), ItemPointerIsValid(), RelationGetRelationName, ScanKeyEntryInitialize(), SK_ISNULL, SK_SEARCHNULL, SpeculativeInsertionWait(), SnapshotData::speculativeToken, table_slot_create(), TransactionIdIsValid, TransactionIdPrecedes(), TupleTableSlot::tts_tid, values, XactLockTableWait(), XLTW_InsertIndex, XLTW_RecheckExclusionConstr, SnapshotData::xmax, SnapshotData::xmin, and IndexScanDescData::xs_recheck.

Referenced by check_exclusion_constraint(), ExecCheckIndexConstraints(), and ExecInsertIndexTuples().

◆ ExecCheckIndexConstraints()

bool ExecCheckIndexConstraints ( ResultRelInfo resultRelInfo,
TupleTableSlot slot,
EState estate,
ItemPointer  conflictTid,
ItemPointer  tupleid,
List arbiterIndexes 
)

Definition at line 532 of file execIndexing.c.

535 {
536  int i;
537  int numIndices;
538  RelationPtr relationDescs;
539  Relation heapRelation;
540  IndexInfo **indexInfoArray;
541  ExprContext *econtext;
543  bool isnull[INDEX_MAX_KEYS];
544  ItemPointerData invalidItemPtr;
545  bool checkedIndex = false;
546 
547  ItemPointerSetInvalid(conflictTid);
548  ItemPointerSetInvalid(&invalidItemPtr);
549 
550  /*
551  * Get information from the result relation info structure.
552  */
553  numIndices = resultRelInfo->ri_NumIndices;
554  relationDescs = resultRelInfo->ri_IndexRelationDescs;
555  indexInfoArray = resultRelInfo->ri_IndexRelationInfo;
556  heapRelation = resultRelInfo->ri_RelationDesc;
557 
558  /*
559  * We will use the EState's per-tuple context for evaluating predicates
560  * and index expressions (creating it if it's not already there).
561  */
562  econtext = GetPerTupleExprContext(estate);
563 
564  /* Arrange for econtext's scan tuple to be the tuple under test */
565  econtext->ecxt_scantuple = slot;
566 
567  /*
568  * For each index, form index tuple and check if it satisfies the
569  * constraint.
570  */
571  for (i = 0; i < numIndices; i++)
572  {
573  Relation indexRelation = relationDescs[i];
574  IndexInfo *indexInfo;
575  bool satisfiesConstraint;
576 
577  if (indexRelation == NULL)
578  continue;
579 
580  indexInfo = indexInfoArray[i];
581 
582  if (!indexInfo->ii_Unique && !indexInfo->ii_ExclusionOps)
583  continue;
584 
585  /* If the index is marked as read-only, ignore it */
586  if (!indexInfo->ii_ReadyForInserts)
587  continue;
588 
589  /* When specific arbiter indexes requested, only examine them */
590  if (arbiterIndexes != NIL &&
591  !list_member_oid(arbiterIndexes,
592  indexRelation->rd_index->indexrelid))
593  continue;
594 
595  if (!indexRelation->rd_index->indimmediate)
596  ereport(ERROR,
597  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
598  errmsg("ON CONFLICT does not support deferrable unique constraints/exclusion constraints as arbiters"),
599  errtableconstraint(heapRelation,
600  RelationGetRelationName(indexRelation))));
601 
602  checkedIndex = true;
603 
604  /* Check for partial index */
605  if (indexInfo->ii_Predicate != NIL)
606  {
607  ExprState *predicate;
608 
609  /*
610  * If predicate state not set up yet, create it (in the estate's
611  * per-query context)
612  */
613  predicate = indexInfo->ii_PredicateState;
614  if (predicate == NULL)
615  {
616  predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
617  indexInfo->ii_PredicateState = predicate;
618  }
619 
620  /* Skip this index-update if the predicate isn't satisfied */
621  if (!ExecQual(predicate, econtext))
622  continue;
623  }
624 
625  /*
626  * FormIndexDatum fills in its values and isnull parameters with the
627  * appropriate values for the column(s) of the index.
628  */
629  FormIndexDatum(indexInfo,
630  slot,
631  estate,
632  values,
633  isnull);
634 
635  satisfiesConstraint =
636  check_exclusion_or_unique_constraint(heapRelation, indexRelation,
637  indexInfo, tupleid,
638  values, isnull, estate, false,
639  CEOUC_WAIT, true,
640  conflictTid);
641  if (!satisfiesConstraint)
642  return false;
643  }
644 
645  if (arbiterIndexes != NIL && !checkedIndex)
646  elog(ERROR, "unexpected failure to find arbiter index");
647 
648  return true;
649 }
ExprState * ExecPrepareQual(List *qual, EState *estate)
Definition: execExpr.c:771
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:424
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition: itemptr.h:184
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:722
#define NIL
Definition: pg_list.h:68
bool ii_Unique
Definition: execnodes.h:199
ExprState * ii_PredicateState
Definition: execnodes.h:192
bool ii_ReadyForInserts
Definition: execnodes.h:201
List * ii_Predicate
Definition: execnodes.h:191
Form_pg_index rd_index
Definition: rel.h:192
int ri_NumIndices
Definition: execnodes.h:461
Relation ri_RelationDesc
Definition: execnodes.h:458
RelationPtr ri_IndexRelationDescs
Definition: execnodes.h:464
IndexInfo ** ri_IndexRelationInfo
Definition: execnodes.h:467

References CEOUC_WAIT, check_exclusion_or_unique_constraint(), ExprContext::ecxt_scantuple, elog, ereport, errcode(), errmsg(), ERROR, errtableconstraint(), ExecPrepareQual(), ExecQual(), FormIndexDatum(), GetPerTupleExprContext, i, IndexInfo::ii_ExclusionOps, IndexInfo::ii_Predicate, IndexInfo::ii_PredicateState, IndexInfo::ii_ReadyForInserts, IndexInfo::ii_Unique, INDEX_MAX_KEYS, ItemPointerSetInvalid(), list_member_oid(), NIL, RelationData::rd_index, RelationGetRelationName, ResultRelInfo::ri_IndexRelationDescs, ResultRelInfo::ri_IndexRelationInfo, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_RelationDesc, and values.

Referenced by ExecInsert(), and FindConflictTuple().

◆ ExecCloseIndices()

void ExecCloseIndices ( ResultRelInfo resultRelInfo)

Definition at line 232 of file execIndexing.c.

233 {
234  int i;
235  int numIndices;
236  RelationPtr indexDescs;
237  IndexInfo **indexInfos;
238 
239  numIndices = resultRelInfo->ri_NumIndices;
240  indexDescs = resultRelInfo->ri_IndexRelationDescs;
241  indexInfos = resultRelInfo->ri_IndexRelationInfo;
242 
243  for (i = 0; i < numIndices; i++)
244  {
245  if (indexDescs[i] == NULL)
246  continue; /* shouldn't happen? */
247 
248  /* Give the index a chance to do some post-insert cleanup */
249  index_insert_cleanup(indexDescs[i], indexInfos[i]);
250 
251  /* Drop lock acquired by ExecOpenIndices */
252  index_close(indexDescs[i], RowExclusiveLock);
253  }
254 
255  /*
256  * XXX should free indexInfo array here too? Currently we assume that
257  * such stuff will be cleaned up automatically in FreeExecutorState.
258  */
259 }
void index_insert_cleanup(Relation indexRelation, IndexInfo *indexInfo)
Definition: indexam.c:241
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
#define RowExclusiveLock
Definition: lockdefs.h:38

References i, index_close(), index_insert_cleanup(), ResultRelInfo::ri_IndexRelationDescs, ResultRelInfo::ri_IndexRelationInfo, ResultRelInfo::ri_NumIndices, and RowExclusiveLock.

Referenced by apply_handle_delete_internal(), apply_handle_insert_internal(), apply_handle_tuple_routing(), apply_handle_update_internal(), CatalogCloseIndexes(), ExecCleanupTupleRouting(), and ExecCloseResultRelations().

◆ ExecInsertIndexTuples()

List* ExecInsertIndexTuples ( ResultRelInfo resultRelInfo,
TupleTableSlot slot,
EState estate,
bool  update,
bool  noDupErr,
bool specConflict,
List arbiterIndexes,
bool  onlySummarizing 
)

Definition at line 299 of file execIndexing.c.

307 {
308  ItemPointer tupleid = &slot->tts_tid;
309  List *result = NIL;
310  int i;
311  int numIndices;
312  RelationPtr relationDescs;
313  Relation heapRelation;
314  IndexInfo **indexInfoArray;
315  ExprContext *econtext;
317  bool isnull[INDEX_MAX_KEYS];
318 
319  Assert(ItemPointerIsValid(tupleid));
320 
321  /*
322  * Get information from the result relation info structure.
323  */
324  numIndices = resultRelInfo->ri_NumIndices;
325  relationDescs = resultRelInfo->ri_IndexRelationDescs;
326  indexInfoArray = resultRelInfo->ri_IndexRelationInfo;
327  heapRelation = resultRelInfo->ri_RelationDesc;
328 
329  /* Sanity check: slot must belong to the same rel as the resultRelInfo. */
330  Assert(slot->tts_tableOid == RelationGetRelid(heapRelation));
331 
332  /*
333  * We will use the EState's per-tuple context for evaluating predicates
334  * and index expressions (creating it if it's not already there).
335  */
336  econtext = GetPerTupleExprContext(estate);
337 
338  /* Arrange for econtext's scan tuple to be the tuple under test */
339  econtext->ecxt_scantuple = slot;
340 
341  /*
342  * for each index, form and insert the index tuple
343  */
344  for (i = 0; i < numIndices; i++)
345  {
346  Relation indexRelation = relationDescs[i];
347  IndexInfo *indexInfo;
348  bool applyNoDupErr;
349  IndexUniqueCheck checkUnique;
350  bool indexUnchanged;
351  bool satisfiesConstraint;
352 
353  if (indexRelation == NULL)
354  continue;
355 
356  indexInfo = indexInfoArray[i];
357 
358  /* If the index is marked as read-only, ignore it */
359  if (!indexInfo->ii_ReadyForInserts)
360  continue;
361 
362  /*
363  * Skip processing of non-summarizing indexes if we only update
364  * summarizing indexes
365  */
366  if (onlySummarizing && !indexInfo->ii_Summarizing)
367  continue;
368 
369  /* Check for partial index */
370  if (indexInfo->ii_Predicate != NIL)
371  {
372  ExprState *predicate;
373 
374  /*
375  * If predicate state not set up yet, create it (in the estate's
376  * per-query context)
377  */
378  predicate = indexInfo->ii_PredicateState;
379  if (predicate == NULL)
380  {
381  predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
382  indexInfo->ii_PredicateState = predicate;
383  }
384 
385  /* Skip this index-update if the predicate isn't satisfied */
386  if (!ExecQual(predicate, econtext))
387  continue;
388  }
389 
390  /*
391  * FormIndexDatum fills in its values and isnull parameters with the
392  * appropriate values for the column(s) of the index.
393  */
394  FormIndexDatum(indexInfo,
395  slot,
396  estate,
397  values,
398  isnull);
399 
400  /* Check whether to apply noDupErr to this index */
401  applyNoDupErr = noDupErr &&
402  (arbiterIndexes == NIL ||
403  list_member_oid(arbiterIndexes,
404  indexRelation->rd_index->indexrelid));
405 
406  /*
407  * The index AM does the actual insertion, plus uniqueness checking.
408  *
409  * For an immediate-mode unique index, we just tell the index AM to
410  * throw error if not unique.
411  *
412  * For a deferrable unique index, we tell the index AM to just detect
413  * possible non-uniqueness, and we add the index OID to the result
414  * list if further checking is needed.
415  *
416  * For a speculative insertion (used by INSERT ... ON CONFLICT), do
417  * the same as for a deferrable unique index.
418  */
419  if (!indexRelation->rd_index->indisunique)
420  checkUnique = UNIQUE_CHECK_NO;
421  else if (applyNoDupErr)
422  checkUnique = UNIQUE_CHECK_PARTIAL;
423  else if (indexRelation->rd_index->indimmediate)
424  checkUnique = UNIQUE_CHECK_YES;
425  else
426  checkUnique = UNIQUE_CHECK_PARTIAL;
427 
428  /*
429  * There's definitely going to be an index_insert() call for this
430  * index. If we're being called as part of an UPDATE statement,
431  * consider if the 'indexUnchanged' = true hint should be passed.
432  */
433  indexUnchanged = update && index_unchanged_by_update(resultRelInfo,
434  estate,
435  indexInfo,
436  indexRelation);
437 
438  satisfiesConstraint =
439  index_insert(indexRelation, /* index relation */
440  values, /* array of index Datums */
441  isnull, /* null flags */
442  tupleid, /* tid of heap tuple */
443  heapRelation, /* heap relation */
444  checkUnique, /* type of uniqueness check to do */
445  indexUnchanged, /* UPDATE without logical change? */
446  indexInfo); /* index AM may need this */
447 
448  /*
449  * If the index has an associated exclusion constraint, check that.
450  * This is simpler than the process for uniqueness checks since we
451  * always insert first and then check. If the constraint is deferred,
452  * we check now anyway, but don't throw error on violation or wait for
453  * a conclusive outcome from a concurrent insertion; instead we'll
454  * queue a recheck event. Similarly, noDupErr callers (speculative
455  * inserters) will recheck later, and wait for a conclusive outcome
456  * then.
457  *
458  * An index for an exclusion constraint can't also be UNIQUE (not an
459  * essential property, we just don't allow it in the grammar), so no
460  * need to preserve the prior state of satisfiesConstraint.
461  */
462  if (indexInfo->ii_ExclusionOps != NULL)
463  {
464  bool violationOK;
465  CEOUC_WAIT_MODE waitMode;
466 
467  if (applyNoDupErr)
468  {
469  violationOK = true;
471  }
472  else if (!indexRelation->rd_index->indimmediate)
473  {
474  violationOK = true;
475  waitMode = CEOUC_NOWAIT;
476  }
477  else
478  {
479  violationOK = false;
480  waitMode = CEOUC_WAIT;
481  }
482 
483  satisfiesConstraint =
485  indexRelation, indexInfo,
486  tupleid, values, isnull,
487  estate, false,
488  waitMode, violationOK, NULL);
489  }
490 
491  if ((checkUnique == UNIQUE_CHECK_PARTIAL ||
492  indexInfo->ii_ExclusionOps != NULL) &&
493  !satisfiesConstraint)
494  {
495  /*
496  * The tuple potentially violates the uniqueness or exclusion
497  * constraint, so make a note of the index so that we can re-check
498  * it later. Speculative inserters are told if there was a
499  * speculative conflict, since that always requires a restart.
500  */
501  result = lappend_oid(result, RelationGetRelid(indexRelation));
502  if (indexRelation->rd_index->indimmediate && specConflict)
503  *specConflict = true;
504  }
505  }
506 
507  return result;
508 }
#define Assert(condition)
Definition: c.h:858
static bool index_unchanged_by_update(ResultRelInfo *resultRelInfo, EState *estate, IndexInfo *indexInfo, Relation indexRelation)
Definition: execIndexing.c:968
IndexUniqueCheck
Definition: genam.h:116
@ UNIQUE_CHECK_NO
Definition: genam.h:117
@ UNIQUE_CHECK_PARTIAL
Definition: genam.h:119
@ UNIQUE_CHECK_YES
Definition: genam.h:118
bool index_insert(Relation indexRelation, Datum *values, bool *isnull, ItemPointer heap_t_ctid, Relation heapRelation, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: indexam.c:213
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
#define RelationGetRelid(relation)
Definition: rel.h:505
bool ii_Summarizing
Definition: execnodes.h:206
Definition: pg_list.h:54
Oid tts_tableOid
Definition: tuptable.h:130

References Assert, CEOUC_LIVELOCK_PREVENTING_WAIT, CEOUC_NOWAIT, CEOUC_WAIT, check_exclusion_or_unique_constraint(), ExprContext::ecxt_scantuple, ExecPrepareQual(), ExecQual(), FormIndexDatum(), GetPerTupleExprContext, i, IndexInfo::ii_ExclusionOps, IndexInfo::ii_Predicate, IndexInfo::ii_PredicateState, IndexInfo::ii_ReadyForInserts, IndexInfo::ii_Summarizing, index_insert(), INDEX_MAX_KEYS, index_unchanged_by_update(), ItemPointerIsValid(), lappend_oid(), list_member_oid(), NIL, RelationData::rd_index, RelationGetRelid, ResultRelInfo::ri_IndexRelationDescs, ResultRelInfo::ri_IndexRelationInfo, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_RelationDesc, TupleTableSlot::tts_tableOid, TupleTableSlot::tts_tid, UNIQUE_CHECK_NO, UNIQUE_CHECK_PARTIAL, UNIQUE_CHECK_YES, and values.

Referenced by CopyFrom(), CopyMultiInsertBufferFlush(), ExecInsert(), ExecSimpleRelationInsert(), ExecSimpleRelationUpdate(), and ExecUpdateEpilogue().

◆ ExecOpenIndices()

void ExecOpenIndices ( ResultRelInfo resultRelInfo,
bool  speculative 
)

Definition at line 156 of file execIndexing.c.

157 {
158  Relation resultRelation = resultRelInfo->ri_RelationDesc;
159  List *indexoidlist;
160  ListCell *l;
161  int len,
162  i;
163  RelationPtr relationDescs;
164  IndexInfo **indexInfoArray;
165 
166  resultRelInfo->ri_NumIndices = 0;
167 
168  /* fast path if no indexes */
169  if (!RelationGetForm(resultRelation)->relhasindex)
170  return;
171 
172  /*
173  * Get cached list of index OIDs
174  */
175  indexoidlist = RelationGetIndexList(resultRelation);
176  len = list_length(indexoidlist);
177  if (len == 0)
178  return;
179 
180  /*
181  * allocate space for result arrays
182  */
183  relationDescs = (RelationPtr) palloc(len * sizeof(Relation));
184  indexInfoArray = (IndexInfo **) palloc(len * sizeof(IndexInfo *));
185 
186  resultRelInfo->ri_NumIndices = len;
187  resultRelInfo->ri_IndexRelationDescs = relationDescs;
188  resultRelInfo->ri_IndexRelationInfo = indexInfoArray;
189 
190  /*
191  * For each index, open the index relation and save pg_index info. We
192  * acquire RowExclusiveLock, signifying we will update the index.
193  *
194  * Note: we do this even if the index is not indisready; it's not worth
195  * the trouble to optimize for the case where it isn't.
196  */
197  i = 0;
198  foreach(l, indexoidlist)
199  {
200  Oid indexOid = lfirst_oid(l);
201  Relation indexDesc;
202  IndexInfo *ii;
203 
204  indexDesc = index_open(indexOid, RowExclusiveLock);
205 
206  /* extract index key information from the index's pg_index info */
207  ii = BuildIndexInfo(indexDesc);
208 
209  /*
210  * If the indexes are to be used for speculative insertion or conflict
211  * detection in logical replication, add extra information required by
212  * unique index entries.
213  */
214  if (speculative && ii->ii_Unique)
215  BuildSpeculativeIndexInfo(indexDesc, ii);
216 
217  relationDescs[i] = indexDesc;
218  indexInfoArray[i] = ii;
219  i++;
220  }
221 
222  list_free(indexoidlist);
223 }
void BuildSpeculativeIndexInfo(Relation index, IndexInfo *ii)
Definition: index.c:2643
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:2404
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
void list_free(List *list)
Definition: list.c:1546
void * palloc(Size size)
Definition: mcxt.c:1317
const void size_t len
static int list_length(const List *l)
Definition: pg_list.h:152
#define lfirst_oid(lc)
Definition: pg_list.h:174
#define RelationGetForm(relation)
Definition: rel.h:499
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4801
Relation * RelationPtr
Definition: relcache.h:35

References BuildIndexInfo(), BuildSpeculativeIndexInfo(), i, IndexInfo::ii_Unique, index_open(), len, lfirst_oid, list_free(), list_length(), palloc(), RelationGetForm, RelationGetIndexList(), ResultRelInfo::ri_IndexRelationDescs, ResultRelInfo::ri_IndexRelationInfo, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_RelationDesc, and RowExclusiveLock.

Referenced by apply_handle_delete_internal(), apply_handle_insert_internal(), apply_handle_tuple_routing(), apply_handle_update_internal(), CatalogOpenIndexes(), CopyFrom(), ExecInitPartitionInfo(), ExecInsert(), and ExecUpdatePrologue().

◆ index_expression_changed_walker()

static bool index_expression_changed_walker ( Node node,
Bitmapset allUpdatedCols 
)
static

Definition at line 1082 of file execIndexing.c.

1083 {
1084  if (node == NULL)
1085  return false;
1086 
1087  if (IsA(node, Var))
1088  {
1089  Var *var = (Var *) node;
1090 
1092  allUpdatedCols))
1093  {
1094  /* Var was updated -- indicates that we should not hint */
1095  return true;
1096  }
1097 
1098  /* Still haven't found a reason to not pass the hint */
1099  return false;
1100  }
1101 
1103  (void *) allUpdatedCols);
1104 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
static bool index_expression_changed_walker(Node *node, Bitmapset *allUpdatedCols)
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
Definition: primnodes.h:248
AttrNumber varattno
Definition: primnodes.h:260
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References bms_is_member(), expression_tree_walker, FirstLowInvalidHeapAttributeNumber, IsA, and Var::varattno.

Referenced by index_unchanged_by_update().

◆ index_recheck_constraint()

static bool index_recheck_constraint ( Relation  index,
const Oid constr_procs,
const Datum existing_values,
const bool existing_isnull,
const Datum new_values 
)
static

Definition at line 937 of file execIndexing.c.

940 {
941  int indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index);
942  int i;
943 
944  for (i = 0; i < indnkeyatts; i++)
945  {
946  /* Assume the exclusion operators are strict */
947  if (existing_isnull[i])
948  return false;
949 
950  if (!DatumGetBool(OidFunctionCall2Coll(constr_procs[i],
951  index->rd_indcollation[i],
952  existing_values[i],
953  new_values[i])))
954  return false;
955  }
956 
957  return true;
958 }
Datum OidFunctionCall2Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1421
static bool DatumGetBool(Datum X)
Definition: postgres.h:90

References DatumGetBool(), i, IndexRelationGetNumberOfKeyAttributes, and OidFunctionCall2Coll().

Referenced by check_exclusion_or_unique_constraint().

◆ index_unchanged_by_update()

static bool index_unchanged_by_update ( ResultRelInfo resultRelInfo,
EState estate,
IndexInfo indexInfo,
Relation  indexRelation 
)
static

Definition at line 968 of file execIndexing.c.

970 {
971  Bitmapset *updatedCols;
972  Bitmapset *extraUpdatedCols;
973  Bitmapset *allUpdatedCols;
974  bool hasexpression = false;
975  List *idxExprs;
976 
977  /*
978  * Check cache first
979  */
980  if (indexInfo->ii_CheckedUnchanged)
981  return indexInfo->ii_IndexUnchanged;
982  indexInfo->ii_CheckedUnchanged = true;
983 
984  /*
985  * Check for indexed attribute overlap with updated columns.
986  *
987  * Only do this for key columns. A change to a non-key column within an
988  * INCLUDE index should not be counted here. Non-key column values are
989  * opaque payload state to the index AM, a little like an extra table TID.
990  *
991  * Note that row-level BEFORE triggers won't affect our behavior, since
992  * they don't affect the updatedCols bitmaps generally. It doesn't seem
993  * worth the trouble of checking which attributes were changed directly.
994  */
995  updatedCols = ExecGetUpdatedCols(resultRelInfo, estate);
996  extraUpdatedCols = ExecGetExtraUpdatedCols(resultRelInfo, estate);
997  for (int attr = 0; attr < indexInfo->ii_NumIndexKeyAttrs; attr++)
998  {
999  int keycol = indexInfo->ii_IndexAttrNumbers[attr];
1000 
1001  if (keycol <= 0)
1002  {
1003  /*
1004  * Skip expressions for now, but remember to deal with them later
1005  * on
1006  */
1007  hasexpression = true;
1008  continue;
1009  }
1010 
1012  updatedCols) ||
1014  extraUpdatedCols))
1015  {
1016  /* Changed key column -- don't hint for this index */
1017  indexInfo->ii_IndexUnchanged = false;
1018  return false;
1019  }
1020  }
1021 
1022  /*
1023  * When we get this far and index has no expressions, return true so that
1024  * index_insert() call will go on to pass 'indexUnchanged' = true hint.
1025  *
1026  * The _absence_ of an indexed key attribute that overlaps with updated
1027  * attributes (in addition to the total absence of indexed expressions)
1028  * shows that the index as a whole is logically unchanged by UPDATE.
1029  */
1030  if (!hasexpression)
1031  {
1032  indexInfo->ii_IndexUnchanged = true;
1033  return true;
1034  }
1035 
1036  /*
1037  * Need to pass only one bms to expression_tree_walker helper function.
1038  * Avoid allocating memory in common case where there are no extra cols.
1039  */
1040  if (!extraUpdatedCols)
1041  allUpdatedCols = updatedCols;
1042  else
1043  allUpdatedCols = bms_union(updatedCols, extraUpdatedCols);
1044 
1045  /*
1046  * We have to work slightly harder in the event of indexed expressions,
1047  * but the principle is the same as before: try to find columns (Vars,
1048  * actually) that overlap with known-updated columns.
1049  *
1050  * If we find any matching Vars, don't pass hint for index. Otherwise
1051  * pass hint.
1052  */
1053  idxExprs = RelationGetIndexExpressions(indexRelation);
1054  hasexpression = index_expression_changed_walker((Node *) idxExprs,
1055  allUpdatedCols);
1056  list_free(idxExprs);
1057  if (extraUpdatedCols)
1058  bms_free(allUpdatedCols);
1059 
1060  if (hasexpression)
1061  {
1062  indexInfo->ii_IndexUnchanged = false;
1063  return false;
1064  }
1065 
1066  /*
1067  * Deliberately don't consider index predicates. We should even give the
1068  * hint when result rel's "updated tuple" has no corresponding index
1069  * tuple, which is possible with a partial index (provided the usual
1070  * conditions are met).
1071  */
1072  indexInfo->ii_IndexUnchanged = true;
1073  return true;
1074 }
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
Bitmapset * ExecGetUpdatedCols(ResultRelInfo *relinfo, EState *estate)
Definition: execUtils.c:1288
Bitmapset * ExecGetExtraUpdatedCols(ResultRelInfo *relinfo, EState *estate)
Definition: execUtils.c:1309
List * RelationGetIndexExpressions(Relation relation)
Definition: relcache.c:5038
bool ii_CheckedUnchanged
Definition: execnodes.h:202
int ii_NumIndexKeyAttrs
Definition: execnodes.h:187
bool ii_IndexUnchanged
Definition: execnodes.h:203
AttrNumber ii_IndexAttrNumbers[INDEX_MAX_KEYS]
Definition: execnodes.h:188
Definition: nodes.h:129

References bms_free(), bms_is_member(), bms_union(), ExecGetExtraUpdatedCols(), ExecGetUpdatedCols(), FirstLowInvalidHeapAttributeNumber, IndexInfo::ii_CheckedUnchanged, IndexInfo::ii_IndexAttrNumbers, IndexInfo::ii_IndexUnchanged, IndexInfo::ii_NumIndexKeyAttrs, index_expression_changed_walker(), list_free(), and RelationGetIndexExpressions().

Referenced by ExecInsertIndexTuples().