]> granicus.if.org Git - postgresql/blob - src/backend/executor/execIndexing.c
Update copyright via script for 2017
[postgresql] / src / backend / executor / execIndexing.c
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-2017, 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 {
120         CEOUC_WAIT,
121         CEOUC_NOWAIT,
122         CEOUC_LIVELOCK_PREVENTING_WAIT
123 } CEOUC_WAIT_MODE;
124
125 static bool check_exclusion_or_unique_constraint(Relation heap, Relation index,
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
224 ExecCloseIndices(ResultRelInfo *resultRelInfo)
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  *              If 'arbiterIndexes' is nonempty, noDupErr applies only to
263  *              those indexes.  NIL means noDupErr applies to all indexes.
264  *
265  *              CAUTION: this must not be called for a HOT update.
266  *              We can't defend against that here for lack of info.
267  *              Should we change the API to make it safer?
268  * ----------------------------------------------------------------
269  */
270 List *
271 ExecInsertIndexTuples(TupleTableSlot *slot,
272                                           ItemPointer tupleid,
273                                           EState *estate,
274                                           bool noDupErr,
275                                           bool *specConflict,
276                                           List *arbiterIndexes)
277 {
278         List       *result = NIL;
279         ResultRelInfo *resultRelInfo;
280         int                     i;
281         int                     numIndices;
282         RelationPtr relationDescs;
283         Relation        heapRelation;
284         IndexInfo **indexInfoArray;
285         ExprContext *econtext;
286         Datum           values[INDEX_MAX_KEYS];
287         bool            isnull[INDEX_MAX_KEYS];
288
289         /*
290          * Get information from the result relation info structure.
291          */
292         resultRelInfo = estate->es_result_relation_info;
293         numIndices = resultRelInfo->ri_NumIndices;
294         relationDescs = resultRelInfo->ri_IndexRelationDescs;
295         indexInfoArray = resultRelInfo->ri_IndexRelationInfo;
296         heapRelation = resultRelInfo->ri_RelationDesc;
297
298         /*
299          * We will use the EState's per-tuple context for evaluating predicates
300          * and index expressions (creating it if it's not already there).
301          */
302         econtext = GetPerTupleExprContext(estate);
303
304         /* Arrange for econtext's scan tuple to be the tuple under test */
305         econtext->ecxt_scantuple = slot;
306
307         /*
308          * for each index, form and insert the index tuple
309          */
310         for (i = 0; i < numIndices; i++)
311         {
312                 Relation        indexRelation = relationDescs[i];
313                 IndexInfo  *indexInfo;
314                 bool            applyNoDupErr;
315                 IndexUniqueCheck checkUnique;
316                 bool            satisfiesConstraint;
317
318                 if (indexRelation == NULL)
319                         continue;
320
321                 indexInfo = indexInfoArray[i];
322
323                 /* If the index is marked as read-only, ignore it */
324                 if (!indexInfo->ii_ReadyForInserts)
325                         continue;
326
327                 /* Check for partial index */
328                 if (indexInfo->ii_Predicate != NIL)
329                 {
330                         List       *predicate;
331
332                         /*
333                          * If predicate state not set up yet, create it (in the estate's
334                          * per-query context)
335                          */
336                         predicate = indexInfo->ii_PredicateState;
337                         if (predicate == NIL)
338                         {
339                                 predicate = (List *)
340                                         ExecPrepareExpr((Expr *) indexInfo->ii_Predicate,
341                                                                         estate);
342                                 indexInfo->ii_PredicateState = predicate;
343                         }
344
345                         /* Skip this index-update if the predicate isn't satisfied */
346                         if (!ExecQual(predicate, econtext, false))
347                                 continue;
348                 }
349
350                 /*
351                  * FormIndexDatum fills in its values and isnull parameters with the
352                  * appropriate values for the column(s) of the index.
353                  */
354                 FormIndexDatum(indexInfo,
355                                            slot,
356                                            estate,
357                                            values,
358                                            isnull);
359
360                 /* Check whether to apply noDupErr to this index */
361                 applyNoDupErr = noDupErr &&
362                         (arbiterIndexes == NIL ||
363                          list_member_oid(arbiterIndexes,
364                                                          indexRelation->rd_index->indexrelid));
365
366                 /*
367                  * The index AM does the actual insertion, plus uniqueness checking.
368                  *
369                  * For an immediate-mode unique index, we just tell the index AM to
370                  * throw error if not unique.
371                  *
372                  * For a deferrable unique index, we tell the index AM to just detect
373                  * possible non-uniqueness, and we add the index OID to the result
374                  * list if further checking is needed.
375                  *
376                  * For a speculative insertion (used by INSERT ... ON CONFLICT), do
377                  * the same as for a deferrable unique index.
378                  */
379                 if (!indexRelation->rd_index->indisunique)
380                         checkUnique = UNIQUE_CHECK_NO;
381                 else if (applyNoDupErr)
382                         checkUnique = UNIQUE_CHECK_PARTIAL;
383                 else if (indexRelation->rd_index->indimmediate)
384                         checkUnique = UNIQUE_CHECK_YES;
385                 else
386                         checkUnique = UNIQUE_CHECK_PARTIAL;
387
388                 satisfiesConstraint =
389                         index_insert(indexRelation, /* index relation */
390                                                  values,        /* array of index Datums */
391                                                  isnull,        /* null flags */
392                                                  tupleid,               /* tid of heap tuple */
393                                                  heapRelation,  /* heap relation */
394                                                  checkUnique);  /* type of uniqueness check to do */
395
396                 /*
397                  * If the index has an associated exclusion constraint, check that.
398                  * This is simpler than the process for uniqueness checks since we
399                  * always insert first and then check.  If the constraint is deferred,
400                  * we check now anyway, but don't throw error on violation or wait for
401                  * a conclusive outcome from a concurrent insertion; instead we'll
402                  * queue a recheck event.  Similarly, noDupErr callers (speculative
403                  * inserters) will recheck later, and wait for a conclusive outcome
404                  * then.
405                  *
406                  * An index for an exclusion constraint can't also be UNIQUE (not an
407                  * essential property, we just don't allow it in the grammar), so no
408                  * need to preserve the prior state of satisfiesConstraint.
409                  */
410                 if (indexInfo->ii_ExclusionOps != NULL)
411                 {
412                         bool            violationOK;
413                         CEOUC_WAIT_MODE waitMode;
414
415                         if (applyNoDupErr)
416                         {
417                                 violationOK = true;
418                                 waitMode = CEOUC_LIVELOCK_PREVENTING_WAIT;
419                         }
420                         else if (!indexRelation->rd_index->indimmediate)
421                         {
422                                 violationOK = true;
423                                 waitMode = CEOUC_NOWAIT;
424                         }
425                         else
426                         {
427                                 violationOK = false;
428                                 waitMode = CEOUC_WAIT;
429                         }
430
431                         satisfiesConstraint =
432                                 check_exclusion_or_unique_constraint(heapRelation,
433                                                                                                          indexRelation, indexInfo,
434                                                                                                          tupleid, values, isnull,
435                                                                                                          estate, false,
436                                                                                                 waitMode, violationOK, NULL);
437                 }
438
439                 if ((checkUnique == UNIQUE_CHECK_PARTIAL ||
440                          indexInfo->ii_ExclusionOps != NULL) &&
441                         !satisfiesConstraint)
442                 {
443                         /*
444                          * The tuple potentially violates the uniqueness or exclusion
445                          * constraint, so make a note of the index so that we can re-check
446                          * it later.  Speculative inserters are told if there was a
447                          * speculative conflict, since that always requires a restart.
448                          */
449                         result = lappend_oid(result, RelationGetRelid(indexRelation));
450                         if (indexRelation->rd_index->indimmediate && specConflict)
451                                 *specConflict = true;
452                 }
453         }
454
455         return result;
456 }
457
458 /* ----------------------------------------------------------------
459  *              ExecCheckIndexConstraints
460  *
461  *              This routine checks if a tuple violates any unique or
462  *              exclusion constraints.  Returns true if there is no conflict.
463  *              Otherwise returns false, and the TID of the conflicting
464  *              tuple is returned in *conflictTid.
465  *
466  *              If 'arbiterIndexes' is given, only those indexes are checked.
467  *              NIL means all indexes.
468  *
469  *              Note that this doesn't lock the values in any way, so it's
470  *              possible that a conflicting tuple is inserted immediately
471  *              after this returns.  But this can be used for a pre-check
472  *              before insertion.
473  * ----------------------------------------------------------------
474  */
475 bool
476 ExecCheckIndexConstraints(TupleTableSlot *slot,
477                                                   EState *estate, ItemPointer conflictTid,
478                                                   List *arbiterIndexes)
479 {
480         ResultRelInfo *resultRelInfo;
481         int                     i;
482         int                     numIndices;
483         RelationPtr relationDescs;
484         Relation        heapRelation;
485         IndexInfo **indexInfoArray;
486         ExprContext *econtext;
487         Datum           values[INDEX_MAX_KEYS];
488         bool            isnull[INDEX_MAX_KEYS];
489         ItemPointerData invalidItemPtr;
490         bool            checkedIndex = false;
491
492         ItemPointerSetInvalid(conflictTid);
493         ItemPointerSetInvalid(&invalidItemPtr);
494
495         /*
496          * Get information from the result relation info structure.
497          */
498         resultRelInfo = estate->es_result_relation_info;
499         numIndices = resultRelInfo->ri_NumIndices;
500         relationDescs = resultRelInfo->ri_IndexRelationDescs;
501         indexInfoArray = resultRelInfo->ri_IndexRelationInfo;
502         heapRelation = resultRelInfo->ri_RelationDesc;
503
504         /*
505          * We will use the EState's per-tuple context for evaluating predicates
506          * and index expressions (creating it if it's not already there).
507          */
508         econtext = GetPerTupleExprContext(estate);
509
510         /* Arrange for econtext's scan tuple to be the tuple under test */
511         econtext->ecxt_scantuple = slot;
512
513         /*
514          * For each index, form index tuple and check if it satisfies the
515          * constraint.
516          */
517         for (i = 0; i < numIndices; i++)
518         {
519                 Relation        indexRelation = relationDescs[i];
520                 IndexInfo  *indexInfo;
521                 bool            satisfiesConstraint;
522
523                 if (indexRelation == NULL)
524                         continue;
525
526                 indexInfo = indexInfoArray[i];
527
528                 if (!indexInfo->ii_Unique && !indexInfo->ii_ExclusionOps)
529                         continue;
530
531                 /* If the index is marked as read-only, ignore it */
532                 if (!indexInfo->ii_ReadyForInserts)
533                         continue;
534
535                 /* When specific arbiter indexes requested, only examine them */
536                 if (arbiterIndexes != NIL &&
537                         !list_member_oid(arbiterIndexes,
538                                                          indexRelation->rd_index->indexrelid))
539                         continue;
540
541                 if (!indexRelation->rd_index->indimmediate)
542                         ereport(ERROR,
543                                         (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
544                                          errmsg("ON CONFLICT does not support deferrable unique constraints/exclusion constraints as arbiters"),
545                                          errtableconstraint(heapRelation,
546                                                                    RelationGetRelationName(indexRelation))));
547
548                 checkedIndex = true;
549
550                 /* Check for partial index */
551                 if (indexInfo->ii_Predicate != NIL)
552                 {
553                         List       *predicate;
554
555                         /*
556                          * If predicate state not set up yet, create it (in the estate's
557                          * per-query context)
558                          */
559                         predicate = indexInfo->ii_PredicateState;
560                         if (predicate == NIL)
561                         {
562                                 predicate = (List *)
563                                         ExecPrepareExpr((Expr *) indexInfo->ii_Predicate,
564                                                                         estate);
565                                 indexInfo->ii_PredicateState = predicate;
566                         }
567
568                         /* Skip this index-update if the predicate isn't satisfied */
569                         if (!ExecQual(predicate, econtext, false))
570                                 continue;
571                 }
572
573                 /*
574                  * FormIndexDatum fills in its values and isnull parameters with the
575                  * appropriate values for the column(s) of the index.
576                  */
577                 FormIndexDatum(indexInfo,
578                                            slot,
579                                            estate,
580                                            values,
581                                            isnull);
582
583                 satisfiesConstraint =
584                         check_exclusion_or_unique_constraint(heapRelation, indexRelation,
585                                                                                                  indexInfo, &invalidItemPtr,
586                                                                                            values, isnull, estate, false,
587                                                                                                  CEOUC_WAIT, true,
588                                                                                                  conflictTid);
589                 if (!satisfiesConstraint)
590                         return false;
591         }
592
593         if (arbiterIndexes != NIL && !checkedIndex)
594                 elog(ERROR, "unexpected failure to find arbiter index");
595
596         return true;
597 }
598
599 /*
600  * Check for violation of an exclusion or unique constraint
601  *
602  * heap: the table containing the new tuple
603  * index: the index supporting the constraint
604  * indexInfo: info about the index, including the exclusion properties
605  * tupleid: heap TID of the new tuple we have just inserted (invalid if we
606  *              haven't inserted a new tuple yet)
607  * values, isnull: the *index* column values computed for the new tuple
608  * estate: an EState we can do evaluation in
609  * newIndex: if true, we are trying to build a new index (this affects
610  *              only the wording of error messages)
611  * waitMode: whether to wait for concurrent inserters/deleters
612  * violationOK: if true, don't throw error for violation
613  * conflictTid: if not-NULL, the TID of the conflicting tuple is returned here
614  *
615  * Returns true if OK, false if actual or potential violation
616  *
617  * 'waitMode' determines what happens if a conflict is detected with a tuple
618  * that was inserted or deleted by a transaction that's still running.
619  * CEOUC_WAIT means that we wait for the transaction to commit, before
620  * throwing an error or returning.  CEOUC_NOWAIT means that we report the
621  * violation immediately; so the violation is only potential, and the caller
622  * must recheck sometime later.  This behavior is convenient for deferred
623  * exclusion checks; we need not bother queuing a deferred event if there is
624  * definitely no conflict at insertion time.
625  *
626  * CEOUC_LIVELOCK_PREVENTING_WAIT is like CEOUC_NOWAIT, but we will sometimes
627  * wait anyway, to prevent livelocking if two transactions try inserting at
628  * the same time.  This is used with speculative insertions, for INSERT ON
629  * CONFLICT statements. (See notes in file header)
630  *
631  * If violationOK is true, we just report the potential or actual violation to
632  * the caller by returning 'false'.  Otherwise we throw a descriptive error
633  * message here.  When violationOK is false, a false result is impossible.
634  *
635  * Note: The indexam is normally responsible for checking unique constraints,
636  * so this normally only needs to be used for exclusion constraints.  But this
637  * function is also called when doing a "pre-check" for conflicts on a unique
638  * constraint, when doing speculative insertion.  Caller may use the returned
639  * conflict TID to take further steps.
640  */
641 static bool
642 check_exclusion_or_unique_constraint(Relation heap, Relation index,
643                                                                          IndexInfo *indexInfo,
644                                                                          ItemPointer tupleid,
645                                                                          Datum *values, bool *isnull,
646                                                                          EState *estate, bool newIndex,
647                                                                          CEOUC_WAIT_MODE waitMode,
648                                                                          bool violationOK,
649                                                                          ItemPointer conflictTid)
650 {
651         Oid                *constr_procs;
652         uint16     *constr_strats;
653         Oid                *index_collations = index->rd_indcollation;
654         int                     index_natts = index->rd_index->indnatts;
655         IndexScanDesc index_scan;
656         HeapTuple       tup;
657         ScanKeyData scankeys[INDEX_MAX_KEYS];
658         SnapshotData DirtySnapshot;
659         int                     i;
660         bool            conflict;
661         bool            found_self;
662         ExprContext *econtext;
663         TupleTableSlot *existing_slot;
664         TupleTableSlot *save_scantuple;
665
666         if (indexInfo->ii_ExclusionOps)
667         {
668                 constr_procs = indexInfo->ii_ExclusionProcs;
669                 constr_strats = indexInfo->ii_ExclusionStrats;
670         }
671         else
672         {
673                 constr_procs = indexInfo->ii_UniqueProcs;
674                 constr_strats = indexInfo->ii_UniqueStrats;
675         }
676
677         /*
678          * If any of the input values are NULL, the constraint check is assumed to
679          * pass (i.e., we assume the operators are strict).
680          */
681         for (i = 0; i < index_natts; i++)
682         {
683                 if (isnull[i])
684                         return true;
685         }
686
687         /*
688          * Search the tuples that are in the index for any violations, including
689          * tuples that aren't visible yet.
690          */
691         InitDirtySnapshot(DirtySnapshot);
692
693         for (i = 0; i < index_natts; i++)
694         {
695                 ScanKeyEntryInitialize(&scankeys[i],
696                                                            0,
697                                                            i + 1,
698                                                            constr_strats[i],
699                                                            InvalidOid,
700                                                            index_collations[i],
701                                                            constr_procs[i],
702                                                            values[i]);
703         }
704
705         /*
706          * Need a TupleTableSlot to put existing tuples in.
707          *
708          * To use FormIndexDatum, we have to make the econtext's scantuple point
709          * to this slot.  Be sure to save and restore caller's value for
710          * scantuple.
711          */
712         existing_slot = MakeSingleTupleTableSlot(RelationGetDescr(heap));
713
714         econtext = GetPerTupleExprContext(estate);
715         save_scantuple = econtext->ecxt_scantuple;
716         econtext->ecxt_scantuple = existing_slot;
717
718         /*
719          * May have to restart scan from this point if a potential conflict is
720          * found.
721          */
722 retry:
723         conflict = false;
724         found_self = false;
725         index_scan = index_beginscan(heap, index, &DirtySnapshot, index_natts, 0);
726         index_rescan(index_scan, scankeys, index_natts, NULL, 0);
727
728         while ((tup = index_getnext(index_scan,
729                                                                 ForwardScanDirection)) != NULL)
730         {
731                 TransactionId xwait;
732                 ItemPointerData ctid_wait;
733                 XLTW_Oper       reason_wait;
734                 Datum           existing_values[INDEX_MAX_KEYS];
735                 bool            existing_isnull[INDEX_MAX_KEYS];
736                 char       *error_new;
737                 char       *error_existing;
738
739                 /*
740                  * Ignore the entry for the tuple we're trying to check.
741                  */
742                 if (ItemPointerIsValid(tupleid) &&
743                         ItemPointerEquals(tupleid, &tup->t_self))
744                 {
745                         if (found_self)         /* should not happen */
746                                 elog(ERROR, "found self tuple multiple times in index \"%s\"",
747                                          RelationGetRelationName(index));
748                         found_self = true;
749                         continue;
750                 }
751
752                 /*
753                  * Extract the index column values and isnull flags from the existing
754                  * tuple.
755                  */
756                 ExecStoreTuple(tup, existing_slot, InvalidBuffer, false);
757                 FormIndexDatum(indexInfo, existing_slot, estate,
758                                            existing_values, existing_isnull);
759
760                 /* If lossy indexscan, must recheck the condition */
761                 if (index_scan->xs_recheck)
762                 {
763                         if (!index_recheck_constraint(index,
764                                                                                   constr_procs,
765                                                                                   existing_values,
766                                                                                   existing_isnull,
767                                                                                   values))
768                                 continue;               /* tuple doesn't actually match, so no
769                                                                  * conflict */
770                 }
771
772                 /*
773                  * At this point we have either a conflict or a potential conflict.
774                  *
775                  * If an in-progress transaction is affecting the visibility of this
776                  * tuple, we need to wait for it to complete and then recheck (unless
777                  * the caller requested not to).  For simplicity we do rechecking by
778                  * just restarting the whole scan --- this case probably doesn't
779                  * happen often enough to be worth trying harder, and anyway we don't
780                  * want to hold any index internal locks while waiting.
781                  */
782                 xwait = TransactionIdIsValid(DirtySnapshot.xmin) ?
783                         DirtySnapshot.xmin : DirtySnapshot.xmax;
784
785                 if (TransactionIdIsValid(xwait) &&
786                         (waitMode == CEOUC_WAIT ||
787                          (waitMode == CEOUC_LIVELOCK_PREVENTING_WAIT &&
788                           DirtySnapshot.speculativeToken &&
789                           TransactionIdPrecedes(GetCurrentTransactionId(), xwait))))
790                 {
791                         ctid_wait = tup->t_data->t_ctid;
792                         reason_wait = indexInfo->ii_ExclusionOps ?
793                                 XLTW_RecheckExclusionConstr : XLTW_InsertIndex;
794                         index_endscan(index_scan);
795                         if (DirtySnapshot.speculativeToken)
796                                 SpeculativeInsertionWait(DirtySnapshot.xmin,
797                                                                                  DirtySnapshot.speculativeToken);
798                         else
799                                 XactLockTableWait(xwait, heap, &ctid_wait, reason_wait);
800                         goto retry;
801                 }
802
803                 /*
804                  * We have a definite conflict (or a potential one, but the caller
805                  * didn't want to wait).  Return it to caller, or report it.
806                  */
807                 if (violationOK)
808                 {
809                         conflict = true;
810                         if (conflictTid)
811                                 *conflictTid = tup->t_self;
812                         break;
813                 }
814
815                 error_new = BuildIndexValueDescription(index, values, isnull);
816                 error_existing = BuildIndexValueDescription(index, existing_values,
817                                                                                                         existing_isnull);
818                 if (newIndex)
819                         ereport(ERROR,
820                                         (errcode(ERRCODE_EXCLUSION_VIOLATION),
821                                          errmsg("could not create exclusion constraint \"%s\"",
822                                                         RelationGetRelationName(index)),
823                                          error_new && error_existing ?
824                                          errdetail("Key %s conflicts with key %s.",
825                                                            error_new, error_existing) :
826                                          errdetail("Key conflicts exist."),
827                                          errtableconstraint(heap,
828                                                                                 RelationGetRelationName(index))));
829                 else
830                         ereport(ERROR,
831                                         (errcode(ERRCODE_EXCLUSION_VIOLATION),
832                                          errmsg("conflicting key value violates exclusion constraint \"%s\"",
833                                                         RelationGetRelationName(index)),
834                                          error_new && error_existing ?
835                                          errdetail("Key %s conflicts with existing key %s.",
836                                                            error_new, error_existing) :
837                                          errdetail("Key conflicts with existing key."),
838                                          errtableconstraint(heap,
839                                                                                 RelationGetRelationName(index))));
840         }
841
842         index_endscan(index_scan);
843
844         /*
845          * Ordinarily, at this point the search should have found the originally
846          * inserted tuple (if any), unless we exited the loop early because of
847          * conflict.  However, it is possible to define exclusion constraints for
848          * which that wouldn't be true --- for instance, if the operator is <>. So
849          * we no longer complain if found_self is still false.
850          */
851
852         econtext->ecxt_scantuple = save_scantuple;
853
854         ExecDropSingleTupleTableSlot(existing_slot);
855
856         return !conflict;
857 }
858
859 /*
860  * Check for violation of an exclusion constraint
861  *
862  * This is a dumbed down version of check_exclusion_or_unique_constraint
863  * for external callers. They don't need all the special modes.
864  */
865 void
866 check_exclusion_constraint(Relation heap, Relation index,
867                                                    IndexInfo *indexInfo,
868                                                    ItemPointer tupleid,
869                                                    Datum *values, bool *isnull,
870                                                    EState *estate, bool newIndex)
871 {
872         (void) check_exclusion_or_unique_constraint(heap, index, indexInfo, tupleid,
873                                                                                                 values, isnull,
874                                                                                                 estate, newIndex,
875                                                                                                 CEOUC_WAIT, false, NULL);
876 }
877
878 /*
879  * Check existing tuple's index values to see if it really matches the
880  * exclusion condition against the new_values.  Returns true if conflict.
881  */
882 static bool
883 index_recheck_constraint(Relation index, Oid *constr_procs,
884                                                  Datum *existing_values, bool *existing_isnull,
885                                                  Datum *new_values)
886 {
887         int                     index_natts = index->rd_index->indnatts;
888         int                     i;
889
890         for (i = 0; i < index_natts; i++)
891         {
892                 /* Assume the exclusion operators are strict */
893                 if (existing_isnull[i])
894                         return false;
895
896                 if (!DatumGetBool(OidFunctionCall2Coll(constr_procs[i],
897                                                                                            index->rd_indcollation[i],
898                                                                                            existing_values[i],
899                                                                                            new_values[i])))
900                         return false;
901         }
902
903         return true;
904 }