1/*-------------------------------------------------------------------------
4 * PostgreSQL CONSTRAINT support code.
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/backend/commands/constraint.c
12 *-------------------------------------------------------------------------
21#include "utils/fmgrprotos.h"
26 * unique_key_recheck - trigger function to do a deferred uniqueness check.
28 * This now also does deferred exclusion-constraint checks, so the name is
29 * somewhat historical.
31 * This is invoked as an AFTER ROW trigger for both INSERT and UPDATE,
32 * for any rows recorded as potentially violating a deferrable unique
33 * or exclusion constraint.
35 * This may be an end-of-statement check, a commit-time check, or a
36 * check triggered by a SET CONSTRAINTS command.
42 const char *
funcname =
"unique_key_recheck";
54 * Make sure this is being called as an AFTER ROW trigger. Note:
55 * translatable error strings are shared with ri_triggers.c, so resist the
56 * temptation to fold the function name into them.
60 (
errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
61 errmsg(
"function \"%s\" was not called by trigger manager",
67 (
errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
68 errmsg(
"function \"%s\" must be fired AFTER ROW",
72 * Get the new data that was inserted/updated.
81 (
errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
82 errmsg(
"function \"%s\" must be fired for INSERT or UPDATE",
90 * If the row pointed at by checktid is now dead (ie, inserted and then
91 * deleted within our transaction), we can skip the check. However, we
92 * have to be careful, because this trigger gets queued only in response
93 * to index insertions; which means it does not get queued e.g. for HOT
94 * updates. The row we are called for might now be dead, but have a live
95 * HOT child, in which case we still need to make the check ---
96 * effectively, we're applying the check against the live child row,
97 * although we can use the values from this row since by definition all
98 * columns of interest to us are the same.
100 * This might look like just an optimization, because the index AM will
101 * make this identical test before throwing an error. But it's actually
102 * needed for correctness, because the index AM will also throw an error
103 * if it doesn't find the index entry for the row. If the row's dead then
104 * it's possible the index entry has also been marked dead, and even
110 bool call_again =
false;
116 * All rows referenced by the index entry are dead, so skip the
127 * Open the index, acquiring a RowExclusiveLock, just as if we were going
128 * to update it. (This protects against possible changes of the index
129 * schema, not against concurrent updates.)
131 indexRel =
index_open(trigdata->tg_trigger->tgconstrindid,
136 * Typically the index won't have expressions, but if it does we need an
137 * EState to evaluate them. We need it for exclusion constraints too,
138 * even if they are just on simple columns.
140 if (indexInfo->ii_Expressions !=
NIL ||
141 indexInfo->ii_ExclusionOps != NULL)
145 econtext->ecxt_scantuple = slot;
151 * Form the index values and isnull flags for the index entry that we need
154 * Note: if the index uses functions that are not as immutable as they are
155 * supposed to be, this could produce an index tuple different from the
156 * original. The index AM can catch such errors by verifying that it
157 * finds a matching index entry with the tuple's TID. For exclusion
158 * constraints we check this in check_exclusion_constraint().
163 * Now do the appropriate check.
165 if (indexInfo->ii_ExclusionOps == NULL)
168 * Note: this is not a real insert; it is a check that the index entry
169 * that has already been inserted is unique. Passing the tuple's tid
170 * (i.e. unmodified by table_index_fetch_tuple()) is correct even if
171 * the row is now dead, because that is the TID the index will know
178 /* Cleanup cache possibly initialized by index_insert. */
184 * For exclusion constraints we just do the normal check, but now it's
185 * okay to throw error. In the HOT-update case, we must use the live
186 * HOT child's TID here, else check_exclusion_constraint will think
187 * the child is a conflict.
195 * If that worked, then this index entry is unique or non-excluded, and we
static Datum values[MAXATTR]
Datum unique_key_recheck(PG_FUNCTION_ARGS)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void check_exclusion_constraint(Relation heap, Relation index, IndexInfo *indexInfo, ItemPointer tupleid, const Datum *values, const bool *isnull, EState *estate, bool newIndex)
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
void FreeExecutorState(EState *estate)
EState * CreateExecutorState(void)
#define GetPerTupleExprContext(estate)
IndexInfo * BuildIndexInfo(Relation index)
void FormIndexDatum(IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
bool index_insert(Relation indexRelation, Datum *values, bool *isnull, ItemPointer heap_t_ctid, Relation heapRelation, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
void index_insert_cleanup(Relation indexRelation, IndexInfo *indexInfo)
void index_close(Relation relation, LOCKMODE lockmode)
Relation index_open(Oid relationId, LOCKMODE lockmode)
if(TABLE==NULL||TABLE_index==NULL)
static void ItemPointerSetInvalid(ItemPointerData *pointer)
static Datum PointerGetDatum(const void *X)
TupleTableSlot * tg_trigslot
TupleTableSlot * tg_newslot
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
static IndexFetchTableData * table_index_fetch_begin(Relation rel)
static void table_index_fetch_end(struct IndexFetchTableData *scan)
static bool table_index_fetch_tuple(struct IndexFetchTableData *scan, ItemPointer tid, Snapshot snapshot, TupleTableSlot *slot, bool *call_again, bool *all_dead)
#define CALLED_AS_TRIGGER(fcinfo)
#define TRIGGER_FIRED_FOR_ROW(event)
#define TRIGGER_FIRED_AFTER(event)
#define TRIGGER_FIRED_BY_INSERT(event)
#define TRIGGER_FIRED_BY_UPDATE(event)