PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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  * exclusive 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-2016, 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/relscan.h"
110 #include "access/xact.h"
111 #include "catalog/index.h"
112 #include "executor/executor.h"
113 #include "nodes/nodeFuncs.h"
114 #include "storage/lmgr.h"
115 #include "utils/tqual.h"
116 
117 /* waitMode argument to check_exclusion_or_unique_constraint() */
118 typedef enum
119 {
124 
126  IndexInfo *indexInfo,
127  ItemPointer tupleid,
128  Datum *values, bool *isnull,
129  EState *estate, bool newIndex,
130  CEOUC_WAIT_MODE waitMode,
131  bool errorOK,
132  ItemPointer conflictTid);
133 
134 static bool index_recheck_constraint(Relation index, Oid *constr_procs,
135  Datum *existing_values, bool *existing_isnull,
136  Datum *new_values);
137 
138 /* ----------------------------------------------------------------
139  * ExecOpenIndices
140  *
141  * Find the indices associated with a result relation, open them,
142  * and save information about them in the result ResultRelInfo.
143  *
144  * At entry, caller has already opened and locked
145  * resultRelInfo->ri_RelationDesc.
146  * ----------------------------------------------------------------
147  */
148 void
149 ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
150 {
151  Relation resultRelation = resultRelInfo->ri_RelationDesc;
152  List *indexoidlist;
153  ListCell *l;
154  int len,
155  i;
156  RelationPtr relationDescs;
157  IndexInfo **indexInfoArray;
158 
159  resultRelInfo->ri_NumIndices = 0;
160 
161  /* fast path if no indexes */
162  if (!RelationGetForm(resultRelation)->relhasindex)
163  return;
164 
165  /*
166  * Get cached list of index OIDs
167  */
168  indexoidlist = RelationGetIndexList(resultRelation);
169  len = list_length(indexoidlist);
170  if (len == 0)
171  return;
172 
173  /*
174  * allocate space for result arrays
175  */
176  relationDescs = (RelationPtr) palloc(len * sizeof(Relation));
177  indexInfoArray = (IndexInfo **) palloc(len * sizeof(IndexInfo *));
178 
179  resultRelInfo->ri_NumIndices = len;
180  resultRelInfo->ri_IndexRelationDescs = relationDescs;
181  resultRelInfo->ri_IndexRelationInfo = indexInfoArray;
182 
183  /*
184  * For each index, open the index relation and save pg_index info. We
185  * acquire RowExclusiveLock, signifying we will update the index.
186  *
187  * Note: we do this even if the index is not IndexIsReady; it's not worth
188  * the trouble to optimize for the case where it isn't.
189  */
190  i = 0;
191  foreach(l, indexoidlist)
192  {
193  Oid indexOid = lfirst_oid(l);
194  Relation indexDesc;
195  IndexInfo *ii;
196 
197  indexDesc = index_open(indexOid, RowExclusiveLock);
198 
199  /* extract index key information from the index's pg_index info */
200  ii = BuildIndexInfo(indexDesc);
201 
202  /*
203  * If the indexes are to be used for speculative insertion, add extra
204  * information required by unique index entries.
205  */
206  if (speculative && ii->ii_Unique)
207  BuildSpeculativeIndexInfo(indexDesc, ii);
208 
209  relationDescs[i] = indexDesc;
210  indexInfoArray[i] = ii;
211  i++;
212  }
213 
214  list_free(indexoidlist);
215 }
216 
217 /* ----------------------------------------------------------------
218  * ExecCloseIndices
219  *
220  * Close the index relations stored in resultRelInfo
221  * ----------------------------------------------------------------
222  */
223 void
225 {
226  int i;
227  int numIndices;
228  RelationPtr indexDescs;
229 
230  numIndices = resultRelInfo->ri_NumIndices;
231  indexDescs = resultRelInfo->ri_IndexRelationDescs;
232 
233  for (i = 0; i < numIndices; i++)
234  {
235  if (indexDescs[i] == NULL)
236  continue; /* shouldn't happen? */
237 
238  /* Drop lock acquired by ExecOpenIndices */
239  index_close(indexDescs[i], RowExclusiveLock);
240  }
241 
242  /*
243  * XXX should free indexInfo array here too? Currently we assume that
244  * such stuff will be cleaned up automatically in FreeExecutorState.
245  */
246 }
247 
248 /* ----------------------------------------------------------------
249  * ExecInsertIndexTuples
250  *
251  * This routine takes care of inserting index tuples
252  * into all the relations indexing the result relation
253  * when a heap tuple is inserted into the result relation.
254  *
255  * Unique and exclusion constraints are enforced at the same
256  * time. This returns a list of index OIDs for any unique or
257  * exclusion constraints that are deferred and that had
258  * potential (unconfirmed) conflicts. (if noDupErr == true,
259  * the same is done for non-deferred constraints, but report
260  * if conflict was speculative or deferred conflict to caller)
261  *
262  * CAUTION: this must not be called for a HOT update.
263  * We can't defend against that here for lack of info.
264  * Should we change the API to make it safer?
265  * ----------------------------------------------------------------
266  */
267 List *
269  ItemPointer tupleid,
270  EState *estate,
271  bool noDupErr,
272  bool *specConflict,
273  List *arbiterIndexes)
274 {
275  List *result = NIL;
276  ResultRelInfo *resultRelInfo;
277  int i;
278  int numIndices;
279  RelationPtr relationDescs;
280  Relation heapRelation;
281  IndexInfo **indexInfoArray;
282  ExprContext *econtext;
284  bool isnull[INDEX_MAX_KEYS];
285 
286  /*
287  * Get information from the result relation info structure.
288  */
289  resultRelInfo = estate->es_result_relation_info;
290  numIndices = resultRelInfo->ri_NumIndices;
291  relationDescs = resultRelInfo->ri_IndexRelationDescs;
292  indexInfoArray = resultRelInfo->ri_IndexRelationInfo;
293  heapRelation = resultRelInfo->ri_RelationDesc;
294 
295  /*
296  * We will use the EState's per-tuple context for evaluating predicates
297  * and index expressions (creating it if it's not already there).
298  */
299  econtext = GetPerTupleExprContext(estate);
300 
301  /* Arrange for econtext's scan tuple to be the tuple under test */
302  econtext->ecxt_scantuple = slot;
303 
304  /*
305  * for each index, form and insert the index tuple
306  */
307  for (i = 0; i < numIndices; i++)
308  {
309  Relation indexRelation = relationDescs[i];
310  IndexInfo *indexInfo;
311  IndexUniqueCheck checkUnique;
312  bool satisfiesConstraint;
313  bool arbiter;
314 
315  if (indexRelation == NULL)
316  continue;
317 
318  indexInfo = indexInfoArray[i];
319 
320  /* Record if speculative insertion arbiter */
321  arbiter = list_member_oid(arbiterIndexes,
322  indexRelation->rd_index->indexrelid);
323 
324  /* If the index is marked as read-only, ignore it */
325  if (!indexInfo->ii_ReadyForInserts)
326  continue;
327 
328  /* Check for partial index */
329  if (indexInfo->ii_Predicate != NIL)
330  {
331  List *predicate;
332 
333  /*
334  * If predicate state not set up yet, create it (in the estate's
335  * per-query context)
336  */
337  predicate = indexInfo->ii_PredicateState;
338  if (predicate == NIL)
339  {
340  predicate = (List *)
341  ExecPrepareExpr((Expr *) indexInfo->ii_Predicate,
342  estate);
343  indexInfo->ii_PredicateState = predicate;
344  }
345 
346  /* Skip this index-update if the predicate isn't satisfied */
347  if (!ExecQual(predicate, econtext, false))
348  continue;
349  }
350 
351  /*
352  * FormIndexDatum fills in its values and isnull parameters with the
353  * appropriate values for the column(s) of the index.
354  */
355  FormIndexDatum(indexInfo,
356  slot,
357  estate,
358  values,
359  isnull);
360 
361  /*
362  * The index AM does the actual insertion, plus uniqueness checking.
363  *
364  * For an immediate-mode unique index, we just tell the index AM to
365  * throw error if not unique.
366  *
367  * For a deferrable unique index, we tell the index AM to just detect
368  * possible non-uniqueness, and we add the index OID to the result
369  * list if further checking is needed.
370  *
371  * For a speculative insertion (used by INSERT ... ON CONFLICT), do
372  * the same as for a deferrable unique index.
373  */
374  if (!indexRelation->rd_index->indisunique)
375  checkUnique = UNIQUE_CHECK_NO;
376  else if (noDupErr && (arbiterIndexes == NIL || arbiter))
377  checkUnique = UNIQUE_CHECK_PARTIAL;
378  else if (indexRelation->rd_index->indimmediate)
379  checkUnique = UNIQUE_CHECK_YES;
380  else
381  checkUnique = UNIQUE_CHECK_PARTIAL;
382 
383  satisfiesConstraint =
384  index_insert(indexRelation, /* index relation */
385  values, /* array of index Datums */
386  isnull, /* null flags */
387  tupleid, /* tid of heap tuple */
388  heapRelation, /* heap relation */
389  checkUnique); /* type of uniqueness check to do */
390 
391  /*
392  * If the index has an associated exclusion constraint, check that.
393  * This is simpler than the process for uniqueness checks since we
394  * always insert first and then check. If the constraint is deferred,
395  * we check now anyway, but don't throw error on violation or wait for
396  * a conclusive outcome from a concurrent insertion; instead we'll
397  * queue a recheck event. Similarly, noDupErr callers (speculative
398  * inserters) will recheck later, and wait for a conclusive outcome
399  * then.
400  *
401  * An index for an exclusion constraint can't also be UNIQUE (not an
402  * essential property, we just don't allow it in the grammar), so no
403  * need to preserve the prior state of satisfiesConstraint.
404  */
405  if (indexInfo->ii_ExclusionOps != NULL)
406  {
407  bool violationOK;
408  CEOUC_WAIT_MODE waitMode;
409 
410  if (noDupErr)
411  {
412  violationOK = true;
414  }
415  else if (!indexRelation->rd_index->indimmediate)
416  {
417  violationOK = true;
418  waitMode = CEOUC_NOWAIT;
419  }
420  else
421  {
422  violationOK = false;
423  waitMode = CEOUC_WAIT;
424  }
425 
426  satisfiesConstraint =
428  indexRelation, indexInfo,
429  tupleid, values, isnull,
430  estate, false,
431  waitMode, violationOK, NULL);
432  }
433 
434  if ((checkUnique == UNIQUE_CHECK_PARTIAL ||
435  indexInfo->ii_ExclusionOps != NULL) &&
436  !satisfiesConstraint)
437  {
438  /*
439  * The tuple potentially violates the uniqueness or exclusion
440  * constraint, so make a note of the index so that we can re-check
441  * it later. Speculative inserters are told if there was a
442  * speculative conflict, since that always requires a restart.
443  */
444  result = lappend_oid(result, RelationGetRelid(indexRelation));
445  if (indexRelation->rd_index->indimmediate && specConflict)
446  *specConflict = true;
447  }
448  }
449 
450  return result;
451 }
452 
453 /* ----------------------------------------------------------------
454  * ExecCheckIndexConstraints
455  *
456  * This routine checks if a tuple violates any unique or
457  * exclusion constraints. Returns true if there is no conflict.
458  * Otherwise returns false, and the TID of the conflicting
459  * tuple is returned in *conflictTid.
460  *
461  * If 'arbiterIndexes' is given, only those indexes are checked.
462  * NIL means all indexes.
463  *
464  * Note that this doesn't lock the values in any way, so it's
465  * possible that a conflicting tuple is inserted immediately
466  * after this returns. But this can be used for a pre-check
467  * before insertion.
468  * ----------------------------------------------------------------
469  */
470 bool
472  EState *estate, ItemPointer conflictTid,
473  List *arbiterIndexes)
474 {
475  ResultRelInfo *resultRelInfo;
476  int i;
477  int numIndices;
478  RelationPtr relationDescs;
479  Relation heapRelation;
480  IndexInfo **indexInfoArray;
481  ExprContext *econtext;
483  bool isnull[INDEX_MAX_KEYS];
484  ItemPointerData invalidItemPtr;
485  bool checkedIndex = false;
486 
487  ItemPointerSetInvalid(conflictTid);
488  ItemPointerSetInvalid(&invalidItemPtr);
489 
490  /*
491  * Get information from the result relation info structure.
492  */
493  resultRelInfo = estate->es_result_relation_info;
494  numIndices = resultRelInfo->ri_NumIndices;
495  relationDescs = resultRelInfo->ri_IndexRelationDescs;
496  indexInfoArray = resultRelInfo->ri_IndexRelationInfo;
497  heapRelation = resultRelInfo->ri_RelationDesc;
498 
499  /*
500  * We will use the EState's per-tuple context for evaluating predicates
501  * and index expressions (creating it if it's not already there).
502  */
503  econtext = GetPerTupleExprContext(estate);
504 
505  /* Arrange for econtext's scan tuple to be the tuple under test */
506  econtext->ecxt_scantuple = slot;
507 
508  /*
509  * For each index, form index tuple and check if it satisfies the
510  * constraint.
511  */
512  for (i = 0; i < numIndices; i++)
513  {
514  Relation indexRelation = relationDescs[i];
515  IndexInfo *indexInfo;
516  bool satisfiesConstraint;
517 
518  if (indexRelation == NULL)
519  continue;
520 
521  indexInfo = indexInfoArray[i];
522 
523  if (!indexInfo->ii_Unique && !indexInfo->ii_ExclusionOps)
524  continue;
525 
526  /* If the index is marked as read-only, ignore it */
527  if (!indexInfo->ii_ReadyForInserts)
528  continue;
529 
530  /* When specific arbiter indexes requested, only examine them */
531  if (arbiterIndexes != NIL &&
532  !list_member_oid(arbiterIndexes,
533  indexRelation->rd_index->indexrelid))
534  continue;
535 
536  if (!indexRelation->rd_index->indimmediate)
537  ereport(ERROR,
538  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
539  errmsg("ON CONFLICT does not support deferrable unique constraints/exclusion constraints as arbiters"),
540  errtableconstraint(heapRelation,
541  RelationGetRelationName(indexRelation))));
542 
543  checkedIndex = true;
544 
545  /* Check for partial index */
546  if (indexInfo->ii_Predicate != NIL)
547  {
548  List *predicate;
549 
550  /*
551  * If predicate state not set up yet, create it (in the estate's
552  * per-query context)
553  */
554  predicate = indexInfo->ii_PredicateState;
555  if (predicate == NIL)
556  {
557  predicate = (List *)
558  ExecPrepareExpr((Expr *) indexInfo->ii_Predicate,
559  estate);
560  indexInfo->ii_PredicateState = predicate;
561  }
562 
563  /* Skip this index-update if the predicate isn't satisfied */
564  if (!ExecQual(predicate, econtext, false))
565  continue;
566  }
567 
568  /*
569  * FormIndexDatum fills in its values and isnull parameters with the
570  * appropriate values for the column(s) of the index.
571  */
572  FormIndexDatum(indexInfo,
573  slot,
574  estate,
575  values,
576  isnull);
577 
578  satisfiesConstraint =
579  check_exclusion_or_unique_constraint(heapRelation, indexRelation,
580  indexInfo, &invalidItemPtr,
581  values, isnull, estate, false,
582  CEOUC_WAIT, true,
583  conflictTid);
584  if (!satisfiesConstraint)
585  return false;
586  }
587 
588  if (arbiterIndexes != NIL && !checkedIndex)
589  elog(ERROR, "unexpected failure to find arbiter index");
590 
591  return true;
592 }
593 
594 /*
595  * Check for violation of an exclusion or unique constraint
596  *
597  * heap: the table containing the new tuple
598  * index: the index supporting the constraint
599  * indexInfo: info about the index, including the exclusion properties
600  * tupleid: heap TID of the new tuple we have just inserted (invalid if we
601  * haven't inserted a new tuple yet)
602  * values, isnull: the *index* column values computed for the new tuple
603  * estate: an EState we can do evaluation in
604  * newIndex: if true, we are trying to build a new index (this affects
605  * only the wording of error messages)
606  * waitMode: whether to wait for concurrent inserters/deleters
607  * violationOK: if true, don't throw error for violation
608  * conflictTid: if not-NULL, the TID of the conflicting tuple is returned here
609  *
610  * Returns true if OK, false if actual or potential violation
611  *
612  * 'waitMode' determines what happens if a conflict is detected with a tuple
613  * that was inserted or deleted by a transaction that's still running.
614  * CEOUC_WAIT means that we wait for the transaction to commit, before
615  * throwing an error or returning. CEOUC_NOWAIT means that we report the
616  * violation immediately; so the violation is only potential, and the caller
617  * must recheck sometime later. This behavior is convenient for deferred
618  * exclusion checks; we need not bother queuing a deferred event if there is
619  * definitely no conflict at insertion time.
620  *
621  * CEOUC_LIVELOCK_PREVENTING_WAIT is like CEOUC_NOWAIT, but we will sometimes
622  * wait anyway, to prevent livelocking if two transactions try inserting at
623  * the same time. This is used with speculative insertions, for INSERT ON
624  * CONFLICT statements. (See notes in file header)
625  *
626  * If violationOK is true, we just report the potential or actual violation to
627  * the caller by returning 'false'. Otherwise we throw a descriptive error
628  * message here. When violationOK is false, a false result is impossible.
629  *
630  * Note: The indexam is normally responsible for checking unique constraints,
631  * so this normally only needs to be used for exclusion constraints. But this
632  * function is also called when doing a "pre-check" for conflicts on a unique
633  * constraint, when doing speculative insertion. Caller may use the returned
634  * conflict TID to take further steps.
635  */
636 static bool
638  IndexInfo *indexInfo,
639  ItemPointer tupleid,
640  Datum *values, bool *isnull,
641  EState *estate, bool newIndex,
642  CEOUC_WAIT_MODE waitMode,
643  bool violationOK,
644  ItemPointer conflictTid)
645 {
646  Oid *constr_procs;
647  uint16 *constr_strats;
648  Oid *index_collations = index->rd_indcollation;
649  int index_natts = index->rd_index->indnatts;
650  IndexScanDesc index_scan;
651  HeapTuple tup;
652  ScanKeyData scankeys[INDEX_MAX_KEYS];
653  SnapshotData DirtySnapshot;
654  int i;
655  bool conflict;
656  bool found_self;
657  ExprContext *econtext;
658  TupleTableSlot *existing_slot;
659  TupleTableSlot *save_scantuple;
660 
661  if (indexInfo->ii_ExclusionOps)
662  {
663  constr_procs = indexInfo->ii_ExclusionProcs;
664  constr_strats = indexInfo->ii_ExclusionStrats;
665  }
666  else
667  {
668  constr_procs = indexInfo->ii_UniqueProcs;
669  constr_strats = indexInfo->ii_UniqueStrats;
670  }
671 
672  /*
673  * If any of the input values are NULL, the constraint check is assumed to
674  * pass (i.e., we assume the operators are strict).
675  */
676  for (i = 0; i < index_natts; i++)
677  {
678  if (isnull[i])
679  return true;
680  }
681 
682  /*
683  * Search the tuples that are in the index for any violations, including
684  * tuples that aren't visible yet.
685  */
686  InitDirtySnapshot(DirtySnapshot);
687 
688  for (i = 0; i < index_natts; i++)
689  {
690  ScanKeyEntryInitialize(&scankeys[i],
691  0,
692  i + 1,
693  constr_strats[i],
694  InvalidOid,
695  index_collations[i],
696  constr_procs[i],
697  values[i]);
698  }
699 
700  /*
701  * Need a TupleTableSlot to put existing tuples in.
702  *
703  * To use FormIndexDatum, we have to make the econtext's scantuple point
704  * to this slot. Be sure to save and restore caller's value for
705  * scantuple.
706  */
707  existing_slot = MakeSingleTupleTableSlot(RelationGetDescr(heap));
708 
709  econtext = GetPerTupleExprContext(estate);
710  save_scantuple = econtext->ecxt_scantuple;
711  econtext->ecxt_scantuple = existing_slot;
712 
713  /*
714  * May have to restart scan from this point if a potential conflict is
715  * found.
716  */
717 retry:
718  conflict = false;
719  found_self = false;
720  index_scan = index_beginscan(heap, index, &DirtySnapshot, index_natts, 0);
721  index_rescan(index_scan, scankeys, index_natts, NULL, 0);
722 
723  while ((tup = index_getnext(index_scan,
725  {
726  TransactionId xwait;
727  ItemPointerData ctid_wait;
728  XLTW_Oper reason_wait;
729  Datum existing_values[INDEX_MAX_KEYS];
730  bool existing_isnull[INDEX_MAX_KEYS];
731  char *error_new;
732  char *error_existing;
733 
734  /*
735  * Ignore the entry for the tuple we're trying to check.
736  */
737  if (ItemPointerIsValid(tupleid) &&
738  ItemPointerEquals(tupleid, &tup->t_self))
739  {
740  if (found_self) /* should not happen */
741  elog(ERROR, "found self tuple multiple times in index \"%s\"",
742  RelationGetRelationName(index));
743  found_self = true;
744  continue;
745  }
746 
747  /*
748  * Extract the index column values and isnull flags from the existing
749  * tuple.
750  */
751  ExecStoreTuple(tup, existing_slot, InvalidBuffer, false);
752  FormIndexDatum(indexInfo, existing_slot, estate,
753  existing_values, existing_isnull);
754 
755  /* If lossy indexscan, must recheck the condition */
756  if (index_scan->xs_recheck)
757  {
758  if (!index_recheck_constraint(index,
759  constr_procs,
760  existing_values,
761  existing_isnull,
762  values))
763  continue; /* tuple doesn't actually match, so no
764  * conflict */
765  }
766 
767  /*
768  * At this point we have either a conflict or a potential conflict.
769  *
770  * If an in-progress transaction is affecting the visibility of this
771  * tuple, we need to wait for it to complete and then recheck (unless
772  * the caller requested not to). For simplicity we do rechecking by
773  * just restarting the whole scan --- this case probably doesn't
774  * happen often enough to be worth trying harder, and anyway we don't
775  * want to hold any index internal locks while waiting.
776  */
777  xwait = TransactionIdIsValid(DirtySnapshot.xmin) ?
778  DirtySnapshot.xmin : DirtySnapshot.xmax;
779 
780  if (TransactionIdIsValid(xwait) &&
781  (waitMode == CEOUC_WAIT ||
782  (waitMode == CEOUC_LIVELOCK_PREVENTING_WAIT &&
783  DirtySnapshot.speculativeToken &&
785  {
786  ctid_wait = tup->t_data->t_ctid;
787  reason_wait = indexInfo->ii_ExclusionOps ?
789  index_endscan(index_scan);
790  if (DirtySnapshot.speculativeToken)
791  SpeculativeInsertionWait(DirtySnapshot.xmin,
792  DirtySnapshot.speculativeToken);
793  else
794  XactLockTableWait(xwait, heap, &ctid_wait, reason_wait);
795  goto retry;
796  }
797 
798  /*
799  * We have a definite conflict (or a potential one, but the caller
800  * didn't want to wait). Return it to caller, or report it.
801  */
802  if (violationOK)
803  {
804  conflict = true;
805  if (conflictTid)
806  *conflictTid = tup->t_self;
807  break;
808  }
809 
810  error_new = BuildIndexValueDescription(index, values, isnull);
811  error_existing = BuildIndexValueDescription(index, existing_values,
812  existing_isnull);
813  if (newIndex)
814  ereport(ERROR,
815  (errcode(ERRCODE_EXCLUSION_VIOLATION),
816  errmsg("could not create exclusion constraint \"%s\"",
817  RelationGetRelationName(index)),
818  error_new && error_existing ?
819  errdetail("Key %s conflicts with key %s.",
820  error_new, error_existing) :
821  errdetail("Key conflicts exist."),
822  errtableconstraint(heap,
823  RelationGetRelationName(index))));
824  else
825  ereport(ERROR,
826  (errcode(ERRCODE_EXCLUSION_VIOLATION),
827  errmsg("conflicting key value violates exclusion constraint \"%s\"",
828  RelationGetRelationName(index)),
829  error_new && error_existing ?
830  errdetail("Key %s conflicts with existing key %s.",
831  error_new, error_existing) :
832  errdetail("Key conflicts with existing key."),
833  errtableconstraint(heap,
834  RelationGetRelationName(index))));
835  }
836 
837  index_endscan(index_scan);
838 
839  /*
840  * Ordinarily, at this point the search should have found the originally
841  * inserted tuple (if any), unless we exited the loop early because of
842  * conflict. However, it is possible to define exclusion constraints for
843  * which that wouldn't be true --- for instance, if the operator is <>. So
844  * we no longer complain if found_self is still false.
845  */
846 
847  econtext->ecxt_scantuple = save_scantuple;
848 
849  ExecDropSingleTupleTableSlot(existing_slot);
850 
851  return !conflict;
852 }
853 
854 /*
855  * Check for violation of an exclusion constraint
856  *
857  * This is a dumbed down version of check_exclusion_or_unique_constraint
858  * for external callers. They don't need all the special modes.
859  */
860 void
862  IndexInfo *indexInfo,
863  ItemPointer tupleid,
864  Datum *values, bool *isnull,
865  EState *estate, bool newIndex)
866 {
867  (void) check_exclusion_or_unique_constraint(heap, index, indexInfo, tupleid,
868  values, isnull,
869  estate, newIndex,
870  CEOUC_WAIT, false, NULL);
871 }
872 
873 /*
874  * Check existing tuple's index values to see if it really matches the
875  * exclusion condition against the new_values. Returns true if conflict.
876  */
877 static bool
879  Datum *existing_values, bool *existing_isnull,
880  Datum *new_values)
881 {
882  int index_natts = index->rd_index->indnatts;
883  int i;
884 
885  for (i = 0; i < index_natts; i++)
886  {
887  /* Assume the exclusion operators are strict */
888  if (existing_isnull[i])
889  return false;
890 
891  if (!DatumGetBool(OidFunctionCall2Coll(constr_procs[i],
892  index->rd_indcollation[i],
893  existing_values[i],
894  new_values[i])))
895  return false;
896  }
897 
898  return true;
899 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:63
void FormIndexDatum(IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
Definition: index.c:1747
int ri_NumIndices
Definition: execnodes.h:329
#define NIL
Definition: pg_list.h:69
TupleTableSlot * ExecStoreTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer, bool shouldFree)
Definition: execTuples.c:323
uint16 * ii_UniqueStrats
Definition: execnodes.h:72
Relation ri_RelationDesc
Definition: execnodes.h:328
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition: execQual.c:5195
List * ii_Predicate
Definition: execnodes.h:65
List * ExecInsertIndexTuples(TupleTableSlot *slot, ItemPointer tupleid, EState *estate, bool noDupErr, bool *specConflict, List *arbiterIndexes)
Definition: execIndexing.c:268
uint32 TransactionId
Definition: c.h:393
bool index_insert(Relation indexRelation, Datum *values, bool *isnull, ItemPointer heap_t_ctid, Relation heapRelation, IndexUniqueCheck checkUnique)
Definition: indexam.c:189
#define RelationGetDescr(relation)
Definition: rel.h:353
#define RelationGetForm(relation)
Definition: rel.h:335
#define InvalidBuffer
Definition: buf.h:25
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:296
Oid * ii_ExclusionProcs
Definition: execnodes.h:68
int errcode(int sqlerrcode)
Definition: elog.c:575
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:1626
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
bool ExecCheckIndexConstraints(TupleTableSlot *slot, EState *estate, ItemPointer conflictTid, List *arbiterIndexes)
Definition: execIndexing.c:471
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:637
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:149
HeapTupleHeader t_data
Definition: htup.h:67
Definition: type.h:90
IndexUniqueCheck
Definition: genam.h:106
#define GetPerTupleExprContext(estate)
Definition: executor.h:322
int errtableconstraint(Relation rel, const char *conname)
Definition: relcache.c:4651
Form_pg_index rd_index
Definition: rel.h:114
unsigned short uint16
Definition: c.h:264
Oid * rd_indcollation
Definition: rel.h:148
static bool index_recheck_constraint(Relation index, Oid *constr_procs, Datum *existing_values, bool *existing_isnull, Datum *new_values)
Definition: execIndexing.c:878
#define ERROR
Definition: elog.h:43
#define InitDirtySnapshot(snapshotdata)
Definition: tqual.h:36
ItemPointerData t_ctid
Definition: htup_details.h:150
ItemPointerData t_self
Definition: htup.h:65
void SpeculativeInsertionWait(TransactionId xid, uint32 token)
Definition: lmgr.c:685
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:414
List * ii_PredicateState
Definition: execnodes.h:66
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:219
Oid * ii_UniqueProcs
Definition: execnodes.h:71
#define RowExclusiveLock
Definition: lockdefs.h:38
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define DatumGetBool(X)
Definition: postgres.h:401
bool ExecQual(List *qual, ExprContext *econtext, bool resultForNull)
Definition: execQual.c:5246
#define RelationGetRelationName(relation)
Definition: rel.h:361
Relation * RelationPtr
Definition: relcache.h:29
TransactionId xmax
Definition: snapshot.h:66
TransactionId xmin
Definition: snapshot.h:65
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc)
Definition: execTuples.c:202
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:326
Datum OidFunctionCall2Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1606
bool ii_ReadyForInserts
Definition: execnodes.h:74
#define ereport(elevel, rest)
Definition: elog.h:122
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
uintptr_t Datum
Definition: postgres.h:374
#define InvalidOid
Definition: postgres_ext.h:36
bool ii_Unique
Definition: execnodes.h:73
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
uint32 speculativeToken
Definition: snapshot.h:101
void XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid, XLTW_Oper oper)
Definition: lmgr.c:554
#define NULL
Definition: c.h:226
#define INDEX_MAX_KEYS
static int list_length(const List *l)
Definition: pg_list.h:89
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:122
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:3843
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:171
static Datum values[MAXATTR]
Definition: bootstrap.c:160
XLTW_Oper
Definition: lmgr.h:24
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:135
Oid * ii_ExclusionOps
Definition: execnodes.h:67
void * palloc(Size size)
Definition: mcxt.c:894
int errmsg(const char *fmt,...)
Definition: elog.c:797
void list_free(List *list)
Definition: list.c:1133
IndexInfo ** ri_IndexRelationInfo
Definition: execnodes.h:331
int i
void BuildSpeculativeIndexInfo(Relation index, IndexInfo *ii)
Definition: index.c:1693
#define elog
Definition: elog.h:218
char * BuildIndexValueDescription(Relation indexRelation, Datum *values, bool *isnull)
Definition: genam.c:171
void check_exclusion_constraint(Relation heap, Relation index, IndexInfo *indexInfo, ItemPointer tupleid, Datum *values, bool *isnull, EState *estate, bool newIndex)
Definition: execIndexing.c:861
#define TransactionIdIsValid(xid)
Definition: transam.h:41
uint16 * ii_ExclusionStrats
Definition: execnodes.h:69
CEOUC_WAIT_MODE
Definition: execIndexing.c:118
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:341
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:146
void ExecCloseIndices(ResultRelInfo *resultRelInfo)
Definition: execIndexing.c:224
RelationPtr ri_IndexRelationDescs
Definition: execnodes.h:330
HeapTuple index_getnext(IndexScanDesc scan, ScanDirection direction)
Definition: indexam.c:533
#define lfirst_oid(lc)
Definition: pg_list.h:108
IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys)
Definition: indexam.c:215
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:373