Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * constraint.c
4 : : * PostgreSQL CONSTRAINT support code.
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : * IDENTIFICATION
10 : : * src/backend/commands/constraint.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : : #include "postgres.h"
15 : :
16 : : #include "access/genam.h"
17 : : #include "access/tableam.h"
18 : : #include "catalog/index.h"
19 : : #include "commands/trigger.h"
20 : : #include "executor/executor.h"
21 : : #include "utils/fmgrprotos.h"
22 : : #include "utils/snapmgr.h"
23 : :
24 : :
25 : : /*
26 : : * unique_key_recheck - trigger function to do a deferred uniqueness check.
27 : : *
28 : : * This now also does deferred exclusion-constraint checks, so the name is
29 : : * somewhat historical.
30 : : *
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.
34 : : *
35 : : * This may be an end-of-statement check, a commit-time check, or a
36 : : * check triggered by a SET CONSTRAINTS command.
37 : : */
38 : : Datum
6262 tgl@sss.pgh.pa.us 39 :CBC 114 : unique_key_recheck(PG_FUNCTION_ARGS)
40 : : {
2361 41 : 114 : TriggerData *trigdata = (TriggerData *) fcinfo->context;
6262 42 : 114 : const char *funcname = "unique_key_recheck";
43 : : ItemPointerData checktid;
44 : : ItemPointerData tmptid;
45 : : Relation indexRel;
46 : : IndexInfo *indexInfo;
47 : : EState *estate;
48 : : ExprContext *econtext;
49 : : TupleTableSlot *slot;
50 : : Datum values[INDEX_MAX_KEYS];
51 : : bool isnull[INDEX_MAX_KEYS];
52 : :
53 : : /*
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.
57 : : */
58 [ + - - + ]: 114 : if (!CALLED_AS_TRIGGER(fcinfo))
6262 tgl@sss.pgh.pa.us 59 [ # # ]:UBC 0 : ereport(ERROR,
60 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
61 : : errmsg("function \"%s\" was not called by trigger manager",
62 : : funcname)));
63 : :
6262 tgl@sss.pgh.pa.us 64 [ + - ]:CBC 114 : if (!TRIGGER_FIRED_AFTER(trigdata->tg_event) ||
65 [ - + ]: 114 : !TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
6262 tgl@sss.pgh.pa.us 66 [ # # ]:UBC 0 : ereport(ERROR,
67 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
68 : : errmsg("function \"%s\" must be fired AFTER ROW",
69 : : funcname)));
70 : :
71 : : /*
72 : : * Get the new data that was inserted/updated.
73 : : */
6262 tgl@sss.pgh.pa.us 74 [ + + ]:CBC 114 : if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
5 pg@bowt.ie 75 :GNC 90 : slot = trigdata->tg_trigslot;
6262 tgl@sss.pgh.pa.us 76 [ + - ]:CBC 24 : else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
5 pg@bowt.ie 77 :GNC 24 : slot = trigdata->tg_newslot;
78 : : else
79 : : {
6262 tgl@sss.pgh.pa.us 80 [ # # ]:UBC 0 : ereport(ERROR,
81 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
82 : : errmsg("function \"%s\" must be fired for INSERT or UPDATE",
83 : : funcname)));
84 : : slot = NULL; /* keep compiler quiet */
85 : : }
5 pg@bowt.ie 86 :GNC 114 : checktid = slot->tts_tid;
87 : :
88 : : /*
89 : : * If the row pointed at by checktid is now dead (ie, inserted and then
90 : : * deleted within our transaction), we can skip the check. However, we
91 : : * have to be careful, because this trigger gets queued only in response
92 : : * to index insertions; which means it does not get queued e.g. for HOT
93 : : * updates. The row we are called for might now be dead, but have a live
94 : : * HOT child, in which case we still need to make the check ---
95 : : * effectively, we're applying the check against the live child row,
96 : : * although we can use the values from this row since by definition all
97 : : * columns of interest to us are the same.
98 : : *
99 : : * This might look like just an optimization, because the index AM will
100 : : * make this identical test before throwing an error. But it's actually
101 : : * needed for correctness, because the index AM will also throw an error
102 : : * if it doesn't find the index entry for the row. If the row's dead then
103 : : * it's possible the index entry has also been marked dead, and even
104 : : * removed.
105 : : */
2750 andres@anarazel.de 106 :CBC 114 : tmptid = checktid;
5 pg@bowt.ie 107 [ + + ]:GNC 114 : if (!table_fetch_tid(trigdata->tg_relation, &tmptid, SnapshotSelf, NULL))
108 : : {
109 : : /*
110 : : * All rows referenced by the index entry are dead, so skip the check
111 : : */
112 : 1 : return PointerGetDatum(NULL);
113 : : }
114 : :
115 : : /*
116 : : * Open the index, acquiring a RowExclusiveLock, just as if we were going
117 : : * to update it. (This protects against possible changes of the index
118 : : * schema, not against concurrent updates.)
119 : : */
6262 tgl@sss.pgh.pa.us 120 :CBC 113 : indexRel = index_open(trigdata->tg_trigger->tgconstrindid,
121 : : RowExclusiveLock);
122 : 113 : indexInfo = BuildIndexInfo(indexRel);
123 : :
124 : : /*
125 : : * Typically the index won't have expressions, but if it does we need an
126 : : * EState to evaluate them. We need it for exclusion constraints too,
127 : : * even if they are just on simple columns.
128 : : */
6131 129 [ + - ]: 113 : if (indexInfo->ii_Expressions != NIL ||
130 [ + + ]: 113 : indexInfo->ii_ExclusionOps != NULL)
131 : : {
6262 132 : 16 : estate = CreateExecutorState();
133 [ - + ]: 16 : econtext = GetPerTupleExprContext(estate);
134 : 16 : econtext->ecxt_scantuple = slot;
135 : : }
136 : : else
137 : 97 : estate = NULL;
138 : :
139 : : /*
140 : : * Form the index values and isnull flags for the index entry that we need
141 : : * to check.
142 : : *
143 : : * Note: if the index uses functions that are not as immutable as they are
144 : : * supposed to be, this could produce an index tuple different from the
145 : : * original. The index AM can catch such errors by verifying that it
146 : : * finds a matching index entry with the tuple's TID. For exclusion
147 : : * constraints we check this in check_exclusion_constraint().
148 : : */
149 : 113 : FormIndexDatum(indexInfo, slot, estate, values, isnull);
150 : :
151 : : /*
152 : : * Now do the appropriate check.
153 : : */
6131 154 [ + + ]: 113 : if (indexInfo->ii_ExclusionOps == NULL)
155 : : {
156 : : /*
157 : : * Note: this is not a real insert; it is a check that the index entry
158 : : * that has already been inserted is unique. Passing the tuple's tid
159 : : * (i.e. unmodified by table_fetch_tid()) is correct even if the row
160 : : * is now dead, because that is the TID the index will know about.
161 : : */
2750 andres@anarazel.de 162 : 97 : index_insert(indexRel, values, isnull, &checktid,
163 : : trigdata->tg_relation, UNIQUE_CHECK_EXISTING,
164 : : false, indexInfo);
165 : :
166 : : /* Cleanup cache possibly initialized by index_insert. */
884 tomas.vondra@postgre 167 : 36 : index_insert_cleanup(indexRel, indexInfo);
168 : : }
169 : : else
170 : : {
171 : : /*
172 : : * For exclusion constraints we just do the normal check, but now it's
173 : : * okay to throw error. In the HOT-update case, we must use the live
174 : : * HOT child's TID here, else check_exclusion_constraint will think
175 : : * the child is a conflict.
176 : : */
6131 tgl@sss.pgh.pa.us 177 : 16 : check_exclusion_constraint(trigdata->tg_relation, indexRel, indexInfo,
178 : : &tmptid, values, isnull,
179 : : estate, false);
180 : : }
181 : :
182 : : /*
183 : : * If that worked, then this index entry is unique or non-excluded, and we
184 : : * are done.
185 : : */
6262 186 [ + + ]: 40 : if (estate != NULL)
187 : 4 : FreeExecutorState(estate);
188 : :
189 : 40 : index_close(indexRel, RowExclusiveLock);
190 : :
191 : 40 : return PointerGetDatum(NULL);
192 : : }
|