PostgreSQL Source Code  git master
constraint.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/tableam.h"
#include "catalog/index.h"
#include "commands/trigger.h"
#include "executor/executor.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
Include dependency graph for constraint.c:

Go to the source code of this file.

Functions

Datum unique_key_recheck (PG_FUNCTION_ARGS)
 

Function Documentation

◆ unique_key_recheck()

Datum unique_key_recheck ( PG_FUNCTION_ARGS  )

Definition at line 41 of file constraint.c.

42 {
43  TriggerData *trigdata = (TriggerData *) fcinfo->context;
44  const char *funcname = "unique_key_recheck";
45  ItemPointerData checktid;
46  ItemPointerData tmptid;
47  Relation indexRel;
48  IndexInfo *indexInfo;
49  EState *estate;
50  ExprContext *econtext;
51  TupleTableSlot *slot;
53  bool isnull[INDEX_MAX_KEYS];
54 
55  /*
56  * Make sure this is being called as an AFTER ROW trigger. Note:
57  * translatable error strings are shared with ri_triggers.c, so resist the
58  * temptation to fold the function name into them.
59  */
60  if (!CALLED_AS_TRIGGER(fcinfo))
61  ereport(ERROR,
62  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
63  errmsg("function \"%s\" was not called by trigger manager",
64  funcname)));
65 
66  if (!TRIGGER_FIRED_AFTER(trigdata->tg_event) ||
67  !TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
68  ereport(ERROR,
69  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
70  errmsg("function \"%s\" must be fired AFTER ROW",
71  funcname)));
72 
73  /*
74  * Get the new data that was inserted/updated.
75  */
76  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
77  checktid = trigdata->tg_trigslot->tts_tid;
78  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
79  checktid = trigdata->tg_newslot->tts_tid;
80  else
81  {
82  ereport(ERROR,
83  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
84  errmsg("function \"%s\" must be fired for INSERT or UPDATE",
85  funcname)));
86  ItemPointerSetInvalid(&checktid); /* keep compiler quiet */
87  }
88 
89  slot = table_slot_create(trigdata->tg_relation, NULL);
90 
91  /*
92  * If the row pointed at by checktid is now dead (ie, inserted and then
93  * deleted within our transaction), we can skip the check. However, we
94  * have to be careful, because this trigger gets queued only in response
95  * to index insertions; which means it does not get queued e.g. for HOT
96  * updates. The row we are called for might now be dead, but have a live
97  * HOT child, in which case we still need to make the check ---
98  * effectively, we're applying the check against the live child row,
99  * although we can use the values from this row since by definition all
100  * columns of interest to us are the same.
101  *
102  * This might look like just an optimization, because the index AM will
103  * make this identical test before throwing an error. But it's actually
104  * needed for correctness, because the index AM will also throw an error
105  * if it doesn't find the index entry for the row. If the row's dead then
106  * it's possible the index entry has also been marked dead, and even
107  * removed.
108  */
109  tmptid = checktid;
110  {
112  bool call_again = false;
113 
114  if (!table_index_fetch_tuple(scan, &tmptid, SnapshotSelf, slot,
115  &call_again, NULL))
116  {
117  /*
118  * All rows referenced by the index entry are dead, so skip the
119  * check.
120  */
122  table_index_fetch_end(scan);
123  return PointerGetDatum(NULL);
124  }
125  table_index_fetch_end(scan);
126  }
127 
128  /*
129  * Open the index, acquiring a RowExclusiveLock, just as if we were going
130  * to update it. (This protects against possible changes of the index
131  * schema, not against concurrent updates.)
132  */
133  indexRel = index_open(trigdata->tg_trigger->tgconstrindid,
135  indexInfo = BuildIndexInfo(indexRel);
136 
137  /*
138  * Typically the index won't have expressions, but if it does we need an
139  * EState to evaluate them. We need it for exclusion constraints too,
140  * even if they are just on simple columns.
141  */
142  if (indexInfo->ii_Expressions != NIL ||
143  indexInfo->ii_ExclusionOps != NULL)
144  {
145  estate = CreateExecutorState();
146  econtext = GetPerTupleExprContext(estate);
147  econtext->ecxt_scantuple = slot;
148  }
149  else
150  estate = NULL;
151 
152  /*
153  * Form the index values and isnull flags for the index entry that we need
154  * to check.
155  *
156  * Note: if the index uses functions that are not as immutable as they are
157  * supposed to be, this could produce an index tuple different from the
158  * original. The index AM can catch such errors by verifying that it
159  * finds a matching index entry with the tuple's TID. For exclusion
160  * constraints we check this in check_exclusion_constraint().
161  */
162  FormIndexDatum(indexInfo, slot, estate, values, isnull);
163 
164  /*
165  * Now do the appropriate check.
166  */
167  if (indexInfo->ii_ExclusionOps == NULL)
168  {
169  /*
170  * Note: this is not a real insert; it is a check that the index entry
171  * that has already been inserted is unique. Passing the tuple's tid
172  * (i.e. unmodified by table_index_fetch_tuple()) is correct even if
173  * the row is now dead, because that is the TID the index will know
174  * about.
175  */
176  index_insert(indexRel, values, isnull, &checktid,
178  false, indexInfo);
179  }
180  else
181  {
182  /*
183  * For exclusion constraints we just do the normal check, but now it's
184  * okay to throw error. In the HOT-update case, we must use the live
185  * HOT child's TID here, else check_exclusion_constraint will think
186  * the child is a conflict.
187  */
188  check_exclusion_constraint(trigdata->tg_relation, indexRel, indexInfo,
189  &tmptid, values, isnull,
190  estate, false);
191  }
192 
193  /*
194  * If that worked, then this index entry is unique or non-excluded, and we
195  * are done.
196  */
197  if (estate != NULL)
198  FreeExecutorState(estate);
199 
201 
202  index_close(indexRel, RowExclusiveLock);
203 
204  return PointerGetDatum(NULL);
205 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void check_exclusion_constraint(Relation heap, Relation index, IndexInfo *indexInfo, ItemPointer tupleid, Datum *values, bool *isnull, EState *estate, bool newIndex)
Definition: execIndexing.c:910
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1255
EState * CreateExecutorState(void)
Definition: execUtils.c:93
void FreeExecutorState(EState *estate)
Definition: execUtils.c:194
#define GetPerTupleExprContext(estate)
Definition: executor.h:549
@ UNIQUE_CHECK_EXISTING
Definition: genam.h:120
#define funcname
Definition: indent_codes.h:69
void FormIndexDatum(IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
Definition: index.c:2721
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:2430
bool index_insert(Relation indexRelation, Datum *values, bool *isnull, ItemPointer heap_t_ctid, Relation heapRelation, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: indexam.c:176
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition: itemptr.h:184
#define RowExclusiveLock
Definition: lockdefs.h:38
#define INDEX_MAX_KEYS
#define NIL
Definition: pg_list.h:68
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
#define SnapshotSelf
Definition: snapmgr.h:66
Relation tg_relation
Definition: trigger.h:35
TriggerEvent tg_event
Definition: trigger.h:34
TupleTableSlot * tg_trigslot
Definition: trigger.h:39
TupleTableSlot * tg_newslot
Definition: trigger.h:40
Trigger * tg_trigger
Definition: trigger.h:38
Oid tgconstrindid
Definition: reltrigger.h:34
ItemPointerData tts_tid
Definition: tuptable.h:130
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:91
static IndexFetchTableData * table_index_fetch_begin(Relation rel)
Definition: tableam.h:1187
static void table_index_fetch_end(struct IndexFetchTableData *scan)
Definition: tableam.h:1206
static bool table_index_fetch_tuple(struct IndexFetchTableData *scan, ItemPointer tid, Snapshot snapshot, TupleTableSlot *slot, bool *call_again, bool *all_dead)
Definition: tableam.h:1236
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:122
#define TRIGGER_FIRED_AFTER(event)
Definition: trigger.h:131
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:110
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:116

References CALLED_AS_TRIGGER, ereport, errcode(), errmsg(), ERROR, funcname, if(), INDEX_MAX_KEYS, ItemPointerSetInvalid(), TriggerData::tg_event, TriggerData::tg_newslot, TriggerData::tg_trigslot, TRIGGER_FIRED_AFTER, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_UPDATE, TRIGGER_FIRED_FOR_ROW, TupleTableSlot::tts_tid, and values.