PostgreSQL Source Code  git master
execIndexing.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * execIndexing.c
4  * routines for inserting index tuples and enforcing unique and
5  * exclusion constraints.
6  *
7  * ExecInsertIndexTuples() is the main entry point. It's called after
8  * inserting a tuple to the heap, and it inserts corresponding index tuples
9  * into all indexes. At the same time, it enforces any unique and
10  * exclusion constraints:
11  *
12  * Unique Indexes
13  * --------------
14  *
15  * Enforcing a unique constraint is straightforward. When the index AM
16  * inserts the tuple to the index, it also checks that there are no
17  * conflicting tuples in the index already. It does so atomically, so that
18  * even if two backends try to insert the same key concurrently, only one
19  * of them will succeed. All the logic to ensure atomicity, and to wait
20  * for in-progress transactions to finish, is handled by the index AM.
21  *
22  * If a unique constraint is deferred, we request the index AM to not
23  * throw an error if a conflict is found. Instead, we make note that there
24  * was a conflict and return the list of indexes with conflicts to the
25  * caller. The caller must re-check them later, by calling index_insert()
26  * with the UNIQUE_CHECK_EXISTING option.
27  *
28  * Exclusion Constraints
29  * ---------------------
30  *
31  * Exclusion constraints are different from unique indexes in that when the
32  * tuple is inserted to the index, the index AM does not check for
33  * duplicate keys at the same time. After the insertion, we perform a
34  * separate scan on the index to check for conflicting tuples, and if one
35  * is found, we throw an error and the transaction is aborted. If the
36  * conflicting tuple's inserter or deleter is in-progress, we wait for it
37  * to finish first.
38  *
39  * There is a chance of deadlock, if two backends insert a tuple at the
40  * same time, and then perform the scan to check for conflicts. They will
41  * find each other's tuple, and both try to wait for each other. The
42  * deadlock detector will detect that, and abort one of the transactions.
43  * That's fairly harmless, as one of them was bound to abort with a
44  * "duplicate key error" anyway, although you get a different error
45  * message.
46  *
47  * If an exclusion constraint is deferred, we still perform the conflict
48  * checking scan immediately after inserting the index tuple. But instead
49  * of throwing an error if a conflict is found, we return that information
50  * to the caller. The caller must re-check them later by calling
51  * check_exclusion_constraint().
52  *
53  * Speculative insertion
54  * ---------------------
55  *
56  * Speculative insertion is a two-phase mechanism used to implement
57  * INSERT ... ON CONFLICT DO UPDATE/NOTHING. The tuple is first inserted
58  * to the heap and update the indexes as usual, but if a constraint is
59  * violated, we can still back out the insertion without aborting the whole
60  * transaction. In an INSERT ... ON CONFLICT statement, if a conflict is
61  * detected, the inserted tuple is backed out and the ON CONFLICT action is
62  * executed instead.
63  *
64  * Insertion to a unique index works as usual: the index AM checks for
65  * duplicate keys atomically with the insertion. But instead of throwing
66  * an error on a conflict, the speculatively inserted heap tuple is backed
67  * out.
68  *
69  * Exclusion constraints are slightly more complicated. As mentioned
70  * earlier, there is a risk of deadlock when two backends insert the same
71  * key concurrently. That was not a problem for regular insertions, when
72  * one of the transactions has to be aborted anyway, but with a speculative
73  * insertion we cannot let a deadlock happen, because we only want to back
74  * out the speculatively inserted tuple on conflict, not abort the whole
75  * transaction.
76  *
77  * When a backend detects that the speculative insertion conflicts with
78  * another in-progress tuple, it has two options:
79  *
80  * 1. back out the speculatively inserted tuple, then wait for the other
81  * transaction, and retry. Or,
82  * 2. wait for the other transaction, with the speculatively inserted tuple
83  * still in place.
84  *
85  * If two backends insert at the same time, and both try to wait for each
86  * other, they will deadlock. So option 2 is not acceptable. Option 1
87  * avoids the deadlock, but it is prone to a livelock instead. Both
88  * transactions will wake up immediately as the other transaction backs
89  * out. Then they both retry, and conflict with each other again, lather,
90  * rinse, repeat.
91  *
92  * To avoid the livelock, one of the backends must back out first, and then
93  * wait, while the other one waits without backing out. It doesn't matter
94  * which one backs out, so we employ an arbitrary rule that the transaction
95  * with the higher XID backs out.
96  *
97  *
98  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
99  * Portions Copyright (c) 1994, Regents of the University of California
100  *
101  *
102  * IDENTIFICATION
103  * src/backend/executor/execIndexing.c
104  *
105  *-------------------------------------------------------------------------
106  */
107 #include "postgres.h"
108 
109 #include "access/genam.h"
110 #include "access/relscan.h"
111 #include "access/tableam.h"
112 #include "access/xact.h"
113 #include "catalog/index.h"
114 #include "executor/executor.h"
115 #include "nodes/nodeFuncs.h"
116 #include "storage/lmgr.h"
117 #include "utils/snapmgr.h"
118 
119 /* waitMode argument to check_exclusion_or_unique_constraint() */
120 typedef enum
121 {
126 
127 /*
128  * The authoritative version of these macro are in executor/execMain.c. Be
129  * sure to keep everything in sync.
130  */
131 #define GetUpdatedColumns(relinfo, estate) \
132  (exec_rt_fetch((relinfo)->ri_RangeTableIndex, estate)->updatedCols)
133 #define GetExtraUpdatedColumns(relinfo, estate) \
134  (exec_rt_fetch((relinfo)->ri_RangeTableIndex, estate)->extraUpdatedCols)
135 
137  IndexInfo *indexInfo,
138  ItemPointer tupleid,
139  Datum *values, bool *isnull,
140  EState *estate, bool newIndex,
141  CEOUC_WAIT_MODE waitMode,
142  bool errorOK,
143  ItemPointer conflictTid);
144 
145 static bool index_recheck_constraint(Relation index, Oid *constr_procs,
146  Datum *existing_values, bool *existing_isnull,
147  Datum *new_values);
148 static bool index_unchanged_by_update(ResultRelInfo *resultRelInfo,
149  EState *estate, IndexInfo *indexInfo,
150  Relation indexRelation);
151 static bool index_expression_changed_walker(Node *node,
152  Bitmapset *allUpdatedCols);
153 
154 /* ----------------------------------------------------------------
155  * ExecOpenIndices
156  *
157  * Find the indices associated with a result relation, open them,
158  * and save information about them in the result ResultRelInfo.
159  *
160  * At entry, caller has already opened and locked
161  * resultRelInfo->ri_RelationDesc.
162  * ----------------------------------------------------------------
163  */
164 void
165 ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
166 {
167  Relation resultRelation = resultRelInfo->ri_RelationDesc;
168  List *indexoidlist;
169  ListCell *l;
170  int len,
171  i;
172  RelationPtr relationDescs;
173  IndexInfo **indexInfoArray;
174 
175  resultRelInfo->ri_NumIndices = 0;
176 
177  /* fast path if no indexes */
178  if (!RelationGetForm(resultRelation)->relhasindex)
179  return;
180 
181  /*
182  * Get cached list of index OIDs
183  */
184  indexoidlist = RelationGetIndexList(resultRelation);
185  len = list_length(indexoidlist);
186  if (len == 0)
187  return;
188 
189  /*
190  * allocate space for result arrays
191  */
192  relationDescs = (RelationPtr) palloc(len * sizeof(Relation));
193  indexInfoArray = (IndexInfo **) palloc(len * sizeof(IndexInfo *));
194 
195  resultRelInfo->ri_NumIndices = len;
196  resultRelInfo->ri_IndexRelationDescs = relationDescs;
197  resultRelInfo->ri_IndexRelationInfo = indexInfoArray;
198 
199  /*
200  * For each index, open the index relation and save pg_index info. We
201  * acquire RowExclusiveLock, signifying we will update the index.
202  *
203  * Note: we do this even if the index is not indisready; it's not worth
204  * the trouble to optimize for the case where it isn't.
205  */
206  i = 0;
207  foreach(l, indexoidlist)
208  {
209  Oid indexOid = lfirst_oid(l);
210  Relation indexDesc;
211  IndexInfo *ii;
212 
213  indexDesc = index_open(indexOid, RowExclusiveLock);
214 
215  /* extract index key information from the index's pg_index info */
216  ii = BuildIndexInfo(indexDesc);
217 
218  /*
219  * If the indexes are to be used for speculative insertion, add extra
220  * information required by unique index entries.
221  */
222  if (speculative && ii->ii_Unique)
223  BuildSpeculativeIndexInfo(indexDesc, ii);
224 
225  relationDescs[i] = indexDesc;
226  indexInfoArray[i] = ii;
227  i++;
228  }
229 
230  list_free(indexoidlist);
231 }
232 
233 /* ----------------------------------------------------------------
234  * ExecCloseIndices
235  *
236  * Close the index relations stored in resultRelInfo
237  * ----------------------------------------------------------------
238  */
239 void
241 {
242  int i;
243  int numIndices;
244  RelationPtr indexDescs;
245 
246  numIndices = resultRelInfo->ri_NumIndices;
247  indexDescs = resultRelInfo->ri_IndexRelationDescs;
248 
249  for (i = 0; i < numIndices; i++)
250  {
251  if (indexDescs[i] == NULL)
252  continue; /* shouldn't happen? */
253 
254  /* Drop lock acquired by ExecOpenIndices */
255  index_close(indexDescs[i], RowExclusiveLock);
256  }
257 
258  /*
259  * XXX should free indexInfo array here too? Currently we assume that
260  * such stuff will be cleaned up automatically in FreeExecutorState.
261  */
262 }
263 
264 /* ----------------------------------------------------------------
265  * ExecInsertIndexTuples
266  *
267  * This routine takes care of inserting index tuples
268  * into all the relations indexing the result relation
269  * when a heap tuple is inserted into the result relation.
270  *
271  * When 'update' is true, executor is performing an UPDATE
272  * that could not use an optimization like heapam's HOT (in
273  * more general terms a call to table_tuple_update() took
274  * place and set 'update_indexes' to true). Receiving this
275  * hint makes us consider if we should pass down the
276  * 'indexUnchanged' hint in turn. That's something that we
277  * figure out for each index_insert() call iff 'update' is
278  * true. (When 'update' is false we already know not to pass
279  * the hint to any index.)
280  *
281  * Unique and exclusion constraints are enforced at the same
282  * time. This returns a list of index OIDs for any unique or
283  * exclusion constraints that are deferred and that had
284  * potential (unconfirmed) conflicts. (if noDupErr == true,
285  * the same is done for non-deferred constraints, but report
286  * if conflict was speculative or deferred conflict to caller)
287  *
288  * If 'arbiterIndexes' is nonempty, noDupErr applies only to
289  * those indexes. NIL means noDupErr applies to all indexes.
290  * ----------------------------------------------------------------
291  */
292 List *
294  TupleTableSlot *slot,
295  EState *estate,
296  bool update,
297  bool noDupErr,
298  bool *specConflict,
299  List *arbiterIndexes)
300 {
301  ItemPointer tupleid = &slot->tts_tid;
302  List *result = NIL;
303  int i;
304  int numIndices;
305  RelationPtr relationDescs;
306  Relation heapRelation;
307  IndexInfo **indexInfoArray;
308  ExprContext *econtext;
310  bool isnull[INDEX_MAX_KEYS];
311 
312  Assert(ItemPointerIsValid(tupleid));
313 
314  /*
315  * Get information from the result relation info structure.
316  */
317  numIndices = resultRelInfo->ri_NumIndices;
318  relationDescs = resultRelInfo->ri_IndexRelationDescs;
319  indexInfoArray = resultRelInfo->ri_IndexRelationInfo;
320  heapRelation = resultRelInfo->ri_RelationDesc;
321 
322  /* Sanity check: slot must belong to the same rel as the resultRelInfo. */
323  Assert(slot->tts_tableOid == RelationGetRelid(heapRelation));
324 
325  /*
326  * We will use the EState's per-tuple context for evaluating predicates
327  * and index expressions (creating it if it's not already there).
328  */
329  econtext = GetPerTupleExprContext(estate);
330 
331  /* Arrange for econtext's scan tuple to be the tuple under test */
332  econtext->ecxt_scantuple = slot;
333 
334  /*
335  * for each index, form and insert the index tuple
336  */
337  for (i = 0; i < numIndices; i++)
338  {
339  Relation indexRelation = relationDescs[i];
340  IndexInfo *indexInfo;
341  bool applyNoDupErr;
342  IndexUniqueCheck checkUnique;
343  bool indexUnchanged;
344  bool satisfiesConstraint;
345 
346  if (indexRelation == NULL)
347  continue;
348 
349  indexInfo = indexInfoArray[i];
350 
351  /* If the index is marked as read-only, ignore it */
352  if (!indexInfo->ii_ReadyForInserts)
353  continue;
354 
355  /* Check for partial index */
356  if (indexInfo->ii_Predicate != NIL)
357  {
358  ExprState *predicate;
359 
360  /*
361  * If predicate state not set up yet, create it (in the estate's
362  * per-query context)
363  */
364  predicate = indexInfo->ii_PredicateState;
365  if (predicate == NULL)
366  {
367  predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
368  indexInfo->ii_PredicateState = predicate;
369  }
370 
371  /* Skip this index-update if the predicate isn't satisfied */
372  if (!ExecQual(predicate, econtext))
373  continue;
374  }
375 
376  /*
377  * FormIndexDatum fills in its values and isnull parameters with the
378  * appropriate values for the column(s) of the index.
379  */
380  FormIndexDatum(indexInfo,
381  slot,
382  estate,
383  values,
384  isnull);
385 
386  /* Check whether to apply noDupErr to this index */
387  applyNoDupErr = noDupErr &&
388  (arbiterIndexes == NIL ||
389  list_member_oid(arbiterIndexes,
390  indexRelation->rd_index->indexrelid));
391 
392  /*
393  * The index AM does the actual insertion, plus uniqueness checking.
394  *
395  * For an immediate-mode unique index, we just tell the index AM to
396  * throw error if not unique.
397  *
398  * For a deferrable unique index, we tell the index AM to just detect
399  * possible non-uniqueness, and we add the index OID to the result
400  * list if further checking is needed.
401  *
402  * For a speculative insertion (used by INSERT ... ON CONFLICT), do
403  * the same as for a deferrable unique index.
404  */
405  if (!indexRelation->rd_index->indisunique)
406  checkUnique = UNIQUE_CHECK_NO;
407  else if (applyNoDupErr)
408  checkUnique = UNIQUE_CHECK_PARTIAL;
409  else if (indexRelation->rd_index->indimmediate)
410  checkUnique = UNIQUE_CHECK_YES;
411  else
412  checkUnique = UNIQUE_CHECK_PARTIAL;
413 
414  /*
415  * There's definitely going to be an index_insert() call for this
416  * index. If we're being called as part of an UPDATE statement,
417  * consider if the 'indexUnchanged' = true hint should be passed.
418  */
419  indexUnchanged = update && index_unchanged_by_update(resultRelInfo,
420  estate,
421  indexInfo,
422  indexRelation);
423 
424  satisfiesConstraint =
425  index_insert(indexRelation, /* index relation */
426  values, /* array of index Datums */
427  isnull, /* null flags */
428  tupleid, /* tid of heap tuple */
429  heapRelation, /* heap relation */
430  checkUnique, /* type of uniqueness check to do */
431  indexUnchanged, /* UPDATE without logical change? */
432  indexInfo); /* index AM may need this */
433 
434  /*
435  * If the index has an associated exclusion constraint, check that.
436  * This is simpler than the process for uniqueness checks since we
437  * always insert first and then check. If the constraint is deferred,
438  * we check now anyway, but don't throw error on violation or wait for
439  * a conclusive outcome from a concurrent insertion; instead we'll
440  * queue a recheck event. Similarly, noDupErr callers (speculative
441  * inserters) will recheck later, and wait for a conclusive outcome
442  * then.
443  *
444  * An index for an exclusion constraint can't also be UNIQUE (not an
445  * essential property, we just don't allow it in the grammar), so no
446  * need to preserve the prior state of satisfiesConstraint.
447  */
448  if (indexInfo->ii_ExclusionOps != NULL)
449  {
450  bool violationOK;
451  CEOUC_WAIT_MODE waitMode;
452 
453  if (applyNoDupErr)
454  {
455  violationOK = true;
457  }
458  else if (!indexRelation->rd_index->indimmediate)
459  {
460  violationOK = true;
461  waitMode = CEOUC_NOWAIT;
462  }
463  else
464  {
465  violationOK = false;
466  waitMode = CEOUC_WAIT;
467  }
468 
469  satisfiesConstraint =
471  indexRelation, indexInfo,
472  tupleid, values, isnull,
473  estate, false,
474  waitMode, violationOK, NULL);
475  }
476 
477  if ((checkUnique == UNIQUE_CHECK_PARTIAL ||
478  indexInfo->ii_ExclusionOps != NULL) &&
479  !satisfiesConstraint)
480  {
481  /*
482  * The tuple potentially violates the uniqueness or exclusion
483  * constraint, so make a note of the index so that we can re-check
484  * it later. Speculative inserters are told if there was a
485  * speculative conflict, since that always requires a restart.
486  */
487  result = lappend_oid(result, RelationGetRelid(indexRelation));
488  if (indexRelation->rd_index->indimmediate && specConflict)
489  *specConflict = true;
490  }
491  }
492 
493  return result;
494 }
495 
496 /* ----------------------------------------------------------------
497  * ExecCheckIndexConstraints
498  *
499  * This routine checks if a tuple violates any unique or
500  * exclusion constraints. Returns true if there is no conflict.
501  * Otherwise returns false, and the TID of the conflicting
502  * tuple is returned in *conflictTid.
503  *
504  * If 'arbiterIndexes' is given, only those indexes are checked.
505  * NIL means all indexes.
506  *
507  * Note that this doesn't lock the values in any way, so it's
508  * possible that a conflicting tuple is inserted immediately
509  * after this returns. But this can be used for a pre-check
510  * before insertion.
511  * ----------------------------------------------------------------
512  */
513 bool
515  EState *estate, ItemPointer conflictTid,
516  List *arbiterIndexes)
517 {
518  int i;
519  int numIndices;
520  RelationPtr relationDescs;
521  Relation heapRelation;
522  IndexInfo **indexInfoArray;
523  ExprContext *econtext;
525  bool isnull[INDEX_MAX_KEYS];
526  ItemPointerData invalidItemPtr;
527  bool checkedIndex = false;
528 
529  ItemPointerSetInvalid(conflictTid);
530  ItemPointerSetInvalid(&invalidItemPtr);
531 
532  /*
533  * Get information from the result relation info structure.
534  */
535  numIndices = resultRelInfo->ri_NumIndices;
536  relationDescs = resultRelInfo->ri_IndexRelationDescs;
537  indexInfoArray = resultRelInfo->ri_IndexRelationInfo;
538  heapRelation = resultRelInfo->ri_RelationDesc;
539 
540  /*
541  * We will use the EState's per-tuple context for evaluating predicates
542  * and index expressions (creating it if it's not already there).
543  */
544  econtext = GetPerTupleExprContext(estate);
545 
546  /* Arrange for econtext's scan tuple to be the tuple under test */
547  econtext->ecxt_scantuple = slot;
548 
549  /*
550  * For each index, form index tuple and check if it satisfies the
551  * constraint.
552  */
553  for (i = 0; i < numIndices; i++)
554  {
555  Relation indexRelation = relationDescs[i];
556  IndexInfo *indexInfo;
557  bool satisfiesConstraint;
558 
559  if (indexRelation == NULL)
560  continue;
561 
562  indexInfo = indexInfoArray[i];
563 
564  if (!indexInfo->ii_Unique && !indexInfo->ii_ExclusionOps)
565  continue;
566 
567  /* If the index is marked as read-only, ignore it */
568  if (!indexInfo->ii_ReadyForInserts)
569  continue;
570 
571  /* When specific arbiter indexes requested, only examine them */
572  if (arbiterIndexes != NIL &&
573  !list_member_oid(arbiterIndexes,
574  indexRelation->rd_index->indexrelid))
575  continue;
576 
577  if (!indexRelation->rd_index->indimmediate)
578  ereport(ERROR,
579  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
580  errmsg("ON CONFLICT does not support deferrable unique constraints/exclusion constraints as arbiters"),
581  errtableconstraint(heapRelation,
582  RelationGetRelationName(indexRelation))));
583 
584  checkedIndex = true;
585 
586  /* Check for partial index */
587  if (indexInfo->ii_Predicate != NIL)
588  {
589  ExprState *predicate;
590 
591  /*
592  * If predicate state not set up yet, create it (in the estate's
593  * per-query context)
594  */
595  predicate = indexInfo->ii_PredicateState;
596  if (predicate == NULL)
597  {
598  predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
599  indexInfo->ii_PredicateState = predicate;
600  }
601 
602  /* Skip this index-update if the predicate isn't satisfied */
603  if (!ExecQual(predicate, econtext))
604  continue;
605  }
606 
607  /*
608  * FormIndexDatum fills in its values and isnull parameters with the
609  * appropriate values for the column(s) of the index.
610  */
611  FormIndexDatum(indexInfo,
612  slot,
613  estate,
614  values,
615  isnull);
616 
617  satisfiesConstraint =
618  check_exclusion_or_unique_constraint(heapRelation, indexRelation,
619  indexInfo, &invalidItemPtr,
620  values, isnull, estate, false,
621  CEOUC_WAIT, true,
622  conflictTid);
623  if (!satisfiesConstraint)
624  return false;
625  }
626 
627  if (arbiterIndexes != NIL && !checkedIndex)
628  elog(ERROR, "unexpected failure to find arbiter index");
629 
630  return true;
631 }
632 
633 /*
634  * Check for violation of an exclusion or unique constraint
635  *
636  * heap: the table containing the new tuple
637  * index: the index supporting the constraint
638  * indexInfo: info about the index, including the exclusion properties
639  * tupleid: heap TID of the new tuple we have just inserted (invalid if we
640  * haven't inserted a new tuple yet)
641  * values, isnull: the *index* column values computed for the new tuple
642  * estate: an EState we can do evaluation in
643  * newIndex: if true, we are trying to build a new index (this affects
644  * only the wording of error messages)
645  * waitMode: whether to wait for concurrent inserters/deleters
646  * violationOK: if true, don't throw error for violation
647  * conflictTid: if not-NULL, the TID of the conflicting tuple is returned here
648  *
649  * Returns true if OK, false if actual or potential violation
650  *
651  * 'waitMode' determines what happens if a conflict is detected with a tuple
652  * that was inserted or deleted by a transaction that's still running.
653  * CEOUC_WAIT means that we wait for the transaction to commit, before
654  * throwing an error or returning. CEOUC_NOWAIT means that we report the
655  * violation immediately; so the violation is only potential, and the caller
656  * must recheck sometime later. This behavior is convenient for deferred
657  * exclusion checks; we need not bother queuing a deferred event if there is
658  * definitely no conflict at insertion time.
659  *
660  * CEOUC_LIVELOCK_PREVENTING_WAIT is like CEOUC_NOWAIT, but we will sometimes
661  * wait anyway, to prevent livelocking if two transactions try inserting at
662  * the same time. This is used with speculative insertions, for INSERT ON
663  * CONFLICT statements. (See notes in file header)
664  *
665  * If violationOK is true, we just report the potential or actual violation to
666  * the caller by returning 'false'. Otherwise we throw a descriptive error
667  * message here. When violationOK is false, a false result is impossible.
668  *
669  * Note: The indexam is normally responsible for checking unique constraints,
670  * so this normally only needs to be used for exclusion constraints. But this
671  * function is also called when doing a "pre-check" for conflicts on a unique
672  * constraint, when doing speculative insertion. Caller may use the returned
673  * conflict TID to take further steps.
674  */
675 static bool
677  IndexInfo *indexInfo,
678  ItemPointer tupleid,
679  Datum *values, bool *isnull,
680  EState *estate, bool newIndex,
681  CEOUC_WAIT_MODE waitMode,
682  bool violationOK,
683  ItemPointer conflictTid)
684 {
685  Oid *constr_procs;
686  uint16 *constr_strats;
687  Oid *index_collations = index->rd_indcollation;
688  int indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index);
689  IndexScanDesc index_scan;
690  ScanKeyData scankeys[INDEX_MAX_KEYS];
691  SnapshotData DirtySnapshot;
692  int i;
693  bool conflict;
694  bool found_self;
695  ExprContext *econtext;
696  TupleTableSlot *existing_slot;
697  TupleTableSlot *save_scantuple;
698 
699  if (indexInfo->ii_ExclusionOps)
700  {
701  constr_procs = indexInfo->ii_ExclusionProcs;
702  constr_strats = indexInfo->ii_ExclusionStrats;
703  }
704  else
705  {
706  constr_procs = indexInfo->ii_UniqueProcs;
707  constr_strats = indexInfo->ii_UniqueStrats;
708  }
709 
710  /*
711  * If any of the input values are NULL, the constraint check is assumed to
712  * pass (i.e., we assume the operators are strict).
713  */
714  for (i = 0; i < indnkeyatts; i++)
715  {
716  if (isnull[i])
717  return true;
718  }
719 
720  /*
721  * Search the tuples that are in the index for any violations, including
722  * tuples that aren't visible yet.
723  */
724  InitDirtySnapshot(DirtySnapshot);
725 
726  for (i = 0; i < indnkeyatts; i++)
727  {
728  ScanKeyEntryInitialize(&scankeys[i],
729  0,
730  i + 1,
731  constr_strats[i],
732  InvalidOid,
733  index_collations[i],
734  constr_procs[i],
735  values[i]);
736  }
737 
738  /*
739  * Need a TupleTableSlot to put existing tuples in.
740  *
741  * To use FormIndexDatum, we have to make the econtext's scantuple point
742  * to this slot. Be sure to save and restore caller's value for
743  * scantuple.
744  */
745  existing_slot = table_slot_create(heap, NULL);
746 
747  econtext = GetPerTupleExprContext(estate);
748  save_scantuple = econtext->ecxt_scantuple;
749  econtext->ecxt_scantuple = existing_slot;
750 
751  /*
752  * May have to restart scan from this point if a potential conflict is
753  * found.
754  */
755 retry:
756  conflict = false;
757  found_self = false;
758  index_scan = index_beginscan(heap, index, &DirtySnapshot, indnkeyatts, 0);
759  index_rescan(index_scan, scankeys, indnkeyatts, NULL, 0);
760 
761  while (index_getnext_slot(index_scan, ForwardScanDirection, existing_slot))
762  {
763  TransactionId xwait;
764  XLTW_Oper reason_wait;
765  Datum existing_values[INDEX_MAX_KEYS];
766  bool existing_isnull[INDEX_MAX_KEYS];
767  char *error_new;
768  char *error_existing;
769 
770  /*
771  * Ignore the entry for the tuple we're trying to check.
772  */
773  if (ItemPointerIsValid(tupleid) &&
774  ItemPointerEquals(tupleid, &existing_slot->tts_tid))
775  {
776  if (found_self) /* should not happen */
777  elog(ERROR, "found self tuple multiple times in index \"%s\"",
778  RelationGetRelationName(index));
779  found_self = true;
780  continue;
781  }
782 
783  /*
784  * Extract the index column values and isnull flags from the existing
785  * tuple.
786  */
787  FormIndexDatum(indexInfo, existing_slot, estate,
788  existing_values, existing_isnull);
789 
790  /* If lossy indexscan, must recheck the condition */
791  if (index_scan->xs_recheck)
792  {
793  if (!index_recheck_constraint(index,
794  constr_procs,
795  existing_values,
796  existing_isnull,
797  values))
798  continue; /* tuple doesn't actually match, so no
799  * conflict */
800  }
801 
802  /*
803  * At this point we have either a conflict or a potential conflict.
804  *
805  * If an in-progress transaction is affecting the visibility of this
806  * tuple, we need to wait for it to complete and then recheck (unless
807  * the caller requested not to). For simplicity we do rechecking by
808  * just restarting the whole scan --- this case probably doesn't
809  * happen often enough to be worth trying harder, and anyway we don't
810  * want to hold any index internal locks while waiting.
811  */
812  xwait = TransactionIdIsValid(DirtySnapshot.xmin) ?
813  DirtySnapshot.xmin : DirtySnapshot.xmax;
814 
815  if (TransactionIdIsValid(xwait) &&
816  (waitMode == CEOUC_WAIT ||
817  (waitMode == CEOUC_LIVELOCK_PREVENTING_WAIT &&
818  DirtySnapshot.speculativeToken &&
820  {
821  reason_wait = indexInfo->ii_ExclusionOps ?
823  index_endscan(index_scan);
824  if (DirtySnapshot.speculativeToken)
825  SpeculativeInsertionWait(DirtySnapshot.xmin,
826  DirtySnapshot.speculativeToken);
827  else
828  XactLockTableWait(xwait, heap,
829  &existing_slot->tts_tid, reason_wait);
830  goto retry;
831  }
832 
833  /*
834  * We have a definite conflict (or a potential one, but the caller
835  * didn't want to wait). Return it to caller, or report it.
836  */
837  if (violationOK)
838  {
839  conflict = true;
840  if (conflictTid)
841  *conflictTid = existing_slot->tts_tid;
842  break;
843  }
844 
845  error_new = BuildIndexValueDescription(index, values, isnull);
846  error_existing = BuildIndexValueDescription(index, existing_values,
847  existing_isnull);
848  if (newIndex)
849  ereport(ERROR,
850  (errcode(ERRCODE_EXCLUSION_VIOLATION),
851  errmsg("could not create exclusion constraint \"%s\"",
852  RelationGetRelationName(index)),
853  error_new && error_existing ?
854  errdetail("Key %s conflicts with key %s.",
855  error_new, error_existing) :
856  errdetail("Key conflicts exist."),
857  errtableconstraint(heap,
858  RelationGetRelationName(index))));
859  else
860  ereport(ERROR,
861  (errcode(ERRCODE_EXCLUSION_VIOLATION),
862  errmsg("conflicting key value violates exclusion constraint \"%s\"",
863  RelationGetRelationName(index)),
864  error_new && error_existing ?
865  errdetail("Key %s conflicts with existing key %s.",
866  error_new, error_existing) :
867  errdetail("Key conflicts with existing key."),
868  errtableconstraint(heap,
869  RelationGetRelationName(index))));
870  }
871 
872  index_endscan(index_scan);
873 
874  /*
875  * Ordinarily, at this point the search should have found the originally
876  * inserted tuple (if any), unless we exited the loop early because of
877  * conflict. However, it is possible to define exclusion constraints for
878  * which that wouldn't be true --- for instance, if the operator is <>. So
879  * we no longer complain if found_self is still false.
880  */
881 
882  econtext->ecxt_scantuple = save_scantuple;
883 
884  ExecDropSingleTupleTableSlot(existing_slot);
885 
886  return !conflict;
887 }
888 
889 /*
890  * Check for violation of an exclusion constraint
891  *
892  * This is a dumbed down version of check_exclusion_or_unique_constraint
893  * for external callers. They don't need all the special modes.
894  */
895 void
897  IndexInfo *indexInfo,
898  ItemPointer tupleid,
899  Datum *values, bool *isnull,
900  EState *estate, bool newIndex)
901 {
902  (void) check_exclusion_or_unique_constraint(heap, index, indexInfo, tupleid,
903  values, isnull,
904  estate, newIndex,
905  CEOUC_WAIT, false, NULL);
906 }
907 
908 /*
909  * Check existing tuple's index values to see if it really matches the
910  * exclusion condition against the new_values. Returns true if conflict.
911  */
912 static bool
914  Datum *existing_values, bool *existing_isnull,
915  Datum *new_values)
916 {
917  int indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index);
918  int i;
919 
920  for (i = 0; i < indnkeyatts; i++)
921  {
922  /* Assume the exclusion operators are strict */
923  if (existing_isnull[i])
924  return false;
925 
926  if (!DatumGetBool(OidFunctionCall2Coll(constr_procs[i],
927  index->rd_indcollation[i],
928  existing_values[i],
929  new_values[i])))
930  return false;
931  }
932 
933  return true;
934 }
935 
936 /*
937  * Check if ExecInsertIndexTuples() should pass indexUnchanged hint.
938  *
939  * When the executor performs an UPDATE that requires a new round of index
940  * tuples, determine if we should pass 'indexUnchanged' = true hint for one
941  * single index.
942  */
943 static bool
945  IndexInfo *indexInfo, Relation indexRelation)
946 {
947  Bitmapset *updatedCols = GetUpdatedColumns(resultRelInfo, estate);
948  Bitmapset *extraUpdatedCols = GetExtraUpdatedColumns(resultRelInfo, estate);
949  Bitmapset *allUpdatedCols;
950  bool hasexpression = false;
951  List *idxExprs;
952 
953  /*
954  * Check for indexed attribute overlap with updated columns.
955  *
956  * Only do this for key columns. A change to a non-key column within an
957  * INCLUDE index should not be counted here. Non-key column values are
958  * opaque payload state to the index AM, a little like an extra table TID.
959  */
960  for (int attr = 0; attr < indexInfo->ii_NumIndexKeyAttrs; attr++)
961  {
962  int keycol = indexInfo->ii_IndexAttrNumbers[attr];
963 
964  if (keycol <= 0)
965  {
966  /*
967  * Skip expressions for now, but remember to deal with them later
968  * on
969  */
970  hasexpression = true;
971  continue;
972  }
973 
975  updatedCols) ||
977  extraUpdatedCols))
978  {
979  /* Changed key column -- don't hint for this index */
980  return false;
981  }
982  }
983 
984  /*
985  * When we get this far and index has no expressions, return true so that
986  * index_insert() call will go on to pass 'indexUnchanged' = true hint.
987  *
988  * The _absence_ of an indexed key attribute that overlaps with updated
989  * attributes (in addition to the total absence of indexed expressions)
990  * shows that the index as a whole is logically unchanged by UPDATE.
991  */
992  if (!hasexpression)
993  return true;
994 
995  /*
996  * Need to pass only one bms to expression_tree_walker helper function.
997  * Avoid allocating memory in common case where there are no extra cols.
998  */
999  if (!extraUpdatedCols)
1000  allUpdatedCols = updatedCols;
1001  else
1002  allUpdatedCols = bms_union(updatedCols, extraUpdatedCols);
1003 
1004  /*
1005  * We have to work slightly harder in the event of indexed expressions,
1006  * but the principle is the same as before: try to find columns (Vars,
1007  * actually) that overlap with known-updated columns.
1008  *
1009  * If we find any matching Vars, don't pass hint for index. Otherwise
1010  * pass hint.
1011  */
1012  idxExprs = RelationGetIndexExpressions(indexRelation);
1013  hasexpression = index_expression_changed_walker((Node *) idxExprs,
1014  allUpdatedCols);
1015  list_free(idxExprs);
1016  if (extraUpdatedCols)
1017  bms_free(allUpdatedCols);
1018 
1019  if (hasexpression)
1020  return false;
1021 
1022  return true;
1023 }
1024 
1025 /*
1026  * Indexed expression helper for index_unchanged_by_update().
1027  *
1028  * Returns true when Var that appears within allUpdatedCols located.
1029  */
1030 static bool
1032 {
1033  if (node == NULL)
1034  return false;
1035 
1036  if (IsA(node, Var))
1037  {
1038  Var *var = (Var *) node;
1039 
1041  allUpdatedCols))
1042  {
1043  /* Var was updated -- indicates that we should not hint */
1044  return true;
1045  }
1046 
1047  /* Still haven't found a reason to not pass the hint */
1048  return false;
1049  }
1050 
1052  (void *) allUpdatedCols);
1053 }
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:91
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:82
void FormIndexDatum(IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
Definition: index.c:2755
int ri_NumIndices
Definition: execnodes.h:415
#define NIL
Definition: pg_list.h:65
Oid tts_tableOid
Definition: tuptable.h:131
uint16 * ii_UniqueStrats
Definition: execnodes.h:169
Relation ri_RelationDesc
Definition: execnodes.h:412
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
#define InitDirtySnapshot(snapshotdata)
Definition: snapmgr.h:75
List * ii_Predicate
Definition: execnodes.h:162
uint32 TransactionId
Definition: c.h:575
ExprState * ii_PredicateState
Definition: execnodes.h:163
#define RelationGetForm(relation)
Definition: rel.h:451
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:297
Definition: nodes.h:528
Oid * ii_ExclusionProcs
Definition: execnodes.h:165
int errcode(int sqlerrcode)
Definition: elog.c:704
AttrNumber varattno
Definition: primnodes.h:186
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:2471
unsigned int Oid
Definition: postgres_ext.h:31
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:372
Definition: primnodes.h:181
List * ExecInsertIndexTuples(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, bool update, bool noDupErr, bool *specConflict, List *arbiterIndexes)
Definition: execIndexing.c:293
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
static bool check_exclusion_or_unique_constraint(Relation heap, Relation index, IndexInfo *indexInfo, ItemPointer tupleid, Datum *values, bool *isnull, EState *estate, bool newIndex, CEOUC_WAIT_MODE waitMode, bool errorOK, ItemPointer conflictTid)
Definition: execIndexing.c:676
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:165
Definition: type.h:89
IndexUniqueCheck
Definition: genam.h:112
#define GetPerTupleExprContext(estate)
Definition: executor.h:509
int errtableconstraint(Relation rel, const char *conname)
Definition: relcache.c:5563
Form_pg_index rd_index
Definition: rel.h:175
unsigned short uint16
Definition: c.h:428
Oid * rd_indcollation
Definition: rel.h:200
static bool index_recheck_constraint(Relation index, Oid *constr_procs, Datum *existing_values, bool *existing_isnull, Datum *new_values)
Definition: execIndexing.c:913
#define ERROR
Definition: elog.h:45
int ii_NumIndexKeyAttrs
Definition: execnodes.h:158
void SpeculativeInsertionWait(TransactionId xid, uint32 token)
Definition: lmgr.c:796
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:438
#define GetExtraUpdatedColumns(relinfo, estate)
Definition: execIndexing.c:133
void ScanKeyEntryInitialize(ScanKey entry, int flags, AttrNumber attributeNumber, StrategyNumber strategy, Oid subtype, Oid collation, RegProcedure procedure, Datum argument)
Definition: scankey.c:32
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1224
List * RelationGetIndexExpressions(Relation relation)
Definition: relcache.c:4760
Oid * ii_UniqueProcs
Definition: execnodes.h:168
#define RowExclusiveLock
Definition: lockdefs.h:38
int errdetail(const char *fmt,...)
Definition: elog.c:1048
#define DatumGetBool(X)
Definition: postgres.h:393
#define RelationGetRelationName(relation)
Definition: rel.h:491
ExprState * ExecPrepareQual(List *qual, EState *estate)
Definition: execExpr.c:520
Relation * RelationPtr
Definition: relcache.h:35
TransactionId xmax
Definition: snapshot.h:158
TransactionId xmin
Definition: snapshot.h:157
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition: rel.h:476
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:323
Datum OidFunctionCall2Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1423
bool ii_ReadyForInserts
Definition: execnodes.h:172
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
static bool index_unchanged_by_update(ResultRelInfo *resultRelInfo, EState *estate, IndexInfo *indexInfo, Relation indexRelation)
Definition: execIndexing.c:944
uintptr_t Datum
Definition: postgres.h:367
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
bool ii_Unique
Definition: execnodes.h:171
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:674
#define GetUpdatedColumns(relinfo, estate)
Definition: execIndexing.c:131
uint32 speculativeToken
Definition: snapshot.h:193
void XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid, XLTW_Oper oper)
Definition: lmgr.c:639
#define Assert(condition)
Definition: c.h:792
#define INDEX_MAX_KEYS
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1879
static int list_length(const List *l)
Definition: pg_list.h:149
bool index_getnext_slot(IndexScanDesc scan, ScanDirection direction, TupleTableSlot *slot)
Definition: indexam.c:616
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:225
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4526
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
static bool index_expression_changed_walker(Node *node, Bitmapset *allUpdatedCols)
static Datum values[MAXATTR]
Definition: bootstrap.c:165
XLTW_Oper
Definition: lmgr.h:24
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
bool ExecCheckIndexConstraints(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, ItemPointer conflictTid, List *arbiterIndexes)
Definition: execIndexing.c:514
Oid * ii_ExclusionOps
Definition: execnodes.h:164
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:915
void list_free(List *list)
Definition: list.c:1376
#define elog(elevel,...)
Definition: elog.h:228
IndexInfo ** ri_IndexRelationInfo
Definition: execnodes.h:421
int i
void BuildSpeculativeIndexInfo(Relation index, IndexInfo *ii)
Definition: index.c:2695
bool index_insert(Relation indexRelation, Datum *values, bool *isnull, ItemPointer heap_t_ctid, Relation heapRelation, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: indexam.c:176
char * BuildIndexValueDescription(Relation indexRelation, Datum *values, bool *isnull)
Definition: genam.c:177
void check_exclusion_constraint(Relation heap, Relation index, IndexInfo *indexInfo, ItemPointer tupleid, Datum *values, bool *isnull, EState *estate, bool newIndex)
Definition: execIndexing.c:896
#define TransactionIdIsValid(xid)
Definition: transam.h:41
AttrNumber ii_IndexAttrNumbers[INDEX_MAX_KEYS]
Definition: execnodes.h:159
uint16 * ii_ExclusionStrats
Definition: execnodes.h:166
CEOUC_WAIT_MODE
Definition: execIndexing.c:120
Definition: pg_list.h:50
ItemPointerData tts_tid
Definition: tuptable.h:130
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
#define RelationGetRelid(relation)
Definition: rel.h:457
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132
void ExecCloseIndices(ResultRelInfo *resultRelInfo)
Definition: execIndexing.c:240
RelationPtr ri_IndexRelationDescs
Definition: execnodes.h:418
#define lfirst_oid(lc)
Definition: pg_list.h:171
IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys)
Definition: indexam.c:205