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, 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 915 of file execIndexing.c.

920 {
921  (void) check_exclusion_or_unique_constraint(heap, index, indexInfo, tupleid,
922  values, isnull,
923  estate, newIndex,
924  CEOUC_WAIT, false, NULL);
925 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
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:689
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 689 of file execIndexing.c.

697 {
698  Oid *constr_procs;
699  uint16 *constr_strats;
700  Oid *index_collations = index->rd_indcollation;
701  int indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index);
702  IndexScanDesc index_scan;
703  ScanKeyData scankeys[INDEX_MAX_KEYS];
704  SnapshotData DirtySnapshot;
705  int i;
706  bool conflict;
707  bool found_self;
708  ExprContext *econtext;
709  TupleTableSlot *existing_slot;
710  TupleTableSlot *save_scantuple;
711 
712  if (indexInfo->ii_ExclusionOps)
713  {
714  constr_procs = indexInfo->ii_ExclusionProcs;
715  constr_strats = indexInfo->ii_ExclusionStrats;
716  }
717  else
718  {
719  constr_procs = indexInfo->ii_UniqueProcs;
720  constr_strats = indexInfo->ii_UniqueStrats;
721  }
722 
723  /*
724  * If any of the input values are NULL, and the index uses the default
725  * nulls-are-distinct mode, the constraint check is assumed to pass (i.e.,
726  * we assume the operators are strict). Otherwise, we interpret the
727  * constraint as specifying IS NULL for each column whose input value is
728  * NULL.
729  */
730  if (!indexInfo->ii_NullsNotDistinct)
731  {
732  for (i = 0; i < indnkeyatts; i++)
733  {
734  if (isnull[i])
735  return true;
736  }
737  }
738 
739  /*
740  * Search the tuples that are in the index for any violations, including
741  * tuples that aren't visible yet.
742  */
743  InitDirtySnapshot(DirtySnapshot);
744 
745  for (i = 0; i < indnkeyatts; i++)
746  {
747  ScanKeyEntryInitialize(&scankeys[i],
748  isnull[i] ? SK_ISNULL | SK_SEARCHNULL : 0,
749  i + 1,
750  constr_strats[i],
751  InvalidOid,
752  index_collations[i],
753  constr_procs[i],
754  values[i]);
755  }
756 
757  /*
758  * Need a TupleTableSlot to put existing tuples in.
759  *
760  * To use FormIndexDatum, we have to make the econtext's scantuple point
761  * to this slot. Be sure to save and restore caller's value for
762  * scantuple.
763  */
764  existing_slot = table_slot_create(heap, NULL);
765 
766  econtext = GetPerTupleExprContext(estate);
767  save_scantuple = econtext->ecxt_scantuple;
768  econtext->ecxt_scantuple = existing_slot;
769 
770  /*
771  * May have to restart scan from this point if a potential conflict is
772  * found.
773  */
774 retry:
775  conflict = false;
776  found_self = false;
777  index_scan = index_beginscan(heap, index, &DirtySnapshot, indnkeyatts, 0);
778  index_rescan(index_scan, scankeys, indnkeyatts, NULL, 0);
779 
780  while (index_getnext_slot(index_scan, ForwardScanDirection, existing_slot))
781  {
782  TransactionId xwait;
783  XLTW_Oper reason_wait;
784  Datum existing_values[INDEX_MAX_KEYS];
785  bool existing_isnull[INDEX_MAX_KEYS];
786  char *error_new;
787  char *error_existing;
788 
789  /*
790  * Ignore the entry for the tuple we're trying to check.
791  */
792  if (ItemPointerIsValid(tupleid) &&
793  ItemPointerEquals(tupleid, &existing_slot->tts_tid))
794  {
795  if (found_self) /* should not happen */
796  elog(ERROR, "found self tuple multiple times in index \"%s\"",
798  found_self = true;
799  continue;
800  }
801 
802  /*
803  * Extract the index column values and isnull flags from the existing
804  * tuple.
805  */
806  FormIndexDatum(indexInfo, existing_slot, estate,
807  existing_values, existing_isnull);
808 
809  /* If lossy indexscan, must recheck the condition */
810  if (index_scan->xs_recheck)
811  {
813  constr_procs,
814  existing_values,
815  existing_isnull,
816  values))
817  continue; /* tuple doesn't actually match, so no
818  * conflict */
819  }
820 
821  /*
822  * At this point we have either a conflict or a potential conflict.
823  *
824  * If an in-progress transaction is affecting the visibility of this
825  * tuple, we need to wait for it to complete and then recheck (unless
826  * the caller requested not to). For simplicity we do rechecking by
827  * just restarting the whole scan --- this case probably doesn't
828  * happen often enough to be worth trying harder, and anyway we don't
829  * want to hold any index internal locks while waiting.
830  */
831  xwait = TransactionIdIsValid(DirtySnapshot.xmin) ?
832  DirtySnapshot.xmin : DirtySnapshot.xmax;
833 
834  if (TransactionIdIsValid(xwait) &&
835  (waitMode == CEOUC_WAIT ||
836  (waitMode == CEOUC_LIVELOCK_PREVENTING_WAIT &&
837  DirtySnapshot.speculativeToken &&
839  {
840  reason_wait = indexInfo->ii_ExclusionOps ?
842  index_endscan(index_scan);
843  if (DirtySnapshot.speculativeToken)
844  SpeculativeInsertionWait(DirtySnapshot.xmin,
845  DirtySnapshot.speculativeToken);
846  else
847  XactLockTableWait(xwait, heap,
848  &existing_slot->tts_tid, reason_wait);
849  goto retry;
850  }
851 
852  /*
853  * We have a definite conflict (or a potential one, but the caller
854  * didn't want to wait). Return it to caller, or report it.
855  */
856  if (violationOK)
857  {
858  conflict = true;
859  if (conflictTid)
860  *conflictTid = existing_slot->tts_tid;
861  break;
862  }
863 
864  error_new = BuildIndexValueDescription(index, values, isnull);
865  error_existing = BuildIndexValueDescription(index, existing_values,
866  existing_isnull);
867  if (newIndex)
868  ereport(ERROR,
869  (errcode(ERRCODE_EXCLUSION_VIOLATION),
870  errmsg("could not create exclusion constraint \"%s\"",
872  error_new && error_existing ?
873  errdetail("Key %s conflicts with key %s.",
874  error_new, error_existing) :
875  errdetail("Key conflicts exist."),
876  errtableconstraint(heap,
878  else
879  ereport(ERROR,
880  (errcode(ERRCODE_EXCLUSION_VIOLATION),
881  errmsg("conflicting key value violates exclusion constraint \"%s\"",
883  error_new && error_existing ?
884  errdetail("Key %s conflicts with existing key %s.",
885  error_new, error_existing) :
886  errdetail("Key conflicts with existing key."),
887  errtableconstraint(heap,
889  }
890 
891  index_endscan(index_scan);
892 
893  /*
894  * Ordinarily, at this point the search should have found the originally
895  * inserted tuple (if any), unless we exited the loop early because of
896  * conflict. However, it is possible to define exclusion constraints for
897  * which that wouldn't be true --- for instance, if the operator is <>. So
898  * we no longer complain if found_self is still false.
899  */
900 
901  econtext->ecxt_scantuple = save_scantuple;
902 
903  ExecDropSingleTupleTableSlot(existing_slot);
904 
905  return !conflict;
906 }
unsigned short uint16
Definition: c.h:492
uint32 TransactionId
Definition: c.h:639
int errdetail(const char *fmt,...)
Definition: elog.c:1205
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#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:932
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1253
#define GetPerTupleExprContext(estate)
Definition: executor.h:550
char * BuildIndexValueDescription(Relation indexRelation, const Datum *values, const bool *isnull)
Definition: genam.c:174
void FormIndexDatum(IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
Definition: index.c:2705
bool index_getnext_slot(IndexScanDesc scan, ScanDirection direction, TupleTableSlot *slot)
Definition: indexam.c:676
IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys)
Definition: indexam.c:257
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:379
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:353
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:667
void SpeculativeInsertionWait(TransactionId xid, uint32 token)
Definition: lmgr.c:824
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:5988
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:255
uint16 * ii_ExclusionStrats
Definition: execnodes.h:193
Oid * ii_ExclusionOps
Definition: execnodes.h:191
bool ii_NullsNotDistinct
Definition: execnodes.h:198
uint16 * ii_UniqueStrats
Definition: execnodes.h:196
Oid * ii_ExclusionProcs
Definition: execnodes.h:192
Oid * ii_UniqueProcs
Definition: execnodes.h:195
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:446

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,
List arbiterIndexes 
)

Definition at line 527 of file execIndexing.c.

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

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().

◆ ExecCloseIndices()

void ExecCloseIndices ( ResultRelInfo resultRelInfo)

Definition at line 231 of file execIndexing.c.

232 {
233  int i;
234  int numIndices;
235  RelationPtr indexDescs;
236  IndexInfo **indexInfos;
237 
238  numIndices = resultRelInfo->ri_NumIndices;
239  indexDescs = resultRelInfo->ri_IndexRelationDescs;
240  indexInfos = resultRelInfo->ri_IndexRelationInfo;
241 
242  for (i = 0; i < numIndices; i++)
243  {
244  if (indexDescs[i] == NULL)
245  continue; /* shouldn't happen? */
246 
247  /* Give the index a chance to do some post-insert cleanup */
248  index_insert_cleanup(indexDescs[i], indexInfos[i]);
249 
250  /* Drop lock acquired by ExecOpenIndices */
251  index_close(indexDescs[i], RowExclusiveLock);
252  }
253 
254  /*
255  * XXX should free indexInfo array here too? Currently we assume that
256  * such stuff will be cleaned up automatically in FreeExecutorState.
257  */
258 }
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 298 of file execIndexing.c.

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

1078 {
1079  if (node == NULL)
1080  return false;
1081 
1082  if (IsA(node, Var))
1083  {
1084  Var *var = (Var *) node;
1085 
1087  allUpdatedCols))
1088  {
1089  /* Var was updated -- indicates that we should not hint */
1090  return true;
1091  }
1092 
1093  /* Still haven't found a reason to not pass the hint */
1094  return false;
1095  }
1096 
1098  (void *) allUpdatedCols);
1099 }
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:234
AttrNumber varattno
Definition: primnodes.h:246
#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 932 of file execIndexing.c.

935 {
936  int indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index);
937  int i;
938 
939  for (i = 0; i < indnkeyatts; i++)
940  {
941  /* Assume the exclusion operators are strict */
942  if (existing_isnull[i])
943  return false;
944 
945  if (!DatumGetBool(OidFunctionCall2Coll(constr_procs[i],
946  index->rd_indcollation[i],
947  existing_values[i],
948  new_values[i])))
949  return false;
950  }
951 
952  return true;
953 }
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 963 of file execIndexing.c.

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