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");
106 args = trigger->tgargs;
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++)
146 (
errcode(ERRCODE_UNDEFINED_COLUMN),
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++)
183 snprintf(sql + strlen(sql),
sizeof(sql) - strlen(sql),
"%s = $%d %s",
184 args[
i + nkeys + 1],
i + 1, (
i < nkeys - 1) ?
"and " :
"");
199 elog(
ERROR,
"check_primary_key: SPI_keepplan failed");
202 *(
plan->splan) = pplan;
214 elog(
ERROR,
"check_primary_key: SPI_execp returned %d", ret);
221 (
errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
222 errmsg(
"tuple references non-existent key"),
223 errdetail(
"Trigger \"%s\" found tuple referencing non-existent key in \"%s\".", trigger->tgname,
relname)));
262 Oid *argtypes = NULL;
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");
313 args = trigger->tgargs;
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);
327 elog(
ERROR,
"check_foreign_key: invalid action %s",
args[1]);
330 nkeys = (nargs - nrefs) / (nrefs + 1);
331 if (nkeys <= 0 || nargs != (nrefs + nkeys * (nrefs + 1)))
333 elog(
ERROR,
"check_foreign_key: invalid number of arguments %d for %d references",
352 snprintf(
ident,
sizeof(
ident),
"%s$%u$%c", trigger->tgname, rel->rd_id, is_update ?
'U' :
'D');
356 if (
plan->nplans <= 0)
362 else if (
plan->nplans != nrefs)
364 elog(
ERROR,
"%s: check_foreign_key: # of plans changed in meantime",
368 for (
i = 0;
i < nkeys;
i++)
376 (
errcode(ERRCODE_UNDEFINED_COLUMN),
377 errmsg(
"there is no attribute \"%s\" in relation \"%s\"",
399 if (newtuple != NULL)
401 char *oldval =
SPI_getvalue(trigtuple, tupdesc, fnumber);
413 if (
plan->nplans <= 0)
423 if (
plan->nplans <= 0)
432 for (r = 0; r < nrefs; r++)
476 for (k = 1; k <= nkeys; k++)
478 int is_char_type = 0;
486 if (strcmp(
type,
"text") == 0 ||
487 strcmp(
type,
"varchar") == 0 ||
488 strcmp(
type,
"char") == 0 ||
489 strcmp(
type,
"bpchar") == 0 ||
490 strcmp(
type,
"date") == 0 ||
491 strcmp(
type,
"timestamp") == 0)
494 elog(
DEBUG4,
"check_foreign_key Debug value %s type %s %d",
495 nv,
type, is_char_type);
501 snprintf(sql + strlen(sql),
sizeof(sql) - strlen(sql),
503 args2[k], (is_char_type > 0) ?
"'" :
"",
504 nv, (is_char_type > 0) ?
"'" :
"", (k < nkeys) ?
", " :
"");
506 strcat(sql,
" where ");
522 for (
i = 1;
i <= nkeys;
i++)
524 snprintf(sql + strlen(sql),
sizeof(sql) - strlen(sql),
526 args2[
i], (
i < nkeys) ?
", " :
"");
528 strcat(sql,
" where ");
532 for (
i = 1;
i <= nkeys;
i++)
534 snprintf(sql + strlen(sql),
sizeof(sql) - strlen(sql),
"%s = $%d %s",
535 args2[
i],
i, (
i < nkeys) ?
"and " :
"");
550 elog(
ERROR,
"check_foreign_key: SPI_keepplan failed");
552 plan->splan[r] = pplan;
556 plan->nplans = nrefs;
558 elog(
DEBUG4,
"check_foreign_key Debug Query is : %s ", sql);
565 if (newtuple != NULL && isequal)
574 for (r = 0; r < nrefs; r++)
580 int tcount = (
action ==
'r') ? 1 : 0;
589 (
errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
590 errmsg(
"SPI_execp returned %d", ret)));
598 (
errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
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++)
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
Assert(PointerIsAligned(start, uint64))
if(TABLE==NULL||TABLE_index==NULL)
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 Datum PointerGetDatum(const void *X)
PG_MODULE_MAGIC_EXT(.name="refint",.version=PG_VERSION)
Datum check_foreign_key(PG_FUNCTION_ARGS)
Datum check_primary_key(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(check_primary_key)
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)