PostgreSQL Source Code  git master
ri_triggers.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "access/tableam.h"
#include "access/xact.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "executor/executor.h"
#include "executor/spi.h"
#include "lib/ilist.h"
#include "parser/parse_coerce.h"
#include "parser/parse_relation.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/rls.h"
#include "utils/ruleutils.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
Include dependency graph for ri_triggers.c:

Go to the source code of this file.

Data Structures

struct  RI_ConstraintInfo
 
struct  RI_QueryKey
 
struct  RI_QueryHashEntry
 
struct  RI_CompareKey
 
struct  RI_CompareHashEntry
 

Macros

#define RI_MAX_NUMKEYS   INDEX_MAX_KEYS
 
#define RI_INIT_CONSTRAINTHASHSIZE   64
 
#define RI_INIT_QUERYHASHSIZE   (RI_INIT_CONSTRAINTHASHSIZE * 4)
 
#define RI_KEYS_ALL_NULL   0
 
#define RI_KEYS_SOME_NULL   1
 
#define RI_KEYS_NONE_NULL   2
 
#define RI_PLAN_CHECK_LOOKUPPK   1
 
#define RI_PLAN_CHECK_LOOKUPPK_FROM_PK   2
 
#define RI_PLAN_LAST_ON_PK   RI_PLAN_CHECK_LOOKUPPK_FROM_PK
 
#define RI_PLAN_CASCADE_DEL_DODELETE   3
 
#define RI_PLAN_CASCADE_UPD_DOUPDATE   4
 
#define RI_PLAN_RESTRICT_CHECKREF   5
 
#define RI_PLAN_SETNULL_DOUPDATE   6
 
#define RI_PLAN_SETDEFAULT_DOUPDATE   7
 
#define MAX_QUOTED_NAME_LEN   (NAMEDATALEN*2+3)
 
#define MAX_QUOTED_REL_NAME_LEN   (MAX_QUOTED_NAME_LEN*2)
 
#define RIAttName(rel, attnum)   NameStr(*attnumAttName(rel, attnum))
 
#define RIAttType(rel, attnum)   attnumTypeId(rel, attnum)
 
#define RIAttCollation(rel, attnum)   attnumCollationId(rel, attnum)
 
#define RI_TRIGTYPE_INSERT   1
 
#define RI_TRIGTYPE_UPDATE   2
 
#define RI_TRIGTYPE_DELETE   3
 

Typedefs

typedef struct RI_ConstraintInfo RI_ConstraintInfo
 
typedef struct RI_QueryKey RI_QueryKey
 
typedef struct RI_QueryHashEntry RI_QueryHashEntry
 
typedef struct RI_CompareKey RI_CompareKey
 
typedef struct RI_CompareHashEntry RI_CompareHashEntry
 

Functions

static bool ri_Check_Pk_Match (Relation pk_rel, Relation fk_rel, TupleTableSlot *oldslot, const RI_ConstraintInfo *riinfo)
 
static Datum ri_restrict (TriggerData *trigdata, bool is_no_action)
 
static Datum ri_set (TriggerData *trigdata, bool is_set_null)
 
static void quoteOneName (char *buffer, const char *name)
 
static void quoteRelationName (char *buffer, Relation rel)
 
static void ri_GenerateQual (StringInfo buf, const char *sep, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
 
static void ri_GenerateQualCollation (StringInfo buf, Oid collation)
 
static int ri_NullCheck (TupleDesc tupdesc, TupleTableSlot *slot, const RI_ConstraintInfo *riinfo, bool rel_is_pk)
 
static void ri_BuildQueryKey (RI_QueryKey *key, const RI_ConstraintInfo *riinfo, int32 constr_queryno)
 
static bool ri_KeysEqual (Relation rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, const RI_ConstraintInfo *riinfo, bool rel_is_pk)
 
static bool ri_AttributesEqual (Oid eq_opr, Oid typeid, Datum oldvalue, Datum newvalue)
 
static void ri_InitHashTables (void)
 
static void InvalidateConstraintCacheCallBack (Datum arg, int cacheid, uint32 hashvalue)
 
static SPIPlanPtr ri_FetchPreparedPlan (RI_QueryKey *key)
 
static void ri_HashPreparedPlan (RI_QueryKey *key, SPIPlanPtr plan)
 
static RI_CompareHashEntryri_HashCompareOp (Oid eq_opr, Oid typeid)
 
static void ri_CheckTrigger (FunctionCallInfo fcinfo, const char *funcname, int tgkind)
 
static const RI_ConstraintInfori_FetchConstraintInfo (Trigger *trigger, Relation trig_rel, bool rel_is_pk)
 
static const RI_ConstraintInfori_LoadConstraintInfo (Oid constraintOid)
 
static SPIPlanPtr ri_PlanCheck (const char *querystr, int nargs, Oid *argtypes, RI_QueryKey *qkey, Relation fk_rel, Relation pk_rel, bool cache_plan)
 
static bool ri_PerformCheck (const RI_ConstraintInfo *riinfo, RI_QueryKey *qkey, SPIPlanPtr qplan, Relation fk_rel, Relation pk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, bool detectNewRows, int expect_OK)
 
static void ri_ExtractValues (Relation rel, TupleTableSlot *slot, const RI_ConstraintInfo *riinfo, bool rel_is_pk, Datum *vals, char *nulls)
 
static void ri_ReportViolation (const RI_ConstraintInfo *riinfo, Relation pk_rel, Relation fk_rel, TupleTableSlot *violatorslot, TupleDesc tupdesc, int queryno, bool partgone) pg_attribute_noreturn()
 
static Datum RI_FKey_check (TriggerData *trigdata)
 
Datum RI_FKey_check_ins (PG_FUNCTION_ARGS)
 
Datum RI_FKey_check_upd (PG_FUNCTION_ARGS)
 
Datum RI_FKey_noaction_del (PG_FUNCTION_ARGS)
 
Datum RI_FKey_restrict_del (PG_FUNCTION_ARGS)
 
Datum RI_FKey_noaction_upd (PG_FUNCTION_ARGS)
 
Datum RI_FKey_restrict_upd (PG_FUNCTION_ARGS)
 
Datum RI_FKey_cascade_del (PG_FUNCTION_ARGS)
 
Datum RI_FKey_cascade_upd (PG_FUNCTION_ARGS)
 
Datum RI_FKey_setnull_del (PG_FUNCTION_ARGS)
 
Datum RI_FKey_setnull_upd (PG_FUNCTION_ARGS)
 
Datum RI_FKey_setdefault_del (PG_FUNCTION_ARGS)
 
Datum RI_FKey_setdefault_upd (PG_FUNCTION_ARGS)
 
bool RI_FKey_pk_upd_check_required (Trigger *trigger, Relation pk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot)
 
bool RI_FKey_fk_upd_check_required (Trigger *trigger, Relation fk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot)
 
bool RI_Initial_Check (Trigger *trigger, Relation fk_rel, Relation pk_rel)
 
void RI_PartitionRemove_Check (Trigger *trigger, Relation fk_rel, Relation pk_rel)
 
int RI_FKey_trigger_type (Oid tgfoid)
 

Variables

static HTABri_constraint_cache = NULL
 
static HTABri_query_cache = NULL
 
static HTABri_compare_cache = NULL
 
static dlist_head ri_constraint_cache_valid_list
 
static int ri_constraint_cache_valid_count = 0
 

Macro Definition Documentation

◆ MAX_QUOTED_NAME_LEN

◆ MAX_QUOTED_REL_NAME_LEN

◆ RI_INIT_CONSTRAINTHASHSIZE

#define RI_INIT_CONSTRAINTHASHSIZE   64

Definition at line 64 of file ri_triggers.c.

Referenced by ri_InitHashTables().

◆ RI_INIT_QUERYHASHSIZE

#define RI_INIT_QUERYHASHSIZE   (RI_INIT_CONSTRAINTHASHSIZE * 4)

Definition at line 65 of file ri_triggers.c.

Referenced by ri_InitHashTables().

◆ RI_KEYS_ALL_NULL

#define RI_KEYS_ALL_NULL   0

Definition at line 67 of file ri_triggers.c.

Referenced by RI_FKey_check(), RI_FKey_fk_upd_check_required(), and ri_NullCheck().

◆ RI_KEYS_NONE_NULL

#define RI_KEYS_NONE_NULL   2

◆ RI_KEYS_SOME_NULL

#define RI_KEYS_SOME_NULL   1

Definition at line 68 of file ri_triggers.c.

Referenced by RI_FKey_check(), RI_FKey_fk_upd_check_required(), and ri_NullCheck().

◆ RI_MAX_NUMKEYS

◆ RI_PLAN_CASCADE_DEL_DODELETE

#define RI_PLAN_CASCADE_DEL_DODELETE   3

Definition at line 77 of file ri_triggers.c.

Referenced by RI_FKey_cascade_del().

◆ RI_PLAN_CASCADE_UPD_DOUPDATE

#define RI_PLAN_CASCADE_UPD_DOUPDATE   4

Definition at line 78 of file ri_triggers.c.

Referenced by RI_FKey_cascade_upd().

◆ RI_PLAN_CHECK_LOOKUPPK

#define RI_PLAN_CHECK_LOOKUPPK   1

Definition at line 73 of file ri_triggers.c.

Referenced by RI_FKey_check(), RI_Initial_Check(), ri_PerformCheck(), and ri_ReportViolation().

◆ RI_PLAN_CHECK_LOOKUPPK_FROM_PK

#define RI_PLAN_CHECK_LOOKUPPK_FROM_PK   2

Definition at line 74 of file ri_triggers.c.

Referenced by ri_Check_Pk_Match(), and ri_PerformCheck().

◆ RI_PLAN_LAST_ON_PK

#define RI_PLAN_LAST_ON_PK   RI_PLAN_CHECK_LOOKUPPK_FROM_PK

Definition at line 75 of file ri_triggers.c.

Referenced by ri_PerformCheck(), and ri_PlanCheck().

◆ RI_PLAN_RESTRICT_CHECKREF

#define RI_PLAN_RESTRICT_CHECKREF   5

Definition at line 79 of file ri_triggers.c.

Referenced by ri_restrict().

◆ RI_PLAN_SETDEFAULT_DOUPDATE

#define RI_PLAN_SETDEFAULT_DOUPDATE   7

Definition at line 81 of file ri_triggers.c.

Referenced by ri_set().

◆ RI_PLAN_SETNULL_DOUPDATE

#define RI_PLAN_SETNULL_DOUPDATE   6

Definition at line 80 of file ri_triggers.c.

Referenced by ri_set().

◆ RI_TRIGTYPE_DELETE

#define RI_TRIGTYPE_DELETE   3

◆ RI_TRIGTYPE_INSERT

#define RI_TRIGTYPE_INSERT   1

Definition at line 90 of file ri_triggers.c.

Referenced by ri_CheckTrigger(), and RI_FKey_check_ins().

◆ RI_TRIGTYPE_UPDATE

◆ RIAttCollation

#define RIAttCollation (   rel,
  attnum 
)    attnumCollationId(rel, attnum)

◆ RIAttName

◆ RIAttType

Typedef Documentation

◆ RI_CompareHashEntry

◆ RI_CompareKey

typedef struct RI_CompareKey RI_CompareKey

◆ RI_ConstraintInfo

◆ RI_QueryHashEntry

◆ RI_QueryKey

typedef struct RI_QueryKey RI_QueryKey

Function Documentation

◆ InvalidateConstraintCacheCallBack()

static void InvalidateConstraintCacheCallBack ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 2092 of file ri_triggers.c.

References Assert, dlist_mutable_iter::cur, dlist_container, dlist_delete(), dlist_foreach_modify, RI_ConstraintInfo::oidHashValue, ri_constraint_cache_valid_count, RI_ConstraintInfo::valid, and RI_ConstraintInfo::valid_link.

Referenced by ri_InitHashTables().

2093 {
2094  dlist_mutable_iter iter;
2095 
2096  Assert(ri_constraint_cache != NULL);
2097 
2098  /*
2099  * If the list of currently valid entries gets excessively large, we mark
2100  * them all invalid so we can empty the list. This arrangement avoids
2101  * O(N^2) behavior in situations where a session touches many foreign keys
2102  * and also does many ALTER TABLEs, such as a restore from pg_dump.
2103  */
2105  hashvalue = 0; /* pretend it's a cache reset */
2106 
2108  {
2110  valid_link, iter.cur);
2111 
2112  if (hashvalue == 0 || riinfo->oidHashValue == hashvalue)
2113  {
2114  riinfo->valid = false;
2115  /* Remove invalidated entries from the list, too */
2116  dlist_delete(iter.cur);
2118  }
2119  }
2120 }
dlist_node * cur
Definition: ilist.h:180
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:524
static HTAB * ri_constraint_cache
Definition: ri_triggers.c:167
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
static int ri_constraint_cache_valid_count
Definition: ri_triggers.c:171
#define Assert(condition)
Definition: c.h:732
static dlist_head ri_constraint_cache_valid_list
Definition: ri_triggers.c:170

◆ quoteOneName()

static void quoteOneName ( char *  buffer,
const char *  name 
)
static

Definition at line 1786 of file ri_triggers.c.

Referenced by quoteRelationName(), ri_Check_Pk_Match(), RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check(), ri_GenerateQualCollation(), RI_Initial_Check(), RI_PartitionRemove_Check(), ri_restrict(), and ri_set().

1787 {
1788  /* Rather than trying to be smart, just always quote it. */
1789  *buffer++ = '"';
1790  while (*name)
1791  {
1792  if (*name == '"')
1793  *buffer++ = '"';
1794  *buffer++ = *name++;
1795  }
1796  *buffer++ = '"';
1797  *buffer = '\0';
1798 }
const char * name
Definition: encode.c:521

◆ quoteRelationName()

static void quoteRelationName ( char *  buffer,
Relation  rel 
)
static

Definition at line 1806 of file ri_triggers.c.

References get_namespace_name(), quoteOneName(), RelationGetNamespace, and RelationGetRelationName.

Referenced by ri_Check_Pk_Match(), RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check(), RI_Initial_Check(), RI_PartitionRemove_Check(), ri_restrict(), and ri_set().

1807 {
1809  buffer += strlen(buffer);
1810  *buffer++ = '.';
1811  quoteOneName(buffer, RelationGetRelationName(rel));
1812 }
static void quoteOneName(char *buffer, const char *name)
Definition: ri_triggers.c:1786
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3094
#define RelationGetRelationName(relation)
Definition: rel.h:453
#define RelationGetNamespace(relation)
Definition: rel.h:460

◆ ri_AttributesEqual()

static bool ri_AttributesEqual ( Oid  eq_opr,
Oid  typeid,
Datum  oldvalue,
Datum  newvalue 
)
static

Definition at line 2733 of file ri_triggers.c.

References BoolGetDatum, RI_CompareHashEntry::cast_func_finfo, DatumGetBool, RI_CompareHashEntry::eq_opr_finfo, FmgrInfo::fn_oid, FunctionCall2Coll(), FunctionCall3, Int32GetDatum, OidIsValid, and ri_HashCompareOp().

Referenced by ri_KeysEqual().

2735 {
2736  RI_CompareHashEntry *entry = ri_HashCompareOp(eq_opr, typeid);
2737 
2738  /* Do we need to cast the values? */
2739  if (OidIsValid(entry->cast_func_finfo.fn_oid))
2740  {
2741  oldvalue = FunctionCall3(&entry->cast_func_finfo,
2742  oldvalue,
2743  Int32GetDatum(-1), /* typmod */
2744  BoolGetDatum(false)); /* implicit coercion */
2745  newvalue = FunctionCall3(&entry->cast_func_finfo,
2746  newvalue,
2747  Int32GetDatum(-1), /* typmod */
2748  BoolGetDatum(false)); /* implicit coercion */
2749  }
2750 
2751  /*
2752  * Apply the comparison operator.
2753  *
2754  * Note: This function is part of a call stack that determines whether an
2755  * update to a row is significant enough that it needs checking or action
2756  * on the other side of a foreign-key constraint. Therefore, the
2757  * comparison here would need to be done with the collation of the *other*
2758  * table. For simplicity (e.g., we might not even have the other table
2759  * open), we'll just use the default collation here, which could lead to
2760  * some false negatives. All this would break if we ever allow
2761  * database-wide collations to be nondeterministic.
2762  */
2764  DEFAULT_COLLATION_OID,
2765  oldvalue, newvalue));
2766 }
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1150
#define OidIsValid(objectId)
Definition: c.h:638
#define FunctionCall3(flinfo, arg1, arg2, arg3)
Definition: fmgr.h:639
#define DatumGetBool(X)
Definition: postgres.h:393
static RI_CompareHashEntry * ri_HashCompareOp(Oid eq_opr, Oid typeid)
Definition: ri_triggers.c:2775
#define BoolGetDatum(X)
Definition: postgres.h:402
Oid fn_oid
Definition: fmgr.h:59
#define Int32GetDatum(X)
Definition: postgres.h:479
FmgrInfo cast_func_finfo
Definition: ri_triggers.c:160

◆ ri_BuildQueryKey()

static void ri_BuildQueryKey ( RI_QueryKey key,
const RI_ConstraintInfo riinfo,
int32  constr_queryno 
)
static

Definition at line 1893 of file ri_triggers.c.

References RI_QueryKey::constr_id, RI_QueryKey::constr_queryno, and RI_ConstraintInfo::constraint_id.

Referenced by ri_Check_Pk_Match(), RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check(), ri_restrict(), and ri_set().

1895 {
1896  /*
1897  * We assume struct RI_QueryKey contains no padding bytes, else we'd need
1898  * to use memset to clear them.
1899  */
1900  key->constr_id = riinfo->constraint_id;
1901  key->constr_queryno = constr_queryno;
1902 }
int32 constr_queryno
Definition: ri_triggers.c:129

◆ ri_Check_Pk_Match()

static bool ri_Check_Pk_Match ( Relation  pk_rel,
Relation  fk_rel,
TupleTableSlot oldslot,
const RI_ConstraintInfo riinfo 
)
static

Definition at line 452 of file ri_triggers.c.

References appendStringInfo(), appendStringInfoString(), Assert, attname, StringInfoData::data, elog, ERROR, i, initStringInfo(), MAX_QUOTED_NAME_LEN, MAX_QUOTED_REL_NAME_LEN, RI_ConstraintInfo::nkeys, RI_ConstraintInfo::pk_attnums, RI_ConstraintInfo::pp_eq_oprs, quoteOneName(), quoteRelationName(), RelationData::rd_rel, RelationGetDescr, ri_BuildQueryKey(), ri_FetchPreparedPlan(), ri_GenerateQual(), RI_KEYS_NONE_NULL, RI_MAX_NUMKEYS, ri_NullCheck(), ri_PerformCheck(), RI_PLAN_CHECK_LOOKUPPK_FROM_PK, ri_PlanCheck(), RIAttName, RIAttType, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_OK_SELECT, and sprintf.

Referenced by ri_restrict().

