Header And Logo

PostgreSQL
| The world's most advanced open source database.

constraint.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * constraint.c
00004  *    PostgreSQL CONSTRAINT support code.
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  * IDENTIFICATION
00010  *    src/backend/commands/constraint.c
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #include "postgres.h"
00015 
00016 #include "catalog/index.h"
00017 #include "commands/trigger.h"
00018 #include "executor/executor.h"
00019 #include "utils/builtins.h"
00020 #include "utils/rel.h"
00021 #include "utils/tqual.h"
00022 
00023 
00024 /*
00025  * unique_key_recheck - trigger function to do a deferred uniqueness check.
00026  *
00027  * This now also does deferred exclusion-constraint checks, so the name is
00028  * somewhat historical.
00029  *
00030  * This is invoked as an AFTER ROW trigger for both INSERT and UPDATE,
00031  * for any rows recorded as potentially violating a deferrable unique
00032  * or exclusion constraint.
00033  *
00034  * This may be an end-of-statement check, a commit-time check, or a
00035  * check triggered by a SET CONSTRAINTS command.
00036  */
00037 Datum
00038 unique_key_recheck(PG_FUNCTION_ARGS)
00039 {
00040     TriggerData *trigdata = (TriggerData *) fcinfo->context;
00041     const char *funcname = "unique_key_recheck";
00042     HeapTuple   new_row;
00043     ItemPointerData tmptid;
00044     Relation    indexRel;
00045     IndexInfo  *indexInfo;
00046     EState     *estate;
00047     ExprContext *econtext;
00048     TupleTableSlot *slot;
00049     Datum       values[INDEX_MAX_KEYS];
00050     bool        isnull[INDEX_MAX_KEYS];
00051 
00052     /*
00053      * Make sure this is being called as an AFTER ROW trigger.  Note:
00054      * translatable error strings are shared with ri_triggers.c, so resist the
00055      * temptation to fold the function name into them.
00056      */
00057     if (!CALLED_AS_TRIGGER(fcinfo))
00058         ereport(ERROR,
00059                 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
00060                  errmsg("function \"%s\" was not called by trigger manager",
00061                         funcname)));
00062 
00063     if (!TRIGGER_FIRED_AFTER(trigdata->tg_event) ||
00064         !TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
00065         ereport(ERROR,
00066                 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
00067                  errmsg("function \"%s\" must be fired AFTER ROW",
00068                         funcname)));
00069 
00070     /*
00071      * Get the new data that was inserted/updated.
00072      */
00073     if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
00074         new_row = trigdata->tg_trigtuple;
00075     else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
00076         new_row = trigdata->tg_newtuple;
00077     else
00078     {
00079         ereport(ERROR,
00080                 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
00081                  errmsg("function \"%s\" must be fired for INSERT or UPDATE",
00082                         funcname)));
00083         new_row = NULL;         /* keep compiler quiet */
00084     }
00085 
00086     /*
00087      * If the new_row is now dead (ie, inserted and then deleted within our
00088      * transaction), we can skip the check.  However, we have to be careful,
00089      * because this trigger gets queued only in response to index insertions;
00090      * which means it does not get queued for HOT updates.  The row we are
00091      * called for might now be dead, but have a live HOT child, in which case
00092      * we still need to make the check.  Therefore we have to use
00093      * heap_hot_search, not just HeapTupleSatisfiesVisibility as is done in
00094      * the comparable test in RI_FKey_check.
00095      *
00096      * This might look like just an optimization, because the index AM will
00097      * make this identical test before throwing an error.  But it's actually
00098      * needed for correctness, because the index AM will also throw an error
00099      * if it doesn't find the index entry for the row.  If the row's dead then
00100      * it's possible the index entry has also been marked dead, and even
00101      * removed.
00102      */
00103     tmptid = new_row->t_self;
00104     if (!heap_hot_search(&tmptid, trigdata->tg_relation, SnapshotSelf, NULL))
00105     {
00106         /*
00107          * All rows in the HOT chain are dead, so skip the check.
00108          */
00109         return PointerGetDatum(NULL);
00110     }
00111 
00112     /*
00113      * Open the index, acquiring a RowExclusiveLock, just as if we were going
00114      * to update it.  (This protects against possible changes of the index
00115      * schema, not against concurrent updates.)
00116      */
00117     indexRel = index_open(trigdata->tg_trigger->tgconstrindid,
00118                           RowExclusiveLock);
00119     indexInfo = BuildIndexInfo(indexRel);
00120 
00121     /*
00122      * The heap tuple must be put into a slot for FormIndexDatum.
00123      */
00124     slot = MakeSingleTupleTableSlot(RelationGetDescr(trigdata->tg_relation));
00125 
00126     ExecStoreTuple(new_row, slot, InvalidBuffer, false);
00127 
00128     /*
00129      * Typically the index won't have expressions, but if it does we need an
00130      * EState to evaluate them.  We need it for exclusion constraints too,
00131      * even if they are just on simple columns.
00132      */
00133     if (indexInfo->ii_Expressions != NIL ||
00134         indexInfo->ii_ExclusionOps != NULL)
00135     {
00136         estate = CreateExecutorState();
00137         econtext = GetPerTupleExprContext(estate);
00138         econtext->ecxt_scantuple = slot;
00139     }
00140     else
00141         estate = NULL;
00142 
00143     /*
00144      * Form the index values and isnull flags for the index entry that we need
00145      * to check.
00146      *
00147      * Note: if the index uses functions that are not as immutable as they are
00148      * supposed to be, this could produce an index tuple different from the
00149      * original.  The index AM can catch such errors by verifying that it
00150      * finds a matching index entry with the tuple's TID.  For exclusion
00151      * constraints we check this in check_exclusion_constraint().
00152      */
00153     FormIndexDatum(indexInfo, slot, estate, values, isnull);
00154 
00155     /*
00156      * Now do the appropriate check.
00157      */
00158     if (indexInfo->ii_ExclusionOps == NULL)
00159     {
00160         /*
00161          * Note: this is not a real insert; it is a check that the index entry
00162          * that has already been inserted is unique.
00163          */
00164         index_insert(indexRel, values, isnull, &(new_row->t_self),
00165                      trigdata->tg_relation, UNIQUE_CHECK_EXISTING);
00166     }
00167     else
00168     {
00169         /*
00170          * For exclusion constraints we just do the normal check, but now it's
00171          * okay to throw error.
00172          */
00173         check_exclusion_constraint(trigdata->tg_relation, indexRel, indexInfo,
00174                                    &(new_row->t_self), values, isnull,
00175                                    estate, false, false);
00176     }
00177 
00178     /*
00179      * If that worked, then this index entry is unique or non-excluded, and we
00180      * are done.
00181      */
00182     if (estate != NULL)
00183         FreeExecutorState(estate);
00184 
00185     ExecDropSingleTupleTableSlot(slot);
00186 
00187     index_close(indexRel, RowExclusiveLock);
00188 
00189     return PointerGetDatum(NULL);
00190 }