70 elog(
DEBUG4,
"check_primary_key: Enter Function");
80 elog(
ERROR,
"check_primary_key: not fired by trigger manager");
85 elog(
ERROR,
"check_primary_key: must be fired for row");
89 elog(
ERROR,
"check_primary_key: must be fired by AFTER trigger");
98 elog(
ERROR,
"check_primary_key: cannot process DELETE events");
110 elog(
ERROR,
"check_primary_key: odd number of arguments should be specified");
134 if (
plan->nplans <= 0)
138 for (
i = 0;
i < nkeys;
i++)
147 errmsg(
"there is no attribute \"%s\" in relation \"%s\"",
164 if (
plan->nplans <= 0)
171 if (
plan->nplans <= 0)
181 for (
i = 0;
i < nkeys;
i++)
184 args[
i + nkeys + 1],
i + 1, (
i < nkeys - 1) ?
"and " :
"");
199 elog(
ERROR,
"check_primary_key: SPI_keepplan failed");
214 elog(
ERROR,
"check_primary_key: SPI_execp returned %d", ret);
222 errmsg(
"tuple references non-existent key"),
272 elog(
DEBUG4,
"check_foreign_key: Enter Function");
282 elog(
ERROR,
"check_foreign_key: not fired by trigger manager");
287 elog(
ERROR,
"check_foreign_key: must be fired for row");
292 elog(
ERROR,
"check_foreign_key: cannot process INSERT events");
296 elog(
ERROR,
"check_foreign_key: must be fired by AFTER trigger");
318 elog(
ERROR,
"check_foreign_key: too short %d (< 5) list of arguments", nargs);
323 elog(
ERROR,
"check_foreign_key: %d (< 1) number of references specified",
nrefs);
325 if (action !=
'r' && action !=
'c' && action !=
's')
327 elog(
ERROR,
"check_foreign_key: invalid action %s", args[1]);
331 if (nkeys <= 0 || nargs != (
nrefs + nkeys * (
nrefs + 1)))
333 elog(
ERROR,
"check_foreign_key: invalid number of arguments %d for %d references",
356 if (
plan->nplans <= 0)
364 elog(
ERROR,
"%s: check_foreign_key: # of plans changed in meantime",
368 for (
i = 0;
i < nkeys;
i++)
377 errmsg(
"there is no attribute \"%s\" in relation \"%s\"",
399 if (newtuple !=
NULL)
413 if (
plan->nplans <= 0)
423 if (
plan->nplans <= 0)
432 for (r = 0; r <
nrefs; r++)
467 else if (action ==
'c')
476 for (k = 1; k <= nkeys; k++)
494 elog(
DEBUG4,
"check_foreign_key Debug value %s type %s %d",
519 else if (action ==
's')
522 for (
i = 1;
i <= nkeys;
i++)
526 args2[
i], (
i < nkeys) ?
", " :
"");
532 for (
i = 1;
i <= nkeys;
i++)
535 args2[
i],
i, (
i < nkeys) ?
"and " :
"");
550 elog(
ERROR,
"check_foreign_key: SPI_keepplan failed");
558 elog(
DEBUG4,
"check_foreign_key Debug Query is : %s ", sql);
574 for (r = 0; r <
nrefs; r++)
580 int tcount = (action ==
'r') ? 1 : 0;
590 errmsg(
"SPI_execp returned %d", ret)));
599 errmsg(
"\"%s\": tuple is referenced in \"%s\"",
605 const char *operation;
608 operation =
is_update ?
"updated" :
"deleted";
610 operation =
"set to null";
639 for (
i = 0;
i < *nplans;
i++)
#define Assert(condition)
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define palloc_object(type)
#define PG_MODULE_MAGIC_EXT(...)
#define PG_FUNCTION_INFO_V1(funcname)
void * MemoryContextAlloc(MemoryContext context, Size size)
char * pstrdup(const char *in)
void * repalloc(void *pointer, Size size)
MemoryContext TopMemoryContext
int32 pg_strtoint32(const char *s)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static unsigned char pg_ascii_tolower(unsigned char ch)
static Datum PointerGetDatum(const void *X)
Datum check_foreign_key(PG_FUNCTION_ARGS)
Datum check_primary_key(PG_FUNCTION_ARGS)
static EPlan * find_plan(char *ident, EPlan **eplan, int *nplans)
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
char * SPI_gettype(TupleDesc tupdesc, int fnumber)
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
const char * SPI_result_code_string(int code)
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
int SPI_keepplan(SPIPlanPtr plan)
int SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls, long tcount)
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
char * SPI_getrelname(Relation rel)
static void * fn(void *arg)
#define TRIGGER_FIRED_BY_DELETE(event)
#define CALLED_AS_TRIGGER(fcinfo)
#define TRIGGER_FIRED_FOR_ROW(event)
#define TRIGGER_FIRED_AFTER(event)
#define TRIGGER_FIRED_BY_INSERT(event)
#define TRIGGER_FIRED_BY_UPDATE(event)