Coverage Report

Created: 2026-08-13 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgres/src/backend/commands/constraint.c
Line
Count
Source
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
39
unique_key_recheck(PG_FUNCTION_ARGS)
40
0
{
41
0
  TriggerData *trigdata = (TriggerData *) fcinfo->context;
42
0
  const char *funcname = "unique_key_recheck";
43
0
  ItemPointerData checktid;
44
0
  ItemPointerData tmptid;
45
0
  Relation  indexRel;
46
0
  IndexInfo  *indexInfo;
47
0
  EState     *estate;
48
0
  ExprContext *econtext;
49
0
  TupleTableSlot *slot;
50
0
  Datum   values[INDEX_MAX_KEYS];
51
0
  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
0
  if (!CALLED_AS_TRIGGER(fcinfo))
59
0
    ereport(ERROR,
60
0
        (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
61
0
         errmsg("function \"%s\" was not called by trigger manager",
62
0
            funcname)));
63
64
0
  if (!TRIGGER_FIRED_AFTER(trigdata->tg_event) ||
65
0
    !TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
66
0
    ereport(ERROR,
67
0
        (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
68
0
         errmsg("function \"%s\" must be fired AFTER ROW",
69
0
            funcname)));
70
71
  /*
72
   * Get the new data that was inserted/updated.
73
   */
74
0
  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
75
0
    checktid = trigdata->tg_trigslot->tts_tid;
76
0
  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
77
0
    checktid = trigdata->tg_newslot->tts_tid;
78
0
  else
79
0
  {
80
0
    ereport(ERROR,
81
0
        (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
82
0
         errmsg("function \"%s\" must be fired for INSERT or UPDATE",
83
0
            funcname)));
84
0
    ItemPointerSetInvalid(&checktid); /* keep compiler quiet */
85
0
  }
86
87
0
  slot = table_slot_create(trigdata->tg_relation, NULL);
88
89
  /*
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.
99
   *
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
105
   * removed.
106
   */
107
0
  tmptid = checktid;
108
0
  {
109
0
    IndexFetchTableData *scan = table_index_fetch_begin(trigdata->tg_relation,
110
0
                              SO_NONE);
111
0
    bool    call_again = false;
112
113
0
    if (!table_index_fetch_tuple(scan, &tmptid, SnapshotSelf, slot,
114
0
                   &call_again, NULL))
115
0
    {
116
      /*
117
       * All rows referenced by the index entry are dead, so skip the
118
       * check.
119
       */
120
0
      ExecDropSingleTupleTableSlot(slot);
121
0
      table_index_fetch_end(scan);
122
0
      return PointerGetDatum(NULL);
123
0
    }
124
0
    table_index_fetch_end(scan);
125
0
  }
126
127
  /*
128
   * Open the index, acquiring a RowExclusiveLock, just as if we were going
129
   * to update it.  (This protects against possible changes of the index
130
   * schema, not against concurrent updates.)
131
   */
132
0
  indexRel = index_open(trigdata->tg_trigger->tgconstrindid,
133
0
              RowExclusiveLock);
134
0
  indexInfo = BuildIndexInfo(indexRel);
135
136
  /*
137
   * Typically the index won't have expressions, but if it does we need an
138
   * EState to evaluate them.  We need it for exclusion constraints too,
139
   * even if they are just on simple columns.
140
   */
141
0
  if (indexInfo->ii_Expressions != NIL ||
142
0
    indexInfo->ii_ExclusionOps != NULL)
143
0
  {
144
0
    estate = CreateExecutorState();
145
0
    econtext = GetPerTupleExprContext(estate);
146
0
    econtext->ecxt_scantuple = slot;
147
0
  }
148
0
  else
149
0
    estate = NULL;
150
151
  /*
152
   * Form the index values and isnull flags for the index entry that we need
153
   * to check.
154
   *
155
   * Note: if the index uses functions that are not as immutable as they are
156
   * supposed to be, this could produce an index tuple different from the
157
   * original.  The index AM can catch such errors by verifying that it
158
   * finds a matching index entry with the tuple's TID.  For exclusion
159
   * constraints we check this in check_exclusion_constraint().
160
   */
161
0
  FormIndexDatum(indexInfo, slot, estate, values, isnull);
162
163
  /*
164
   * Now do the appropriate check.
165
   */
166
0
  if (indexInfo->ii_ExclusionOps == NULL)
167
0
  {
168
    /*
169
     * Note: this is not a real insert; it is a check that the index entry
170
     * that has already been inserted is unique.  Passing the tuple's tid
171
     * (i.e. unmodified by table_index_fetch_tuple()) is correct even if
172
     * the row is now dead, because that is the TID the index will know
173
     * about.
174
     */
175
0
    index_insert(indexRel, values, isnull, &checktid,
176
0
           trigdata->tg_relation, UNIQUE_CHECK_EXISTING,
177
0
           false, indexInfo);
178
179
    /* Cleanup cache possibly initialized by index_insert. */
180
0
    index_insert_cleanup(indexRel, indexInfo);
181
0
  }
182
0
  else
183
0
  {
184
    /*
185
     * For exclusion constraints we just do the normal check, but now it's
186
     * okay to throw error.  In the HOT-update case, we must use the live
187
     * HOT child's TID here, else check_exclusion_constraint will think
188
     * the child is a conflict.
189
     */
190
0
    check_exclusion_constraint(trigdata->tg_relation, indexRel, indexInfo,
191
0
                   &tmptid, values, isnull,
192
0
                   estate, false);
193
0
  }
194
195
  /*
196
   * If that worked, then this index entry is unique or non-excluded, and we
197
   * are done.
198
   */
199
0
  if (estate != NULL)
200
0
    FreeExecutorState(estate);
201
202
0
  ExecDropSingleTupleTableSlot(slot);
203
204
0
  index_close(indexRel, RowExclusiveLock);
205
206
  return PointerGetDatum(NULL);
207
0
}