455 {
456  SPIPlanPtr qplan;
457  RI_QueryKey qkey;
458  bool result;
459 
460  /* Only called for non-null rows */
461  Assert(ri_NullCheck(RelationGetDescr(pk_rel), oldslot, riinfo, true) == RI_KEYS_NONE_NULL);
462 
463  if (SPI_connect() != SPI_OK_CONNECT)
464  elog(ERROR, "SPI_connect failed");
465 
466  /*
467  * Fetch or prepare a saved plan for checking PK table with values coming
468  * from a PK row
469  */
471 
472  if ((qplan = ri_FetchPreparedPlan(&qkey)) == NULL)
473  {
474  StringInfoData querybuf;
475  char pkrelname[MAX_QUOTED_REL_NAME_LEN];
477  char paramname[16];
478  const char *querysep;
479  const char *pk_only;
480  Oid queryoids[RI_MAX_NUMKEYS];
481 
482  /* ----------
483  * The query string built is
484  * SELECT 1 FROM [ONLY] <pktable> x WHERE pkatt1 = $1 [AND ...]
485  * FOR KEY SHARE OF x
486  * The type id's for the $ parameters are those of the
487  * PK attributes themselves.
488  * ----------
489  */
490  initStringInfo(&querybuf);
491  pk_only = pk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ?
492  "" : "ONLY ";
493  quoteRelationName(pkrelname, pk_rel);
494  appendStringInfo(&querybuf, "SELECT 1 FROM %s%s x",
495  pk_only, pkrelname);
496  querysep = "WHERE";
497  for (int i = 0; i < riinfo->nkeys; i++)
498  {
499  Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
500 
501  quoteOneName(attname,
502  RIAttName(pk_rel, riinfo->pk_attnums[i]));
503  sprintf(paramname, "$%d", i + 1);
504  ri_GenerateQual(&querybuf, querysep,
505  attname, pk_type,
506  riinfo->pp_eq_oprs[i],
507  paramname, pk_type);
508  querysep = "AND";
509  queryoids[i] = pk_type;
510  }
511  appendStringInfoString(&querybuf, " FOR KEY SHARE OF x");
512 
513  /* Prepare and save the plan */
514  qplan = ri_PlanCheck(querybuf.data, riinfo->nkeys, queryoids,
515  &qkey, fk_rel, pk_rel, true);
516  }
517 
518  /*
519  * We have a plan now. Run it.
520  */
521  result = ri_PerformCheck(riinfo, &qkey, qplan,
522  fk_rel, pk_rel,
523  oldslot, NULL,
524  true, /* treat like update */
525  SPI_OK_SELECT);
526 
527  if (SPI_finish() != SPI_OK_FINISH)
528  elog(ERROR, "SPI_finish failed");
529 
530  return result;
531 }
static void ri_GenerateQual(StringInfo buf, const char *sep, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
Definition: ri_triggers.c:1823
#define SPI_OK_CONNECT
Definition: spi.h:53
static void quoteRelationName(char *buffer, Relation rel)
Definition: ri_triggers.c:1806
#define RelationGetDescr(relation)
Definition: rel.h:445
int SPI_connect(void)
Definition: spi.c:89
int SPI_finish(void)
Definition: spi.c:176
#define RIAttType(rel, attnum)
Definition: ri_triggers.c:87
#define RIAttName(rel, attnum)
Definition: ri_triggers.c:86
Form_pg_class rd_rel
Definition: rel.h:83
unsigned int Oid
Definition: postgres_ext.h:31
#define sprintf
Definition: port.h:194
static void quoteOneName(char *buffer, const char *name)
Definition: ri_triggers.c:1786
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
NameData attname
Definition: pg_attribute.h:40
#define MAX_QUOTED_NAME_LEN
Definition: ri_triggers.c:83
#define MAX_QUOTED_REL_NAME_LEN
Definition: ri_triggers.c:84
int16 pk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:113
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:163
static SPIPlanPtr ri_FetchPreparedPlan(RI_QueryKey *key)
Definition: ri_triggers.c:2576
Oid pp_eq_oprs[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:116
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
static bool ri_PerformCheck(const RI_ConstraintInfo *riinfo, RI_QueryKey *qkey, SPIPlanPtr qplan, Relation fk_rel, Relation pk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, bool detectNewRows, int expect_OK)
Definition: ri_triggers.c:2177
#define SPI_OK_SELECT
Definition: spi.h:57
#define RI_PLAN_CHECK_LOOKUPPK_FROM_PK
Definition: ri_triggers.c:74
#define Assert(condition)
Definition: c.h:732
static int ri_NullCheck(TupleDesc tupdesc, TupleTableSlot *slot, const RI_ConstraintInfo *riinfo, bool rel_is_pk)
Definition: ri_triggers.c:2500
static SPIPlanPtr ri_PlanCheck(const char *querystr, int nargs, Oid *argtypes, RI_QueryKey *qkey, Relation fk_rel, Relation pk_rel, bool cache_plan)
Definition: ri_triggers.c:2130
#define SPI_OK_FINISH
Definition: spi.h:54
#define elog(elevel,...)
Definition: elog.h:226
int i
static void ri_BuildQueryKey(RI_QueryKey *key, const RI_ConstraintInfo *riinfo, int32 constr_queryno)
Definition: ri_triggers.c:1893
#define RI_MAX_NUMKEYS
Definition: ri_triggers.c:62
#define RI_KEYS_NONE_NULL
Definition: ri_triggers.c:69

◆ ri_CheckTrigger()

static void ri_CheckTrigger ( FunctionCallInfo  fcinfo,
const char *  funcname,
int  tgkind 
)
static

Definition at line 1908 of file ri_triggers.c.

References CALLED_AS_TRIGGER, FunctionCallInfoBaseData::context, ereport, errcode(), errmsg(), ERROR, RI_TRIGTYPE_DELETE, RI_TRIGTYPE_INSERT, RI_TRIGTYPE_UPDATE, TriggerData::tg_event, TRIGGER_FIRED_AFTER, TRIGGER_FIRED_BY_DELETE, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_UPDATE, and TRIGGER_FIRED_FOR_ROW.

Referenced by RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check_ins(), RI_FKey_check_upd(), RI_FKey_noaction_del(), RI_FKey_noaction_upd(), RI_FKey_restrict_del(), RI_FKey_restrict_upd(), RI_FKey_setdefault_del(), RI_FKey_setdefault_upd(), RI_FKey_setnull_del(), and RI_FKey_setnull_upd().

1909 {
1910  TriggerData *trigdata = (TriggerData *) fcinfo->context;
1911 
1912  if (!CALLED_AS_TRIGGER(fcinfo))
1913  ereport(ERROR,
1914  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1915  errmsg("function \"%s\" was not called by trigger manager", funcname)));
1916 
1917  /*
1918  * Check proper event
1919  */
1920  if (!TRIGGER_FIRED_AFTER(trigdata->tg_event) ||
1921  !TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
1922  ereport(ERROR,
1923  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1924  errmsg("function \"%s\" must be fired AFTER ROW", funcname)));
1925 
1926  switch (tgkind)
1927  {
1928  case RI_TRIGTYPE_INSERT:
1929  if (!TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
1930  ereport(ERROR,
1931  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1932  errmsg("function \"%s\" must be fired for INSERT", funcname)));
1933  break;
1934  case RI_TRIGTYPE_UPDATE:
1935  if (!TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
1936  ereport(ERROR,
1937  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1938  errmsg("function \"%s\" must be fired for UPDATE", funcname)));
1939  break;
1940  case RI_TRIGTYPE_DELETE:
1941  if (!TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
1942  ereport(ERROR,
1943  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1944  errmsg("function \"%s\" must be fired for DELETE", funcname)));
1945  break;
1946  }
1947 }
int errcode(int sqlerrcode)
Definition: elog.c:570
fmNodePtr context
Definition: fmgr.h:88
#define TRIGGER_FIRED_AFTER(event)
Definition: trigger.h:137
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define TRIGGER_FIRED_BY_DELETE(event)
Definition: trigger.h:119
#define RI_TRIGTYPE_DELETE
Definition: ri_triggers.c:92
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
#define RI_TRIGTYPE_UPDATE
Definition: ri_triggers.c:91
TriggerEvent tg_event
Definition: trigger.h:33
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:116
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:128
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:122
#define RI_TRIGTYPE_INSERT
Definition: ri_triggers.c:90

◆ ri_ExtractValues()

static void ri_ExtractValues ( Relation  rel,
TupleTableSlot slot,
const RI_ConstraintInfo riinfo,
bool  rel_is_pk,
Datum vals,
char *  nulls 
)
static

Definition at line 2314 of file ri_triggers.c.

References RI_ConstraintInfo::fk_attnums, i, RI_ConstraintInfo::nkeys, RI_ConstraintInfo::pk_attnums, and slot_getattr().

Referenced by ri_PerformCheck().

2317 {
2318  const int16 *attnums;
2319  bool isnull;
2320 
2321  if (rel_is_pk)
2322  attnums = riinfo->pk_attnums;
2323  else
2324  attnums = riinfo->fk_attnums;
2325 
2326  for (int i = 0; i < riinfo->nkeys; i++)
2327  {
2328  vals[i] = slot_getattr(slot, attnums[i], &isnull);
2329  nulls[i] = isnull ? 'n' : ' ';
2330  }
2331 }
signed short int16
Definition: c.h:345
int16 pk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:113
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:382
int i
int16 fk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:114

◆ ri_FetchConstraintInfo()

static const RI_ConstraintInfo * ri_FetchConstraintInfo ( Trigger trigger,
Relation  trig_rel,
bool  rel_is_pk 
)
static

Definition at line 1954 of file ri_triggers.c.

References RI_ConstraintInfo::confmatchtype, elog, ereport, errcode(), errhint(), errmsg(), ERROR, RI_ConstraintInfo::fk_relid, FKCONSTR_MATCH_FULL, FKCONSTR_MATCH_PARTIAL, FKCONSTR_MATCH_SIMPLE, OidIsValid, RI_ConstraintInfo::pk_relid, RelationGetRelationName, RelationGetRelid, ri_LoadConstraintInfo(), Trigger::tgconstraint, Trigger::tgconstrrelid, and Trigger::tgname.

Referenced by RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check(), RI_FKey_fk_upd_check_required(), RI_FKey_pk_upd_check_required(), RI_Initial_Check(), RI_PartitionRemove_Check(), ri_restrict(), and ri_set().

1955 {
1956  Oid constraintOid = trigger->tgconstraint;
1957  const RI_ConstraintInfo *riinfo;
1958 
1959  /*
1960  * Check that the FK constraint's OID is available; it might not be if
1961  * we've been invoked via an ordinary trigger or an old-style "constraint
1962  * trigger".
1963  */
1964  if (!OidIsValid(constraintOid))
1965  ereport(ERROR,
1966  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1967  errmsg("no pg_constraint entry for trigger \"%s\" on table \"%s\"",
1968  trigger->tgname, RelationGetRelationName(trig_rel)),
1969  errhint("Remove this referential integrity trigger and its mates, then do ALTER TABLE ADD CONSTRAINT.")));
1970 
1971  /* Find or create a hashtable entry for the constraint */
1972  riinfo = ri_LoadConstraintInfo(constraintOid);
1973 
1974  /* Do some easy cross-checks against the trigger call data */
1975  if (rel_is_pk)
1976  {
1977  if (riinfo->fk_relid != trigger->tgconstrrelid ||
1978  riinfo->pk_relid != RelationGetRelid(trig_rel))
1979  elog(ERROR, "wrong pg_constraint entry for trigger \"%s\" on table \"%s\"",
1980  trigger->tgname, RelationGetRelationName(trig_rel));
1981  }
1982  else
1983  {
1984  if (riinfo->fk_relid != RelationGetRelid(trig_rel) ||
1985  riinfo->pk_relid != trigger->tgconstrrelid)
1986  elog(ERROR, "wrong pg_constraint entry for trigger \"%s\" on table \"%s\"",
1987  trigger->tgname, RelationGetRelationName(trig_rel));
1988  }
1989 
1990  if (riinfo->confmatchtype != FKCONSTR_MATCH_FULL &&
1993  elog(ERROR, "unrecognized confmatchtype: %d",
1994  riinfo->confmatchtype);
1995 
1996  if (riinfo->confmatchtype == FKCONSTR_MATCH_PARTIAL)
1997  ereport(ERROR,
1998  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1999  errmsg("MATCH PARTIAL not yet implemented")));
2000 
2001  return riinfo;
2002 }
#define FKCONSTR_MATCH_SIMPLE
Definition: parsenodes.h:2124
int errhint(const char *fmt,...)
Definition: elog.c:974
int errcode(int sqlerrcode)
Definition: elog.c:570
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#define ERROR
Definition: elog.h:43
Oid tgconstraint
Definition: reltrigger.h:34
char * tgname
Definition: reltrigger.h:27
#define RelationGetRelationName(relation)
Definition: rel.h:453
Oid tgconstrrelid
Definition: reltrigger.h:32
#define ereport(elevel, rest)
Definition: elog.h:141
#define FKCONSTR_MATCH_FULL
Definition: parsenodes.h:2122
static const RI_ConstraintInfo * ri_LoadConstraintInfo(Oid constraintOid)
Definition: ri_triggers.c:2008
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define FKCONSTR_MATCH_PARTIAL
Definition: parsenodes.h:2123
#define RelationGetRelid(relation)
Definition: rel.h:419

◆ ri_FetchPreparedPlan()

static SPIPlanPtr ri_FetchPreparedPlan ( RI_QueryKey key)
static

Definition at line 2576 of file ri_triggers.c.

References HASH_FIND, hash_search(), sort-test::key, RI_QueryHashEntry::plan, ri_InitHashTables(), SPI_freeplan(), and SPI_plan_is_valid().

Referenced by ri_Check_Pk_Match(), RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check(), ri_restrict(), and ri_set().

2577 {
2578  RI_QueryHashEntry *entry;
2579  SPIPlanPtr plan;
2580 
2581  /*
2582  * On the first call initialize the hashtable
2583  */
2584  if (!ri_query_cache)
2586 
2587  /*
2588  * Lookup for the key
2589  */
2591  (void *) key,
2592  HASH_FIND, NULL);
2593  if (entry == NULL)
2594  return NULL;
2595 
2596  /*
2597  * Check whether the plan is still valid. If it isn't, we don't want to
2598  * simply rely on plancache.c to regenerate it; rather we should start
2599  * from scratch and rebuild the query text too. This is to cover cases
2600  * such as table/column renames. We depend on the plancache machinery to
2601  * detect possible invalidations, though.
2602  *
2603  * CAUTION: this check is only trustworthy if the caller has already
2604  * locked both FK and PK rels.
2605  */
2606  plan = entry->plan;
2607  if (plan && SPI_plan_is_valid(plan))
2608  return plan;
2609 
2610  /*
2611  * Otherwise we might as well flush the cached plan now, to free a little
2612  * memory space before we make a new one.
2613  */
2614  entry->plan = NULL;
2615  if (plan)
2616  SPI_freeplan(plan);
2617 
2618  return NULL;
2619 }
static HTAB * ri_query_cache
Definition: ri_triggers.c:168
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
SPIPlanPtr plan
Definition: ri_triggers.c:138
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:801
bool SPI_plan_is_valid(SPIPlanPtr plan)
Definition: spi.c:1681
static void ri_InitHashTables(void)
Definition: ri_triggers.c:2537

◆ RI_FKey_cascade_del()

Datum RI_FKey_cascade_del ( PG_FUNCTION_ARGS  )

Definition at line 734 of file ri_triggers.c.

References appendStringInfo(), attname, StringInfoData::data, elog, ERROR, get_collation_isdeterministic(), i, initStringInfo(), MAX_QUOTED_NAME_LEN, MAX_QUOTED_REL_NAME_LEN, PointerGetDatum, quoteOneName(), quoteRelationName(), ri_BuildQueryKey(), ri_CheckTrigger(), ri_FetchConstraintInfo(), ri_FetchPreparedPlan(), ri_GenerateQual(), ri_GenerateQualCollation(), RI_MAX_NUMKEYS, ri_PerformCheck(), RI_PLAN_CASCADE_DEL_DODELETE, ri_PlanCheck(), RI_TRIGTYPE_DELETE, RIAttCollation, RIAttName, RIAttType, RowExclusiveLock, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_DELETE, SPI_OK_FINISH, sprintf, table_close(), table_open(), TriggerData::tg_relation, TriggerData::tg_trigger, and TriggerData::tg_trigslot.

