1997-09-11 14:51:23 +08:00
|
|
|
/*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/spi/refint.c
|
2008-05-17 09:28:26 +08:00
|
|
|
*
|
|
|
|
*
|
1997-09-11 14:51:23 +08:00
|
|
|
* refint.c -- set of functions to define referential integrity
|
|
|
|
* constraints using general triggers.
|
|
|
|
*/
|
2009-01-07 21:44:37 +08:00
|
|
|
#include "postgres.h"
|
1997-09-11 14:51:23 +08:00
|
|
|
|
2000-12-04 04:45:40 +08:00
|
|
|
#include <ctype.h>
|
1997-09-11 14:51:23 +08:00
|
|
|
|
2009-01-07 21:44:37 +08:00
|
|
|
#include "commands/trigger.h"
|
|
|
|
#include "executor/spi.h"
|
|
|
|
#include "utils/builtins.h"
|
2020-01-08 09:02:55 +08:00
|
|
|
#include "utils/memutils.h"
|
2011-02-24 01:18:09 +08:00
|
|
|
#include "utils/rel.h"
|
1999-05-10 23:12:29 +08:00
|
|
|
|
2006-05-31 06:12:16 +08:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
1997-09-11 14:51:23 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *ident;
|
|
|
|
int nplans;
|
2007-03-16 07:12:07 +08:00
|
|
|
SPIPlanPtr *splan;
|
1997-09-11 14:51:23 +08:00
|
|
|
} EPlan;
|
|
|
|
|
|
|
|
static EPlan *FPlans = NULL;
|
|
|
|
static int nFPlans = 0;
|
|
|
|
static EPlan *PPlans = NULL;
|
|
|
|
static int nPPlans = 0;
|
|
|
|
|
|
|
|
static EPlan *find_plan(char *ident, EPlan **eplan, int *nplans);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check_primary_key () -- check that key in tuple being inserted/updated
|
|
|
|
* references existing tuple in "primary" table.
|
|
|
|
* Though it's called without args You have to specify referenced
|
|
|
|
* table/keys while creating trigger: key field names in triggered table,
|
1999-05-10 23:12:29 +08:00
|
|
|
* referenced table name, referenced key field names:
|
1997-09-11 14:51:23 +08:00
|
|
|
* EXECUTE PROCEDURE
|
1999-05-10 23:12:29 +08:00
|
|
|
* check_primary_key ('Fkey1', 'Fkey2', 'Ptable', 'Pkey1', 'Pkey2').
|
1997-09-11 14:51:23 +08:00
|
|
|
*/
|
|
|
|
|
2000-11-21 04:36:57 +08:00
|
|
|
PG_FUNCTION_INFO_V1(check_primary_key);
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
Datum
|
|
|
|
check_primary_key(PG_FUNCTION_ARGS)
|
1997-09-11 14:51:23 +08:00
|
|
|
{
|
2000-05-29 09:59:17 +08:00
|
|
|
TriggerData *trigdata = (TriggerData *) fcinfo->context;
|
1997-09-11 14:51:23 +08:00
|
|
|
Trigger *trigger; /* to get trigger name */
|
|
|
|
int nargs; /* # of args specified in CREATE TRIGGER */
|
|
|
|
char **args; /* arguments: column names and table name */
|
1999-05-10 23:12:29 +08:00
|
|
|
int nkeys; /* # of key columns (= nargs / 2) */
|
1997-09-12 12:58:59 +08:00
|
|
|
Datum *kvals; /* key values */
|
1997-09-11 14:51:23 +08:00
|
|
|
char *relname; /* referenced relation name */
|
|
|
|
Relation rel; /* triggered relation */
|
|
|
|
HeapTuple tuple = NULL; /* tuple to return */
|
|
|
|
TupleDesc tupdesc; /* tuple description */
|
|
|
|
EPlan *plan; /* prepared plan */
|
|
|
|
Oid *argtypes = NULL; /* key types to prepare execution plan */
|
|
|
|
bool isnull; /* to know is some column NULL or not */
|
|
|
|
char ident[2 * NAMEDATALEN]; /* to identify myself */
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
1999-05-10 23:12:29 +08:00
|
|
|
#ifdef DEBUG_QUERY
|
2003-07-25 01:52:50 +08:00
|
|
|
elog(DEBUG4, "check_primary_key: Enter Function");
|
1999-05-10 23:12:29 +08:00
|
|
|
#endif
|
2000-05-29 09:59:17 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Some checks first...
|
|
|
|
*/
|
|
|
|
|
1997-09-11 14:51:23 +08:00
|
|
|
/* Called by trigger manager ? */
|
2000-05-29 09:59:17 +08:00
|
|
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
2000-05-29 09:59:17 +08:00
|
|
|
elog(ERROR, "check_primary_key: not fired by trigger manager");
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/* Should be called for ROW trigger */
|
2010-10-09 01:27:31 +08:00
|
|
|
if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
2010-10-09 01:27:31 +08:00
|
|
|
elog(ERROR, "check_primary_key: must be fired for row");
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/* If INSERTion then must check Tuple to being inserted */
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
|
|
|
|
tuple = trigdata->tg_trigtuple;
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/* Not should be called for DELETE */
|
2000-05-29 09:59:17 +08:00
|
|
|
else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-02 03:10:30 +08:00
|
|
|
elog(ERROR, "check_primary_key: cannot process DELETE events");
|
1997-09-11 14:51:23 +08:00
|
|
|
|
2017-02-06 17:33:58 +08:00
|
|
|
/* If UPDATE, then must check new Tuple, not old one */
|
1997-09-11 14:51:23 +08:00
|
|
|
else
|
2000-05-29 09:59:17 +08:00
|
|
|
tuple = trigdata->tg_newtuple;
|
1997-09-11 14:51:23 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
trigger = trigdata->tg_trigger;
|
1997-09-11 14:51:23 +08:00
|
|
|
nargs = trigger->tgnargs;
|
|
|
|
args = trigger->tgargs;
|
|
|
|
|
1999-05-10 23:12:29 +08:00
|
|
|
if (nargs % 2 != 1) /* odd number of arguments! */
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
1999-05-10 23:12:29 +08:00
|
|
|
elog(ERROR, "check_primary_key: odd number of arguments should be specified");
|
1997-09-11 14:51:23 +08:00
|
|
|
|
1999-05-10 23:12:29 +08:00
|
|
|
nkeys = nargs / 2;
|
1997-09-11 14:51:23 +08:00
|
|
|
relname = args[nkeys];
|
2000-05-29 09:59:17 +08:00
|
|
|
rel = trigdata->tg_relation;
|
1997-09-11 14:51:23 +08:00
|
|
|
tupdesc = rel->rd_att;
|
|
|
|
|
|
|
|
/* Connect to SPI manager */
|
|
|
|
if ((ret = SPI_connect()) < 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "check_primary_key: SPI_connect returned %d", ret);
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We use SPI plan preparation feature, so allocate space to place key
|
|
|
|
* values.
|
|
|
|
*/
|
1997-09-12 12:58:59 +08:00
|
|
|
kvals = (Datum *) palloc(nkeys * sizeof(Datum));
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct ident string as TriggerName $ TriggeredRelationId and try to
|
|
|
|
* find prepared execution plan.
|
|
|
|
*/
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(ident, sizeof(ident), "%s$%u", trigger->tgname, rel->rd_id);
|
1997-09-11 14:51:23 +08:00
|
|
|
plan = find_plan(ident, &PPlans, &nPPlans);
|
|
|
|
|
|
|
|
/* if there is no plan then allocate argtypes for preparation */
|
|
|
|
if (plan->nplans <= 0)
|
|
|
|
argtypes = (Oid *) palloc(nkeys * sizeof(Oid));
|
|
|
|
|
|
|
|
/* For each column in key ... */
|
|
|
|
for (i = 0; i < nkeys; i++)
|
|
|
|
{
|
|
|
|
/* get index of column in tuple */
|
|
|
|
int fnumber = SPI_fnumber(tupdesc, args[i]);
|
|
|
|
|
|
|
|
/* Bad guys may give us un-existing column in CREATE TRIGGER */
|
2016-11-09 02:11:15 +08:00
|
|
|
if (fnumber <= 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
|
|
|
errmsg("there is no attribute \"%s\" in relation \"%s\"",
|
|
|
|
args[i], SPI_getrelname(rel))));
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/* Well, get binary (in internal format) value of column */
|
|
|
|
kvals[i] = SPI_getbinval(tuple, tupdesc, fnumber, &isnull);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's NULL then nothing to do! DON'T FORGET call SPI_finish ()!
|
|
|
|
* DON'T FORGET return tuple! Executor inserts tuple you're returning!
|
|
|
|
* If you return NULL then nothing will be inserted!
|
|
|
|
*/
|
|
|
|
if (isnull)
|
|
|
|
{
|
|
|
|
SPI_finish();
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(tuple);
|
1997-09-11 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (plan->nplans <= 0) /* Get typeId of column */
|
|
|
|
argtypes[i] = SPI_gettypeid(tupdesc, fnumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have to prepare plan ...
|
|
|
|
*/
|
|
|
|
if (plan->nplans <= 0)
|
|
|
|
{
|
2007-03-16 07:12:07 +08:00
|
|
|
SPIPlanPtr pplan;
|
1997-09-11 14:51:23 +08:00
|
|
|
char sql[8192];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct query: SELECT 1 FROM _referenced_relation_ WHERE Pkey1 =
|
|
|
|
* $1 [AND Pkey2 = $2 [...]]
|
|
|
|
*/
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(sql, sizeof(sql), "select 1 from %s where ", relname);
|
1997-09-11 14:51:23 +08:00
|
|
|
for (i = 0; i < nkeys; i++)
|
|
|
|
{
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), "%s = $%d %s",
|
1997-09-11 14:51:23 +08:00
|
|
|
args[i + nkeys + 1], i + 1, (i < nkeys - 1) ? "and " : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare plan for query */
|
|
|
|
pplan = SPI_prepare(sql, nkeys, argtypes);
|
|
|
|
if (pplan == NULL)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
2017-08-31 10:16:50 +08:00
|
|
|
elog(ERROR, "check_primary_key: SPI_prepare returned %s", SPI_result_code_string(SPI_result));
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remember that SPI_prepare places plan in current memory context -
|
2020-01-08 09:02:55 +08:00
|
|
|
* so, we have to save plan in TopMemoryContext for later use.
|
1997-09-11 14:51:23 +08:00
|
|
|
*/
|
2011-09-16 12:42:53 +08:00
|
|
|
if (SPI_keepplan(pplan))
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
2011-09-16 12:42:53 +08:00
|
|
|
elog(ERROR, "check_primary_key: SPI_keepplan failed");
|
2020-01-08 09:02:55 +08:00
|
|
|
plan->splan = (SPIPlanPtr *) MemoryContextAlloc(TopMemoryContext,
|
|
|
|
sizeof(SPIPlanPtr));
|
1997-09-11 14:51:23 +08:00
|
|
|
*(plan->splan) = pplan;
|
|
|
|
plan->nplans = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, execute prepared plan.
|
|
|
|
*/
|
|
|
|
ret = SPI_execp(*(plan->splan), kvals, NULL, 1);
|
|
|
|
/* we have no NULLs - so we pass ^^^^ here */
|
|
|
|
|
|
|
|
if (ret < 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "check_primary_key: SPI_execp returned %d", ret);
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there are no tuples returned by SELECT then ...
|
|
|
|
*/
|
1999-05-10 23:12:29 +08:00
|
|
|
if (SPI_processed == 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
|
|
|
|
errmsg("tuple references non-existent key"),
|
|
|
|
errdetail("Trigger \"%s\" found tuple referencing non-existent key in \"%s\".", trigger->tgname, relname)));
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
SPI_finish();
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(tuple);
|
1997-09-11 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check_foreign_key () -- check that key in tuple being deleted/updated
|
|
|
|
* is not referenced by tuples in "foreign" table(s).
|
|
|
|
* Though it's called without args You have to specify (while creating trigger):
|
|
|
|
* number of references, action to do if key referenced
|
|
|
|
* ('restrict' | 'setnull' | 'cascade'), key field names in triggered
|
|
|
|
* ("primary") table and referencing table(s)/keys:
|
|
|
|
* EXECUTE PROCEDURE
|
|
|
|
* check_foreign_key (2, 'restrict', 'Pkey1', 'Pkey2',
|
|
|
|
* 'Ftable1', 'Fkey11', 'Fkey12', 'Ftable2', 'Fkey21', 'Fkey22').
|
|
|
|
*/
|
|
|
|
|
2000-11-21 04:36:57 +08:00
|
|
|
PG_FUNCTION_INFO_V1(check_foreign_key);
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
Datum
|
|
|
|
check_foreign_key(PG_FUNCTION_ARGS)
|
1997-09-11 14:51:23 +08:00
|
|
|
{
|
2000-05-29 09:59:17 +08:00
|
|
|
TriggerData *trigdata = (TriggerData *) fcinfo->context;
|
1997-09-11 14:51:23 +08:00
|
|
|
Trigger *trigger; /* to get trigger name */
|
|
|
|
int nargs; /* # of args specified in CREATE TRIGGER */
|
|
|
|
char **args; /* arguments: as described above */
|
1999-05-10 23:12:29 +08:00
|
|
|
char **args_temp;
|
1997-09-11 14:51:23 +08:00
|
|
|
int nrefs; /* number of references (== # of plans) */
|
|
|
|
char action; /* 'R'estrict | 'S'etnull | 'C'ascade */
|
|
|
|
int nkeys; /* # of key columns */
|
1997-09-12 12:58:59 +08:00
|
|
|
Datum *kvals; /* key values */
|
1997-09-11 14:51:23 +08:00
|
|
|
char *relname; /* referencing relation name */
|
|
|
|
Relation rel; /* triggered relation */
|
|
|
|
HeapTuple trigtuple = NULL; /* tuple to being changed */
|
|
|
|
HeapTuple newtuple = NULL; /* tuple to return */
|
|
|
|
TupleDesc tupdesc; /* tuple description */
|
|
|
|
EPlan *plan; /* prepared plan(s) */
|
|
|
|
Oid *argtypes = NULL; /* key types to prepare execution plan */
|
|
|
|
bool isnull; /* to know is some column NULL or not */
|
|
|
|
bool isequal = true; /* are keys in both tuples equal (in UPDATE) */
|
|
|
|
char ident[2 * NAMEDATALEN]; /* to identify myself */
|
1999-05-10 23:12:29 +08:00
|
|
|
int is_update = 0;
|
1997-09-11 14:51:23 +08:00
|
|
|
int ret;
|
|
|
|
int i,
|
|
|
|
r;
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1999-05-10 23:12:29 +08:00
|
|
|
#ifdef DEBUG_QUERY
|
2003-07-25 01:52:50 +08:00
|
|
|
elog(DEBUG4, "check_foreign_key: Enter Function");
|
1999-05-10 23:12:29 +08:00
|
|
|
#endif
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1997-09-11 14:51:23 +08:00
|
|
|
/*
|
|
|
|
* Some checks first...
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Called by trigger manager ? */
|
2000-05-29 09:59:17 +08:00
|
|
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
2000-05-29 09:59:17 +08:00
|
|
|
elog(ERROR, "check_foreign_key: not fired by trigger manager");
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/* Should be called for ROW trigger */
|
2010-10-09 01:27:31 +08:00
|
|
|
if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
2010-10-09 01:27:31 +08:00
|
|
|
elog(ERROR, "check_foreign_key: must be fired for row");
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/* Not should be called for INSERT */
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-02 03:10:30 +08:00
|
|
|
elog(ERROR, "check_foreign_key: cannot process INSERT events");
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/* Have to check tg_trigtuple - tuple being deleted */
|
2000-05-29 09:59:17 +08:00
|
|
|
trigtuple = trigdata->tg_trigtuple;
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* But if this is UPDATE then we have to return tg_newtuple. Also, if key
|
|
|
|
* in tg_newtuple is the same as in tg_trigtuple then nothing to do.
|
|
|
|
*/
|
1999-05-10 23:12:29 +08:00
|
|
|
is_update = 0;
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
|
1999-05-10 23:12:29 +08:00
|
|
|
{
|
2000-05-29 09:59:17 +08:00
|
|
|
newtuple = trigdata->tg_newtuple;
|
1999-05-10 23:12:29 +08:00
|
|
|
is_update = 1;
|
|
|
|
}
|
2000-05-29 09:59:17 +08:00
|
|
|
trigger = trigdata->tg_trigger;
|
1997-09-11 14:51:23 +08:00
|
|
|
nargs = trigger->tgnargs;
|
|
|
|
args = trigger->tgargs;
|
|
|
|
|
|
|
|
if (nargs < 5) /* nrefs, action, key, Relation, key - at
|
|
|
|
* least */
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "check_foreign_key: too short %d (< 5) list of arguments", nargs);
|
1999-05-26 00:15:34 +08:00
|
|
|
|
2018-07-23 05:58:01 +08:00
|
|
|
nrefs = pg_strtoint32(args[0]);
|
1997-09-11 14:51:23 +08:00
|
|
|
if (nrefs < 1)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "check_foreign_key: %d (< 1) number of references specified", nrefs);
|
2000-12-04 04:45:40 +08:00
|
|
|
action = tolower((unsigned char) *(args[1]));
|
1997-09-11 14:51:23 +08:00
|
|
|
if (action != 'r' && action != 'c' && action != 's')
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "check_foreign_key: invalid action %s", args[1]);
|
1997-09-11 14:51:23 +08:00
|
|
|
nargs -= 2;
|
|
|
|
args += 2;
|
|
|
|
nkeys = (nargs - nrefs) / (nrefs + 1);
|
|
|
|
if (nkeys <= 0 || nargs != (nrefs + nkeys * (nrefs + 1)))
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "check_foreign_key: invalid number of arguments %d for %d references",
|
1997-09-11 14:51:23 +08:00
|
|
|
nargs + 2, nrefs);
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
rel = trigdata->tg_relation;
|
1997-09-11 14:51:23 +08:00
|
|
|
tupdesc = rel->rd_att;
|
|
|
|
|
|
|
|
/* Connect to SPI manager */
|
|
|
|
if ((ret = SPI_connect()) < 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "check_foreign_key: SPI_connect returned %d", ret);
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We use SPI plan preparation feature, so allocate space to place key
|
|
|
|
* values.
|
|
|
|
*/
|
1997-09-12 12:58:59 +08:00
|
|
|
kvals = (Datum *) palloc(nkeys * sizeof(Datum));
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct ident string as TriggerName $ TriggeredRelationId and try to
|
|
|
|
* find prepared execution plan(s).
|
|
|
|
*/
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(ident, sizeof(ident), "%s$%u", trigger->tgname, rel->rd_id);
|
1997-09-11 14:51:23 +08:00
|
|
|
plan = find_plan(ident, &FPlans, &nFPlans);
|
|
|
|
|
|
|
|
/* if there is no plan(s) then allocate argtypes for preparation */
|
|
|
|
if (plan->nplans <= 0)
|
|
|
|
argtypes = (Oid *) palloc(nkeys * sizeof(Oid));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* else - check that we have exactly nrefs plan(s) ready
|
|
|
|
*/
|
|
|
|
else if (plan->nplans != nrefs)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "%s: check_foreign_key: # of plans changed in meantime",
|
1997-09-11 14:51:23 +08:00
|
|
|
trigger->tgname);
|
|
|
|
|
|
|
|
/* For each column in key ... */
|
|
|
|
for (i = 0; i < nkeys; i++)
|
|
|
|
{
|
|
|
|
/* get index of column in tuple */
|
|
|
|
int fnumber = SPI_fnumber(tupdesc, args[i]);
|
|
|
|
|
|
|
|
/* Bad guys may give us un-existing column in CREATE TRIGGER */
|
2016-11-09 02:11:15 +08:00
|
|
|
if (fnumber <= 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
|
|
|
errmsg("there is no attribute \"%s\" in relation \"%s\"",
|
|
|
|
args[i], SPI_getrelname(rel))));
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/* Well, get binary (in internal format) value of column */
|
|
|
|
kvals[i] = SPI_getbinval(trigtuple, tupdesc, fnumber, &isnull);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's NULL then nothing to do! DON'T FORGET call SPI_finish ()!
|
|
|
|
* DON'T FORGET return tuple! Executor inserts tuple you're returning!
|
|
|
|
* If you return NULL then nothing will be inserted!
|
|
|
|
*/
|
|
|
|
if (isnull)
|
|
|
|
{
|
|
|
|
SPI_finish();
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum((newtuple == NULL) ? trigtuple : newtuple);
|
1997-09-11 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If UPDATE then get column value from new tuple being inserted and
|
|
|
|
* compare is this the same as old one. For the moment we use string
|
|
|
|
* presentation of values...
|
|
|
|
*/
|
|
|
|
if (newtuple != NULL)
|
|
|
|
{
|
|
|
|
char *oldval = SPI_getvalue(trigtuple, tupdesc, fnumber);
|
|
|
|
char *newval;
|
|
|
|
|
|
|
|
/* this shouldn't happen! SPI_ERROR_NOOUTFUNC ? */
|
|
|
|
if (oldval == NULL)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
2017-08-31 10:16:50 +08:00
|
|
|
elog(ERROR, "check_foreign_key: SPI_getvalue returned %s", SPI_result_code_string(SPI_result));
|
1997-09-11 14:51:23 +08:00
|
|
|
newval = SPI_getvalue(newtuple, tupdesc, fnumber);
|
|
|
|
if (newval == NULL || strcmp(oldval, newval) != 0)
|
|
|
|
isequal = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plan->nplans <= 0) /* Get typeId of column */
|
|
|
|
argtypes[i] = SPI_gettypeid(tupdesc, fnumber);
|
|
|
|
}
|
1999-05-10 23:12:29 +08:00
|
|
|
args_temp = args;
|
1997-09-11 14:51:23 +08:00
|
|
|
nargs -= nkeys;
|
|
|
|
args += nkeys;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have to prepare plans ...
|
|
|
|
*/
|
|
|
|
if (plan->nplans <= 0)
|
|
|
|
{
|
2007-03-16 07:12:07 +08:00
|
|
|
SPIPlanPtr pplan;
|
1997-09-11 14:51:23 +08:00
|
|
|
char sql[8192];
|
1999-05-10 23:12:29 +08:00
|
|
|
char **args2 = args;
|
1997-09-11 14:51:23 +08:00
|
|
|
|
2020-01-08 09:02:55 +08:00
|
|
|
plan->splan = (SPIPlanPtr *) MemoryContextAlloc(TopMemoryContext,
|
|
|
|
nrefs * sizeof(SPIPlanPtr));
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
for (r = 0; r < nrefs; r++)
|
|
|
|
{
|
|
|
|
relname = args2[0];
|
1999-05-26 00:15:34 +08:00
|
|
|
|
2001-03-23 12:49:58 +08:00
|
|
|
/*---------
|
|
|
|
* For 'R'estrict action we construct SELECT query:
|
|
|
|
*
|
|
|
|
* SELECT 1
|
|
|
|
* FROM _referencing_relation_
|
|
|
|
* WHERE Fkey1 = $1 [AND Fkey2 = $2 [...]]
|
|
|
|
*
|
|
|
|
* to check is tuple referenced or not.
|
|
|
|
*---------
|
1997-09-11 14:51:23 +08:00
|
|
|
*/
|
|
|
|
if (action == 'r')
|
|
|
|
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(sql, sizeof(sql), "select 1 from %s where ", relname);
|
1997-09-11 14:51:23 +08:00
|
|
|
|
2001-03-23 12:49:58 +08:00
|
|
|
/*---------
|
|
|
|
* For 'C'ascade action we construct DELETE query
|
|
|
|
*
|
|
|
|
* DELETE
|
|
|
|
* FROM _referencing_relation_
|
|
|
|
* WHERE Fkey1 = $1 [AND Fkey2 = $2 [...]]
|
|
|
|
*
|
|
|
|
* to delete all referencing tuples.
|
|
|
|
*---------
|
1999-05-10 23:12:29 +08:00
|
|
|
*/
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1999-05-10 23:12:29 +08:00
|
|
|
/*
|
|
|
|
* Max : Cascade with UPDATE query i create update query that
|
|
|
|
* updates new key values in referenced tables
|
|
|
|
*/
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
|
1999-05-10 23:12:29 +08:00
|
|
|
else if (action == 'c')
|
|
|
|
{
|
|
|
|
if (is_update == 1)
|
|
|
|
{
|
1999-05-26 00:15:34 +08:00
|
|
|
int fn;
|
1999-05-10 23:12:29 +08:00
|
|
|
char *nv;
|
1999-05-26 00:15:34 +08:00
|
|
|
int k;
|
|
|
|
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(sql, sizeof(sql), "update %s set ", relname);
|
1999-05-10 23:12:29 +08:00
|
|
|
for (k = 1; k <= nkeys; k++)
|
1999-05-26 00:15:34 +08:00
|
|
|
{
|
1999-05-10 23:12:29 +08:00
|
|
|
int is_char_type = 0;
|
|
|
|
char *type;
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1999-05-10 23:12:29 +08:00
|
|
|
fn = SPI_fnumber(tupdesc, args_temp[k - 1]);
|
2016-11-09 02:11:15 +08:00
|
|
|
Assert(fn > 0); /* already checked above */
|
1999-05-10 23:12:29 +08:00
|
|
|
nv = SPI_getvalue(newtuple, tupdesc, fn);
|
|
|
|
type = SPI_gettype(tupdesc, fn);
|
1999-05-26 00:15:34 +08:00
|
|
|
|
2019-04-12 09:16:49 +08:00
|
|
|
if (strcmp(type, "text") == 0 ||
|
|
|
|
strcmp(type, "varchar") == 0 ||
|
|
|
|
strcmp(type, "char") == 0 ||
|
|
|
|
strcmp(type, "bpchar") == 0 ||
|
|
|
|
strcmp(type, "date") == 0 ||
|
|
|
|
strcmp(type, "timestamp") == 0)
|
1999-05-10 23:12:29 +08:00
|
|
|
is_char_type = 1;
|
|
|
|
#ifdef DEBUG_QUERY
|
2003-07-25 01:52:50 +08:00
|
|
|
elog(DEBUG4, "check_foreign_key Debug value %s type %s %d",
|
1999-05-10 23:12:29 +08:00
|
|
|
nv, type, is_char_type);
|
|
|
|
#endif
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1999-05-10 23:12:29 +08:00
|
|
|
/*
|
|
|
|
* is_char_type =1 i set ' ' for define a new value
|
|
|
|
*/
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql),
|
2002-08-15 10:58:29 +08:00
|
|
|
" %s = %s%s%s %s ",
|
1999-05-10 23:12:29 +08:00
|
|
|
args2[k], (is_char_type > 0) ? "'" : "",
|
|
|
|
nv, (is_char_type > 0) ? "'" : "", (k < nkeys) ? ", " : "");
|
1999-05-26 00:15:34 +08:00
|
|
|
}
|
1999-05-10 23:12:29 +08:00
|
|
|
strcat(sql, " where ");
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1999-05-10 23:12:29 +08:00
|
|
|
}
|
|
|
|
else
|
2004-10-07 23:21:58 +08:00
|
|
|
/* DELETE */
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(sql, sizeof(sql), "delete from %s where ", relname);
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1999-05-10 23:12:29 +08:00
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1997-09-11 14:51:23 +08:00
|
|
|
/*
|
|
|
|
* For 'S'etnull action we construct UPDATE query - UPDATE
|
|
|
|
* _referencing_relation_ SET Fkey1 null [, Fkey2 null [...]]
|
|
|
|
* WHERE Fkey1 = $1 [AND Fkey2 = $2 [...]] - to set key columns in
|
|
|
|
* all referencing tuples to NULL.
|
|
|
|
*/
|
|
|
|
else if (action == 's')
|
|
|
|
{
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(sql, sizeof(sql), "update %s set ", relname);
|
1997-09-11 14:51:23 +08:00
|
|
|
for (i = 1; i <= nkeys; i++)
|
|
|
|
{
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql),
|
2002-08-15 10:58:29 +08:00
|
|
|
"%s = null%s",
|
1997-09-11 14:51:23 +08:00
|
|
|
args2[i], (i < nkeys) ? ", " : "");
|
|
|
|
}
|
|
|
|
strcat(sql, " where ");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Construct WHERE qual */
|
|
|
|
for (i = 1; i <= nkeys; i++)
|
|
|
|
{
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), "%s = $%d %s",
|
1997-09-11 14:51:23 +08:00
|
|
|
args2[i], i, (i < nkeys) ? "and " : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare plan for query */
|
|
|
|
pplan = SPI_prepare(sql, nkeys, argtypes);
|
|
|
|
if (pplan == NULL)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
2017-08-31 10:16:50 +08:00
|
|
|
elog(ERROR, "check_foreign_key: SPI_prepare returned %s", SPI_result_code_string(SPI_result));
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remember that SPI_prepare places plan in current memory context
|
2011-09-16 12:42:53 +08:00
|
|
|
* - so, we have to save plan in Top memory context for later use.
|
1997-09-11 14:51:23 +08:00
|
|
|
*/
|
2011-09-16 12:42:53 +08:00
|
|
|
if (SPI_keepplan(pplan))
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
2011-09-16 12:42:53 +08:00
|
|
|
elog(ERROR, "check_foreign_key: SPI_keepplan failed");
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
plan->splan[r] = pplan;
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1997-09-11 14:51:23 +08:00
|
|
|
args2 += nkeys + 1; /* to the next relation */
|
|
|
|
}
|
|
|
|
plan->nplans = nrefs;
|
1999-05-10 23:12:29 +08:00
|
|
|
#ifdef DEBUG_QUERY
|
2003-07-25 01:52:50 +08:00
|
|
|
elog(DEBUG4, "check_foreign_key Debug Query is : %s ", sql);
|
1999-05-10 23:12:29 +08:00
|
|
|
#endif
|
1997-09-11 14:51:23 +08:00
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1997-09-11 14:51:23 +08:00
|
|
|
/*
|
|
|
|
* If UPDATE and key is not changed ...
|
|
|
|
*/
|
|
|
|
if (newtuple != NULL && isequal)
|
|
|
|
{
|
|
|
|
SPI_finish();
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(newtuple);
|
1997-09-11 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, execute prepared plan(s).
|
|
|
|
*/
|
|
|
|
for (r = 0; r < nrefs; r++)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* For 'R'estrict we may to execute plan for one tuple only, for other
|
|
|
|
* actions - for all tuples.
|
|
|
|
*/
|
|
|
|
int tcount = (action == 'r') ? 1 : 0;
|
|
|
|
|
|
|
|
relname = args[0];
|
|
|
|
|
2002-09-02 14:11:43 +08:00
|
|
|
snprintf(ident, sizeof(ident), "%s$%u", trigger->tgname, rel->rd_id);
|
1998-10-06 11:12:59 +08:00
|
|
|
plan = find_plan(ident, &FPlans, &nFPlans);
|
1997-09-11 14:51:23 +08:00
|
|
|
ret = SPI_execp(plan->splan[r], kvals, NULL, tcount);
|
|
|
|
/* we have no NULLs - so we pass ^^^^ here */
|
|
|
|
|
|
|
|
if (ret < 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
|
|
|
|
errmsg("SPI_execp returned %d", ret)));
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
/* If action is 'R'estrict ... */
|
|
|
|
if (action == 'r')
|
|
|
|
{
|
|
|
|
/* If there is tuple returned by SELECT then ... */
|
|
|
|
if (SPI_processed > 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
|
|
|
|
errmsg("\"%s\": tuple is referenced in \"%s\"",
|
|
|
|
trigger->tgname, relname)));
|
1997-09-11 14:51:23 +08:00
|
|
|
}
|
|
|
|
else
|
2002-10-04 02:40:02 +08:00
|
|
|
{
|
|
|
|
#ifdef REFINT_VERBOSE
|
Widen query numbers-of-tuples-processed counters to uint64.
This patch widens SPI_processed, EState's es_processed field, PortalData's
portalPos field, FuncCallContext's call_cntr and max_calls fields,
ExecutorRun's count argument, PortalRunFetch's result, and the max number
of rows in a SPITupleTable to uint64, and deals with (I hope) all the
ensuing fallout. Some of these values were declared uint32 before, and
others "long".
I also removed PortalData's posOverflow field, since that logic seems
pretty useless given that portalPos is now always 64 bits.
The user-visible results are that command tags for SELECT etc will
correctly report tuple counts larger than 4G, as will plpgsql's GET
GET DIAGNOSTICS ... ROW_COUNT command. Queries processing more tuples
than that are still not exactly the norm, but they're becoming more
common.
Most values associated with FETCH/MOVE distances, such as PortalRun's count
argument and the count argument of most SPI functions that have one, remain
declared as "long". It's not clear whether it would be worth promoting
those to int64; but it would definitely be a large dollop of additional
API churn on top of this, and it would only help 32-bit platforms which
seem relatively less likely to see any benefit.
Andreas Scherbaum, reviewed by Christian Ullrich, additional hacking by me
2016-03-13 05:05:10 +08:00
|
|
|
elog(NOTICE, "%s: " UINT64_FORMAT " tuple(s) of %s are %s",
|
1997-09-11 14:51:23 +08:00
|
|
|
trigger->tgname, SPI_processed, relname,
|
2003-07-25 01:52:50 +08:00
|
|
|
(action == 'c') ? "deleted" : "set to null");
|
1997-09-11 14:51:23 +08:00
|
|
|
#endif
|
2002-10-04 02:40:02 +08:00
|
|
|
}
|
1997-09-11 14:51:23 +08:00
|
|
|
args += nkeys + 1; /* to the next relation */
|
|
|
|
}
|
|
|
|
|
|
|
|
SPI_finish();
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum((newtuple == NULL) ? trigtuple : newtuple);
|
1997-09-11 14:51:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static EPlan *
|
|
|
|
find_plan(char *ident, EPlan **eplan, int *nplans)
|
|
|
|
{
|
|
|
|
EPlan *newp;
|
|
|
|
int i;
|
2020-01-08 09:02:55 +08:00
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All allocations done for the plans need to happen in a session-safe
|
|
|
|
* context.
|
|
|
|
*/
|
|
|
|
oldcontext = MemoryContextSwitchTo(TopMemoryContext);
|
1997-09-11 14:51:23 +08:00
|
|
|
|
|
|
|
if (*nplans > 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < *nplans; i++)
|
|
|
|
{
|
|
|
|
if (strcmp((*eplan)[i].ident, ident) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i != *nplans)
|
2020-01-08 09:02:55 +08:00
|
|
|
{
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
1997-09-11 14:51:23 +08:00
|
|
|
return (*eplan + i);
|
2020-01-08 09:02:55 +08:00
|
|
|
}
|
|
|
|
*eplan = (EPlan *) repalloc(*eplan, (i + 1) * sizeof(EPlan));
|
1997-09-11 14:51:23 +08:00
|
|
|
newp = *eplan + i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-08 09:02:55 +08:00
|
|
|
newp = *eplan = (EPlan *) palloc(sizeof(EPlan));
|
1997-09-11 14:51:23 +08:00
|
|
|
(*nplans) = i = 0;
|
|
|
|
}
|
|
|
|
|
2020-01-08 09:02:55 +08:00
|
|
|
newp->ident = pstrdup(ident);
|
1997-09-11 14:51:23 +08:00
|
|
|
newp->nplans = 0;
|
|
|
|
newp->splan = NULL;
|
|
|
|
(*nplans)++;
|
|
|
|
|
2020-01-08 09:02:55 +08:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2017-08-18 00:39:20 +08:00
|
|
|
return newp;
|
1997-09-11 14:51:23 +08:00
|
|
|
}
|