LCOV - differential code coverage report
Current view: top level - src/backend/commands - constraint.c (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 84.6 % 39 33 6 33
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 1 1 1
Baseline: 16@8cea358b128 Branches: 53.3 % 30 16 14 16
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed (240..) days: 84.6 % 39 33 6 33
Function coverage date bins:
(240..) days: 100.0 % 1 1 1
Branch coverage date bins:
(240..) days: 53.3 % 30 16 14 16

 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-2024, 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
 5373 tgl@sss.pgh.pa.us          39                 :CBC          61 : unique_key_recheck(PG_FUNCTION_ARGS)
                                 40                 :                : {
 1472                            41                 :             61 :     TriggerData *trigdata = (TriggerData *) fcinfo->context;
 5373                            42                 :             61 :     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   [ +  -  -  + ]:             61 :     if (!CALLED_AS_TRIGGER(fcinfo))
 5373 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                 :                : 
 5373 tgl@sss.pgh.pa.us          64         [ +  - ]:CBC          61 :     if (!TRIGGER_FIRED_AFTER(trigdata->tg_event) ||
                                 65         [ -  + ]:             61 :         !TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
 5373 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                 :                :      */
 5373 tgl@sss.pgh.pa.us          74         [ +  + ]:CBC          61 :     if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
 1861 andres@anarazel.de         75                 :             43 :         checktid = trigdata->tg_trigslot->tts_tid;
 5373 tgl@sss.pgh.pa.us          76         [ +  - ]:             18 :     else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
 1861 andres@anarazel.de         77                 :             18 :         checktid = trigdata->tg_newslot->tts_tid;
                                 78                 :                :     else
                                 79                 :                :     {
 5373 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                 :                :         ItemPointerSetInvalid(&checktid);   /* keep compiler quiet */
                                 85                 :                :     }
                                 86                 :                : 
 1861 andres@anarazel.de         87                 :CBC          61 :     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                 :             61 :     tmptid = checktid;
                                108                 :                :     {
                                109                 :             61 :         IndexFetchTableData *scan = table_index_fetch_begin(trigdata->tg_relation);
 1789 tgl@sss.pgh.pa.us         110                 :             61 :         bool        call_again = false;
                                111                 :                : 
 1861 andres@anarazel.de        112         [ -  + ]:             61 :         if (!table_index_fetch_tuple(scan, &tmptid, SnapshotSelf, slot,
                                113                 :                :                                      &call_again, NULL))
                                114                 :                :         {
                                115                 :                :             /*
                                116                 :                :              * All rows referenced by the index entry are dead, so skip the
                                117                 :                :              * check.
                                118                 :                :              */
 1861 andres@anarazel.de        119                 :UBC           0 :             ExecDropSingleTupleTableSlot(slot);
                                120                 :              0 :             table_index_fetch_end(scan);
                                121                 :              0 :             return PointerGetDatum(NULL);
                                122                 :                :         }
 1861 andres@anarazel.de        123                 :CBC          61 :         table_index_fetch_end(scan);
                                124                 :                :     }
                                125                 :                : 
                                126                 :                :     /*
                                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.)
                                130                 :                :      */
 5373 tgl@sss.pgh.pa.us         131                 :             61 :     indexRel = index_open(trigdata->tg_trigger->tgconstrindid,
                                132                 :                :                           RowExclusiveLock);
                                133                 :             61 :     indexInfo = BuildIndexInfo(indexRel);
                                134                 :                : 
                                135                 :                :     /*
                                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.
                                139                 :                :      */
 5242                           140         [ +  - ]:             61 :     if (indexInfo->ii_Expressions != NIL ||
                                141         [ +  + ]:             61 :         indexInfo->ii_ExclusionOps != NULL)
                                142                 :                :     {
 5373                           143                 :             12 :         estate = CreateExecutorState();
                                144         [ -  + ]:             12 :         econtext = GetPerTupleExprContext(estate);
                                145                 :             12 :         econtext->ecxt_scantuple = slot;
                                146                 :                :     }
                                147                 :                :     else
                                148                 :             49 :         estate = NULL;
                                149                 :                : 
                                150                 :                :     /*
                                151                 :                :      * Form the index values and isnull flags for the index entry that we need
                                152                 :                :      * to check.
                                153                 :                :      *
                                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().
                                159                 :                :      */
                                160                 :             61 :     FormIndexDatum(indexInfo, slot, estate, values, isnull);
                                161                 :                : 
                                162                 :                :     /*
                                163                 :                :      * Now do the appropriate check.
                                164                 :                :      */
 5242                           165         [ +  + ]:             61 :     if (indexInfo->ii_ExclusionOps == NULL)
                                166                 :                :     {
                                167                 :                :         /*
                                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
                                172                 :                :          * about.
                                173                 :                :          */
 1861 andres@anarazel.de        174                 :             49 :         index_insert(indexRel, values, isnull, &checktid,
                                175                 :                :                      trigdata->tg_relation, UNIQUE_CHECK_EXISTING,
                                176                 :                :                      false, indexInfo);
                                177                 :                :     }
                                178                 :                :     else
                                179                 :                :     {
                                180                 :                :         /*
                                181                 :                :          * For exclusion constraints we just do the normal check, but now it's
                                182                 :                :          * okay to throw error.  In the HOT-update case, we must use the live
                                183                 :                :          * HOT child's TID here, else check_exclusion_constraint will think
                                184                 :                :          * the child is a conflict.
                                185                 :                :          */
 5242 tgl@sss.pgh.pa.us         186                 :             12 :         check_exclusion_constraint(trigdata->tg_relation, indexRel, indexInfo,
                                187                 :                :                                    &tmptid, values, isnull,
                                188                 :                :                                    estate, false);
                                189                 :                :     }
                                190                 :                : 
                                191                 :                :     /*
                                192                 :                :      * If that worked, then this index entry is unique or non-excluded, and we
                                193                 :                :      * are done.
                                194                 :                :      */
 5373                           195         [ +  + ]:             30 :     if (estate != NULL)
                                196                 :              3 :         FreeExecutorState(estate);
                                197                 :                : 
                                198                 :             30 :     ExecDropSingleTupleTableSlot(slot);
                                199                 :                : 
                                200                 :             30 :     index_close(indexRel, RowExclusiveLock);
                                201                 :                : 
                                202                 :             30 :     return PointerGetDatum(NULL);
                                203                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622