735 {
736  TriggerData *trigdata = (TriggerData *) fcinfo->context;
737  const RI_ConstraintInfo *riinfo;
738  Relation fk_rel;
739  Relation pk_rel;
740  TupleTableSlot *oldslot;
741  RI_QueryKey qkey;
742  SPIPlanPtr qplan;
743 
744  /* Check that this is a valid trigger call on the right time and event. */
745  ri_CheckTrigger(fcinfo, "RI_FKey_cascade_del", RI_TRIGTYPE_DELETE);
746 
747  riinfo = ri_FetchConstraintInfo(trigdata->tg_trigger,
748  trigdata->tg_relation, true);
749 
750  /*
751  * Get the relation descriptors of the FK and PK tables and the old tuple.
752  *
753  * fk_rel is opened in RowExclusiveLock mode since that's what our
754  * eventual DELETE will get on it.
755  */
756  fk_rel = table_open(riinfo->fk_relid, RowExclusiveLock);
757  pk_rel = trigdata->tg_relation;
758  oldslot = trigdata->tg_trigslot;
759 
760  if (SPI_connect() != SPI_OK_CONNECT)
761  elog(ERROR, "SPI_connect failed");
762 
763  /* Fetch or prepare a saved plan for the cascaded delete */
765 
766  if ((qplan = ri_FetchPreparedPlan(&qkey)) == NULL)
767  {
768  StringInfoData querybuf;
769  char fkrelname[MAX_QUOTED_REL_NAME_LEN];
771  char paramname[16];
772  const char *querysep;
773  Oid queryoids[RI_MAX_NUMKEYS];
774  const char *fk_only;
775 
776  /* ----------
777  * The query string built is
778  * DELETE FROM [ONLY] <fktable> WHERE $1 = fkatt1 [AND ...]
779  * The type id's for the $ parameters are those of the
780  * corresponding PK attributes.
781  * ----------
782  */
783  initStringInfo(&querybuf);
784  fk_only = fk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ?
785  "" : "ONLY ";
786  quoteRelationName(fkrelname, fk_rel);
787  appendStringInfo(&querybuf, "DELETE FROM %s%s",
788  fk_only, fkrelname);
789  querysep = "WHERE";
790  for (int i = 0; i < riinfo->nkeys; i++)
791  {
792  Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
793  Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
794  Oid pk_coll = RIAttCollation(pk_rel, riinfo->pk_attnums[i]);
795  Oid fk_coll = RIAttCollation(fk_rel, riinfo->fk_attnums[i]);
796 
797  quoteOneName(attname,
798  RIAttName(fk_rel, riinfo->fk_attnums[i]));
799  sprintf(paramname, "$%d", i + 1);
800  ri_GenerateQual(&querybuf, querysep,
801  paramname, pk_type,
802  riinfo->pf_eq_oprs[i],
803  attname, fk_type);
804  if (pk_coll != fk_coll && !get_collation_isdeterministic(pk_coll))
805  ri_GenerateQualCollation(&querybuf, pk_coll);
806  querysep = "AND";
807  queryoids[i] = pk_type;
808  }
809 
810  /* Prepare and save the plan */
811  qplan = ri_PlanCheck(querybuf.data, riinfo->nkeys, queryoids,
812  &qkey, fk_rel, pk_rel, true);
813  }
814 
815  /*
816  * We have a plan now. Build up the arguments from the key values in the
817  * deleted PK tuple and delete the referencing rows
818  */
819  ri_PerformCheck(riinfo, &qkey, qplan,
820  fk_rel, pk_rel,
821  oldslot, NULL,
822  true, /* must detect new rows */
823  SPI_OK_DELETE);
824 
825  if (SPI_finish() != SPI_OK_FINISH)
826  elog(ERROR, "SPI_finish failed");
827 
828  table_close(fk_rel, RowExclusiveLock);
829 
830  return PointerGetDatum(NULL);
831 }
static void ri_GenerateQualCollation(StringInfo buf, Oid collation)
Definition: ri_triggers.c:1852
static void ri_GenerateQual(StringInfo buf, const char *sep, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
Definition: ri_triggers.c:1823
#define SPI_OK_CONNECT
Definition: spi.h:53
TupleTableSlot * tg_trigslot
Definition: trigger.h:38
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static void quoteRelationName(char *buffer, Relation rel)
Definition: ri_triggers.c:1806
int SPI_connect(void)
Definition: spi.c:89
#define PointerGetDatum(X)
Definition: postgres.h:556
#define SPI_OK_DELETE
Definition: spi.h:60
int SPI_finish(void)
Definition: spi.c:176
bool get_collation_isdeterministic(Oid colloid)
Definition: lsyscache.c:942
#define RIAttType(rel, attnum)
Definition: ri_triggers.c:87
#define RIAttName(rel, attnum)
Definition: ri_triggers.c:86
unsigned int Oid
Definition: postgres_ext.h:31
#define sprintf
Definition: port.h:194
#define RIAttCollation(rel, attnum)
Definition: ri_triggers.c:88
static void quoteOneName(char *buffer, const char *name)
Definition: ri_triggers.c:1786
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
NameData attname
Definition: pg_attribute.h:40
#define MAX_QUOTED_NAME_LEN
Definition: ri_triggers.c:83
#define RI_PLAN_CASCADE_DEL_DODELETE
Definition: ri_triggers.c:77
#define MAX_QUOTED_REL_NAME_LEN
Definition: ri_triggers.c:84
static void ri_CheckTrigger(FunctionCallInfo fcinfo, const char *funcname, int tgkind)
Definition: ri_triggers.c:1908
static SPIPlanPtr ri_FetchPreparedPlan(RI_QueryKey *key)
Definition: ri_triggers.c:2576
#define RowExclusiveLock
Definition: lockdefs.h:38
static const RI_ConstraintInfo * ri_FetchConstraintInfo(Trigger *trigger, Relation trig_rel, bool rel_is_pk)
Definition: ri_triggers.c:1954
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define RI_TRIGTYPE_DELETE
Definition: ri_triggers.c:92
static bool ri_PerformCheck(const RI_ConstraintInfo *riinfo, RI_QueryKey *qkey, SPIPlanPtr qplan, Relation fk_rel, Relation pk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, bool detectNewRows, int expect_OK)
Definition: ri_triggers.c:2177
Trigger * tg_trigger
Definition: trigger.h:37
static SPIPlanPtr ri_PlanCheck(const char *querystr, int nargs, Oid *argtypes, RI_QueryKey *qkey, Relation fk_rel, Relation pk_rel, bool cache_plan)
Definition: ri_triggers.c:2130
#define SPI_OK_FINISH
Definition: spi.h:54
#define elog(elevel,...)
Definition: elog.h:226
int i
static void ri_BuildQueryKey(RI_QueryKey *key, const RI_ConstraintInfo *riinfo, int32 constr_queryno)
Definition: ri_triggers.c:1893
#define RI_MAX_NUMKEYS
Definition: ri_triggers.c:62
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Relation tg_relation
Definition: trigger.h:34

◆ RI_FKey_cascade_upd()

Datum RI_FKey_cascade_upd ( PG_FUNCTION_ARGS  )

Definition at line 840 of file ri_triggers.c.

References appendBinaryStringInfo(), appendStringInfo(), attname, StringInfoData::data, elog, ERROR, get_collation_isdeterministic(), i, initStringInfo(), StringInfoData::len, MAX_QUOTED_NAME_LEN, MAX_QUOTED_REL_NAME_LEN, RI_ConstraintInfo::nkeys, PointerGetDatum, quoteOneName(), quoteRelationName(), ri_BuildQueryKey(), ri_CheckTrigger(), ri_FetchConstraintInfo(), ri_FetchPreparedPlan(), ri_GenerateQual(), ri_GenerateQualCollation(), RI_MAX_NUMKEYS, ri_PerformCheck(), RI_PLAN_CASCADE_UPD_DOUPDATE, ri_PlanCheck(), RI_TRIGTYPE_UPDATE, RIAttCollation, RIAttName, RIAttType, RowExclusiveLock, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_OK_UPDATE, sprintf, table_close(), table_open(), TriggerData::tg_newslot, TriggerData::tg_relation, TriggerData::tg_trigger, and TriggerData::tg_trigslot.

841 {
842  TriggerData *trigdata = (TriggerData *) fcinfo->context;
843  const RI_ConstraintInfo *riinfo;
844  Relation fk_rel;
845  Relation pk_rel;
846  TupleTableSlot *newslot;
847  TupleTableSlot *oldslot;
848  RI_QueryKey qkey;
849  SPIPlanPtr qplan;
850 
851  /* Check that this is a valid trigger call on the right time and event. */
852  ri_CheckTrigger(fcinfo, "RI_FKey_cascade_upd", RI_TRIGTYPE_UPDATE);
853 
854  riinfo = ri_FetchConstraintInfo(trigdata->tg_trigger,
855  trigdata->tg_relation, true);
856 
857  /*
858  * Get the relation descriptors of the FK and PK tables and the new and
859  * old tuple.
860  *
861  * fk_rel is opened in RowExclusiveLock mode since that's what our
862  * eventual UPDATE will get on it.
863  */
864  fk_rel = table_open(riinfo->fk_relid, RowExclusiveLock);
865  pk_rel = trigdata->tg_relation;
866  newslot = trigdata->tg_newslot;
867  oldslot = trigdata->tg_trigslot;
868 
869  if (SPI_connect() != SPI_OK_CONNECT)
870  elog(ERROR, "SPI_connect failed");
871 
872  /* Fetch or prepare a saved plan for the cascaded update */
874 
875  if ((qplan = ri_FetchPreparedPlan(&qkey)) == NULL)
876  {
877  StringInfoData querybuf;
878  StringInfoData qualbuf;
879  char fkrelname[MAX_QUOTED_REL_NAME_LEN];
881  char paramname[16];
882  const char *querysep;
883  const char *qualsep;
884  Oid queryoids[RI_MAX_NUMKEYS * 2];
885  const char *fk_only;
886 
887  /* ----------
888  * The query string built is
889  * UPDATE [ONLY] <fktable> SET fkatt1 = $1 [, ...]
890  * WHERE $n = fkatt1 [AND ...]
891  * The type id's for the $ parameters are those of the
892  * corresponding PK attributes. Note that we are assuming
893  * there is an assignment cast from the PK to the FK type;
894  * else the parser will fail.
895  * ----------
896  */
897  initStringInfo(&querybuf);
898  initStringInfo(&qualbuf);
899  fk_only = fk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ?
900  "" : "ONLY ";
901  quoteRelationName(fkrelname, fk_rel);
902  appendStringInfo(&querybuf, "UPDATE %s%s SET",
903  fk_only, fkrelname);
904  querysep = "";
905  qualsep = "WHERE";
906  for (int i = 0, j = riinfo->nkeys; i < riinfo->nkeys; i++, j++)
907  {
908  Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
909  Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
910  Oid pk_coll = RIAttCollation(pk_rel, riinfo->pk_attnums[i]);
911  Oid fk_coll = RIAttCollation(fk_rel, riinfo->fk_attnums[i]);
912 
913  quoteOneName(attname,
914  RIAttName(fk_rel, riinfo->fk_attnums[i]));
915  appendStringInfo(&querybuf,
916  "%s %s = $%d",
917  querysep, attname, i + 1);
918  sprintf(paramname, "$%d", j + 1);
919  ri_GenerateQual(&qualbuf, qualsep,
920  paramname, pk_type,
921  riinfo->pf_eq_oprs[i],
922  attname, fk_type);
923  if (pk_coll != fk_coll && !get_collation_isdeterministic(pk_coll))
924  ri_GenerateQualCollation(&querybuf, pk_coll);
925  querysep = ",";
926  qualsep = "AND";
927  queryoids[i] = pk_type;
928  queryoids[j] = pk_type;
929  }
930  appendBinaryStringInfo(&querybuf, qualbuf.data, qualbuf.len);
931 
932  /* Prepare and save the plan */
933  qplan = ri_PlanCheck(querybuf.data, riinfo->nkeys * 2, queryoids,
934  &qkey, fk_rel, pk_rel, true);
935  }
936 
937  /*
938  * We have a plan now. Run it to update the existing references.
939  */
940  ri_PerformCheck(riinfo, &qkey, qplan,
941  fk_rel, pk_rel,
942  oldslot, newslot,
943  true, /* must detect new rows */
944  SPI_OK_UPDATE);
945 
946  if (SPI_finish() != SPI_OK_FINISH)
947  elog(ERROR, "SPI_finish failed");
948 
949  table_close(fk_rel, RowExclusiveLock);
950 
951  return PointerGetDatum(NULL);
952 }
static void ri_GenerateQualCollation(StringInfo buf, Oid collation)
Definition: ri_triggers.c:1852
static void ri_GenerateQual(StringInfo buf, const char *sep, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
Definition: ri_triggers.c:1823
#define SPI_OK_CONNECT
Definition: spi.h:53
TupleTableSlot * tg_trigslot
Definition: trigger.h:38
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static void quoteRelationName(char *buffer, Relation rel)
Definition: ri_triggers.c:1806
int SPI_connect(void)
Definition: spi.c:89
#define PointerGetDatum(X)
Definition: postgres.h:556
int SPI_finish(void)
Definition: spi.c:176
bool get_collation_isdeterministic(Oid colloid)
Definition: lsyscache.c:942
#define RIAttType(rel, attnum)
Definition: ri_triggers.c:87
#define RIAttName(rel, attnum)
Definition: ri_triggers.c:86
unsigned int Oid
Definition: postgres_ext.h:31
#define sprintf
Definition: port.h:194
#define RIAttCollation(rel, attnum)
Definition: ri_triggers.c:88
static void quoteOneName(char *buffer, const char *name)
Definition: ri_triggers.c:1786
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
NameData attname
Definition: pg_attribute.h:40
#define MAX_QUOTED_NAME_LEN
Definition: ri_triggers.c:83
#define MAX_QUOTED_REL_NAME_LEN
Definition: ri_triggers.c:84
static void ri_CheckTrigger(FunctionCallInfo fcinfo, const char *funcname, int tgkind)
Definition: ri_triggers.c:1908
static SPIPlanPtr ri_FetchPreparedPlan(RI_QueryKey *key)
Definition: ri_triggers.c:2576
#define RowExclusiveLock
Definition: lockdefs.h:38
static const RI_ConstraintInfo * ri_FetchConstraintInfo(Trigger *trigger, Relation trig_rel, bool rel_is_pk)
Definition: ri_triggers.c:1954
#define RI_PLAN_CASCADE_UPD_DOUPDATE
Definition: ri_triggers.c:78
TupleTableSlot * tg_newslot
Definition: trigger.h:39
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
static bool ri_PerformCheck(const RI_ConstraintInfo *riinfo, RI_QueryKey *qkey, SPIPlanPtr qplan, Relation fk_rel, Relation pk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, bool detectNewRows, int expect_OK)
Definition: ri_triggers.c:2177
Trigger * tg_trigger
Definition: trigger.h:37
#define RI_TRIGTYPE_UPDATE
Definition: ri_triggers.c:91
static SPIPlanPtr ri_PlanCheck(const char *querystr, int nargs, Oid *argtypes, RI_QueryKey *qkey, Relation fk_rel, Relation pk_rel, bool cache_plan)
Definition: ri_triggers.c:2130
#define SPI_OK_FINISH
Definition: spi.h:54
#define SPI_OK_UPDATE
Definition: spi.h:61
#define elog(elevel,...)
Definition: elog.h:226
int i
static void ri_BuildQueryKey(RI_QueryKey *key, const RI_ConstraintInfo *riinfo, int32 constr_queryno)
Definition: ri_triggers.c:1893
#define RI_MAX_NUMKEYS
Definition: ri_triggers.c:62
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:214
Relation tg_relation
Definition: trigger.h:34

◆ RI_FKey_check()

static Datum RI_FKey_check ( TriggerData trigdata)
static

Definition at line 234 of file ri_triggers.c.

References appendStringInfo(), appendStringInfoString(), attname, RI_ConstraintInfo::confmatchtype, RI_ConstraintInfo::conname, StringInfoData::data, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, errtableconstraint(), RI_ConstraintInfo::fk_attnums, FKCONSTR_MATCH_FULL, FKCONSTR_MATCH_PARTIAL, FKCONSTR_MATCH_SIMPLE, i, initStringInfo(), MAX_QUOTED_NAME_LEN, MAX_QUOTED_REL_NAME_LEN, NameStr, RI_ConstraintInfo::nkeys, RI_ConstraintInfo::pf_eq_oprs, RI_ConstraintInfo::pk_attnums, RI_ConstraintInfo::pk_relid, PointerGetDatum, quoteOneName(), quoteRelationName(), RelationData::rd_rel, RelationGetDescr, RelationGetRelationName, ri_BuildQueryKey(), ri_FetchConstraintInfo(), ri_FetchPreparedPlan(), ri_GenerateQual(), RI_KEYS_ALL_NULL, RI_KEYS_NONE_NULL, RI_KEYS_SOME_NULL, RI_MAX_NUMKEYS, ri_NullCheck(), ri_PerformCheck(), RI_PLAN_CHECK_LOOKUPPK, ri_PlanCheck(), RIAttName, RIAttType, RowShareLock, SnapshotSelf, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_OK_SELECT, sprintf, table_close(), table_open(), table_tuple_satisfies_snapshot(), TriggerData::tg_event, TriggerData::tg_newslot, TriggerData::tg_relation, TriggerData::tg_trigger, TriggerData::tg_trigslot, and TRIGGER_FIRED_BY_UPDATE.

Referenced by RI_FKey_check_ins(), and RI_FKey_check_upd().

235 {
236  const RI_ConstraintInfo *riinfo;
237  Relation fk_rel;
238  Relation pk_rel;
239  TupleTableSlot *newslot;
240  RI_QueryKey qkey;
241  SPIPlanPtr qplan;
242 
243  riinfo = ri_FetchConstraintInfo(trigdata->tg_trigger,
244  trigdata->tg_relation, false);
245 
246  if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
247  newslot = trigdata->tg_newslot;
248  else
249  newslot = trigdata->tg_trigslot;
250 
251  /*
252  * We should not even consider checking the row if it is no longer valid,
253  * since it was either deleted (so the deferred check should be skipped)
254  * or updated (in which case only the latest version of the row should be
255  * checked). Test its liveness according to SnapshotSelf. We need pin
256  * and lock on the buffer to call HeapTupleSatisfiesVisibility. Caller
257  * should be holding pin, but not lock.
258  */
259  if (!table_tuple_satisfies_snapshot(trigdata->tg_relation, newslot, SnapshotSelf))
260  return PointerGetDatum(NULL);
261 
262  /*
263  * Get the relation descriptors of the FK and PK tables.
264  *
265  * pk_rel is opened in RowShareLock mode since that's what our eventual
266  * SELECT FOR KEY SHARE will get on it.
267  */
268  fk_rel = trigdata->tg_relation;
269  pk_rel = table_open(riinfo->pk_relid, RowShareLock);
270 
271  switch (ri_NullCheck(RelationGetDescr(fk_rel), newslot, riinfo, false))
272  {
273  case RI_KEYS_ALL_NULL:
274 
275  /*
276  * No further check needed - an all-NULL key passes every type of
277  * foreign key constraint.
278  */
279  table_close(pk_rel, RowShareLock);
280  return PointerGetDatum(NULL);
281 
282  case RI_KEYS_SOME_NULL:
283 
284  /*
285  * This is the only case that differs between the three kinds of
286  * MATCH.
287  */
288  switch (riinfo->confmatchtype)
289  {
290  case FKCONSTR_MATCH_FULL:
291 
292  /*
293  * Not allowed - MATCH FULL says either all or none of the
294  * attributes can be NULLs
295  */
296  ereport(ERROR,
297  (errcode(ERRCODE_FOREIGN_KEY_VIOLATION),
298  errmsg("insert or update on table \"%s\" violates foreign key constraint \"%s\"",
299  RelationGetRelationName(fk_rel),
300  NameStr(riinfo->conname)),
301  errdetail("MATCH FULL does not allow mixing of null and nonnull key values."),
302  errtableconstraint(fk_rel,
303  NameStr(riinfo->conname))));
304  table_close(pk_rel, RowShareLock);
305  return PointerGetDatum(NULL);
306 
308 
309  /*
310  * MATCH SIMPLE - if ANY column is null, the key passes
311  * the constraint.
312  */
313  table_close(pk_rel, RowShareLock);
314  return PointerGetDatum(NULL);
315 
316 #ifdef NOT_USED
318 
319  /*
320  * MATCH PARTIAL - all non-null columns must match. (not
321  * implemented, can be done by modifying the query below
322  * to only include non-null columns, or by writing a
323  * special version here)
324  */
325  break;
326 #endif
327  }
328 
329  case RI_KEYS_NONE_NULL:
330 
331  /*
332  * Have a full qualified key - continue below for all three kinds
333  * of MATCH.
334  */
335  break;
336  }
337 
338  if (SPI_connect() != SPI_OK_CONNECT)
339  elog(ERROR, "SPI_connect failed");
340 
341  /* Fetch or prepare a saved plan for the real check */
343 
344  if ((qplan = ri_FetchPreparedPlan(&qkey)) == NULL)
345  {
346  StringInfoData querybuf;
347  char pkrelname[MAX_QUOTED_REL_NAME_LEN];
349  char paramname[16];
350  const char *querysep;
351  Oid queryoids[RI_MAX_NUMKEYS];
352  const char *pk_only;
353 
354  /* ----------
355  * The query string built is
356  * SELECT 1 FROM [ONLY] <pktable> x WHERE pkatt1 = $1 [AND ...]
357  * FOR KEY SHARE OF x
358  * The type id's for the $ parameters are those of the
359  * corresponding FK attributes.
360  * ----------
361  */
362  initStringInfo(&querybuf);
363  pk_only = pk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ?
364  "" : "ONLY ";
365  quoteRelationName(pkrelname, pk_rel);
366  appendStringInfo(&querybuf, "SELECT 1 FROM %s%s x",
367  pk_only, pkrelname);
368  querysep = "WHERE";
369  for (int i = 0; i < riinfo->nkeys; i++)
370  {
371  Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
372  Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
373 
374  quoteOneName(attname,
375  RIAttName(pk_rel, riinfo->pk_attnums[i]));
376  sprintf(paramname, "$%d", i + 1);
377  ri_GenerateQual(&querybuf, querysep,
378  attname, pk_type,
379  riinfo->pf_eq_oprs[i],
380  paramname, fk_type);
381  querysep = "AND";
382  queryoids[i] = fk_type;
383  }
384  appendStringInfoString(&querybuf, " FOR KEY SHARE OF x");
385 
386  /* Prepare and save the plan */
387  qplan = ri_PlanCheck(querybuf.data, riinfo->nkeys, queryoids,
388  &qkey, fk_rel, pk_rel, true);
389  }
390 
391  /*
392  * Now check that foreign key exists in PK table
393  */
394  ri_PerformCheck(riinfo, &qkey, qplan,
395  fk_rel, pk_rel,
396  NULL, newslot,
397  false,
398  SPI_OK_SELECT);
399 
400  if (SPI_finish() != SPI_OK_FINISH)
401  elog(ERROR, "SPI_finish failed");
402 
403  table_close(pk_rel, RowShareLock);
404 
405  return PointerGetDatum(NULL);
406 }
static void ri_GenerateQual(StringInfo buf, const char *sep, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
Definition: ri_triggers.c:1823
#define SPI_OK_CONNECT
Definition: spi.h:53
#define FKCONSTR_MATCH_SIMPLE
Definition: parsenodes.h:2124
TupleTableSlot * tg_trigslot
Definition: trigger.h:38
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static void quoteRelationName(char *buffer, Relation rel)
Definition: ri_triggers.c:1806
#define RelationGetDescr(relation)
Definition: rel.h:445
int SPI_connect(void)
Definition: spi.c:89
#define PointerGetDatum(X)
Definition: postgres.h:556
int SPI_finish(void)
Definition: spi.c:176
int errcode(int sqlerrcode)
Definition: elog.c:570
#define RIAttType(rel, attnum)
Definition: ri_triggers.c:87
#define RIAttName(rel, attnum)
Definition: ri_triggers.c:86
Form_pg_class rd_rel
Definition: rel.h:83
unsigned int Oid
Definition: postgres_ext.h:31
#define sprintf
Definition: port.h:194
int errtableconstraint(Relation rel, const char *conname)
Definition: relcache.c:5222
static void quoteOneName(char *buffer, const char *name)
Definition: ri_triggers.c:1786
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
NameData attname
Definition: pg_attribute.h:40
#define MAX_QUOTED_NAME_LEN
Definition: ri_triggers.c:83
#define MAX_QUOTED_REL_NAME_LEN
Definition: ri_triggers.c:84
int16 pk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:113
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:163
#define SnapshotSelf
Definition: snapmgr.h:68
static SPIPlanPtr ri_FetchPreparedPlan(RI_QueryKey *key)
Definition: ri_triggers.c:2576
int errdetail(const char *fmt,...)
Definition: elog.c:860
Oid pf_eq_oprs[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:115
#define RelationGetRelationName(relation)
Definition: rel.h:453
static const RI_ConstraintInfo * ri_FetchConstraintInfo(Trigger *trigger, Relation trig_rel, bool rel_is_pk)
Definition: ri_triggers.c:1954
#define RI_PLAN_CHECK_LOOKUPPK
Definition: ri_triggers.c:73
TupleTableSlot * tg_newslot
Definition: trigger.h:39
#define ereport(elevel, rest)
Definition: elog.h:141
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define FKCONSTR_MATCH_FULL
Definition: parsenodes.h:2122
#define RowShareLock
Definition: lockdefs.h:37
static bool ri_PerformCheck(const RI_ConstraintInfo *riinfo, RI_QueryKey *qkey, SPIPlanPtr qplan, Relation fk_rel, Relation pk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, bool detectNewRows, int expect_OK)
Definition: ri_triggers.c:2177
#define RI_KEYS_ALL_NULL
Definition: ri_triggers.c:67
Trigger * tg_trigger
Definition: trigger.h:37
#define SPI_OK_SELECT
Definition: spi.h:57
static bool table_tuple_satisfies_snapshot(Relation rel, TupleTableSlot *slot, Snapshot snapshot)
Definition: tableam.h:1059
static int ri_NullCheck(TupleDesc tupdesc, TupleTableSlot *slot, const RI_ConstraintInfo *riinfo, bool rel_is_pk)
Definition: ri_triggers.c:2500
TriggerEvent tg_event
Definition: trigger.h:33
static SPIPlanPtr ri_PlanCheck(const char *querystr, int nargs, Oid *argtypes, RI_QueryKey *qkey, Relation fk_rel, Relation pk_rel, bool cache_plan)
Definition: ri_triggers.c:2130
#define SPI_OK_FINISH
Definition: spi.h:54
#define RI_KEYS_SOME_NULL
Definition: ri_triggers.c:68
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
int i
#define NameStr(name)
Definition: c.h:609
static void ri_BuildQueryKey(RI_QueryKey *key, const RI_ConstraintInfo *riinfo, int32 constr_queryno)
Definition: ri_triggers.c:1893
int16 fk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:114
#define RI_MAX_NUMKEYS
Definition: ri_triggers.c:62
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:122
#define FKCONSTR_MATCH_PARTIAL
Definition: parsenodes.h:2123
#define RI_KEYS_NONE_NULL
Definition: ri_triggers.c:69
Relation tg_relation
Definition: trigger.h:34

◆ RI_FKey_check_ins()

Datum RI_FKey_check_ins ( PG_FUNCTION_ARGS  )

Definition at line 415 of file ri_triggers.c.

References ri_CheckTrigger(), RI_FKey_check(), and RI_TRIGTYPE_INSERT.

Referenced by validateForeignKeyConstraint().

416 {
417  /* Check that this is a valid trigger call on the right time and event. */
418  ri_CheckTrigger(fcinfo, "RI_FKey_check_ins", RI_TRIGTYPE_INSERT);
419 
420  /* Share code with UPDATE case. */
421  return RI_FKey_check((TriggerData *) fcinfo->context);
422 }
static Datum RI_FKey_check(TriggerData *trigdata)
Definition: ri_triggers.c:234
static void ri_CheckTrigger(FunctionCallInfo fcinfo, const char *funcname, int tgkind)
Definition: ri_triggers.c:1908
#define RI_TRIGTYPE_INSERT
Definition: ri_triggers.c:90

◆ RI_FKey_check_upd()

Datum RI_FKey_check_upd ( PG_FUNCTION_ARGS  )

Definition at line 431 of file ri_triggers.c.

References ri_CheckTrigger(), RI_FKey_check(), and RI_TRIGTYPE_UPDATE.

432 {
433  /* Check that this is a valid trigger call on the right time and event. */
434  ri_CheckTrigger(fcinfo, "RI_FKey_check_upd", RI_TRIGTYPE_UPDATE);
435 
436  /* Share code with INSERT case. */
437  return RI_FKey_check((TriggerData *) fcinfo->context);
438 }
static Datum RI_FKey_check(TriggerData *trigdata)
Definition: ri_triggers.c:234
static void ri_CheckTrigger(FunctionCallInfo fcinfo, const char *funcname, int tgkind)
Definition: ri_triggers.c:1908
#define RI_TRIGTYPE_UPDATE
Definition: ri_triggers.c:91

◆ RI_FKey_fk_upd_check_required()

bool RI_FKey_fk_upd_check_required ( Trigger trigger,
Relation  fk_rel,
TupleTableSlot oldslot,
TupleTableSlot newslot 
)

Definition at line 1196 of file ri_triggers.c.

References Assert, RI_ConstraintInfo::confmatchtype, DatumGetTransactionId, FKCONSTR_MATCH_FULL, FKCONSTR_MATCH_PARTIAL, FKCONSTR_MATCH_SIMPLE, MinTransactionIdAttributeNumber, RelationGetDescr, ri_FetchConstraintInfo(), RI_KEYS_ALL_NULL, RI_KEYS_SOME_NULL, ri_KeysEqual(), ri_NullCheck(), slot_getsysattr(), and TransactionIdIsCurrentTransactionId().

Referenced by AfterTriggerSaveEvent().

1198 {
1199  const RI_ConstraintInfo *riinfo;
1200  int ri_nullcheck;
1201  Datum xminDatum;
1202  TransactionId xmin;
1203  bool isnull;
1204 
1205  riinfo = ri_FetchConstraintInfo(trigger, fk_rel, false);
1206 
1207  ri_nullcheck = ri_NullCheck(RelationGetDescr(fk_rel), newslot, riinfo, false);
1208 
1209  /*
1210  * If all new key values are NULL, the row satisfies the constraint, so no
1211  * check is needed.
1212  */
1213  if (ri_nullcheck == RI_KEYS_ALL_NULL)
1214  return false;
1215 
1216  /*
1217  * If some new key values are NULL, the behavior depends on the match
1218  * type.
1219  */
1220  else if (ri_nullcheck == RI_KEYS_SOME_NULL)
1221  {
1222  switch (riinfo->confmatchtype)
1223  {
1224  case FKCONSTR_MATCH_SIMPLE:
1225 
1226  /*
1227  * If any new key value is NULL, the row must satisfy the
1228  * constraint, so no check is needed.
1229  */
1230  return false;
1231 
1233 
1234  /*
1235  * Don't know, must run full check.
1236  */
1237  break;
1238 
1239  case FKCONSTR_MATCH_FULL:
1240 
1241  /*
1242  * If some new key values are NULL, the row fails the
1243  * constraint. We must not throw error here, because the row
1244  * might get invalidated before the constraint is to be
1245  * checked, but we should queue the event to apply the check
1246  * later.
1247  */
1248  return true;
1249  }
1250  }
1251 
1252  /*
1253  * Continues here for no new key values are NULL, or we couldn't decide
1254  * yet.
1255  */
1256 
1257  /*
1258  * If the original row was inserted by our own transaction, we must fire
1259  * the trigger whether or not the keys are equal. This is because our
1260  * UPDATE will invalidate the INSERT so that the INSERT RI trigger will
1261  * not do anything; so we had better do the UPDATE check. (We could skip
1262  * this if we knew the INSERT trigger already fired, but there is no easy
1263  * way to know that.)
1264  */
1265  xminDatum = slot_getsysattr(oldslot, MinTransactionIdAttributeNumber, &isnull);
1266  Assert(!isnull);
1267  xmin = DatumGetTransactionId(xminDatum);
1269  return true;
1270 
1271  /* If all old and new key values are equal, no check is needed */
1272  if (ri_KeysEqual(fk_rel, oldslot, newslot, riinfo, false))
1273  return false;
1274 
1275  /* Else we need to fire the trigger. */
1276  return true;
1277 }
#define FKCONSTR_MATCH_SIMPLE
Definition: parsenodes.h:2124
static bool ri_KeysEqual(Relation rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, const RI_ConstraintInfo *riinfo, bool rel_is_pk)
Definition: ri_triggers.c:2662
uint32 TransactionId
Definition: c.h:507
#define RelationGetDescr(relation)
Definition: rel.h:445
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:854
static const RI_ConstraintInfo * ri_FetchConstraintInfo(Trigger *trigger, Relation trig_rel, bool rel_is_pk)
Definition: ri_triggers.c:1954
#define FKCONSTR_MATCH_FULL
Definition: parsenodes.h:2122
uintptr_t Datum
Definition: postgres.h:367
#define RI_KEYS_ALL_NULL
Definition: ri_triggers.c:67
#define Assert(condition)
Definition: c.h:732
static int ri_NullCheck(TupleDesc tupdesc, TupleTableSlot *slot, const RI_ConstraintInfo *riinfo, bool rel_is_pk)
Definition: ri_triggers.c:2500
#define RI_KEYS_SOME_NULL
Definition: ri_triggers.c:68
#define DatumGetTransactionId(X)
Definition: postgres.h:514
static Datum slot_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:403
#define MinTransactionIdAttributeNumber
Definition: sysattr.h:22
#define FKCONSTR_MATCH_PARTIAL
Definition: parsenodes.h:2123

◆ RI_FKey_noaction_del()

Datum RI_FKey_noaction_del ( PG_FUNCTION_ARGS  )

Definition at line 542 of file ri_triggers.c.

References ri_CheckTrigger(), ri_restrict(), and RI_TRIGTYPE_DELETE.

543 {
544  /* Check that this is a valid trigger call on the right time and event. */
545  ri_CheckTrigger(fcinfo, "RI_FKey_noaction_del", RI_TRIGTYPE_DELETE);
546 
547  /* Share code with RESTRICT/UPDATE cases. */
548  return ri_restrict((TriggerData *) fcinfo->context, true);
549 }
static Datum ri_restrict(TriggerData *trigdata, bool is_no_action)
Definition: ri_triggers.c:615
static void ri_CheckTrigger(FunctionCallInfo fcinfo, const char *funcname, int tgkind)
Definition: ri_triggers.c:1908
#define RI_TRIGTYPE_DELETE
Definition: ri_triggers.c:92

◆ RI_FKey_noaction_upd()

Datum RI_FKey_noaction_upd ( PG_FUNCTION_ARGS  )

Definition at line 579 of file ri_triggers.c.

References ri_CheckTrigger(), ri_restrict(), and RI_TRIGTYPE_UPDATE.

580 {
581  /* Check that this is a valid trigger call on the right time and event. */
582  ri_CheckTrigger(fcinfo, "RI_FKey_noaction_upd", RI_TRIGTYPE_UPDATE);
583 
584  /* Share code with RESTRICT/DELETE cases. */
585  return ri_restrict((TriggerData *) fcinfo->context, true);
586 }
static Datum ri_restrict(TriggerData *trigdata, bool is_no_action)
Definition: ri_triggers.c:615
static void ri_CheckTrigger(FunctionCallInfo fcinfo, const char *funcname, int tgkind)
Definition: ri_triggers.c:1908
#define RI_TRIGTYPE_UPDATE
Definition: ri_triggers.c:91

◆ RI_FKey_pk_upd_check_required()

bool RI_FKey_pk_upd_check_required ( Trigger trigger,
Relation  pk_rel,
TupleTableSlot oldslot,
TupleTableSlot newslot 
)

Definition at line 1164 of file ri_triggers.c.

References RelationGetDescr, ri_FetchConstraintInfo(), RI_KEYS_NONE_NULL, ri_KeysEqual(), and ri_NullCheck().

Referenced by AfterTriggerSaveEvent().

1166 {
1167  const RI_ConstraintInfo *riinfo;
1168 
1169  riinfo = ri_FetchConstraintInfo(trigger, pk_rel, true);
1170 
1171  /*
1172  * If any old key value is NULL, the row could not have been referenced by
1173  * an FK row, so no check is needed.
1174  */
1175  if (ri_NullCheck(RelationGetDescr(pk_rel), oldslot, riinfo, true) != RI_KEYS_NONE_NULL)
1176  return false;
1177 
1178  /* If all old and new key values are equal, no check is needed */
1179  if (newslot && ri_KeysEqual(pk_rel, oldslot, newslot, riinfo, true))
1180  return false;
1181 
1182  /* Else we need to fire the trigger. */
1183  return true;
1184 }
static bool ri_KeysEqual(Relation rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, const RI_ConstraintInfo *riinfo, bool rel_is_pk)
Definition: ri_triggers.c:2662
#define RelationGetDescr(relation)
Definition: rel.h:445
static const RI_ConstraintInfo * ri_FetchConstraintInfo(Trigger *trigger, Relation trig_rel, bool rel_is_pk)
Definition: ri_triggers.c:1954
static int ri_NullCheck(TupleDesc tupdesc, TupleTableSlot *slot, const RI_ConstraintInfo *riinfo, bool rel_is_pk)
Definition: ri_triggers.c:2500
#define RI_KEYS_NONE_NULL
Definition: ri_triggers.c:69

◆ RI_FKey_restrict_del()

Datum RI_FKey_restrict_del ( PG_FUNCTION_ARGS  )

Definition at line 562 of file ri_triggers.c.

References ri_CheckTrigger(), ri_restrict(), and RI_TRIGTYPE_DELETE.

563 {
564  /* Check that this is a valid trigger call on the right time and event. */
565  ri_CheckTrigger(fcinfo, "RI_FKey_restrict_del", RI_TRIGTYPE_DELETE);
566 
567  /* Share code with NO ACTION/UPDATE cases. */
568  return ri_restrict((TriggerData *) fcinfo->context, false);
569 }
static Datum ri_restrict(TriggerData *trigdata, bool is_no_action)
Definition: ri_triggers.c:615
static void ri_CheckTrigger(FunctionCallInfo fcinfo, const char *funcname, int tgkind)
Definition: ri_triggers.c:1908
#define RI_TRIGTYPE_DELETE
Definition: ri_triggers.c:92

◆ RI_FKey_restrict_upd()

Datum RI_FKey_restrict_upd ( PG_FUNCTION_ARGS  )

Definition at line 599 of file ri_triggers.c.

References ri_CheckTrigger(), ri_restrict(), and RI_TRIGTYPE_UPDATE.

600 {
601  /* Check that this is a valid trigger call on the right time and event. */
602  ri_CheckTrigger(fcinfo, "RI_FKey_restrict_upd", RI_TRIGTYPE_UPDATE);
603 
604  /* Share code with NO ACTION/DELETE cases. */
605  return ri_restrict((TriggerData *) fcinfo->context, false);
606 }
static Datum ri_restrict(TriggerData *trigdata, bool is_no_action)
Definition: ri_triggers.c:615
static void ri_CheckTrigger(FunctionCallInfo fcinfo, const char *funcname, int tgkind)
Definition: ri_triggers.c:1908
#define RI_TRIGTYPE_UPDATE
Definition: ri_triggers.c:91

◆ RI_FKey_setdefault_del()

Datum RI_FKey_setdefault_del ( PG_FUNCTION_ARGS  )

Definition at line 991 of file ri_triggers.c.

References ri_CheckTrigger(), ri_set(), and RI_TRIGTYPE_DELETE.

992 {
993  /* Check that this is a valid trigger call on the right time and event. */
994  ri_CheckTrigger(fcinfo, "RI_FKey_setdefault_del", RI_TRIGTYPE_DELETE);
995 
996  /* Share code with UPDATE case */
997  return ri_set((TriggerData *) fcinfo->context, false);
998 }
static Datum ri_set(TriggerData *trigdata, bool is_set_null)
Definition: ri_triggers.c:1022
static void ri_CheckTrigger(FunctionCallInfo fcinfo, const char *funcname, int tgkind)
Definition: ri_triggers.c:1908
#define RI_TRIGTYPE_DELETE
Definition: ri_triggers.c:92

◆ RI_FKey_setdefault_upd()

Datum RI_FKey_setdefault_upd ( PG_FUNCTION_ARGS  )

Definition at line 1006 of file ri_triggers.c.

References ri_CheckTrigger(), ri_set(), and RI_TRIGTYPE_UPDATE.

1007 {
1008  /* Check that this is a valid trigger call on the right time and event. */
1009  ri_CheckTrigger(fcinfo, "RI_FKey_setdefault_upd", RI_TRIGTYPE_UPDATE);
1010 
1011  /* Share code with DELETE case */
1012  return ri_set((TriggerData *) fcinfo->context, false);
1013 }
static Datum ri_set(TriggerData *trigdata, bool is_set_null)
Definition: ri_triggers.c:1022
static void ri_CheckTrigger(FunctionCallInfo fcinfo, const char *funcname, int tgkind)
Definition: ri_triggers.c:1908
#define RI_TRIGTYPE_UPDATE
Definition: ri_triggers.c:91

◆ RI_FKey_setnull_del()

Datum RI_FKey_setnull_del ( PG_FUNCTION_ARGS  )

Definition at line 961 of file ri_triggers.c.

References ri_CheckTrigger(), ri_set(), and RI_TRIGTYPE_DELETE.

962 {
963  /* Check that this is a valid trigger call on the right time and event. */
964  ri_CheckTrigger(fcinfo, "RI_FKey_setnull_del", RI_TRIGTYPE_DELETE);
965 
966  /* Share code with UPDATE case */
967  return ri_set((TriggerData *) fcinfo->context, true);
968 }
static Datum ri_set(TriggerData *trigdata, bool is_set_null)
Definition: ri_triggers.c:1022
static void ri_CheckTrigger(FunctionCallInfo fcinfo, const char *funcname, int tgkind)
Definition: ri_triggers.c:1908
#define RI_TRIGTYPE_DELETE
Definition: ri_triggers.c:92

◆ RI_FKey_setnull_upd()

Datum RI_FKey_setnull_upd ( PG_FUNCTION_ARGS  )

Definition at line 976 of file ri_triggers.c.

References ri_CheckTrigger(), ri_set(), and RI_TRIGTYPE_UPDATE.

977 {
978  /* Check that this is a valid trigger call on the right time and event. */
979  ri_CheckTrigger(fcinfo, "RI_FKey_setnull_upd", RI_TRIGTYPE_UPDATE);
980 
981  /* Share code with DELETE case */
982  return ri_set((TriggerData *) fcinfo->context, true);
983 }
static Datum ri_set(TriggerData *trigdata, bool is_set_null)
Definition: ri_triggers.c:1022
static void ri_CheckTrigger(FunctionCallInfo fcinfo, const char *funcname, int tgkind)
Definition: ri_triggers.c:1908
#define RI_TRIGTYPE_UPDATE
Definition: ri_triggers.c:91

◆ RI_FKey_trigger_type()

int RI_FKey_trigger_type ( Oid  tgfoid)

Definition at line 2868 of file ri_triggers.c.

References RI_TRIGGER_FK, RI_TRIGGER_NONE, and RI_TRIGGER_PK.

Referenced by AfterTriggerSaveEvent(), and CreateTrigger().

2869 {
2870  switch (tgfoid)
2871  {
2872  case F_RI_FKEY_CASCADE_DEL:
2873  case F_RI_FKEY_CASCADE_UPD:
2874  case F_RI_FKEY_RESTRICT_DEL:
2875  case F_RI_FKEY_RESTRICT_UPD:
2876  case F_RI_FKEY_SETNULL_DEL:
2877  case F_RI_FKEY_SETNULL_UPD:
2878  case F_RI_FKEY_SETDEFAULT_DEL:
2879  case F_RI_FKEY_SETDEFAULT_UPD:
2880  case F_RI_FKEY_NOACTION_DEL:
2881  case F_RI_FKEY_NOACTION_UPD:
2882  return RI_TRIGGER_PK;
2883 
2884  case F_RI_FKEY_CHECK_INS:
2885  case F_RI_FKEY_CHECK_UPD:
2886  return RI_TRIGGER_FK;
2887  }
2888 
2889  return RI_TRIGGER_NONE;
2890 }
#define RI_TRIGGER_FK
Definition: trigger.h:271
#define RI_TRIGGER_PK
Definition: trigger.h:270
#define RI_TRIGGER_NONE
Definition: trigger.h:272

◆ ri_GenerateQual()

static void ri_GenerateQual ( StringInfo  buf,
const char *  sep,
const char *  leftop,
Oid  leftoptype,
Oid  opoid,
const char *  rightop,
Oid  rightoptype 
)
static

Definition at line 1823 of file ri_triggers.c.

References appendStringInfo(), and generate_operator_clause().

Referenced by ri_Check_Pk_Match(), RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check(), RI_Initial_Check(), RI_PartitionRemove_Check(), ri_restrict(), and ri_set().

1828 {
1829  appendStringInfo(buf, " %s ", sep);
1830  generate_operator_clause(buf, leftop, leftoptype, opoid,
1831  rightop, rightoptype);
1832 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
void generate_operator_clause(StringInfo buf, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
Definition: ruleutils.c:11040

◆ ri_GenerateQualCollation()

static void ri_GenerateQualCollation ( StringInfo  buf,
Oid  collation 
)
static

Definition at line 1852 of file ri_triggers.c.

References appendStringInfo(), COLLOID, elog, ERROR, get_namespace_name(), GETSTRUCT, HeapTupleIsValid, MAX_QUOTED_NAME_LEN, NameStr, ObjectIdGetDatum, OidIsValid, quoteOneName(), ReleaseSysCache(), and SearchSysCache1().

Referenced by RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_Initial_Check(), RI_PartitionRemove_Check(), ri_restrict(), and ri_set().

1853 {
1854  HeapTuple tp;
1855  Form_pg_collation colltup;
1856  char *collname;
1857  char onename[MAX_QUOTED_NAME_LEN];
1858 
1859  /* Nothing to do if it's a noncollatable data type */
1860  if (!OidIsValid(collation))
1861  return;
1862 
1863  tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collation));
1864  if (!HeapTupleIsValid(tp))
1865  elog(ERROR, "cache lookup failed for collation %u", collation);
1866  colltup = (Form_pg_collation) GETSTRUCT(tp);
1867  collname = NameStr(colltup->collname);
1868 
1869  /*
1870  * We qualify the name always, for simplicity and to ensure the query is
1871  * not search-path-dependent.
1872  */
1873  quoteOneName(onename, get_namespace_name(colltup->collnamespace));
1874  appendStringInfo(buf, " COLLATE %s", onename);
1875  quoteOneName(onename, collname);
1876  appendStringInfo(buf, ".%s", onename);
1877 
1878  ReleaseSysCache(tp);
1879 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define OidIsValid(objectId)
Definition: c.h:638
static void quoteOneName(char *buffer, const char *name)
Definition: ri_triggers.c:1786
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define MAX_QUOTED_NAME_LEN
Definition: ri_triggers.c:83
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3094
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:51
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609

◆ ri_HashCompareOp()

static RI_CompareHashEntry * ri_HashCompareOp ( Oid  eq_opr,
Oid  typeid 
)
static

Definition at line 2775 of file ri_triggers.c.

References Assert, RI_CompareHashEntry::cast_func_finfo, COERCION_IMPLICIT, COERCION_PATH_FUNC, COERCION_PATH_RELABELTYPE, elog, RI_CompareKey::eq_opr, RI_CompareHashEntry::eq_opr_finfo, ERROR, find_coercion_pathway(), fmgr_info_cxt(), FmgrInfo::fn_oid, format_type_be(), get_opcode(), HASH_ENTER, hash_search(), InvalidOid, IsBinaryCoercible(), sort-test::key, OidIsValid, op_input_types(), ri_InitHashTables(), TopMemoryContext, RI_CompareKey::typeid, and RI_CompareHashEntry::valid.

Referenced by ri_AttributesEqual().

2776 {
2778  RI_CompareHashEntry *entry;
2779  bool found;
2780 
2781  /*
2782  * On the first call initialize the hashtable
2783  */
2784  if (!ri_compare_cache)
2786 
2787  /*
2788  * Find or create a hash entry. Note we're assuming RI_CompareKey
2789  * contains no struct padding.
2790  */
2791  key.eq_opr = eq_opr;
2792  key.typeid = typeid;
2794  (void *) &key,
2795  HASH_ENTER, &found);
2796  if (!found)
2797  entry->valid = false;
2798 
2799  /*
2800  * If not already initialized, do so. Since we'll keep this hash entry
2801  * for the life of the backend, put any subsidiary info for the function
2802  * cache structs into TopMemoryContext.
2803  */
2804  if (!entry->valid)
2805  {
2806  Oid lefttype,
2807  righttype,
2808  castfunc;
2809  CoercionPathType pathtype;
2810 
2811  /* We always need to know how to call the equality operator */
2812  fmgr_info_cxt(get_opcode(eq_opr), &entry->eq_opr_finfo,
2814 
2815  /*
2816  * If we chose to use a cast from FK to PK type, we may have to apply
2817  * the cast function to get to the operator's input type.
2818  *
2819  * XXX eventually it would be good to support array-coercion cases
2820  * here and in ri_AttributesEqual(). At the moment there is no point
2821  * because cases involving nonidentical array types will be rejected
2822  * at constraint creation time.
2823  *
2824  * XXX perhaps also consider supporting CoerceViaIO? No need at the
2825  * moment since that will never be generated for implicit coercions.
2826  */
2827  op_input_types(eq_opr, &lefttype, &righttype);
2828  Assert(lefttype == righttype);
2829  if (typeid == lefttype)
2830  castfunc = InvalidOid; /* simplest case */
2831  else
2832  {
2833  pathtype = find_coercion_pathway(lefttype, typeid,
2835  &castfunc);
2836  if (pathtype != COERCION_PATH_FUNC &&
2837  pathtype != COERCION_PATH_RELABELTYPE)
2838  {
2839  /*
2840  * The declared input type of the eq_opr might be a
2841  * polymorphic type such as ANYARRAY or ANYENUM, or other
2842  * special cases such as RECORD; find_coercion_pathway
2843  * currently doesn't subsume these special cases.
2844  */
2845  if (!IsBinaryCoercible(typeid, lefttype))
2846  elog(ERROR, "no conversion function from %s to %s",
2847  format_type_be(typeid),
2848  format_type_be(lefttype));
2849  }
2850  }
2851  if (OidIsValid(castfunc))
2852  fmgr_info_cxt(castfunc, &entry->cast_func_finfo,
2854  else
2856  entry->valid = true;
2857  }
2858 
2859  return entry;
2860 }
char * format_type_be(Oid type_oid)
Definition: format_type.c:326
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
CoercionPathType
Definition: parse_coerce.h:24
#define ERROR
Definition: elog.h:43
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1165
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:134
MemoryContext TopMemoryContext
Definition: mcxt.c:44
bool IsBinaryCoercible(Oid srctype, Oid targettype)
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1092
Oid fn_oid
Definition: fmgr.h:59
#define Assert(condition)
Definition: c.h:732
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
#define elog(elevel,...)
Definition: elog.h:226
static void ri_InitHashTables(void)
Definition: ri_triggers.c:2537
FmgrInfo cast_func_finfo
Definition: ri_triggers.c:160
static HTAB * ri_compare_cache
Definition: ri_triggers.c:169

◆ ri_HashPreparedPlan()

static void ri_HashPreparedPlan ( RI_QueryKey key,
SPIPlanPtr  plan 
)
static

Definition at line 2628 of file ri_triggers.c.

References Assert, HASH_ENTER, hash_search(), sort-test::key, RI_QueryHashEntry::plan, and ri_InitHashTables().

Referenced by ri_PlanCheck().

2629 {
2630  RI_QueryHashEntry *entry;
2631  bool found;
2632 
2633  /*
2634  * On the first call initialize the hashtable
2635  */
2636  if (!ri_query_cache)
2638 
2639  /*
2640  * Add the new plan. We might be overwriting an entry previously found
2641  * invalid by ri_FetchPreparedPlan.
2642  */
2644  (void *) key,
2645  HASH_ENTER, &found);
2646  Assert(!found || entry->plan == NULL);
2647  entry->plan = plan;
2648 }
static HTAB * ri_query_cache
Definition: ri_triggers.c:168
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
SPIPlanPtr plan
Definition: ri_triggers.c:138
#define Assert(condition)
Definition: c.h:732
static void ri_InitHashTables(void)
Definition: ri_triggers.c:2537

◆ ri_InitHashTables()

static void ri_InitHashTables ( void  )
static

Definition at line 2537 of file ri_triggers.c.

References CacheRegisterSyscacheCallback(), CONSTROID, HASHCTL::entrysize, HASH_BLOBS, hash_create(), HASH_ELEM, InvalidateConstraintCacheCallBack(), HASHCTL::keysize, RI_INIT_CONSTRAINTHASHSIZE, and RI_INIT_QUERYHASHSIZE.

Referenced by ri_FetchPreparedPlan(), ri_HashCompareOp(), ri_HashPreparedPlan(), and ri_LoadConstraintInfo().

2538 {
2539  HASHCTL ctl;
2540 
2541  memset(&ctl, 0, sizeof(ctl));
2542  ctl.keysize = sizeof(Oid);
2543  ctl.entrysize = sizeof(RI_ConstraintInfo);
2544  ri_constraint_cache = hash_create("RI constraint cache",
2546  &ctl, HASH_ELEM | HASH_BLOBS);
2547 
2548  /* Arrange to flush cache on pg_constraint changes */
2551  (Datum) 0);
2552 
2553  memset(&ctl, 0, sizeof(ctl));
2554  ctl.keysize = sizeof(RI_QueryKey);
2555  ctl.entrysize = sizeof(RI_QueryHashEntry);
2556  ri_query_cache = hash_create("RI query cache",
2558  &ctl, HASH_ELEM | HASH_BLOBS);
2559 
2560  memset(&ctl, 0, sizeof(ctl));
2561  ctl.keysize = sizeof(RI_CompareKey);
2562  ctl.entrysize = sizeof(RI_CompareHashEntry);
2563  ri_compare_cache = hash_create("RI compare cache",
2565  &ctl, HASH_ELEM | HASH_BLOBS);
2566 }
static HTAB * ri_query_cache
Definition: ri_triggers.c:168
struct RI_CompareKey RI_CompareKey
#define HASH_ELEM
Definition: hsearch.h:87
static HTAB * ri_constraint_cache
Definition: ri_triggers.c:167
Size entrysize
Definition: hsearch.h:73
unsigned int Oid
Definition: postgres_ext.h:31
struct RI_ConstraintInfo RI_ConstraintInfo
#define RI_INIT_QUERYHASHSIZE
Definition: ri_triggers.c:65
static void InvalidateConstraintCacheCallBack(Datum arg, int cacheid, uint32 hashvalue)
Definition: ri_triggers.c:2092
struct RI_CompareHashEntry RI_CompareHashEntry
#define HASH_BLOBS
Definition: hsearch.h:88
struct RI_QueryKey RI_QueryKey
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1426
uintptr_t Datum
Definition: postgres.h:367
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
Size keysize
Definition: hsearch.h:72
#define RI_INIT_CONSTRAINTHASHSIZE
Definition: ri_triggers.c:64
static HTAB * ri_compare_cache
Definition: ri_triggers.c:169
struct RI_QueryHashEntry RI_QueryHashEntry

◆ RI_Initial_Check()

bool RI_Initial_Check ( Trigger trigger,
Relation  fk_rel,
Relation  pk_rel 
)

Definition at line 1297 of file ri_triggers.c.

References AccessShareLock, ACL_SELECT, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), AtEOXact_GUC(), bms_add_member(), RI_ConstraintInfo::confmatchtype, RI_ConstraintInfo::conname, StringInfoData::data, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, errtableconstraint(), ExecCheckRTPerms(), ExecDropSingleTupleTableSlot(), ExecStoreVirtualTuple(), FirstLowInvalidHeapAttributeNumber, RI_ConstraintInfo::fk_attnums, FKCONSTR_MATCH_FULL, FKCONSTR_MATCH_SIMPLE, GetLatestSnapshot(), GetUserId(), GUC_ACTION_SAVE, has_bypassrls_privilege(), heap_deform_tuple(), i, initStringInfo(), InvalidSnapshot, list_make2, maintenance_work_mem, makeNode, MakeSingleTupleTableSlot(), MAX_QUOTED_NAME_LEN, MAX_QUOTED_REL_NAME_LEN, NameStr, NewGUCNestLevel(), RI_ConstraintInfo::nkeys, RI_ConstraintInfo::pf_eq_oprs, pg_class_ownercheck(), PGC_S_SESSION, PGC_USERSET, RI_ConstraintInfo::pk_attnums, quoteOneName(), quoteRelationName(), RelationData::rd_rel, RelationGetRelationName, RelationGetRelid, RangeTblEntry::relid, RangeTblEntry::relkind, RangeTblEntry::rellockmode, RangeTblEntry::requiredPerms, ri_FetchConstraintInfo(), ri_GenerateQual(), ri_GenerateQualCollation(), RI_KEYS_NONE_NULL, ri_NullCheck(), RI_PLAN_CHECK_LOOKUPPK, ri_ReportViolation(), RIAttCollation, RIAttName, RIAttType, RTE_RELATION, RangeTblEntry::rtekind, RangeTblEntry::selectedCols, set_config_option(), snprintf, SPI_connect(), SPI_execute_snapshot(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_OK_SELECT, SPI_prepare(), SPI_processed, SPI_result, SPI_result_code_string(), SPI_tuptable, TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, TTSOpsVirtual, SPITupleTable::tupdesc, and SPITupleTable::vals.

Referenced by validateForeignKeyConstraint().

1298 {
1299  const RI_ConstraintInfo *riinfo;
1300  StringInfoData querybuf;
1301  char pkrelname[MAX_QUOTED_REL_NAME_LEN];
1302  char fkrelname[MAX_QUOTED_REL_NAME_LEN];
1303  char pkattname[MAX_QUOTED_NAME_LEN + 3];
1304  char fkattname[MAX_QUOTED_NAME_LEN + 3];
1305  RangeTblEntry *pkrte;
1306  RangeTblEntry *fkrte;
1307  const char *sep;
1308  const char *fk_only;
1309  const char *pk_only;
1310  int save_nestlevel;
1311  char workmembuf[32];
1312  int spi_result;
1313  SPIPlanPtr qplan;
1314 
1315  riinfo = ri_FetchConstraintInfo(trigger, fk_rel, false);
1316 
1317  /*
1318  * Check to make sure current user has enough permissions to do the test
1319  * query. (If not, caller can fall back to the trigger method, which
1320  * works because it changes user IDs on the fly.)
1321  *
1322  * XXX are there any other show-stopper conditions to check?
1323  */
1324  pkrte = makeNode(RangeTblEntry);
1325  pkrte->rtekind = RTE_RELATION;
1326  pkrte->relid = RelationGetRelid(pk_rel);
1327  pkrte->relkind = pk_rel->rd_rel->relkind;
1328  pkrte->rellockmode = AccessShareLock;
1329  pkrte->requiredPerms = ACL_SELECT;
1330 
1331  fkrte = makeNode(RangeTblEntry);
1332  fkrte->rtekind = RTE_RELATION;
1333  fkrte->relid = RelationGetRelid(fk_rel);
1334  fkrte->relkind = fk_rel->rd_rel->relkind;
1335  fkrte->rellockmode = AccessShareLock;
1336  fkrte->requiredPerms = ACL_SELECT;
1337 
1338  for (int i = 0; i < riinfo->nkeys; i++)
1339  {
1340  int attno;
1341 
1342  attno = riinfo->pk_attnums[i] - FirstLowInvalidHeapAttributeNumber;
1343  pkrte->selectedCols = bms_add_member(pkrte->selectedCols, attno);
1344 
1345  attno = riinfo->fk_attnums[i] - FirstLowInvalidHeapAttributeNumber;
1346  fkrte->selectedCols = bms_add_member(fkrte->selectedCols, attno);
1347  }
1348 
1349  if (!ExecCheckRTPerms(list_make2(fkrte, pkrte), false))
1350  return false;
1351 
1352  /*
1353  * Also punt if RLS is enabled on either table unless this role has the
1354  * bypassrls right or is the table owner of the table(s) involved which
1355  * have RLS enabled.
1356  */
1358  ((pk_rel->rd_rel->relrowsecurity &&
1359  !pg_class_ownercheck(pkrte->relid, GetUserId())) ||
1360  (fk_rel->rd_rel->relrowsecurity &&
1361  !pg_class_ownercheck(fkrte->relid, GetUserId()))))
1362  return false;
1363 
1364  /*----------
1365  * The query string built is:
1366  * SELECT fk.keycols FROM [ONLY] relname fk
1367  * LEFT OUTER JOIN [ONLY] pkrelname pk
1368  * ON (pk.pkkeycol1=fk.keycol1 [AND ...])
1369  * WHERE pk.pkkeycol1 IS NULL AND
1370  * For MATCH SIMPLE:
1371  * (fk.keycol1 IS NOT NULL [AND ...])
1372  * For MATCH FULL:
1373  * (fk.keycol1 IS NOT NULL [OR ...])
1374  *
1375  * We attach COLLATE clauses to the operators when comparing columns
1376  * that have different collations.
1377  *----------
1378  */
1379  initStringInfo(&querybuf);
1380  appendStringInfoString(&querybuf, "SELECT ");
1381  sep = "";
1382  for (int i = 0; i < riinfo->nkeys; i++)
1383  {
1384  quoteOneName(fkattname,
1385  RIAttName(fk_rel, riinfo->fk_attnums[i]));
1386  appendStringInfo(&querybuf, "%sfk.%s", sep, fkattname);
1387  sep = ", ";
1388  }
1389 
1390  quoteRelationName(pkrelname, pk_rel);
1391  quoteRelationName(fkrelname, fk_rel);
1392  fk_only = fk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ?
1393  "" : "ONLY ";
1394  pk_only = pk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ?
1395  "" : "ONLY ";
1396  appendStringInfo(&querybuf,
1397  " FROM %s%s fk LEFT OUTER JOIN %s%s pk ON",
1398  fk_only, fkrelname, pk_only, pkrelname);
1399 
1400  strcpy(pkattname, "pk.");
1401  strcpy(fkattname, "fk.");
1402  sep = "(";
1403  for (int i = 0; i < riinfo->nkeys; i++)
1404  {
1405  Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
1406  Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
1407  Oid pk_coll = RIAttCollation(pk_rel, riinfo->pk_attnums[i]);
1408  Oid fk_coll = RIAttCollation(fk_rel, riinfo->fk_attnums[i]);
1409 
1410  quoteOneName(pkattname + 3,
1411  RIAttName(pk_rel, riinfo->pk_attnums[i]));
1412  quoteOneName(fkattname + 3,
1413  RIAttName(fk_rel, riinfo->fk_attnums[i]));
1414  ri_GenerateQual(&querybuf, sep,
1415  pkattname, pk_type,
1416  riinfo->pf_eq_oprs[i],
1417  fkattname, fk_type);
1418  if (pk_coll != fk_coll)
1419  ri_GenerateQualCollation(&querybuf, pk_coll);
1420  sep = "AND";
1421  }
1422 
1423  /*
1424  * It's sufficient to test any one pk attribute for null to detect a join
1425  * failure.
1426  */
1427  quoteOneName(pkattname, RIAttName(pk_rel, riinfo->pk_attnums[0]));
1428  appendStringInfo(&querybuf, ") WHERE pk.%s IS NULL AND (", pkattname);
1429 
1430  sep = "";
1431  for (int i = 0; i < riinfo->nkeys; i++)
1432  {
1433  quoteOneName(fkattname, RIAttName(fk_rel, riinfo->fk_attnums[i]));
1434  appendStringInfo(&querybuf,
1435  "%sfk.%s IS NOT NULL",
1436  sep, fkattname);
1437  switch (riinfo->confmatchtype)
1438  {
1439  case FKCONSTR_MATCH_SIMPLE:
1440  sep = " AND ";
1441  break;
1442  case FKCONSTR_MATCH_FULL:
1443  sep = " OR ";
1444  break;
1445  }
1446  }
1447  appendStringInfoChar(&querybuf, ')');
1448 
1449  /*
1450  * Temporarily increase work_mem so that the check query can be executed
1451  * more efficiently. It seems okay to do this because the query is simple
1452  * enough to not use a multiple of work_mem, and one typically would not
1453  * have many large foreign-key validations happening concurrently. So
1454  * this seems to meet the criteria for being considered a "maintenance"
1455  * operation, and accordingly we use maintenance_work_mem.
1456  *
1457  * We use the equivalent of a function SET option to allow the setting to
1458  * persist for exactly the duration of the check query. guc.c also takes
1459  * care of undoing the setting on error.
1460  */
1461  save_nestlevel = NewGUCNestLevel();
1462 
1463  snprintf(workmembuf, sizeof(workmembuf), "%d", maintenance_work_mem);
1464  (void) set_config_option("work_mem", workmembuf,
1466  GUC_ACTION_SAVE, true, 0, false);
1467 
1468  if (SPI_connect() != SPI_OK_CONNECT)
1469  elog(ERROR, "SPI_connect failed");
1470 
1471  /*
1472  * Generate the plan. We don't need to cache it, and there are no
1473  * arguments to the plan.
1474  */
1475  qplan = SPI_prepare(querybuf.data, 0, NULL);
1476 
1477  if (qplan == NULL)
1478  elog(ERROR, "SPI_prepare returned %s for %s",
1480 
1481  /*
1482  * Run the plan. For safety we force a current snapshot to be used. (In
1483  * transaction-snapshot mode, this arguably violates transaction isolation
1484  * rules, but we really haven't got much choice.) We don't need to
1485  * register the snapshot, because SPI_execute_snapshot will see to it. We
1486  * need at most one tuple returned, so pass limit = 1.
1487  */
1488  spi_result = SPI_execute_snapshot(qplan,
1489  NULL, NULL,
1492  true, false, 1);
1493 
1494  /* Check result */
1495  if (spi_result != SPI_OK_SELECT)
1496  elog(ERROR, "SPI_execute_snapshot returned %s", SPI_result_code_string(spi_result));
1497 
1498  /* Did we find a tuple violating the constraint? */
1499  if (SPI_processed > 0)
1500  {
1501  TupleTableSlot *slot;
1502  HeapTuple tuple = SPI_tuptable->vals[0];
1503  TupleDesc tupdesc = SPI_tuptable->tupdesc;
1504  RI_ConstraintInfo fake_riinfo;
1505 
1506  slot = MakeSingleTupleTableSlot(tupdesc, &TTSOpsVirtual);
1507 
1508  heap_deform_tuple(tuple, tupdesc,
1509  slot->tts_values, slot->tts_isnull);
1510  ExecStoreVirtualTuple(slot);
1511 
1512  /*
1513  * The columns to look at in the result tuple are 1..N, not whatever
1514  * they are in the fk_rel. Hack up riinfo so that the subroutines
1515  * called here will behave properly.
1516  *
1517  * In addition to this, we have to pass the correct tupdesc to
1518  * ri_ReportViolation, overriding its normal habit of using the pk_rel
1519  * or fk_rel's tupdesc.
1520  */
1521  memcpy(&fake_riinfo, riinfo, sizeof(RI_ConstraintInfo));
1522  for (int i = 0; i < fake_riinfo.nkeys; i++)
1523  fake_riinfo.fk_attnums[i] = i + 1;
1524 
1525  /*
1526  * If it's MATCH FULL, and there are any nulls in the FK keys,
1527  * complain about that rather than the lack of a match. MATCH FULL
1528  * disallows partially-null FK rows.
1529  */
1530  if (fake_riinfo.confmatchtype == FKCONSTR_MATCH_FULL &&
1531  ri_NullCheck(tupdesc, slot, &fake_riinfo, false) != RI_KEYS_NONE_NULL)
1532  ereport(ERROR,
1533  (errcode(ERRCODE_FOREIGN_KEY_VIOLATION),
1534  errmsg("insert or update on table \"%s\" violates foreign key constraint \"%s\"",
1535  RelationGetRelationName(fk_rel),
1536  NameStr(fake_riinfo.conname)),
1537  errdetail("MATCH FULL does not allow mixing of null and nonnull key values."),
1538  errtableconstraint(fk_rel,
1539  NameStr(fake_riinfo.conname))));
1540 
1541  /*
1542  * We tell ri_ReportViolation we were doing the RI_PLAN_CHECK_LOOKUPPK
1543  * query, which isn't true, but will cause it to use
1544  * fake_riinfo.fk_attnums as we need.
1545  */
1546  ri_ReportViolation(&fake_riinfo,
1547  pk_rel, fk_rel,
1548  slot, tupdesc,
1549  RI_PLAN_CHECK_LOOKUPPK, false);
1550 
1552  }
1553 
1554  if (SPI_finish() != SPI_OK_FINISH)
1555  elog(ERROR, "SPI_finish failed");
1556 
1557  /*
1558  * Restore work_mem.
1559  */
1560  AtEOXact_GUC(true, save_nestlevel);
1561 
1562  return true;
1563 }
static void ri_GenerateQualCollation(StringInfo buf, Oid collation)
Definition: ri_triggers.c:1852
#define list_make2(x1, x2)
Definition: pg_list.h:229
static void ri_GenerateQual(StringInfo buf, const char *sep, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
Definition: ri_triggers.c:1823
#define SPI_OK_CONNECT
Definition: spi.h:53
#define FKCONSTR_MATCH_SIMPLE
Definition: parsenodes.h:2124
static void quoteRelationName(char *buffer, Relation rel)
Definition: ri_triggers.c:1806
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1203
Oid GetUserId(void)
Definition: miscinit.c:380
int SPI_connect(void)
Definition: spi.c:89
static void ri_ReportViolation(const RI_ConstraintInfo *riinfo, Relation pk_rel, Relation fk_rel, TupleTableSlot *violatorslot, TupleDesc tupdesc, int queryno, bool partgone) pg_attribute_noreturn()
Definition: ri_triggers.c:2343
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:674
int SPI_finish(void)
Definition: spi.c:176
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:84
#define AccessShareLock
Definition: lockdefs.h:36
SPITupleTable * SPI_tuptable
Definition: spi.c:46
int errcode(int sqlerrcode)
Definition: elog.c:570
#define RIAttType(rel, attnum)
Definition: ri_triggers.c:87
#define RIAttName(rel, attnum)
Definition: ri_triggers.c:86
Datum * tts_values
Definition: tuptable.h:126
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
AclMode requiredPerms
Definition: parsenodes.h:1096
Form_pg_class rd_rel
Definition: rel.h:83
unsigned int Oid
Definition: postgres_ext.h:31
HeapTuple * vals
Definition: spi.h:26
uint64 SPI_processed
Definition: spi.c:45
bool has_bypassrls_privilege(Oid roleid)
Definition: aclchk.c:5399
int SPI_result
Definition: spi.c:47
int errtableconstraint(Relation rel, const char *conname)
Definition: relcache.c:5222
#define RIAttCollation(rel, attnum)
Definition: ri_triggers.c:88
Bitmapset * selectedCols
Definition: parsenodes.h:1098
static void quoteOneName(char *buffer, const char *name)
Definition: ri_triggers.c:1786
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
#define MAX_QUOTED_NAME_LEN
Definition: ri_triggers.c:83
#define MAX_QUOTED_REL_NAME_LEN
Definition: ri_triggers.c:84
int16 pk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:113
const char * SPI_result_code_string(int code)
Definition: spi.c:1705
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:163
bool * tts_isnull
Definition: tuptable.h:128
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1219
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5729
int errdetail(const char *fmt,...)
Definition: elog.c:860
Oid pf_eq_oprs[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:115
#define RelationGetRelationName(relation)
Definition: rel.h:453
static const RI_ConstraintInfo * ri_FetchConstraintInfo(Trigger *trigger, Relation trig_rel, bool rel_is_pk)
Definition: ri_triggers.c:1954
#define RI_PLAN_CHECK_LOOKUPPK
Definition: ri_triggers.c:73
#define ereport(elevel, rest)
Definition: elog.h:141
int SPI_execute_snapshot(SPIPlanPtr plan, Datum *Values, const char *Nulls, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, long tcount)
Definition: spi.c:599
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:175
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define InvalidSnapshot
Definition: snapshot.h:123
#define FKCONSTR_MATCH_FULL
Definition: parsenodes.h:2122
#define ACL_SELECT
Definition: parsenodes.h:75
TupleDesc tupdesc
Definition: spi.h:25
#define SPI_OK_SELECT
Definition: spi.h:57
int maintenance_work_mem
Definition: globals.c:122
#define makeNode(_type_)
Definition: nodes.h:573
static int ri_NullCheck(TupleDesc tupdesc, TupleTableSlot *slot, const RI_ConstraintInfo *riinfo, bool rel_is_pk)
Definition: ri_triggers.c:2500
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4755
#define SPI_OK_FINISH
Definition: spi.h:54
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:381
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
RTEKind rtekind
Definition: parsenodes.h:974
int NewGUCNestLevel(void)
Definition: guc.c:5715
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
int i
bool ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
Definition: execMain.c:571
#define NameStr(name)
Definition: c.h:609
int16 fk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:114
int set_config_option(const char *name, const char *value, GucContext context, GucSource source, GucAction action, bool changeVal, int elevel, bool is_reload)
Definition: guc.c:6722
#define snprintf
Definition: port.h:192
#define RI_KEYS_NONE_NULL
Definition: ri_triggers.c:69
#define RelationGetRelid(relation)
Definition: rel.h:419
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1517

◆ ri_KeysEqual()

static bool ri_KeysEqual ( Relation  rel,
TupleTableSlot oldslot,
TupleTableSlot newslot,
const RI_ConstraintInfo riinfo,
bool  rel_is_pk 
)
static

Definition at line 2662 of file ri_triggers.c.

References datum_image_eq(), RI_ConstraintInfo::ff_eq_oprs, RI_ConstraintInfo::fk_attnums, i, RI_ConstraintInfo::nkeys, RI_ConstraintInfo::pk_attnums, ri_AttributesEqual(), RIAttType, slot_getattr(), TupleTableSlot::tts_tupleDescriptor, and TupleDescAttr.

Referenced by RI_FKey_fk_upd_check_required(), and RI_FKey_pk_upd_check_required().

2664 {
2665  const int16 *attnums;
2666 
2667  if (rel_is_pk)
2668  attnums = riinfo->pk_attnums;
2669  else
2670  attnums = riinfo->fk_attnums;
2671 
2672  /* XXX: could be worthwhile to fetch all necessary attrs at once */
2673  for (int i = 0; i < riinfo->nkeys; i++)
2674  {
2675  Datum oldvalue;
2676  Datum newvalue;
2677  bool isnull;
2678 
2679  /*
2680  * Get one attribute's oldvalue. If it is NULL - they're not equal.
2681  */
2682  oldvalue = slot_getattr(oldslot, attnums[i], &isnull);
2683  if (isnull)
2684  return false;
2685 
2686  /*
2687  * Get one attribute's newvalue. If it is NULL - they're not equal.
2688  */
2689  newvalue = slot_getattr(newslot, attnums[i], &isnull);
2690  if (isnull)
2691  return false;
2692 
2693  if (rel_is_pk)
2694  {
2695  /*
2696  * If we are looking at the PK table, then do a bytewise
2697  * comparison. We must propagate PK changes if the value is
2698  * changed to one that "looks" different but would compare as
2699  * equal using the equality operator. This only makes a
2700  * difference for ON UPDATE CASCADE, but for consistency we treat
2701  * all changes to the PK the same.
2702  */
2703  Form_pg_attribute att = TupleDescAttr(oldslot->tts_tupleDescriptor, attnums[i] - 1);
2704 
2705  if (!datum_image_eq(oldvalue, newvalue, att->attbyval, att->attlen))
2706  return false;
2707  }
2708  else
2709  {
2710  /*
2711  * For the FK table, compare with the appropriate equality
2712  * operator. Changes that compare equal will still satisfy the
2713  * constraint after the update.
2714  */
2715  if (!ri_AttributesEqual(riinfo->ff_eq_oprs[i], RIAttType(rel, attnums[i]),
2716  oldvalue, newvalue))
2717  return false;
2718  }
2719  }
2720 
2721  return true;
2722 }
signed short int16
Definition: c.h:345
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define RIAttType(rel, attnum)
Definition: ri_triggers.c:87
static bool ri_AttributesEqual(Oid eq_opr, Oid typeid, Datum oldvalue, Datum newvalue)
Definition: ri_triggers.c:2733
int16 pk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:113
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
Oid ff_eq_oprs[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:117
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
uintptr_t Datum
Definition: postgres.h:367
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:382
int i
bool datum_image_eq(Datum value1, Datum value2, bool typByVal, int typLen)
Definition: datum.c:264
int16 fk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:114

◆ ri_LoadConstraintInfo()

static const RI_ConstraintInfo * ri_LoadConstraintInfo ( Oid  constraintOid)
static

Definition at line 2008 of file ri_triggers.c.

References Assert, RI_ConstraintInfo::confdeltype, RI_ConstraintInfo::confmatchtype, RI_ConstraintInfo::confupdtype, RI_ConstraintInfo::conname, RI_ConstraintInfo::constraint_id, CONSTROID, DeconstructFkConstraintRow(), dlist_push_tail(), elog, ERROR, RI_ConstraintInfo::ff_eq_oprs, RI_ConstraintInfo::fk_attnums, RI_ConstraintInfo::fk_relid, GETSTRUCT, GetSysCacheHashValue1, HASH_ENTER, hash_search(), HeapTupleIsValid, RI_ConstraintInfo::nkeys, ObjectIdGetDatum, RI_ConstraintInfo::oidHashValue, RI_ConstraintInfo::pf_eq_oprs, RI_ConstraintInfo::pk_attnums, RI_ConstraintInfo::pk_relid, RI_ConstraintInfo::pp_eq_oprs, ReleaseSysCache(), ri_constraint_cache_valid_count, ri_InitHashTables(), SearchSysCache1(), RI_ConstraintInfo::valid, and RI_ConstraintInfo::valid_link.

Referenced by ri_FetchConstraintInfo().

2009 {
2010  RI_ConstraintInfo *riinfo;
2011  bool found;
2012  HeapTuple tup;
2013  Form_pg_constraint conForm;
2014 
2015  /*
2016  * On the first call initialize the hashtable
2017  */
2018  if (!ri_constraint_cache)
2020 
2021  /*
2022  * Find or create a hash entry. If we find a valid one, just return it.
2023  */
2025  (void *) &constraintOid,
2026  HASH_ENTER, &found);
2027  if (!found)
2028  riinfo->valid = false;
2029  else if (riinfo->valid)
2030  return riinfo;
2031 
2032  /*
2033  * Fetch the pg_constraint row so we can fill in the entry.
2034  */
2035  tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constraintOid));
2036  if (!HeapTupleIsValid(tup)) /* should not happen */
2037  elog(ERROR, "cache lookup failed for constraint %u", constraintOid);
2038  conForm = (Form_pg_constraint) GETSTRUCT(tup);
2039 
2040  if (conForm->contype != CONSTRAINT_FOREIGN) /* should not happen */
2041  elog(ERROR, "constraint %u is not a foreign key constraint",
2042  constraintOid);
2043 
2044  /* And extract data */
2045  Assert(riinfo->constraint_id == constraintOid);
2047  ObjectIdGetDatum(constraintOid));
2048  memcpy(&riinfo->conname, &conForm->conname, sizeof(NameData));
2049  riinfo->pk_relid = conForm->confrelid;
2050  riinfo->fk_relid = conForm->conrelid;
2051  riinfo->confupdtype = conForm->confupdtype;
2052  riinfo->confdeltype = conForm->confdeltype;
2053  riinfo->confmatchtype = conForm->confmatchtype;
2054 
2056  &riinfo->nkeys,
2057  riinfo->fk_attnums,
2058  riinfo->pk_attnums,
2059  riinfo->pf_eq_oprs,
2060  riinfo->pp_eq_oprs,
2061  riinfo->ff_eq_oprs);
2062 
2063  ReleaseSysCache(tup);
2064 
2065  /*
2066  * For efficient processing of invalidation messages below, we keep a
2067  * doubly-linked list, and a count, of all currently valid entries.
2068  */
2071 
2072  riinfo->valid = true;
2073 
2074  return riinfo;
2075 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks, AttrNumber *conkey, AttrNumber *confkey, Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs)
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
static HTAB * ri_constraint_cache
Definition: ri_triggers.c:167
#define GetSysCacheHashValue1(cacheId, key1)
Definition: syscache.h:201
dlist_node valid_link
Definition: ri_triggers.c:118
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
int16 pk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:113
Definition: c.h:603
Oid pf_eq_oprs[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:115
Oid pp_eq_oprs[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:116
Oid ff_eq_oprs[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:117
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
static int ri_constraint_cache_valid_count
Definition: ri_triggers.c:171
FormData_pg_constraint * Form_pg_constraint
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
static dlist_head ri_constraint_cache_valid_list
Definition: ri_triggers.c:170
#define elog(elevel,...)
Definition: elog.h:226
static void ri_InitHashTables(void)
Definition: ri_triggers.c:2537
int16 fk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:114

◆ ri_NullCheck()

static int ri_NullCheck ( TupleDesc  tupdesc,
TupleTableSlot slot,
const RI_ConstraintInfo riinfo,
bool  rel_is_pk 
)
static

Definition at line 2500 of file ri_triggers.c.

References RI_ConstraintInfo::fk_attnums, i, RI_ConstraintInfo::nkeys, RI_ConstraintInfo::pk_attnums, RI_KEYS_ALL_NULL, RI_KEYS_NONE_NULL, RI_KEYS_SOME_NULL, and slot_attisnull().

Referenced by ri_Check_Pk_Match(), RI_FKey_check(), RI_FKey_fk_upd_check_required(), RI_FKey_pk_upd_check_required(), and RI_Initial_Check().

2503 {
2504  const int16 *attnums;
2505  bool allnull = true;
2506  bool nonenull = true;
2507 
2508  if (rel_is_pk)
2509  attnums = riinfo->pk_attnums;
2510  else
2511  attnums = riinfo->fk_attnums;
2512 
2513  for (int i = 0; i < riinfo->nkeys; i++)
2514  {
2515  if (slot_attisnull(slot, attnums[i]))
2516  nonenull = false;
2517  else
2518  allnull = false;
2519  }
2520 
2521  if (allnull)
2522  return RI_KEYS_ALL_NULL;
2523 
2524  if (nonenull)
2525  return RI_KEYS_NONE_NULL;
2526 
2527  return RI_KEYS_SOME_NULL;
2528 }
signed short int16
Definition: c.h:345
int16 pk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:113
#define RI_KEYS_ALL_NULL
Definition: ri_triggers.c:67
#define RI_KEYS_SOME_NULL
Definition: ri_triggers.c:68
int i
int16 fk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:114
static bool slot_attisnull(TupleTableSlot *slot, int attnum)
Definition: tuptable.h:368
#define RI_KEYS_NONE_NULL
Definition: ri_triggers.c:69

◆ RI_PartitionRemove_Check()

void RI_PartitionRemove_Check ( Trigger trigger,
Relation  fk_rel,
Relation  pk_rel 
)

Definition at line 1572 of file ri_triggers.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), AtEOXact_GUC(), RI_ConstraintInfo::confmatchtype, StringInfoData::data, elog, ERROR, ExecStoreVirtualTuple(), RI_ConstraintInfo::fk_attnums, FKCONSTR_MATCH_FULL, FKCONSTR_MATCH_SIMPLE, GetLatestSnapshot(), GUC_ACTION_SAVE, heap_deform_tuple(), i, initStringInfo(), InvalidSnapshot, maintenance_work_mem, MakeSingleTupleTableSlot(), MAX_QUOTED_NAME_LEN, MAX_QUOTED_REL_NAME_LEN, NewGUCNestLevel(), RI_ConstraintInfo::nkeys, RI_ConstraintInfo::pf_eq_oprs, pg_get_partconstrdef_string(), PGC_S_SESSION, PGC_USERSET, RI_ConstraintInfo::pk_attnums, quoteOneName(), quoteRelationName(), RelationData::rd_rel, RelationGetRelid, ri_FetchConstraintInfo(), ri_GenerateQual(), ri_GenerateQualCollation(), ri_ReportViolation(), RIAttCollation, RIAttName, RIAttType, set_config_option(), snprintf, SPI_connect(), SPI_execute_snapshot(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_OK_SELECT, SPI_prepare(), SPI_processed, SPI_result, SPI_result_code_string(), SPI_tuptable, TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, TTSOpsVirtual, SPITupleTable::tupdesc, and SPITupleTable::vals.

Referenced by ATDetachCheckNoForeignKeyRefs().

1573 {
1574  const RI_ConstraintInfo *riinfo;
1575  StringInfoData querybuf;
1576  char *constraintDef;
1577  char pkrelname[MAX_QUOTED_REL_NAME_LEN];
1578  char fkrelname[MAX_QUOTED_REL_NAME_LEN];
1579  char pkattname[MAX_QUOTED_NAME_LEN + 3];
1580  char fkattname[MAX_QUOTED_NAME_LEN + 3];
1581  const char *sep;
1582  const char *fk_only;
1583  int save_nestlevel;
1584  char workmembuf[32];
1585  int spi_result;
1586  SPIPlanPtr qplan;
1587  int i;
1588 
1589  riinfo = ri_FetchConstraintInfo(trigger, fk_rel, false);
1590 
1591  /*
1592  * We don't check permissions before displaying the error message, on the
1593  * assumption that the user detaching the partition must have enough
1594  * privileges to examine the table contents anyhow.
1595  */
1596 
1597  /*----------
1598  * The query string built is:
1599  * SELECT fk.keycols FROM [ONLY] relname fk
1600  * JOIN pkrelname pk
1601  * ON (pk.pkkeycol1=fk.keycol1 [AND ...])
1602  * WHERE (<partition constraint>) AND
1603  * For MATCH SIMPLE:
1604  * (fk.keycol1 IS NOT NULL [AND ...])
1605  * For MATCH FULL:
1606  * (fk.keycol1 IS NOT NULL [OR ...])
1607  *
1608  * We attach COLLATE clauses to the operators when comparing columns
1609  * that have different collations.
1610  *----------
1611  */
1612  initStringInfo(&querybuf);
1613  appendStringInfoString(&querybuf, "SELECT ");
1614  sep = "";
1615  for (i = 0; i < riinfo->nkeys; i++)
1616  {
1617  quoteOneName(fkattname,
1618  RIAttName(fk_rel, riinfo->fk_attnums[i]));
1619  appendStringInfo(&querybuf, "%sfk.%s", sep, fkattname);
1620  sep = ", ";
1621  }
1622 
1623  quoteRelationName(pkrelname, pk_rel);
1624  quoteRelationName(fkrelname, fk_rel);
1625  fk_only = fk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ?
1626  "" : "ONLY ";
1627  appendStringInfo(&querybuf,
1628  " FROM %s%s fk JOIN %s pk ON",
1629  fk_only, fkrelname, pkrelname);
1630  strcpy(pkattname, "pk.");
1631  strcpy(fkattname, "fk.");
1632  sep = "(";
1633  for (i = 0; i < riinfo->nkeys; i++)
1634  {
1635  Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
1636  Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
1637  Oid pk_coll = RIAttCollation(pk_rel, riinfo->pk_attnums[i]);
1638  Oid fk_coll = RIAttCollation(fk_rel, riinfo->fk_attnums[i]);
1639 
1640  quoteOneName(pkattname + 3,
1641  RIAttName(pk_rel, riinfo->pk_attnums[i]));
1642  quoteOneName(fkattname + 3,
1643  RIAttName(fk_rel, riinfo->fk_attnums[i]));
1644  ri_GenerateQual(&querybuf, sep,
1645  pkattname, pk_type,
1646  riinfo->pf_eq_oprs[i],
1647  fkattname, fk_type);
1648  if (pk_coll != fk_coll)
1649  ri_GenerateQualCollation(&querybuf, pk_coll);
1650  sep = "AND";
1651  }
1652 
1653  /*
1654  * Start the WHERE clause with the partition constraint (except if this is
1655  * the default partition and there's no other partition, because the
1656  * partition constraint is the empty string in that case.)
1657  */
1658  constraintDef = pg_get_partconstrdef_string(RelationGetRelid(pk_rel), "pk");
1659  if (constraintDef && constraintDef[0] != '\0')
1660  appendStringInfo(&querybuf, ") WHERE %s AND (",
1661  constraintDef);
1662  else
1663  appendStringInfo(&querybuf, ") WHERE (");
1664 
1665  sep = "";
1666  for (i = 0; i < riinfo->nkeys; i++)
1667  {
1668  quoteOneName(fkattname, RIAttName(fk_rel, riinfo->fk_attnums[i]));
1669  appendStringInfo(&querybuf,
1670  "%sfk.%s IS NOT NULL",
1671  sep, fkattname);
1672  switch (riinfo->confmatchtype)
1673  {
1674  case FKCONSTR_MATCH_SIMPLE:
1675  sep = " AND ";
1676  break;
1677  case FKCONSTR_MATCH_FULL:
1678  sep = " OR ";
1679  break;
1680  }
1681  }
1682  appendStringInfoChar(&querybuf, ')');
1683 
1684  /*
1685  * Temporarily increase work_mem so that the check query can be executed
1686  * more efficiently. It seems okay to do this because the query is simple
1687  * enough to not use a multiple of work_mem, and one typically would not
1688  * have many large foreign-key validations happening concurrently. So
1689  * this seems to meet the criteria for being considered a "maintenance"
1690  * operation, and accordingly we use maintenance_work_mem.
1691  *
1692  * We use the equivalent of a function SET option to allow the setting to
1693  * persist for exactly the duration of the check query. guc.c also takes
1694  * care of undoing the setting on error.
1695  */
1696  save_nestlevel = NewGUCNestLevel();
1697 
1698  snprintf(workmembuf, sizeof(workmembuf), "%d", maintenance_work_mem);
1699  (void) set_config_option("work_mem", workmembuf,
1701  GUC_ACTION_SAVE, true, 0, false);
1702 
1703  if (SPI_connect() != SPI_OK_CONNECT)
1704  elog(ERROR, "SPI_connect failed");
1705 
1706  /*
1707  * Generate the plan. We don't need to cache it, and there are no
1708  * arguments to the plan.
1709  */
1710  qplan = SPI_prepare(querybuf.data, 0, NULL);
1711 
1712  if (qplan == NULL)
1713  elog(ERROR, "SPI_prepare returned %s for %s",
1715 
1716  /*
1717  * Run the plan. For safety we force a current snapshot to be used. (In
1718  * transaction-snapshot mode, this arguably violates transaction isolation
1719  * rules, but we really haven't got much choice.) We don't need to
1720  * register the snapshot, because SPI_execute_snapshot will see to it. We
1721  * need at most one tuple returned, so pass limit = 1.
1722  */
1723  spi_result = SPI_execute_snapshot(qplan,
1724  NULL, NULL,
1727  true, false, 1);
1728 
1729  /* Check result */
1730  if (spi_result != SPI_OK_SELECT)
1731  elog(ERROR, "SPI_execute_snapshot returned %s", SPI_result_code_string(spi_result));
1732 
1733  /* Did we find a tuple that would violate the constraint? */
1734  if (SPI_processed > 0)
1735  {
1736  TupleTableSlot *slot;
1737  HeapTuple tuple = SPI_tuptable->vals[0];
1738  TupleDesc tupdesc = SPI_tuptable->tupdesc;
1739  RI_ConstraintInfo fake_riinfo;
1740 
1741  slot = MakeSingleTupleTableSlot(tupdesc, &TTSOpsVirtual);
1742 
1743  heap_deform_tuple(tuple, tupdesc,
1744  slot->tts_values, slot->tts_isnull);
1745  ExecStoreVirtualTuple(slot);
1746 
1747  /*
1748  * The columns to look at in the result tuple are 1..N, not whatever
1749  * they are in the fk_rel. Hack up riinfo so that ri_ReportViolation
1750  * will behave properly.
1751  *
1752  * In addition to this, we have to pass the correct tupdesc to
1753  * ri_ReportViolation, overriding its normal habit of using the pk_rel
1754  * or fk_rel's tupdesc.
1755  */
1756  memcpy(&fake_riinfo, riinfo, sizeof(RI_ConstraintInfo));
1757  for (i = 0; i < fake_riinfo.nkeys; i++)
1758  fake_riinfo.pk_attnums[i] = i + 1;
1759 
1760  ri_ReportViolation(&fake_riinfo, pk_rel, fk_rel,
1761  slot, tupdesc, 0, true);
1762  }
1763 
1764  if (SPI_finish() != SPI_OK_FINISH)
1765  elog(ERROR, "SPI_finish failed");
1766 
1767  /*
1768  * Restore work_mem.
1769  */
1770  AtEOXact_GUC(true, save_nestlevel);
1771 }
static void ri_GenerateQualCollation(StringInfo buf, Oid collation)
Definition: ri_triggers.c:1852
static void ri_GenerateQual(StringInfo buf, const char *sep, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
Definition: ri_triggers.c:1823
#define SPI_OK_CONNECT
Definition: spi.h:53
#define FKCONSTR_MATCH_SIMPLE
Definition: parsenodes.h:2124
static void quoteRelationName(char *buffer, Relation rel)
Definition: ri_triggers.c:1806
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1203
int SPI_connect(void)
Definition: spi.c:89
static void ri_ReportViolation(const RI_ConstraintInfo *riinfo, Relation pk_rel, Relation fk_rel, TupleTableSlot *violatorslot, TupleDesc tupdesc, int queryno, bool partgone) pg_attribute_noreturn()
Definition: ri_triggers.c:2343
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:674
int SPI_finish(void)
Definition: spi.c:176
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:84
SPITupleTable * SPI_tuptable
Definition: spi.c:46
#define RIAttType(rel, attnum)
Definition: ri_triggers.c:87
#define RIAttName(rel, attnum)
Definition: ri_triggers.c:86
Datum * tts_values
Definition: tuptable.h:126
Form_pg_class rd_rel
Definition: rel.h:83
unsigned int Oid
Definition: postgres_ext.h:31
HeapTuple * vals
Definition: spi.h:26
uint64 SPI_processed
Definition: spi.c:45
int SPI_result
Definition: spi.c:47
#define RIAttCollation(rel, attnum)
Definition: ri_triggers.c:88
static void quoteOneName(char *buffer, const char *name)
Definition: ri_triggers.c:1786
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
#define MAX_QUOTED_NAME_LEN
Definition: ri_triggers.c:83
#define MAX_QUOTED_REL_NAME_LEN
Definition: ri_triggers.c:84
int16 pk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:113
const char * SPI_result_code_string(int code)
Definition: spi.c:1705
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:163
bool * tts_isnull
Definition: tuptable.h:128
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5729
Oid pf_eq_oprs[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:115
static const RI_ConstraintInfo * ri_FetchConstraintInfo(Trigger *trigger, Relation trig_rel, bool rel_is_pk)
Definition: ri_triggers.c:1954
int SPI_execute_snapshot(SPIPlanPtr plan, Datum *Values, const char *Nulls, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, long tcount)
Definition: spi.c:599
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:175
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define InvalidSnapshot
Definition: snapshot.h:123
#define FKCONSTR_MATCH_FULL
Definition: parsenodes.h:2122
TupleDesc tupdesc
Definition: spi.h:25
#define SPI_OK_SELECT
Definition: spi.h:57
int maintenance_work_mem
Definition: globals.c:122
#define SPI_OK_FINISH
Definition: spi.h:54
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:381
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
int NewGUCNestLevel(void)
Definition: guc.c:5715
#define elog(elevel,...)
Definition: elog.h:226
int i
char * pg_get_partconstrdef_string(Oid partitionId, char *aliasname)
Definition: ruleutils.c:1846
int16 fk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:114
int set_config_option(const char *name, const char *value, GucContext context, GucSource source, GucAction action, bool changeVal, int elevel, bool is_reload)
Definition: guc.c:6722
#define snprintf
Definition: port.h:192
#define RelationGetRelid(relation)
Definition: rel.h:419
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1517

◆ ri_PerformCheck()

static bool ri_PerformCheck ( const RI_ConstraintInfo riinfo,
RI_QueryKey qkey,
SPIPlanPtr  qplan,
Relation  fk_rel,
Relation  pk_rel,
TupleTableSlot oldslot,
TupleTableSlot newslot,
bool  detectNewRows,
int  expect_OK 
)
static

Definition at line 2177 of file ri_triggers.c.

References CommandCounterIncrement(), RI_ConstraintInfo::conname, RI_QueryKey::constr_queryno, elog, ereport, errcode(), errhint(), errmsg(), ERROR, GetLatestSnapshot(), GetTransactionSnapshot(), GetUserIdAndSecContext(), InvalidSnapshot, IsolationUsesXactSnapshot, NameStr, RI_ConstraintInfo::nkeys, RelationGetForm, RelationGetRelationName, ri_ExtractValues(), RI_MAX_NUMKEYS, RI_PLAN_CHECK_LOOKUPPK, RI_PLAN_CHECK_LOOKUPPK_FROM_PK, RI_PLAN_LAST_ON_PK, ri_ReportViolation(), SECURITY_LOCAL_USERID_CHANGE, SECURITY_NOFORCE_RLS, SetUserIdAndSecContext(), SPI_execute_snapshot(), SPI_OK_SELECT, SPI_processed, and SPI_result_code_string().

Referenced by ri_Check_Pk_Match(), RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check(), ri_restrict(), and ri_set().

2182 {
2183  Relation query_rel,
2184  source_rel;
2185  bool source_is_pk;
2186  Snapshot test_snapshot;
2187  Snapshot crosscheck_snapshot;
2188  int limit;
2189  int spi_result;
2190  Oid save_userid;
2191  int save_sec_context;
2192  Datum vals[RI_MAX_NUMKEYS * 2];
2193  char nulls[RI_MAX_NUMKEYS * 2];
2194 
2195  /*
2196  * Use the query type code to determine whether the query is run against
2197  * the PK or FK table; we'll do the check as that table's owner
2198  */
2199  if (qkey->constr_queryno <= RI_PLAN_LAST_ON_PK)
2200  query_rel = pk_rel;
2201  else
2202  query_rel = fk_rel;
2203 
2204  /*
2205  * The values for the query are taken from the table on which the trigger
2206  * is called - it is normally the other one with respect to query_rel. An
2207  * exception is ri_Check_Pk_Match(), which uses the PK table for both (and
2208  * sets queryno to RI_PLAN_CHECK_LOOKUPPK_FROM_PK). We might eventually
2209  * need some less klugy way to determine this.
2210  */
2212  {
2213  source_rel = fk_rel;
2214  source_is_pk = false;
2215  }
2216  else
2217  {
2218  source_rel = pk_rel;
2219  source_is_pk = true;
2220  }
2221 
2222  /* Extract the parameters to be passed into the query */
2223  if (newslot)
2224  {
2225  ri_ExtractValues(source_rel, newslot, riinfo, source_is_pk,
2226  vals, nulls);
2227  if (oldslot)
2228  ri_ExtractValues(source_rel, oldslot, riinfo, source_is_pk,
2229  vals + riinfo->nkeys, nulls + riinfo->nkeys);
2230  }
2231  else
2232  {
2233  ri_ExtractValues(source_rel, oldslot, riinfo, source_is_pk,
2234  vals, nulls);
2235  }
2236 
2237  /*
2238  * In READ COMMITTED mode, we just need to use an up-to-date regular
2239  * snapshot, and we will see all rows that could be interesting. But in
2240  * transaction-snapshot mode, we can't change the transaction snapshot. If
2241  * the caller passes detectNewRows == false then it's okay to do the query
2242  * with the transaction snapshot; otherwise we use a current snapshot, and
2243  * tell the executor to error out if it finds any rows under the current
2244  * snapshot that wouldn't be visible per the transaction snapshot. Note
2245  * that SPI_execute_snapshot will register the snapshots, so we don't need
2246  * to bother here.
2247  */
2248  if (IsolationUsesXactSnapshot() && detectNewRows)
2249  {
2250  CommandCounterIncrement(); /* be sure all my own work is visible */
2251  test_snapshot = GetLatestSnapshot();
2252  crosscheck_snapshot = GetTransactionSnapshot();
2253  }
2254  else
2255  {
2256  /* the default SPI behavior is okay */
2257  test_snapshot = InvalidSnapshot;
2258  crosscheck_snapshot = InvalidSnapshot;
2259  }
2260 
2261  /*
2262  * If this is a select query (e.g., for a 'no action' or 'restrict'
2263  * trigger), we only need to see if there is a single row in the table,
2264  * matching the key. Otherwise, limit = 0 - because we want the query to
2265  * affect ALL the matching rows.
2266  */
2267  limit = (expect_OK == SPI_OK_SELECT) ? 1 : 0;
2268 
2269  /* Switch to proper UID to perform check as */
2270  GetUserIdAndSecContext(&save_userid, &save_sec_context);
2271  SetUserIdAndSecContext(RelationGetForm(query_rel)->relowner,
2272  save_sec_context | SECURITY_LOCAL_USERID_CHANGE |
2274 
2275  /* Finally we can run the query. */
2276  spi_result = SPI_execute_snapshot(qplan,
2277  vals, nulls,
2278  test_snapshot, crosscheck_snapshot,
2279  false, false, limit);
2280 
2281  /* Restore UID and security context */
2282  SetUserIdAndSecContext(save_userid, save_sec_context);
2283 
2284  /* Check result */
2285  if (spi_result < 0)
2286  elog(ERROR, "SPI_execute_snapshot returned %s", SPI_result_code_string(spi_result));
2287 
2288  if (expect_OK >= 0 && spi_result != expect_OK)
2289  ereport(ERROR,
2290  (errcode(ERRCODE_INTERNAL_ERROR),
2291  errmsg("referential integrity query on \"%s\" from constraint \"%s\" on \"%s\" gave unexpected result",
2292  RelationGetRelationName(pk_rel),
2293  NameStr(riinfo->conname),
2294  RelationGetRelationName(fk_rel)),
2295  errhint("This is most likely due to a rule having rewritten the query.")));
2296 
2297  /* XXX wouldn't it be clearer to do this part at the caller? */
2299  expect_OK == SPI_OK_SELECT &&
2301  ri_ReportViolation(riinfo,
2302  pk_rel, fk_rel,
2303  newslot ? newslot : oldslot,
2304  NULL,
2305  qkey->constr_queryno, false);
2306 
2307  return SPI_processed != 0;
2308 }
int errhint(const char *fmt,...)
Definition: elog.c:974
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:492
static void ri_ReportViolation(const RI_ConstraintInfo *riinfo, Relation pk_rel, Relation fk_rel, TupleTableSlot *violatorslot, TupleDesc tupdesc, int queryno, bool partgone) pg_attribute_noreturn()
Definition: ri_triggers.c:2343
#define RI_PLAN_LAST_ON_PK
Definition: ri_triggers.c:75
#define RelationGetForm(relation)
Definition: rel.h:413
int32 constr_queryno
Definition: ri_triggers.c:129
#define IsolationUsesXactSnapshot()
Definition: xact.h:51
int errcode(int sqlerrcode)
Definition: elog.c:570
unsigned int Oid
Definition: postgres_ext.h:31
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:306
uint64 SPI_processed
Definition: spi.c:45
#define ERROR
Definition: elog.h:43
const char * SPI_result_code_string(int code)
Definition: spi.c:1705
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:485
#define RelationGetRelationName(relation)
Definition: rel.h:453
#define RI_PLAN_CHECK_LOOKUPPK
Definition: ri_triggers.c:73
#define ereport(elevel, rest)
Definition: elog.h:141
int SPI_execute_snapshot(SPIPlanPtr plan, Datum *Values, const char *Nulls, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, long tcount)
Definition: spi.c:599
#define InvalidSnapshot
Definition: snapshot.h:123
uintptr_t Datum
Definition: postgres.h:367
void CommandCounterIncrement(void)
Definition: xact.c:1003
#define SECURITY_LOCAL_USERID_CHANGE
Definition: miscadmin.h:299
#define SPI_OK_SELECT
Definition: spi.h:57
#define RI_PLAN_CHECK_LOOKUPPK_FROM_PK
Definition: ri_triggers.c:74
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:381
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609
#define SECURITY_NOFORCE_RLS
Definition: miscadmin.h:301
#define RI_MAX_NUMKEYS
Definition: ri_triggers.c:62
static void ri_ExtractValues(Relation rel, TupleTableSlot *slot, const RI_ConstraintInfo *riinfo, bool rel_is_pk, Datum *vals, char *nulls)
Definition: ri_triggers.c:2314

◆ ri_PlanCheck()

static SPIPlanPtr ri_PlanCheck ( const char *  querystr,
int  nargs,
Oid argtypes,
RI_QueryKey qkey,
Relation  fk_rel,
Relation  pk_rel,
bool  cache_plan 
)
static

Definition at line 2130 of file ri_triggers.c.

References RI_QueryKey::constr_queryno, elog, ERROR, GetUserIdAndSecContext(), RelationGetForm, ri_HashPreparedPlan(), RI_PLAN_LAST_ON_PK, SECURITY_LOCAL_USERID_CHANGE, SECURITY_NOFORCE_RLS, SetUserIdAndSecContext(), SPI_keepplan(), SPI_prepare(), SPI_result, and SPI_result_code_string().

Referenced by ri_Check_Pk_Match(), RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check(), ri_restrict(), and ri_set().

2133 {
2134  SPIPlanPtr qplan;
2135  Relation query_rel;
2136  Oid save_userid;
2137  int save_sec_context;
2138 
2139  /*
2140  * Use the query type code to determine whether the query is run against
2141  * the PK or FK table; we'll do the check as that table's owner
2142  */
2143  if (qkey->constr_queryno <= RI_PLAN_LAST_ON_PK)
2144  query_rel = pk_rel;
2145  else
2146  query_rel = fk_rel;
2147 
2148  /* Switch to proper UID to perform check as */
2149  GetUserIdAndSecContext(&save_userid, &save_sec_context);
2150  SetUserIdAndSecContext(RelationGetForm(query_rel)->relowner,
2151  save_sec_context | SECURITY_LOCAL_USERID_CHANGE |
2153 
2154  /* Create the plan */
2155  qplan = SPI_prepare(querystr, nargs, argtypes);
2156 
2157  if (qplan == NULL)
2158  elog(ERROR, "SPI_prepare returned %s for %s", SPI_result_code_string(SPI_result), querystr);
2159 
2160  /* Restore UID and security context */
2161  SetUserIdAndSecContext(save_userid, save_sec_context);
2162 
2163  /* Save the plan if requested */
2164  if (cache_plan)
2165  {
2166  SPI_keepplan(qplan);
2167  ri_HashPreparedPlan(qkey, qplan);
2168  }
2169 
2170  return qplan;
2171 }
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:492
#define RI_PLAN_LAST_ON_PK
Definition: ri_triggers.c:75
#define RelationGetForm(relation)
Definition: rel.h:413
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:674
int32 constr_queryno
Definition: ri_triggers.c:129
unsigned int Oid
Definition: postgres_ext.h:31
int SPI_result
Definition: spi.c:47
#define ERROR
Definition: elog.h:43
const char * SPI_result_code_string(int code)
Definition: spi.c:1705
static void ri_HashPreparedPlan(RI_QueryKey *key, SPIPlanPtr plan)
Definition: ri_triggers.c:2628
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:752
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:485
#define SECURITY_LOCAL_USERID_CHANGE
Definition: miscadmin.h:299
#define elog(elevel,...)
Definition: elog.h:226
#define SECURITY_NOFORCE_RLS
Definition: miscadmin.h:301

◆ ri_ReportViolation()

static void ri_ReportViolation ( const RI_ConstraintInfo riinfo,
Relation  pk_rel,
Relation  fk_rel,
TupleTableSlot violatorslot,
TupleDesc  tupdesc,
int  queryno,
bool  partgone 
)
static

Definition at line 2343 of file ri_triggers.c.

References ACL_SELECT, ACLCHECK_OK, appendStringInfoString(), check_enable_rls(), RI_ConstraintInfo::conname, StringInfoData::data, ereport, errcode(), errdetail(), errmsg(), ERROR, errtableconstraint(), RI_ConstraintInfo::fk_attnums, getTypeOutputInfo(), GetUserId(), idx(), initStringInfo(), InvalidOid, name, NameStr, RI_ConstraintInfo::nkeys, OidOutputFunctionCall(), pg_attribute_aclcheck(), pg_class_aclcheck(), RI_ConstraintInfo::pk_attnums, RelationData::rd_att, RelationData::rd_id, RelationGetRelationName, RI_PLAN_CHECK_LOOKUPPK, RLS_ENABLED, slot_getattr(), TupleDescAttr, and val.

Referenced by RI_Initial_Check(), RI_PartitionRemove_Check(), and ri_PerformCheck().

2347 {
2348  StringInfoData key_names;
2349  StringInfoData key_values;
2350  bool onfk;
2351  const int16 *attnums;
2352  Oid rel_oid;
2353  AclResult aclresult;
2354  bool has_perm = true;
2355 
2356  /*
2357  * Determine which relation to complain about. If tupdesc wasn't passed
2358  * by caller, assume the violator tuple came from there.
2359  */
2360  onfk = (queryno == RI_PLAN_CHECK_LOOKUPPK);
2361  if (onfk)
2362  {
2363  attnums = riinfo->fk_attnums;
2364  rel_oid = fk_rel->rd_id;
2365  if (tupdesc == NULL)
2366  tupdesc = fk_rel->rd_att;
2367  }
2368  else
2369  {
2370  attnums = riinfo->pk_attnums;
2371  rel_oid = pk_rel->rd_id;
2372  if (tupdesc == NULL)
2373  tupdesc = pk_rel->rd_att;
2374  }
2375 
2376  /*
2377  * Check permissions- if the user does not have access to view the data in
2378  * any of the key columns then we don't include the errdetail() below.
2379  *
2380  * Check if RLS is enabled on the relation first. If so, we don't return
2381  * any specifics to avoid leaking data.
2382  *
2383  * Check table-level permissions next and, failing that, column-level
2384  * privileges.
2385  *
2386  * When a partition at the referenced side is being detached/dropped, we
2387  * needn't check, since the user must be the table owner anyway.
2388  */
2389  if (partgone)
2390  has_perm = true;
2391  else if (check_enable_rls(rel_oid, InvalidOid, true) != RLS_ENABLED)
2392  {
2393  aclresult = pg_class_aclcheck(rel_oid, GetUserId(), ACL_SELECT);
2394  if (aclresult != ACLCHECK_OK)
2395  {
2396  /* Try for column-level permissions */
2397  for (int idx = 0; idx < riinfo->nkeys; idx++)
2398  {
2399  aclresult = pg_attribute_aclcheck(rel_oid, attnums[idx],
2400  GetUserId(),
2401  ACL_SELECT);
2402 
2403  /* No access to the key */
2404  if (aclresult != ACLCHECK_OK)
2405  {
2406  has_perm = false;
2407  break;
2408  }
2409  }
2410  }
2411  }
2412  else
2413  has_perm = false;
2414 
2415  if (has_perm)
2416  {
2417  /* Get printable versions of the keys involved */
2418  initStringInfo(&key_names);
2419  initStringInfo(&key_values);
2420  for (int idx = 0; idx < riinfo->nkeys; idx++)
2421  {
2422  int fnum = attnums[idx];
2423  Form_pg_attribute att = TupleDescAttr(tupdesc, fnum - 1);
2424  char *name,
2425  *val;
2426  Datum datum;
2427  bool isnull;
2428 
2429  name = NameStr(att->attname);
2430 
2431  datum = slot_getattr(violatorslot, fnum, &isnull);
2432  if (!isnull)
2433  {
2434  Oid foutoid;
2435  bool typisvarlena;
2436 
2437  getTypeOutputInfo(att->atttypid, &foutoid, &typisvarlena);
2438  val = OidOutputFunctionCall(foutoid, datum);
2439  }
2440  else
2441  val = "null";
2442 
2443  if (idx > 0)
2444  {
2445  appendStringInfoString(&key_names, ", ");
2446  appendStringInfoString(&key_values, ", ");
2447  }
2448  appendStringInfoString(&key_names, name);
2449  appendStringInfoString(&key_values, val);
2450  }
2451  }
2452 
2453  if (partgone)
2454  ereport(ERROR,
2455  (errcode(ERRCODE_FOREIGN_KEY_VIOLATION),
2456  errmsg("removing partition \"%s\" violates foreign key constraint \"%s\"",
2457  RelationGetRelationName(pk_rel),
2458  NameStr(riinfo->conname)),
2459  errdetail("Key (%s)=(%s) is still referenced from table \"%s\".",
2460  key_names.data, key_values.data,
2461  RelationGetRelationName(fk_rel))));
2462  else if (onfk)
2463  ereport(ERROR,
2464  (errcode(ERRCODE_FOREIGN_KEY_VIOLATION),
2465  errmsg("insert or update on table \"%s\" violates foreign key constraint \"%s\"",
2466  RelationGetRelationName(fk_rel),
2467  NameStr(riinfo->conname)),
2468  has_perm ?
2469  errdetail("Key (%s)=(%s) is not present in table \"%s\".",
2470  key_names.data, key_values.data,
2471  RelationGetRelationName(pk_rel)) :
2472  errdetail("Key is not present in table \"%s\".",
2473  RelationGetRelationName(pk_rel)),
2474  errtableconstraint(fk_rel, NameStr(riinfo->conname))));
2475  else
2476  ereport(ERROR,
2477  (errcode(ERRCODE_FOREIGN_KEY_VIOLATION),
2478  errmsg("update or delete on table \"%s\" violates foreign key constraint \"%s\" on table \"%s\"",
2479  RelationGetRelationName(pk_rel),
2480  NameStr(riinfo->conname),
2481  RelationGetRelationName(fk_rel)),
2482  has_perm ?
2483  errdetail("Key (%s)=(%s) is still referenced from table \"%s\".",
2484  key_names.data, key_values.data,
2485  RelationGetRelationName(fk_rel)) :
2486  errdetail("Key is still referenced from table \"%s\".",
2487  RelationGetRelationName(fk_rel)),
2488  errtableconstraint(fk_rel, NameStr(riinfo->conname))));
2489 }
signed short int16
Definition: c.h:345
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2674
AclResult pg_attribute_aclcheck(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode)
Definition: aclchk.c:4517
Oid GetUserId(void)
Definition: miscinit.c:380
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int errcode(int sqlerrcode)
Definition: elog.c:570
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
unsigned int Oid
Definition: postgres_ext.h:31
int errtableconstraint(Relation rel, const char *conname)
Definition: relcache.c:5222
#define ERROR
Definition: elog.h:43
int16 pk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:113
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:163
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define RelationGetRelationName(relation)
Definition: rel.h:453
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define RI_PLAN_CHECK_LOOKUPPK
Definition: ri_triggers.c:73
#define ereport(elevel, rest)
Definition: elog.h:141
Oid rd_id
Definition: rel.h:85
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
AclResult
Definition: acl.h:177
uintptr_t Datum
Definition: postgres.h:367
#define ACL_SELECT
Definition: parsenodes.h:75
TupleDesc rd_att
Definition: rel.h:84
#define InvalidOid
Definition: postgres_ext.h:36
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:382
int check_enable_rls(Oid relid, Oid checkAsUser, bool noError)
Definition: rls.c:52
const char * name
Definition: encode.c:521
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4631
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1655
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define NameStr(name)
Definition: c.h:609
int16 fk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:114
long val
Definition: informix.c:684

◆ ri_restrict()

static Datum ri_restrict ( TriggerData trigdata,
bool  is_no_action 
)
static

Definition at line 615 of file ri_triggers.c.

References appendStringInfo(), appendStringInfoString(), attname, StringInfoData::data, elog, ERROR, RI_ConstraintInfo::fk_attnums, RI_ConstraintInfo::fk_relid, get_collation_isdeterministic(), i, initStringInfo(), MAX_QUOTED_NAME_LEN, MAX_QUOTED_REL_NAME_LEN, RI_ConstraintInfo::nkeys, RI_ConstraintInfo::pf_eq_oprs, RI_ConstraintInfo::pk_attnums, PointerGetDatum, quoteOneName(), quoteRelationName(), RelationData::rd_rel, ri_BuildQueryKey(), ri_Check_Pk_Match(), ri_FetchConstraintInfo(), ri_FetchPreparedPlan(), ri_GenerateQual(), ri_GenerateQualCollation(), RI_MAX_NUMKEYS, ri_PerformCheck(), RI_PLAN_RESTRICT_CHECKREF, ri_PlanCheck(), RIAttCollation, RIAttName, RIAttType, RowShareLock, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_OK_SELECT, sprintf, table_close(), table_open(), TriggerData::tg_relation, TriggerData::tg_trigger, and TriggerData::tg_trigslot.

Referenced by RI_FKey_noaction_del(), RI_FKey_noaction_upd(), RI_FKey_restrict_del(), RI_FKey_restrict_upd(), and ri_set().

616 {
617  const RI_ConstraintInfo *riinfo;
618  Relation fk_rel;
619  Relation pk_rel;
620  TupleTableSlot *oldslot;
621  RI_QueryKey qkey;
622  SPIPlanPtr qplan;
623 
624  riinfo = ri_FetchConstraintInfo(trigdata->tg_trigger,
625  trigdata->tg_relation, true);
626 
627  /*
628  * Get the relation descriptors of the FK and PK tables and the old tuple.
629  *
630  * fk_rel is opened in RowShareLock mode since that's what our eventual
631  * SELECT FOR KEY SHARE will get on it.
632  */
633  fk_rel = table_open(riinfo->fk_relid, RowShareLock);
634  pk_rel = trigdata->tg_relation;
635  oldslot = trigdata->tg_trigslot;
636 
637  /*
638  * If another PK row now exists providing the old key values, we should
639  * not do anything. However, this check should only be made in the NO
640  * ACTION case; in RESTRICT cases we don't wish to allow another row to be
641  * substituted.
642  */
643  if (is_no_action &&
644  ri_Check_Pk_Match(pk_rel, fk_rel, oldslot, riinfo))
645  {
646  table_close(fk_rel, RowShareLock);
647  return PointerGetDatum(NULL);
648  }
649 
650  if (SPI_connect() != SPI_OK_CONNECT)
651  elog(ERROR, "SPI_connect failed");
652 
653  /*
654  * Fetch or prepare a saved plan for the restrict lookup (it's the same
655  * query for delete and update cases)
656  */
658 
659  if ((qplan = ri_FetchPreparedPlan(&qkey)) == NULL)
660  {
661  StringInfoData querybuf;
662  char fkrelname[MAX_QUOTED_REL_NAME_LEN];
664  char paramname[16];
665  const char *querysep;
666  Oid queryoids[RI_MAX_NUMKEYS];
667  const char *fk_only;
668 
669  /* ----------
670  * The query string built is
671  * SELECT 1 FROM [ONLY] <fktable> x WHERE $1 = fkatt1 [AND ...]
672  * FOR KEY SHARE OF x
673  * The type id's for the $ parameters are those of the
674  * corresponding PK attributes.
675  * ----------
676  */
677  initStringInfo(&querybuf);
678  fk_only = fk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ?
679  "" : "ONLY ";
680  quoteRelationName(fkrelname, fk_rel);
681  appendStringInfo(&querybuf, "SELECT 1 FROM %s%s x",
682  fk_only, fkrelname);
683  querysep = "WHERE";
684  for (int i = 0; i < riinfo->nkeys; i++)
685  {
686  Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
687  Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
688  Oid pk_coll = RIAttCollation(pk_rel, riinfo->pk_attnums[i]);
689  Oid fk_coll = RIAttCollation(fk_rel, riinfo->fk_attnums[i]);
690 
691  quoteOneName(attname,
692  RIAttName(fk_rel, riinfo->fk_attnums[i]));
693  sprintf(paramname, "$%d", i + 1);
694  ri_GenerateQual(&querybuf, querysep,
695  paramname, pk_type,
696  riinfo->pf_eq_oprs[i],
697  attname, fk_type);
698  if (pk_coll != fk_coll && !get_collation_isdeterministic(pk_coll))
699  ri_GenerateQualCollation(&querybuf, pk_coll);
700  querysep = "AND";
701  queryoids[i] = pk_type;
702  }
703  appendStringInfoString(&querybuf, " FOR KEY SHARE OF x");
704 
705  /* Prepare and save the plan */
706  qplan = ri_PlanCheck(querybuf.data, riinfo->nkeys, queryoids,
707  &qkey, fk_rel, pk_rel, true);
708  }
709 
710  /*
711  * We have a plan now. Run it to check for existing references.
712  */
713  ri_PerformCheck(riinfo, &qkey, qplan,
714  fk_rel, pk_rel,
715  oldslot, NULL,
716  true, /* must detect new rows */
717  SPI_OK_SELECT);
718 
719  if (SPI_finish() != SPI_OK_FINISH)
720  elog(ERROR, "SPI_finish failed");
721 
722  table_close(fk_rel, RowShareLock);
723 
724  return PointerGetDatum(NULL);
725 }
static void ri_GenerateQualCollation(StringInfo buf, Oid collation)
Definition: ri_triggers.c:1852
static void ri_GenerateQual(StringInfo buf, const char *sep, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
Definition: ri_triggers.c:1823
#define SPI_OK_CONNECT
Definition: spi.h:53
TupleTableSlot * tg_trigslot
Definition: trigger.h:38
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static void quoteRelationName(char *buffer, Relation rel)
Definition: ri_triggers.c:1806
int SPI_connect(void)
Definition: spi.c:89
#define PointerGetDatum(X)
Definition: postgres.h:556
int SPI_finish(void)
Definition: spi.c:176
bool get_collation_isdeterministic(Oid colloid)
Definition: lsyscache.c:942
static bool ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel, TupleTableSlot *oldslot, const RI_ConstraintInfo *riinfo)
Definition: ri_triggers.c:452
#define RIAttType(rel, attnum)
Definition: ri_triggers.c:87
#define RIAttName(rel, attnum)
Definition: ri_triggers.c:86
Form_pg_class rd_rel
Definition: rel.h:83
unsigned int Oid
Definition: postgres_ext.h:31
#define sprintf
Definition: port.h:194
#define RIAttCollation(rel, attnum)
Definition: ri_triggers.c:88
static void quoteOneName(char *buffer, const char *name)
Definition: ri_triggers.c:1786
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
NameData attname
Definition: pg_attribute.h:40
#define MAX_QUOTED_NAME_LEN
Definition: ri_triggers.c:83
#define MAX_QUOTED_REL_NAME_LEN
Definition: ri_triggers.c:84
int16 pk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:113
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:163
static SPIPlanPtr ri_FetchPreparedPlan(RI_QueryKey *key)
Definition: ri_triggers.c:2576
Oid pf_eq_oprs[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:115
static const RI_ConstraintInfo * ri_FetchConstraintInfo(Trigger *trigger, Relation trig_rel, bool rel_is_pk)
Definition: ri_triggers.c:1954
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define RowShareLock
Definition: lockdefs.h:37
#define RI_PLAN_RESTRICT_CHECKREF
Definition: ri_triggers.c:79
static bool ri_PerformCheck(const RI_ConstraintInfo *riinfo, RI_QueryKey *qkey, SPIPlanPtr qplan, Relation fk_rel, Relation pk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, bool detectNewRows, int expect_OK)
Definition: ri_triggers.c:2177
Trigger * tg_trigger
Definition: trigger.h:37
#define SPI_OK_SELECT
Definition: spi.h:57
static SPIPlanPtr ri_PlanCheck(const char *querystr, int nargs, Oid *argtypes, RI_QueryKey *qkey, Relation fk_rel, Relation pk_rel, bool cache_plan)
Definition: ri_triggers.c:2130
#define SPI_OK_FINISH
Definition: spi.h:54
#define elog(elevel,...)
Definition: elog.h:226
int i
static void ri_BuildQueryKey(RI_QueryKey *key, const RI_ConstraintInfo *riinfo, int32 constr_queryno)
Definition: ri_triggers.c:1893
int16 fk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:114
#define RI_MAX_NUMKEYS
Definition: ri_triggers.c:62
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Relation tg_relation
Definition: trigger.h:34

◆ ri_set()

static Datum ri_set ( TriggerData trigdata,
bool  is_set_null 
)
static

Definition at line 1022 of file ri_triggers.c.

References appendBinaryStringInfo(), appendStringInfo(), attname, StringInfoData::data, elog, ERROR, RI_ConstraintInfo::fk_attnums, RI_ConstraintInfo::fk_relid, get_collation_isdeterministic(), i, initStringInfo(), StringInfoData::len, MAX_QUOTED_NAME_LEN, MAX_QUOTED_REL_NAME_LEN, RI_ConstraintInfo::nkeys, RI_ConstraintInfo::pf_eq_oprs, RI_ConstraintInfo::pk_attnums, PointerGetDatum, quoteOneName(), quoteRelationName(), RelationData::rd_rel, ri_BuildQueryKey(), ri_FetchConstraintInfo(), ri_FetchPreparedPlan(), ri_GenerateQual(), ri_GenerateQualCollation(), RI_MAX_NUMKEYS, ri_PerformCheck(), RI_PLAN_SETDEFAULT_DOUPDATE, RI_PLAN_SETNULL_DOUPDATE, ri_PlanCheck(), ri_restrict(), RIAttCollation, RIAttName, RIAttType, RowExclusiveLock, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_OK_UPDATE, sprintf, table_close(), table_open(), TriggerData::tg_relation, TriggerData::tg_trigger, and TriggerData::tg_trigslot.

Referenced by RI_FKey_setdefault_del(), RI_FKey_setdefault_upd(), RI_FKey_setnull_del(), and RI_FKey_setnull_upd().

1023 {
1024  const RI_ConstraintInfo *riinfo;
1025  Relation fk_rel;
1026  Relation pk_rel;
1027  TupleTableSlot *oldslot;
1028  RI_QueryKey qkey;
1029  SPIPlanPtr qplan;
1030 
1031  riinfo = ri_FetchConstraintInfo(trigdata->tg_trigger,
1032  trigdata->tg_relation, true);
1033 
1034  /*
1035  * Get the relation descriptors of the FK and PK tables and the old tuple.
1036  *
1037  * fk_rel is opened in RowExclusiveLock mode since that's what our
1038  * eventual UPDATE will get on it.
1039  */
1040  fk_rel = table_open(riinfo->fk_relid, RowExclusiveLock);
1041  pk_rel = trigdata->tg_relation;
1042  oldslot = trigdata->tg_trigslot;
1043 
1044  if (SPI_connect() != SPI_OK_CONNECT)
1045  elog(ERROR, "SPI_connect failed");
1046 
1047  /*
1048  * Fetch or prepare a saved plan for the set null/default operation (it's
1049  * the same query for delete and update cases)
1050  */
1051  ri_BuildQueryKey(&qkey, riinfo,
1052  (is_set_null
1055 
1056  if ((qplan = ri_FetchPreparedPlan(&qkey)) == NULL)
1057  {
1058  StringInfoData querybuf;
1059  StringInfoData qualbuf;
1060  char fkrelname[MAX_QUOTED_REL_NAME_LEN];
1062  char paramname[16];
1063  const char *querysep;
1064  const char *qualsep;
1065  Oid queryoids[RI_MAX_NUMKEYS];
1066  const char *fk_only;
1067 
1068  /* ----------
1069  * The query string built is
1070  * UPDATE [ONLY] <fktable> SET fkatt1 = {NULL|DEFAULT} [, ...]
1071  * WHERE $1 = fkatt1 [AND ...]
1072  * The type id's for the $ parameters are those of the
1073  * corresponding PK attributes.
1074  * ----------
1075  */
1076  initStringInfo(&querybuf);
1077  initStringInfo(&qualbuf);
1078  fk_only = fk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ?
1079  "" : "ONLY ";
1080  quoteRelationName(fkrelname, fk_rel);
1081  appendStringInfo(&querybuf, "UPDATE %s%s SET",
1082  fk_only, fkrelname);
1083  querysep = "";
1084  qualsep = "WHERE";
1085  for (int i = 0; i < riinfo->nkeys; i++)
1086  {
1087  Oid pk_type = RIAttType(pk_rel, riinfo->pk_attnums[i]);
1088  Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[i]);
1089  Oid pk_coll = RIAttCollation(pk_rel, riinfo->pk_attnums[i]);
1090  Oid fk_coll = RIAttCollation(fk_rel, riinfo->fk_attnums[i]);
1091 
1092  quoteOneName(attname,
1093  RIAttName(fk_rel, riinfo->fk_attnums[i]));
1094  appendStringInfo(&querybuf,
1095  "%s %s = %s",
1096  querysep, attname,
1097  is_set_null ? "NULL" : "DEFAULT");
1098  sprintf(paramname, "$%d", i + 1);
1099  ri_GenerateQual(&qualbuf, qualsep,
1100  paramname, pk_type,
1101  riinfo->pf_eq_oprs[i],
1102  attname, fk_type);
1103  if (pk_coll != fk_coll && !get_collation_isdeterministic(pk_coll))
1104  ri_GenerateQualCollation(&querybuf, pk_coll);
1105  querysep = ",";
1106  qualsep = "AND";
1107  queryoids[i] = pk_type;
1108  }
1109  appendBinaryStringInfo(&querybuf, qualbuf.data, qualbuf.len);
1110 
1111  /* Prepare and save the plan */
1112  qplan = ri_PlanCheck(querybuf.data, riinfo->nkeys, queryoids,
1113  &qkey, fk_rel, pk_rel, true);
1114  }
1115 
1116  /*
1117  * We have a plan now. Run it to update the existing references.
1118  */
1119  ri_PerformCheck(riinfo, &qkey, qplan,
1120  fk_rel, pk_rel,
1121  oldslot, NULL,
1122  true, /* must detect new rows */
1123  SPI_OK_UPDATE);
1124 
1125  if (SPI_finish() != SPI_OK_FINISH)
1126  elog(ERROR, "SPI_finish failed");
1127 
1128  table_close(fk_rel, RowExclusiveLock);
1129 
1130  if (is_set_null)
1131  return PointerGetDatum(NULL);
1132  else
1133  {
1134  /*
1135  * If we just deleted or updated the PK row whose key was equal to the
1136  * FK columns' default values, and a referencing row exists in the FK
1137  * table, we would have updated that row to the same values it already
1138  * had --- and RI_FKey_fk_upd_check_required would hence believe no
1139  * check is necessary. So we need to do another lookup now and in
1140  * case a reference still exists, abort the operation. That is
1141  * already implemented in the NO ACTION trigger, so just run it. (This
1142  * recheck is only needed in the SET DEFAULT case, since CASCADE would
1143  * remove such rows in case of a DELETE operation or would change the
1144  * FK key values in case of an UPDATE, while SET NULL is certain to
1145  * result in rows that satisfy the FK constraint.)
1146  */
1147  return ri_restrict(trigdata, true);
1148  }
1149 }
static void ri_GenerateQualCollation(StringInfo buf, Oid collation)
Definition: ri_triggers.c:1852
static void ri_GenerateQual(StringInfo buf, const char *sep, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
Definition: ri_triggers.c:1823
#define SPI_OK_CONNECT
Definition: spi.h:53
TupleTableSlot * tg_trigslot
Definition: trigger.h:38
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static void quoteRelationName(char *buffer, Relation rel)
Definition: ri_triggers.c:1806
int SPI_connect(void)
Definition: spi.c:89
#define PointerGetDatum(X)
Definition: postgres.h:556
int SPI_finish(void)
Definition: spi.c:176
bool get_collation_isdeterministic(Oid colloid)
Definition: lsyscache.c:942
static Datum ri_restrict(TriggerData *trigdata, bool is_no_action)
Definition: ri_triggers.c:615
#define RIAttType(rel, attnum)
Definition: ri_triggers.c:87
#define RIAttName(rel, attnum)
Definition: ri_triggers.c:86
Form_pg_class rd_rel
Definition: rel.h:83
unsigned int Oid
Definition: postgres_ext.h:31
#define sprintf
Definition: port.h:194
#define RIAttCollation(rel, attnum)
Definition: ri_triggers.c:88
static void quoteOneName(char *buffer, const char *name)
Definition: ri_triggers.c:1786
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
NameData attname
Definition: pg_attribute.h:40
#define MAX_QUOTED_NAME_LEN
Definition: ri_triggers.c:83
#define RI_PLAN_SETDEFAULT_DOUPDATE
Definition: ri_triggers.c:81
#define MAX_QUOTED_REL_NAME_LEN
Definition: ri_triggers.c:84
int16 pk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:113
static SPIPlanPtr ri_FetchPreparedPlan(RI_QueryKey *key)
Definition: ri_triggers.c:2576
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid pf_eq_oprs[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:115
#define RI_PLAN_SETNULL_DOUPDATE
Definition: ri_triggers.c:80
static const RI_ConstraintInfo * ri_FetchConstraintInfo(Trigger *trigger, Relation trig_rel, bool rel_is_pk)
Definition: ri_triggers.c:1954
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
static bool ri_PerformCheck(const RI_ConstraintInfo *riinfo, RI_QueryKey *qkey, SPIPlanPtr qplan, Relation fk_rel, Relation pk_rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, bool detectNewRows, int expect_OK)
Definition: ri_triggers.c:2177
Trigger * tg_trigger
Definition: trigger.h:37
static SPIPlanPtr ri_PlanCheck(const char *querystr, int nargs, Oid *argtypes, RI_QueryKey *qkey, Relation fk_rel, Relation pk_rel, bool cache_plan)
Definition: ri_triggers.c:2130
#define SPI_OK_FINISH
Definition: spi.h:54
#define SPI_OK_UPDATE
Definition: spi.h:61
#define elog(elevel,...)
Definition: elog.h:226
int i
static void ri_BuildQueryKey(RI_QueryKey *key, const RI_ConstraintInfo *riinfo, int32 constr_queryno)
Definition: ri_triggers.c:1893
int16 fk_attnums[RI_MAX_NUMKEYS]
Definition: ri_triggers.c:114
#define RI_MAX_NUMKEYS
Definition: ri_triggers.c:62
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:214
Relation tg_relation
Definition: trigger.h:34

Variable Documentation

◆ ri_compare_cache

HTAB* ri_compare_cache = NULL
static

Definition at line 169 of file ri_triggers.c.

◆ ri_constraint_cache

HTAB* ri_constraint_cache = NULL
static

Definition at line 167 of file ri_triggers.c.

◆ ri_constraint_cache_valid_count

int ri_constraint_cache_valid_count = 0
static

Definition at line 171 of file ri_triggers.c.

Referenced by InvalidateConstraintCacheCallBack(), and ri_LoadConstraintInfo().

◆ ri_constraint_cache_valid_list

dlist_head ri_constraint_cache_valid_list
static

Definition at line 170 of file ri_triggers.c.

◆ ri_query_cache

HTAB* ri_query_cache = NULL
static

Definition at line 168 of file ri_triggers.c.