Line data Source code
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-2025, 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/multirangetypes.h"
118 : #include "utils/rangetypes.h"
119 : #include "utils/snapmgr.h"
120 :
121 : /* waitMode argument to check_exclusion_or_unique_constraint() */
122 : typedef enum
123 : {
124 : CEOUC_WAIT,
125 : CEOUC_NOWAIT,
126 : CEOUC_LIVELOCK_PREVENTING_WAIT,
127 : } CEOUC_WAIT_MODE;
128 :
129 : static bool check_exclusion_or_unique_constraint(Relation heap, Relation index,
130 : IndexInfo *indexInfo,
131 : ItemPointer tupleid,
132 : const Datum *values, const bool *isnull,
133 : EState *estate, bool newIndex,
134 : CEOUC_WAIT_MODE waitMode,
135 : bool violationOK,
136 : ItemPointer conflictTid);
137 :
138 : static bool index_recheck_constraint(Relation index, const Oid *constr_procs,
139 : const Datum *existing_values, const bool *existing_isnull,
140 : const Datum *new_values);
141 : static bool index_unchanged_by_update(ResultRelInfo *resultRelInfo,
142 : EState *estate, IndexInfo *indexInfo,
143 : Relation indexRelation);
144 : static bool index_expression_changed_walker(Node *node,
145 : Bitmapset *allUpdatedCols);
146 : static void ExecWithoutOverlapsNotEmpty(Relation rel, NameData attname, Datum attval,
147 : char typtype, Oid atttypid);
148 :
149 : /* ----------------------------------------------------------------
150 : * ExecOpenIndices
151 : *
152 : * Find the indices associated with a result relation, open them,
153 : * and save information about them in the result ResultRelInfo.
154 : *
155 : * At entry, caller has already opened and locked
156 : * resultRelInfo->ri_RelationDesc.
157 : * ----------------------------------------------------------------
158 : */
159 : void
160 1733104 : ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
161 : {
162 1733104 : Relation resultRelation = resultRelInfo->ri_RelationDesc;
163 : List *indexoidlist;
164 : ListCell *l;
165 : int len,
166 : i;
167 : RelationPtr relationDescs;
168 : IndexInfo **indexInfoArray;
169 :
170 1733104 : resultRelInfo->ri_NumIndices = 0;
171 :
172 : /* fast path if no indexes */
173 1733104 : if (!RelationGetForm(resultRelation)->relhasindex)
174 73508 : return;
175 :
176 : /*
177 : * Get cached list of index OIDs
178 : */
179 1659596 : indexoidlist = RelationGetIndexList(resultRelation);
180 1659596 : len = list_length(indexoidlist);
181 1659596 : if (len == 0)
182 40814 : return;
183 :
184 : /* This Assert will fail if ExecOpenIndices is called twice */
185 : Assert(resultRelInfo->ri_IndexRelationDescs == NULL);
186 :
187 : /*
188 : * allocate space for result arrays
189 : */
190 1618782 : relationDescs = (RelationPtr) palloc(len * sizeof(Relation));
191 1618782 : indexInfoArray = (IndexInfo **) palloc(len * sizeof(IndexInfo *));
192 :
193 1618782 : resultRelInfo->ri_NumIndices = len;
194 1618782 : resultRelInfo->ri_IndexRelationDescs = relationDescs;
195 1618782 : resultRelInfo->ri_IndexRelationInfo = indexInfoArray;
196 :
197 : /*
198 : * For each index, open the index relation and save pg_index info. We
199 : * acquire RowExclusiveLock, signifying we will update the index.
200 : *
201 : * Note: we do this even if the index is not indisready; it's not worth
202 : * the trouble to optimize for the case where it isn't.
203 : */
204 1618782 : i = 0;
205 4855170 : foreach(l, indexoidlist)
206 : {
207 3236388 : Oid indexOid = lfirst_oid(l);
208 : Relation indexDesc;
209 : IndexInfo *ii;
210 :
211 3236388 : indexDesc = index_open(indexOid, RowExclusiveLock);
212 :
213 : /* extract index key information from the index's pg_index info */
214 3236388 : ii = BuildIndexInfo(indexDesc);
215 :
216 : /*
217 : * If the indexes are to be used for speculative insertion or conflict
218 : * detection in logical replication, add extra information required by
219 : * unique index entries.
220 : */
221 3236388 : if (speculative && ii->ii_Unique && !indexDesc->rd_index->indisexclusion)
222 176562 : BuildSpeculativeIndexInfo(indexDesc, ii);
223 :
224 3236388 : relationDescs[i] = indexDesc;
225 3236388 : indexInfoArray[i] = ii;
226 3236388 : i++;
227 : }
228 :
229 1618782 : list_free(indexoidlist);
230 : }
231 :
232 : /* ----------------------------------------------------------------
233 : * ExecCloseIndices
234 : *
235 : * Close the index relations stored in resultRelInfo
236 : * ----------------------------------------------------------------
237 : */
238 : void
239 1818704 : ExecCloseIndices(ResultRelInfo *resultRelInfo)
240 : {
241 : int i;
242 : int numIndices;
243 : RelationPtr indexDescs;
244 : IndexInfo **indexInfos;
245 :
246 1818704 : numIndices = resultRelInfo->ri_NumIndices;
247 1818704 : indexDescs = resultRelInfo->ri_IndexRelationDescs;
248 1818704 : indexInfos = resultRelInfo->ri_IndexRelationInfo;
249 :
250 5053162 : for (i = 0; i < numIndices; i++)
251 : {
252 : /* This Assert will fail if ExecCloseIndices is called twice */
253 : Assert(indexDescs[i] != NULL);
254 :
255 : /* Give the index a chance to do some post-insert cleanup */
256 3234458 : index_insert_cleanup(indexDescs[i], indexInfos[i]);
257 :
258 : /* Drop lock acquired by ExecOpenIndices */
259 3234458 : index_close(indexDescs[i], RowExclusiveLock);
260 :
261 : /* Mark the index as closed */
262 3234458 : indexDescs[i] = NULL;
263 : }
264 :
265 : /*
266 : * We don't attempt to free the IndexInfo data structures or the arrays,
267 : * instead assuming that such stuff will be cleaned up automatically in
268 : * FreeExecutorState.
269 : */
270 1818704 : }
271 :
272 : /* ----------------------------------------------------------------
273 : * ExecInsertIndexTuples
274 : *
275 : * This routine takes care of inserting index tuples
276 : * into all the relations indexing the result relation
277 : * when a heap tuple is inserted into the result relation.
278 : *
279 : * When 'update' is true and 'onlySummarizing' is false,
280 : * executor is performing an UPDATE that could not use an
281 : * optimization like heapam's HOT (in more general terms a
282 : * call to table_tuple_update() took place and set
283 : * 'update_indexes' to TUUI_All). Receiving this hint makes
284 : * us consider if we should pass down the 'indexUnchanged'
285 : * hint in turn. That's something that we figure out for
286 : * each index_insert() call iff 'update' is true.
287 : * (When 'update' is false we already know not to pass the
288 : * hint to any index.)
289 : *
290 : * If onlySummarizing is set, an equivalent optimization to
291 : * HOT has been applied and any updated columns are indexed
292 : * only by summarizing indexes (or in more general terms a
293 : * call to table_tuple_update() took place and set
294 : * 'update_indexes' to TUUI_Summarizing). We can (and must)
295 : * therefore only update the indexes that have
296 : * 'amsummarizing' = true.
297 : *
298 : * Unique and exclusion constraints are enforced at the same
299 : * time. This returns a list of index OIDs for any unique or
300 : * exclusion constraints that are deferred and that had
301 : * potential (unconfirmed) conflicts. (if noDupErr == true,
302 : * the same is done for non-deferred constraints, but report
303 : * if conflict was speculative or deferred conflict to caller)
304 : *
305 : * If 'arbiterIndexes' is nonempty, noDupErr applies only to
306 : * those indexes. NIL means noDupErr applies to all indexes.
307 : * ----------------------------------------------------------------
308 : */
309 : List *
310 3570062 : ExecInsertIndexTuples(ResultRelInfo *resultRelInfo,
311 : TupleTableSlot *slot,
312 : EState *estate,
313 : bool update,
314 : bool noDupErr,
315 : bool *specConflict,
316 : List *arbiterIndexes,
317 : bool onlySummarizing)
318 : {
319 3570062 : ItemPointer tupleid = &slot->tts_tid;
320 3570062 : List *result = NIL;
321 : int i;
322 : int numIndices;
323 : RelationPtr relationDescs;
324 : Relation heapRelation;
325 : IndexInfo **indexInfoArray;
326 : ExprContext *econtext;
327 : Datum values[INDEX_MAX_KEYS];
328 : bool isnull[INDEX_MAX_KEYS];
329 :
330 : Assert(ItemPointerIsValid(tupleid));
331 :
332 : /*
333 : * Get information from the result relation info structure.
334 : */
335 3570062 : numIndices = resultRelInfo->ri_NumIndices;
336 3570062 : relationDescs = resultRelInfo->ri_IndexRelationDescs;
337 3570062 : indexInfoArray = resultRelInfo->ri_IndexRelationInfo;
338 3570062 : heapRelation = resultRelInfo->ri_RelationDesc;
339 :
340 : /* Sanity check: slot must belong to the same rel as the resultRelInfo. */
341 : Assert(slot->tts_tableOid == RelationGetRelid(heapRelation));
342 :
343 : /*
344 : * We will use the EState's per-tuple context for evaluating predicates
345 : * and index expressions (creating it if it's not already there).
346 : */
347 3570062 : econtext = GetPerTupleExprContext(estate);
348 :
349 : /* Arrange for econtext's scan tuple to be the tuple under test */
350 3570062 : econtext->ecxt_scantuple = slot;
351 :
352 : /*
353 : * for each index, form and insert the index tuple
354 : */
355 7494402 : for (i = 0; i < numIndices; i++)
356 : {
357 3925032 : Relation indexRelation = relationDescs[i];
358 : IndexInfo *indexInfo;
359 : bool applyNoDupErr;
360 : IndexUniqueCheck checkUnique;
361 : bool indexUnchanged;
362 : bool satisfiesConstraint;
363 :
364 3925032 : if (indexRelation == NULL)
365 0 : continue;
366 :
367 3925032 : indexInfo = indexInfoArray[i];
368 :
369 : /* If the index is marked as read-only, ignore it */
370 3925032 : if (!indexInfo->ii_ReadyForInserts)
371 168 : continue;
372 :
373 : /*
374 : * Skip processing of non-summarizing indexes if we only update
375 : * summarizing indexes
376 : */
377 3924864 : if (onlySummarizing && !indexInfo->ii_Summarizing)
378 6 : continue;
379 :
380 : /* Check for partial index */
381 3924858 : if (indexInfo->ii_Predicate != NIL)
382 : {
383 : ExprState *predicate;
384 :
385 : /*
386 : * If predicate state not set up yet, create it (in the estate's
387 : * per-query context)
388 : */
389 401090 : predicate = indexInfo->ii_PredicateState;
390 401090 : if (predicate == NULL)
391 : {
392 260 : predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
393 260 : indexInfo->ii_PredicateState = predicate;
394 : }
395 :
396 : /* Skip this index-update if the predicate isn't satisfied */
397 401090 : if (!ExecQual(predicate, econtext))
398 400548 : continue;
399 : }
400 :
401 : /*
402 : * FormIndexDatum fills in its values and isnull parameters with the
403 : * appropriate values for the column(s) of the index.
404 : */
405 3524310 : FormIndexDatum(indexInfo,
406 : slot,
407 : estate,
408 : values,
409 : isnull);
410 :
411 : /* Check whether to apply noDupErr to this index */
412 3680504 : applyNoDupErr = noDupErr &&
413 156194 : (arbiterIndexes == NIL ||
414 156194 : list_member_oid(arbiterIndexes,
415 156194 : indexRelation->rd_index->indexrelid));
416 :
417 : /*
418 : * The index AM does the actual insertion, plus uniqueness checking.
419 : *
420 : * For an immediate-mode unique index, we just tell the index AM to
421 : * throw error if not unique.
422 : *
423 : * For a deferrable unique index, we tell the index AM to just detect
424 : * possible non-uniqueness, and we add the index OID to the result
425 : * list if further checking is needed.
426 : *
427 : * For a speculative insertion (used by INSERT ... ON CONFLICT), do
428 : * the same as for a deferrable unique index.
429 : */
430 3524310 : if (!indexRelation->rd_index->indisunique)
431 1886792 : checkUnique = UNIQUE_CHECK_NO;
432 1637518 : else if (applyNoDupErr)
433 156258 : checkUnique = UNIQUE_CHECK_PARTIAL;
434 1481260 : else if (indexRelation->rd_index->indimmediate)
435 1481110 : checkUnique = UNIQUE_CHECK_YES;
436 : else
437 150 : checkUnique = UNIQUE_CHECK_PARTIAL;
438 :
439 : /*
440 : * There's definitely going to be an index_insert() call for this
441 : * index. If we're being called as part of an UPDATE statement,
442 : * consider if the 'indexUnchanged' = true hint should be passed.
443 : */
444 3524310 : indexUnchanged = update && index_unchanged_by_update(resultRelInfo,
445 : estate,
446 : indexInfo,
447 : indexRelation);
448 :
449 : satisfiesConstraint =
450 3524310 : index_insert(indexRelation, /* index relation */
451 : values, /* array of index Datums */
452 : isnull, /* null flags */
453 : tupleid, /* tid of heap tuple */
454 : heapRelation, /* heap relation */
455 : checkUnique, /* type of uniqueness check to do */
456 : indexUnchanged, /* UPDATE without logical change? */
457 : indexInfo); /* index AM may need this */
458 :
459 : /*
460 : * If the index has an associated exclusion constraint, check that.
461 : * This is simpler than the process for uniqueness checks since we
462 : * always insert first and then check. If the constraint is deferred,
463 : * we check now anyway, but don't throw error on violation or wait for
464 : * a conclusive outcome from a concurrent insertion; instead we'll
465 : * queue a recheck event. Similarly, noDupErr callers (speculative
466 : * inserters) will recheck later, and wait for a conclusive outcome
467 : * then.
468 : *
469 : * An index for an exclusion constraint can't also be UNIQUE (not an
470 : * essential property, we just don't allow it in the grammar), so no
471 : * need to preserve the prior state of satisfiesConstraint.
472 : */
473 3523792 : if (indexInfo->ii_ExclusionOps != NULL)
474 : {
475 : bool violationOK;
476 : CEOUC_WAIT_MODE waitMode;
477 :
478 2252 : if (applyNoDupErr)
479 : {
480 144 : violationOK = true;
481 144 : waitMode = CEOUC_LIVELOCK_PREVENTING_WAIT;
482 : }
483 2108 : else if (!indexRelation->rd_index->indimmediate)
484 : {
485 42 : violationOK = true;
486 42 : waitMode = CEOUC_NOWAIT;
487 : }
488 : else
489 : {
490 2066 : violationOK = false;
491 2066 : waitMode = CEOUC_WAIT;
492 : }
493 :
494 : satisfiesConstraint =
495 2252 : check_exclusion_or_unique_constraint(heapRelation,
496 : indexRelation, indexInfo,
497 : tupleid, values, isnull,
498 : estate, false,
499 : waitMode, violationOK, NULL);
500 : }
501 :
502 3523618 : if ((checkUnique == UNIQUE_CHECK_PARTIAL ||
503 3367210 : indexInfo->ii_ExclusionOps != NULL) &&
504 158342 : !satisfiesConstraint)
505 : {
506 : /*
507 : * The tuple potentially violates the uniqueness or exclusion
508 : * constraint, so make a note of the index so that we can re-check
509 : * it later. Speculative inserters are told if there was a
510 : * speculative conflict, since that always requires a restart.
511 : */
512 150 : result = lappend_oid(result, RelationGetRelid(indexRelation));
513 150 : if (indexRelation->rd_index->indimmediate && specConflict)
514 28 : *specConflict = true;
515 : }
516 : }
517 :
518 3569370 : return result;
519 : }
520 :
521 : /* ----------------------------------------------------------------
522 : * ExecCheckIndexConstraints
523 : *
524 : * This routine checks if a tuple violates any unique or
525 : * exclusion constraints. Returns true if there is no conflict.
526 : * Otherwise returns false, and the TID of the conflicting
527 : * tuple is returned in *conflictTid.
528 : *
529 : * If 'arbiterIndexes' is given, only those indexes are checked.
530 : * NIL means all indexes.
531 : *
532 : * Note that this doesn't lock the values in any way, so it's
533 : * possible that a conflicting tuple is inserted immediately
534 : * after this returns. This can be used for either a pre-check
535 : * before insertion or a re-check after finding a conflict.
536 : *
537 : * 'tupleid' should be the TID of the tuple that has been recently
538 : * inserted (or can be invalid if we haven't inserted a new tuple yet).
539 : * This tuple will be excluded from conflict checking.
540 : * ----------------------------------------------------------------
541 : */
542 : bool
543 9570 : ExecCheckIndexConstraints(ResultRelInfo *resultRelInfo, TupleTableSlot *slot,
544 : EState *estate, ItemPointer conflictTid,
545 : ItemPointer tupleid, List *arbiterIndexes)
546 : {
547 : int i;
548 : int numIndices;
549 : RelationPtr relationDescs;
550 : Relation heapRelation;
551 : IndexInfo **indexInfoArray;
552 : ExprContext *econtext;
553 : Datum values[INDEX_MAX_KEYS];
554 : bool isnull[INDEX_MAX_KEYS];
555 : ItemPointerData invalidItemPtr;
556 9570 : bool checkedIndex = false;
557 :
558 9570 : ItemPointerSetInvalid(conflictTid);
559 9570 : ItemPointerSetInvalid(&invalidItemPtr);
560 :
561 : /*
562 : * Get information from the result relation info structure.
563 : */
564 9570 : numIndices = resultRelInfo->ri_NumIndices;
565 9570 : relationDescs = resultRelInfo->ri_IndexRelationDescs;
566 9570 : indexInfoArray = resultRelInfo->ri_IndexRelationInfo;
567 9570 : heapRelation = resultRelInfo->ri_RelationDesc;
568 :
569 : /*
570 : * We will use the EState's per-tuple context for evaluating predicates
571 : * and index expressions (creating it if it's not already there).
572 : */
573 9570 : econtext = GetPerTupleExprContext(estate);
574 :
575 : /* Arrange for econtext's scan tuple to be the tuple under test */
576 9570 : econtext->ecxt_scantuple = slot;
577 :
578 : /*
579 : * For each index, form index tuple and check if it satisfies the
580 : * constraint.
581 : */
582 13788 : for (i = 0; i < numIndices; i++)
583 : {
584 9658 : Relation indexRelation = relationDescs[i];
585 : IndexInfo *indexInfo;
586 : bool satisfiesConstraint;
587 :
588 9658 : if (indexRelation == NULL)
589 0 : continue;
590 :
591 9658 : indexInfo = indexInfoArray[i];
592 :
593 9658 : if (!indexInfo->ii_Unique && !indexInfo->ii_ExclusionOps)
594 4 : continue;
595 :
596 : /* If the index is marked as read-only, ignore it */
597 9654 : if (!indexInfo->ii_ReadyForInserts)
598 0 : continue;
599 :
600 : /* When specific arbiter indexes requested, only examine them */
601 9654 : if (arbiterIndexes != NIL &&
602 9402 : !list_member_oid(arbiterIndexes,
603 9402 : indexRelation->rd_index->indexrelid))
604 78 : continue;
605 :
606 9576 : if (!indexRelation->rd_index->indimmediate)
607 6 : ereport(ERROR,
608 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
609 : errmsg("ON CONFLICT does not support deferrable unique constraints/exclusion constraints as arbiters"),
610 : errtableconstraint(heapRelation,
611 : RelationGetRelationName(indexRelation))));
612 :
613 9570 : checkedIndex = true;
614 :
615 : /* Check for partial index */
616 9570 : if (indexInfo->ii_Predicate != NIL)
617 : {
618 : ExprState *predicate;
619 :
620 : /*
621 : * If predicate state not set up yet, create it (in the estate's
622 : * per-query context)
623 : */
624 36 : predicate = indexInfo->ii_PredicateState;
625 36 : if (predicate == NULL)
626 : {
627 36 : predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
628 36 : indexInfo->ii_PredicateState = predicate;
629 : }
630 :
631 : /* Skip this index-update if the predicate isn't satisfied */
632 36 : if (!ExecQual(predicate, econtext))
633 0 : continue;
634 : }
635 :
636 : /*
637 : * FormIndexDatum fills in its values and isnull parameters with the
638 : * appropriate values for the column(s) of the index.
639 : */
640 9570 : FormIndexDatum(indexInfo,
641 : slot,
642 : estate,
643 : values,
644 : isnull);
645 :
646 : satisfiesConstraint =
647 9570 : check_exclusion_or_unique_constraint(heapRelation, indexRelation,
648 : indexInfo, tupleid,
649 : values, isnull, estate, false,
650 : CEOUC_WAIT, true,
651 : conflictTid);
652 9568 : if (!satisfiesConstraint)
653 5432 : return false;
654 : }
655 :
656 4130 : if (arbiterIndexes != NIL && !checkedIndex)
657 0 : elog(ERROR, "unexpected failure to find arbiter index");
658 :
659 4130 : return true;
660 : }
661 :
662 : /*
663 : * Check for violation of an exclusion or unique constraint
664 : *
665 : * heap: the table containing the new tuple
666 : * index: the index supporting the constraint
667 : * indexInfo: info about the index, including the exclusion properties
668 : * tupleid: heap TID of the new tuple we have just inserted (invalid if we
669 : * haven't inserted a new tuple yet)
670 : * values, isnull: the *index* column values computed for the new tuple
671 : * estate: an EState we can do evaluation in
672 : * newIndex: if true, we are trying to build a new index (this affects
673 : * only the wording of error messages)
674 : * waitMode: whether to wait for concurrent inserters/deleters
675 : * violationOK: if true, don't throw error for violation
676 : * conflictTid: if not-NULL, the TID of the conflicting tuple is returned here
677 : *
678 : * Returns true if OK, false if actual or potential violation
679 : *
680 : * 'waitMode' determines what happens if a conflict is detected with a tuple
681 : * that was inserted or deleted by a transaction that's still running.
682 : * CEOUC_WAIT means that we wait for the transaction to commit, before
683 : * throwing an error or returning. CEOUC_NOWAIT means that we report the
684 : * violation immediately; so the violation is only potential, and the caller
685 : * must recheck sometime later. This behavior is convenient for deferred
686 : * exclusion checks; we need not bother queuing a deferred event if there is
687 : * definitely no conflict at insertion time.
688 : *
689 : * CEOUC_LIVELOCK_PREVENTING_WAIT is like CEOUC_NOWAIT, but we will sometimes
690 : * wait anyway, to prevent livelocking if two transactions try inserting at
691 : * the same time. This is used with speculative insertions, for INSERT ON
692 : * CONFLICT statements. (See notes in file header)
693 : *
694 : * If violationOK is true, we just report the potential or actual violation to
695 : * the caller by returning 'false'. Otherwise we throw a descriptive error
696 : * message here. When violationOK is false, a false result is impossible.
697 : *
698 : * Note: The indexam is normally responsible for checking unique constraints,
699 : * so this normally only needs to be used for exclusion constraints. But this
700 : * function is also called when doing a "pre-check" for conflicts on a unique
701 : * constraint, when doing speculative insertion. Caller may use the returned
702 : * conflict TID to take further steps.
703 : */
704 : static bool
705 12300 : check_exclusion_or_unique_constraint(Relation heap, Relation index,
706 : IndexInfo *indexInfo,
707 : ItemPointer tupleid,
708 : const Datum *values, const bool *isnull,
709 : EState *estate, bool newIndex,
710 : CEOUC_WAIT_MODE waitMode,
711 : bool violationOK,
712 : ItemPointer conflictTid)
713 : {
714 : Oid *constr_procs;
715 : uint16 *constr_strats;
716 12300 : Oid *index_collations = index->rd_indcollation;
717 12300 : int indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index);
718 : IndexScanDesc index_scan;
719 : ScanKeyData scankeys[INDEX_MAX_KEYS];
720 : SnapshotData DirtySnapshot;
721 : int i;
722 : bool conflict;
723 : bool found_self;
724 : ExprContext *econtext;
725 : TupleTableSlot *existing_slot;
726 : TupleTableSlot *save_scantuple;
727 :
728 12300 : if (indexInfo->ii_ExclusionOps)
729 : {
730 2886 : constr_procs = indexInfo->ii_ExclusionProcs;
731 2886 : constr_strats = indexInfo->ii_ExclusionStrats;
732 : }
733 : else
734 : {
735 9414 : constr_procs = indexInfo->ii_UniqueProcs;
736 9414 : constr_strats = indexInfo->ii_UniqueStrats;
737 : }
738 :
739 : /*
740 : * If this is a WITHOUT OVERLAPS constraint, we must also forbid empty
741 : * ranges/multiranges. This must happen before we look for NULLs below, or
742 : * a UNIQUE constraint could insert an empty range along with a NULL
743 : * scalar part.
744 : */
745 12300 : if (indexInfo->ii_WithoutOverlaps)
746 : {
747 : /*
748 : * Look up the type from the heap tuple, but check the Datum from the
749 : * index tuple.
750 : */
751 2480 : AttrNumber attno = indexInfo->ii_IndexAttrNumbers[indnkeyatts - 1];
752 :
753 2480 : if (!isnull[indnkeyatts - 1])
754 : {
755 2420 : TupleDesc tupdesc = RelationGetDescr(heap);
756 2420 : Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
757 2420 : TypeCacheEntry *typcache = lookup_type_cache(att->atttypid, 0);
758 :
759 2420 : ExecWithoutOverlapsNotEmpty(heap, att->attname,
760 2420 : values[indnkeyatts - 1],
761 2420 : typcache->typtype, att->atttypid);
762 : }
763 : }
764 :
765 : /*
766 : * If any of the input values are NULL, and the index uses the default
767 : * nulls-are-distinct mode, the constraint check is assumed to pass (i.e.,
768 : * we assume the operators are strict). Otherwise, we interpret the
769 : * constraint as specifying IS NULL for each column whose input value is
770 : * NULL.
771 : */
772 12216 : if (!indexInfo->ii_NullsNotDistinct)
773 : {
774 26830 : for (i = 0; i < indnkeyatts; i++)
775 : {
776 14740 : if (isnull[i])
777 120 : return true;
778 : }
779 : }
780 :
781 : /*
782 : * Search the tuples that are in the index for any violations, including
783 : * tuples that aren't visible yet.
784 : */
785 12096 : InitDirtySnapshot(DirtySnapshot);
786 :
787 26662 : for (i = 0; i < indnkeyatts; i++)
788 : {
789 14566 : ScanKeyEntryInitialize(&scankeys[i],
790 14566 : isnull[i] ? SK_ISNULL | SK_SEARCHNULL : 0,
791 14566 : i + 1,
792 14566 : constr_strats[i],
793 : InvalidOid,
794 14566 : index_collations[i],
795 14566 : constr_procs[i],
796 14566 : values[i]);
797 : }
798 :
799 : /*
800 : * Need a TupleTableSlot to put existing tuples in.
801 : *
802 : * To use FormIndexDatum, we have to make the econtext's scantuple point
803 : * to this slot. Be sure to save and restore caller's value for
804 : * scantuple.
805 : */
806 12096 : existing_slot = table_slot_create(heap, NULL);
807 :
808 12096 : econtext = GetPerTupleExprContext(estate);
809 12096 : save_scantuple = econtext->ecxt_scantuple;
810 12096 : econtext->ecxt_scantuple = existing_slot;
811 :
812 : /*
813 : * May have to restart scan from this point if a potential conflict is
814 : * found.
815 : */
816 12168 : retry:
817 12168 : conflict = false;
818 12168 : found_self = false;
819 12168 : index_scan = index_beginscan(heap, index, &DirtySnapshot, indnkeyatts, 0);
820 12168 : index_rescan(index_scan, scankeys, indnkeyatts, NULL, 0);
821 :
822 14588 : while (index_getnext_slot(index_scan, ForwardScanDirection, existing_slot))
823 : {
824 : TransactionId xwait;
825 : XLTW_Oper reason_wait;
826 : Datum existing_values[INDEX_MAX_KEYS];
827 : bool existing_isnull[INDEX_MAX_KEYS];
828 : char *error_new;
829 : char *error_existing;
830 :
831 : /*
832 : * Ignore the entry for the tuple we're trying to check.
833 : */
834 10748 : if (ItemPointerIsValid(tupleid) &&
835 2630 : ItemPointerEquals(tupleid, &existing_slot->tts_tid))
836 : {
837 2366 : if (found_self) /* should not happen */
838 0 : elog(ERROR, "found self tuple multiple times in index \"%s\"",
839 : RelationGetRelationName(index));
840 2366 : found_self = true;
841 2420 : continue;
842 : }
843 :
844 : /*
845 : * Extract the index column values and isnull flags from the existing
846 : * tuple.
847 : */
848 5752 : FormIndexDatum(indexInfo, existing_slot, estate,
849 : existing_values, existing_isnull);
850 :
851 : /* If lossy indexscan, must recheck the condition */
852 5752 : if (index_scan->xs_recheck)
853 : {
854 138 : if (!index_recheck_constraint(index,
855 : constr_procs,
856 : existing_values,
857 : existing_isnull,
858 : values))
859 54 : continue; /* tuple doesn't actually match, so no
860 : * conflict */
861 : }
862 :
863 : /*
864 : * At this point we have either a conflict or a potential conflict.
865 : *
866 : * If an in-progress transaction is affecting the visibility of this
867 : * tuple, we need to wait for it to complete and then recheck (unless
868 : * the caller requested not to). For simplicity we do rechecking by
869 : * just restarting the whole scan --- this case probably doesn't
870 : * happen often enough to be worth trying harder, and anyway we don't
871 : * want to hold any index internal locks while waiting.
872 : */
873 11396 : xwait = TransactionIdIsValid(DirtySnapshot.xmin) ?
874 5698 : DirtySnapshot.xmin : DirtySnapshot.xmax;
875 :
876 5698 : if (TransactionIdIsValid(xwait) &&
877 0 : (waitMode == CEOUC_WAIT ||
878 0 : (waitMode == CEOUC_LIVELOCK_PREVENTING_WAIT &&
879 0 : DirtySnapshot.speculativeToken &&
880 0 : TransactionIdPrecedes(GetCurrentTransactionId(), xwait))))
881 : {
882 148 : reason_wait = indexInfo->ii_ExclusionOps ?
883 74 : XLTW_RecheckExclusionConstr : XLTW_InsertIndex;
884 74 : index_endscan(index_scan);
885 74 : if (DirtySnapshot.speculativeToken)
886 2 : SpeculativeInsertionWait(DirtySnapshot.xmin,
887 : DirtySnapshot.speculativeToken);
888 : else
889 72 : XactLockTableWait(xwait, heap,
890 : &existing_slot->tts_tid, reason_wait);
891 72 : goto retry;
892 : }
893 :
894 : /*
895 : * We have a definite conflict (or a potential one, but the caller
896 : * didn't want to wait). Return it to caller, or report it.
897 : */
898 5624 : if (violationOK)
899 : {
900 5456 : conflict = true;
901 5456 : if (conflictTid)
902 5432 : *conflictTid = existing_slot->tts_tid;
903 5456 : break;
904 : }
905 :
906 168 : error_new = BuildIndexValueDescription(index, values, isnull);
907 168 : error_existing = BuildIndexValueDescription(index, existing_values,
908 : existing_isnull);
909 168 : if (newIndex)
910 36 : ereport(ERROR,
911 : (errcode(ERRCODE_EXCLUSION_VIOLATION),
912 : errmsg("could not create exclusion constraint \"%s\"",
913 : RelationGetRelationName(index)),
914 : error_new && error_existing ?
915 : errdetail("Key %s conflicts with key %s.",
916 : error_new, error_existing) :
917 : errdetail("Key conflicts exist."),
918 : errtableconstraint(heap,
919 : RelationGetRelationName(index))));
920 : else
921 132 : ereport(ERROR,
922 : (errcode(ERRCODE_EXCLUSION_VIOLATION),
923 : errmsg("conflicting key value violates exclusion constraint \"%s\"",
924 : RelationGetRelationName(index)),
925 : error_new && error_existing ?
926 : errdetail("Key %s conflicts with existing key %s.",
927 : error_new, error_existing) :
928 : errdetail("Key conflicts with existing key."),
929 : errtableconstraint(heap,
930 : RelationGetRelationName(index))));
931 : }
932 :
933 11926 : index_endscan(index_scan);
934 :
935 : /*
936 : * Ordinarily, at this point the search should have found the originally
937 : * inserted tuple (if any), unless we exited the loop early because of
938 : * conflict. However, it is possible to define exclusion constraints for
939 : * which that wouldn't be true --- for instance, if the operator is <>. So
940 : * we no longer complain if found_self is still false.
941 : */
942 :
943 11926 : econtext->ecxt_scantuple = save_scantuple;
944 :
945 11926 : ExecDropSingleTupleTableSlot(existing_slot);
946 :
947 11926 : return !conflict;
948 : }
949 :
950 : /*
951 : * Check for violation of an exclusion constraint
952 : *
953 : * This is a dumbed down version of check_exclusion_or_unique_constraint
954 : * for external callers. They don't need all the special modes.
955 : */
956 : void
957 478 : check_exclusion_constraint(Relation heap, Relation index,
958 : IndexInfo *indexInfo,
959 : ItemPointer tupleid,
960 : const Datum *values, const bool *isnull,
961 : EState *estate, bool newIndex)
962 : {
963 478 : (void) check_exclusion_or_unique_constraint(heap, index, indexInfo, tupleid,
964 : values, isnull,
965 : estate, newIndex,
966 : CEOUC_WAIT, false, NULL);
967 400 : }
968 :
969 : /*
970 : * Check existing tuple's index values to see if it really matches the
971 : * exclusion condition against the new_values. Returns true if conflict.
972 : */
973 : static bool
974 138 : index_recheck_constraint(Relation index, const Oid *constr_procs,
975 : const Datum *existing_values, const bool *existing_isnull,
976 : const Datum *new_values)
977 : {
978 138 : int indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index);
979 : int i;
980 :
981 342 : for (i = 0; i < indnkeyatts; i++)
982 : {
983 : /* Assume the exclusion operators are strict */
984 258 : if (existing_isnull[i])
985 0 : return false;
986 :
987 258 : if (!DatumGetBool(OidFunctionCall2Coll(constr_procs[i],
988 258 : index->rd_indcollation[i],
989 258 : existing_values[i],
990 258 : new_values[i])))
991 54 : return false;
992 : }
993 :
994 84 : return true;
995 : }
996 :
997 : /*
998 : * Check if ExecInsertIndexTuples() should pass indexUnchanged hint.
999 : *
1000 : * When the executor performs an UPDATE that requires a new round of index
1001 : * tuples, determine if we should pass 'indexUnchanged' = true hint for one
1002 : * single index.
1003 : */
1004 : static bool
1005 336188 : index_unchanged_by_update(ResultRelInfo *resultRelInfo, EState *estate,
1006 : IndexInfo *indexInfo, Relation indexRelation)
1007 : {
1008 : Bitmapset *updatedCols;
1009 : Bitmapset *extraUpdatedCols;
1010 : Bitmapset *allUpdatedCols;
1011 336188 : bool hasexpression = false;
1012 : List *idxExprs;
1013 :
1014 : /*
1015 : * Check cache first
1016 : */
1017 336188 : if (indexInfo->ii_CheckedUnchanged)
1018 292558 : return indexInfo->ii_IndexUnchanged;
1019 43630 : indexInfo->ii_CheckedUnchanged = true;
1020 :
1021 : /*
1022 : * Check for indexed attribute overlap with updated columns.
1023 : *
1024 : * Only do this for key columns. A change to a non-key column within an
1025 : * INCLUDE index should not be counted here. Non-key column values are
1026 : * opaque payload state to the index AM, a little like an extra table TID.
1027 : *
1028 : * Note that row-level BEFORE triggers won't affect our behavior, since
1029 : * they don't affect the updatedCols bitmaps generally. It doesn't seem
1030 : * worth the trouble of checking which attributes were changed directly.
1031 : */
1032 43630 : updatedCols = ExecGetUpdatedCols(resultRelInfo, estate);
1033 43630 : extraUpdatedCols = ExecGetExtraUpdatedCols(resultRelInfo, estate);
1034 46686 : for (int attr = 0; attr < indexInfo->ii_NumIndexKeyAttrs; attr++)
1035 : {
1036 45008 : int keycol = indexInfo->ii_IndexAttrNumbers[attr];
1037 :
1038 45008 : if (keycol <= 0)
1039 : {
1040 : /*
1041 : * Skip expressions for now, but remember to deal with them later
1042 : * on
1043 : */
1044 30 : hasexpression = true;
1045 30 : continue;
1046 : }
1047 :
1048 44978 : if (bms_is_member(keycol - FirstLowInvalidHeapAttributeNumber,
1049 3026 : updatedCols) ||
1050 3026 : bms_is_member(keycol - FirstLowInvalidHeapAttributeNumber,
1051 : extraUpdatedCols))
1052 : {
1053 : /* Changed key column -- don't hint for this index */
1054 41952 : indexInfo->ii_IndexUnchanged = false;
1055 41952 : return false;
1056 : }
1057 : }
1058 :
1059 : /*
1060 : * When we get this far and index has no expressions, return true so that
1061 : * index_insert() call will go on to pass 'indexUnchanged' = true hint.
1062 : *
1063 : * The _absence_ of an indexed key attribute that overlaps with updated
1064 : * attributes (in addition to the total absence of indexed expressions)
1065 : * shows that the index as a whole is logically unchanged by UPDATE.
1066 : */
1067 1678 : if (!hasexpression)
1068 : {
1069 1654 : indexInfo->ii_IndexUnchanged = true;
1070 1654 : return true;
1071 : }
1072 :
1073 : /*
1074 : * Need to pass only one bms to expression_tree_walker helper function.
1075 : * Avoid allocating memory in common case where there are no extra cols.
1076 : */
1077 24 : if (!extraUpdatedCols)
1078 24 : allUpdatedCols = updatedCols;
1079 : else
1080 0 : allUpdatedCols = bms_union(updatedCols, extraUpdatedCols);
1081 :
1082 : /*
1083 : * We have to work slightly harder in the event of indexed expressions,
1084 : * but the principle is the same as before: try to find columns (Vars,
1085 : * actually) that overlap with known-updated columns.
1086 : *
1087 : * If we find any matching Vars, don't pass hint for index. Otherwise
1088 : * pass hint.
1089 : */
1090 24 : idxExprs = RelationGetIndexExpressions(indexRelation);
1091 24 : hasexpression = index_expression_changed_walker((Node *) idxExprs,
1092 : allUpdatedCols);
1093 24 : list_free(idxExprs);
1094 24 : if (extraUpdatedCols)
1095 0 : bms_free(allUpdatedCols);
1096 :
1097 24 : if (hasexpression)
1098 : {
1099 18 : indexInfo->ii_IndexUnchanged = false;
1100 18 : return false;
1101 : }
1102 :
1103 : /*
1104 : * Deliberately don't consider index predicates. We should even give the
1105 : * hint when result rel's "updated tuple" has no corresponding index
1106 : * tuple, which is possible with a partial index (provided the usual
1107 : * conditions are met).
1108 : */
1109 6 : indexInfo->ii_IndexUnchanged = true;
1110 6 : return true;
1111 : }
1112 :
1113 : /*
1114 : * Indexed expression helper for index_unchanged_by_update().
1115 : *
1116 : * Returns true when Var that appears within allUpdatedCols located.
1117 : */
1118 : static bool
1119 76 : index_expression_changed_walker(Node *node, Bitmapset *allUpdatedCols)
1120 : {
1121 76 : if (node == NULL)
1122 0 : return false;
1123 :
1124 76 : if (IsA(node, Var))
1125 : {
1126 24 : Var *var = (Var *) node;
1127 :
1128 24 : if (bms_is_member(var->varattno - FirstLowInvalidHeapAttributeNumber,
1129 : allUpdatedCols))
1130 : {
1131 : /* Var was updated -- indicates that we should not hint */
1132 18 : return true;
1133 : }
1134 :
1135 : /* Still haven't found a reason to not pass the hint */
1136 6 : return false;
1137 : }
1138 :
1139 52 : return expression_tree_walker(node, index_expression_changed_walker,
1140 : allUpdatedCols);
1141 : }
1142 :
1143 : /*
1144 : * ExecWithoutOverlapsNotEmpty - raise an error if the tuple has an empty
1145 : * range or multirange in the given attribute.
1146 : */
1147 : static void
1148 2420 : ExecWithoutOverlapsNotEmpty(Relation rel, NameData attname, Datum attval, char typtype, Oid atttypid)
1149 : {
1150 : bool isempty;
1151 : RangeType *r;
1152 : MultirangeType *mr;
1153 :
1154 2420 : switch (typtype)
1155 : {
1156 1340 : case TYPTYPE_RANGE:
1157 1340 : r = DatumGetRangeTypeP(attval);
1158 1340 : isempty = RangeIsEmpty(r);
1159 1340 : break;
1160 1080 : case TYPTYPE_MULTIRANGE:
1161 1080 : mr = DatumGetMultirangeTypeP(attval);
1162 1080 : isempty = MultirangeIsEmpty(mr);
1163 1080 : break;
1164 0 : default:
1165 0 : elog(ERROR, "WITHOUT OVERLAPS column \"%s\" is not a range or multirange",
1166 : NameStr(attname));
1167 : }
1168 :
1169 : /* Report a CHECK_VIOLATION */
1170 2420 : if (isempty)
1171 84 : ereport(ERROR,
1172 : (errcode(ERRCODE_CHECK_VIOLATION),
1173 : errmsg("empty WITHOUT OVERLAPS value found in column \"%s\" in relation \"%s\"",
1174 : NameStr(attname), RelationGetRelationName(rel))));
1175 2336 : }
|