patch: CHECK FUNCTION statement
Hello all,
I am sending updated patch, that implements a CHECK FUNCTION and CHECK
TRIGGER statements.
This patch is significantly redesigned to previous version (PL/pgSQL
part) - it is more readable, more accurate. There are new regress
tests.
Please, can some English native speaker fix doc and comments?
Usage is very simply
postgres=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+---------+-----------
a | integer |
b | integer |
Triggers:
t1_f1 BEFORE INSERT ON t1 FOR EACH ROW EXECUTE PROCEDURE f1_trg()
postgres=# \sf+ f1_trg
CREATE OR REPLACE FUNCTION public.f1_trg()
RETURNS trigger
LANGUAGE plpgsql
1 AS $function$
2 begin
3 new.a := new.a + 10;
4 new.b := new.b + 10;
5 new.c := 30;
6 return new;
7 end;
8 $function$
postgres=# check trigger t1_f1 on t1;
NOTICE: checking function "f1_trg()"
ERROR: record "new" has no field "c"
CONTEXT: checking of PL/pgSQL function "f1_trg" line 5 at assignment
Checker handler should be called explicitly
postgres=# select plpgsql_checker('f1'::regproc, 0);
ERROR: column "c" of relation "t1" does not exist
LINE 1: update t1 set c = 30
^
QUERY: update t1 set c = 30
CONTEXT: checking of PL/pgSQL function "f1" line 4 at SQL statement
or (check or plpgsql custom functions)
DO $$
DECLARE r regprocedure;
BEGIN
FOR r IN SELECT p.oid
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON
n.oid = p.pronamespace
LEFT JOIN pg_catalog.pg_language l ON
l.oid = p.prolang
WHERE l.lanname = 'plpgsql'
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND p.prorettype <>
'pg_catalog.trigger'::pg_catalog.regtype
LOOP
RAISE NOTICE 'check %', r;
PERFORM plpgsql_checker(r, 0);
END LOOP;
END;
$$;
ToDo:
CHECK FUNCTION search function according to function signature - it
should be changes for using a actual types - it can be solution for
polymorphic types and useful tool for work with overloaded functions -
when is not clean, that function was executed.
check function foo(int, int);
NOTICE: checking function foo(variadic anyarray)
...
and maybe some support for named parameters
check function foo(name text, surname text);
NOTICE: checking function foo(text, text, text, text)
...
Regards
Pavel Stehule
Attachments:
check_pl-2011-11-26.difftext/x-patch; charset=US-ASCII; name=check_pl-2011-11-26.diffDownload
*** ./doc/src/sgml/ref/allfiles.sgml.orig 2011-11-25 12:04:21.509588672 +0100
--- ./doc/src/sgml/ref/allfiles.sgml 2011-11-25 12:05:31.844784492 +0100
***************
*** 40,45 ****
--- 40,46 ----
<!ENTITY alterView SYSTEM "alter_view.sgml">
<!ENTITY analyze SYSTEM "analyze.sgml">
<!ENTITY begin SYSTEM "begin.sgml">
+ <!ENTITY checkFunction SYSTEM "check_function.sgml">
<!ENTITY checkpoint SYSTEM "checkpoint.sgml">
<!ENTITY close SYSTEM "close.sgml">
<!ENTITY cluster SYSTEM "cluster.sgml">
*** ./doc/src/sgml/ref/create_language.sgml.orig 2011-11-24 12:51:44.663317228 +0100
--- ./doc/src/sgml/ref/create_language.sgml 2011-11-25 12:05:31.845784481 +0100
***************
*** 23,29 ****
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
--- 23,29 ----
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ] [ CHECK <replaceable>checkfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
***************
*** 217,222 ****
--- 217,236 ----
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><literal>CHECK</literal> <replaceable class="parameter">checkfunction</replaceable></term>
+
+ <listitem>
+ <para><replaceable class="parameter">checkfunction</replaceable> is the
+ name of a previously registered function that will be called
+ when a new function in the language is created, to check the
+ function by statemnt <command>CHECK FUNCTION</command> or
+ <command>CHECK TRIGGER</command>.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
*** ./doc/src/sgml/reference.sgml.orig 2011-11-25 12:04:21.530588430 +0100
--- ./doc/src/sgml/reference.sgml 2011-11-25 12:05:31.849784436 +0100
***************
*** 68,73 ****
--- 68,74 ----
&alterView;
&analyze;
&begin;
+ &checkFunction;
&checkpoint;
&close;
&cluster;
*** ./doc/src/sgml/ref/check_function.sgml.orig 2011-11-25 12:05:11.416017756 +0100
--- ./doc/src/sgml/ref/check_function.sgml 2011-11-25 12:05:31.849784436 +0100
***************
*** 0 ****
--- 1,37 ----
+ <!--
+ doc/src/sgml/ref/check_function.sgml
+ -->
+
+ <refentry id="SQL-CHECKFUNCTION">
+ <refmeta>
+ <refentrytitle>CHECK FUNCTION</refentrytitle>
+ <manvolnum>7</manvolnum>
+ <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+ <refname>CHECK FUNCTION</refname>
+ <refpurpose>ensure a deep checking of existing function</refpurpose>
+ </refnamediv>
+
+ <indexterm zone="sql-checkfunction">
+ <primary>CHECK FUNCTION</primary>
+ </indexterm>
+
+ <refsynopsisdiv>
+ <synopsis>
+ CREATE FUNCTION <replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] )
+ | CREATE TRIGGER <replaceable class="parameter">name</replaceable> ON <replaceable class="parameter">tablename</replaceable>
+ </synopsis>
+ </refsynopsisdiv>
+
+ <refsect1 id="sql-checkfunction-description">
+ <title>Description</title>
+
+ <para>
+ <command>CHECK FUNCTION</command> check a existing function.
+ <command>CHECK TRIGGER</command> check a trigger function.
+ </para>
+ </refsect1>
+
+ </refentry>
*** ./src/backend/catalog/pg_proc.c.orig 2011-11-24 12:51:44.745316295 +0100
--- ./src/backend/catalog/pg_proc.c 2011-11-25 12:05:31.850784424 +0100
***************
*** 1101,1103 ****
--- 1101,1104 ----
*newcursorpos = newcp;
return false;
}
+
*** ./src/backend/commands/functioncmds.c.orig 2011-11-24 12:51:44.754316192 +0100
--- ./src/backend/commands/functioncmds.c 2011-11-25 20:01:45.521469608 +0100
***************
*** 44,53 ****
--- 44,55 ----
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_proc_fn.h"
+ #include "catalog/pg_trigger.h"
#include "catalog/pg_type.h"
#include "catalog/pg_type_fn.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
+ #include "commands/trigger.h"
#include "miscadmin.h"
#include "optimizer/var.h"
#include "parser/parse_coerce.h"
***************
*** 60,65 ****
--- 62,68 ----
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
+ #include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
***************
*** 1009,1014 ****
--- 1012,1152 ----
}
}
+ /*
+ * CheckFunction
+ * call a PL checker function when this function exists.
+ */
+ void
+ CheckFunction(CheckFunctionStmt *stmt)
+ {
+ List *functionName = stmt->funcname;
+ List *argTypes = stmt->args; /* list of TypeName nodes */
+ Oid funcOid;
+
+ HeapTuple tup;
+ Form_pg_proc proc;
+
+ HeapTuple languageTuple;
+ Form_pg_language languageStruct;
+ Oid languageChecker;
+ Oid trgOid = InvalidOid;
+ Oid relid = InvalidOid;
+
+ /* when we should to check trigger, then we should to find a trigger handler */
+ if (functionName == NULL)
+ {
+ HeapTuple ht_trig;
+ Form_pg_trigger trigrec;
+ ScanKeyData skey[1];
+ Relation tgrel;
+ SysScanDesc tgscan;
+ char *fname;
+
+ relid = RangeVarGetRelid(stmt->relation, ShareLock, false, false);
+ trgOid = get_trigger_oid(relid, stmt->trgname, false);
+
+ /*
+ * Fetch the pg_trigger tuple by the Oid of the trigger
+ */
+ tgrel = heap_open(TriggerRelationId, AccessShareLock);
+
+ ScanKeyInit(&skey[0],
+ ObjectIdAttributeNumber,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(trgOid));
+
+ tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
+ SnapshotNow, 1, skey);
+
+ ht_trig = systable_getnext(tgscan);
+
+ if (!HeapTupleIsValid(ht_trig))
+ elog(ERROR, "could not find tuple for trigger %u", trgOid);
+
+ trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
+
+ /* we need to know trigger function to get PL checker function */
+ funcOid = trigrec->tgfoid;
+ fname = format_procedure(funcOid);
+ /* Clean up */
+ systable_endscan(tgscan);
+
+ elog(NOTICE, "checking function \"%s\"", fname);
+ pfree(fname);
+
+ heap_close(tgrel, AccessShareLock);
+ }
+ else
+ {
+ /*
+ * Find the function,
+ */
+ funcOid = LookupFuncNameTypeNames(functionName, argTypes, false);
+ }
+
+ tup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
+ if (!HeapTupleIsValid(tup)) /* should not happen */
+ elog(ERROR, "cache lookup failed for function %u", funcOid);
+
+ proc = (Form_pg_proc) GETSTRUCT(tup);
+
+ languageTuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(proc->prolang));
+ Assert(HeapTupleIsValid(languageTuple));
+
+ languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
+ languageChecker = languageStruct->lanchecker;
+
+ /* Check a function body */
+ if (OidIsValid(languageChecker))
+ {
+ ArrayType *set_items = NULL;
+ int save_nestlevel;
+ Datum datum;
+ bool isnull;
+ MemoryContext oldCxt;
+ MemoryContext checkCxt;
+
+ datum = SysCacheGetAttr(PROCOID, tup, Anum_pg_proc_proconfig, &isnull);
+
+ if (!isnull)
+ {
+ /* Set per-function configuration parameters */
+ set_items = (ArrayType *) DatumGetPointer(datum);
+ if (set_items) /* Need a new GUC nesting level */
+ {
+ save_nestlevel = NewGUCNestLevel();
+ ProcessGUCArray(set_items,
+ (superuser() ? PGC_SUSET : PGC_USERSET),
+ PGC_S_SESSION,
+ GUC_ACTION_SAVE);
+ }
+ else
+ save_nestlevel = 0; /* keep compiler quiet */
+ }
+
+ checkCxt = AllocSetContextCreate(CurrentMemoryContext,
+ "Check temporary context",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
+
+ oldCxt = MemoryContextSwitchTo(checkCxt);
+
+ OidFunctionCall2(languageChecker, ObjectIdGetDatum(funcOid),
+ ObjectIdGetDatum(relid));
+
+ MemoryContextSwitchTo(oldCxt);
+
+ if (set_items)
+ AtEOXact_GUC(true, save_nestlevel);
+ }
+ else
+ elog(WARNING, "language \"%s\" has no defined checker function",
+ NameStr(languageStruct->lanname));
+
+ ReleaseSysCache(languageTuple);
+ ReleaseSysCache(tup);
+ }
/*
* Rename function
*** ./src/backend/commands/proclang.c.orig 2011-11-24 12:51:44.756316170 +0100
--- ./src/backend/commands/proclang.c 2011-11-25 12:17:46.031559228 +0100
***************
*** 46,57 ****
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
--- 46,58 ----
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
+ char *tmplchecker; /* name of checker function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
***************
*** 67,75 ****
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid;
Oid funcrettype;
! Oid funcargtypes[1];
/*
* If we have template information for the language, ignore the supplied
--- 68,77 ----
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid,
! checkerOid;
Oid funcrettype;
! Oid funcargtypes[2];
/*
* If we have template information for the language, ignore the supplied
***************
*** 219,228 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, pltemplate->tmpltrusted);
}
else
{
--- 221,269 ----
else
valOid = InvalidOid;
+ /*
+ * Likewise for the checker, if required; but we don't care about
+ * its return type.
+ */
+ if (pltemplate->tmplchecker)
+ {
+ funcname = SystemFuncName(pltemplate->tmplchecker);
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(funcname, 2, funcargtypes, true);
+ if (!OidIsValid(checkerOid))
+ {
+ checkerOid = ProcedureCreate(pltemplate->tmplchecker,
+ PG_CATALOG_NAMESPACE,
+ false, /* replace */
+ false, /* returnsSet */
+ VOIDOID,
+ ClanguageId,
+ F_FMGR_C_VALIDATOR,
+ pltemplate->tmplchecker,
+ pltemplate->tmpllibrary,
+ false, /* isAgg */
+ false, /* isWindowFunc */
+ false, /* security_definer */
+ true, /* isStrict */
+ PROVOLATILE_VOLATILE,
+ buildoidvector(funcargtypes, 2),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ NIL,
+ PointerGetDatum(NULL),
+ 1,
+ 0);
+ }
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, pltemplate->tmpltrusted);
}
else
{
***************
*** 294,303 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, stmt->pltrusted);
}
}
--- 335,355 ----
else
valOid = InvalidOid;
+ /* validate the checker function */
+ if (stmt->plchecker)
+ {
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(stmt->plchecker, 2, funcargtypes, false);
+ /* return value is ignored, so we don't check the type */
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, stmt->pltrusted);
}
}
***************
*** 307,313 ****
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
--- 359,365 ----
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
***************
*** 337,342 ****
--- 389,395 ----
values[Anum_pg_language_lanplcallfoid - 1] = ObjectIdGetDatum(handlerOid);
values[Anum_pg_language_laninline - 1] = ObjectIdGetDatum(inlineOid);
values[Anum_pg_language_lanvalidator - 1] = ObjectIdGetDatum(valOid);
+ values[Anum_pg_language_lanchecker - 1] = ObjectIdGetDatum(checkerOid);
nulls[Anum_pg_language_lanacl - 1] = true;
/* Check for pre-existing definition */
***************
*** 423,428 ****
--- 476,490 ----
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
+ /* dependency on the checker function, if any */
+ if (OidIsValid(checkerOid))
+ {
+ referenced.classId = ProcedureRelationId;
+ referenced.objectId = checkerOid;
+ referenced.objectSubId = 0;
+ recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
+ }
+
/* Post creation hook for new procedural language */
InvokeObjectAccessHook(OAT_POST_CREATE,
LanguageRelationId, myself.objectId, 0);
***************
*** 478,483 ****
--- 540,550 ----
if (!isnull)
result->tmplvalidator = TextDatumGetCString(datum);
+ datum = heap_getattr(tup, Anum_pg_pltemplate_tmplchecker,
+ RelationGetDescr(rel), &isnull);
+ if (!isnull)
+ result->tmplchecker = TextDatumGetCString(datum);
+
datum = heap_getattr(tup, Anum_pg_pltemplate_tmpllibrary,
RelationGetDescr(rel), &isnull);
if (!isnull)
*** ./src/backend/parser/gram.y.orig 2011-11-24 12:51:44.810315553 +0100
--- ./src/backend/parser/gram.y 2011-11-25 12:05:31.859784321 +0100
***************
*** 227,232 ****
--- 227,233 ----
DeallocateStmt PrepareStmt ExecuteStmt
DropOwnedStmt ReassignOwnedStmt
AlterTSConfigurationStmt AlterTSDictionaryStmt
+ CheckFunctionStmt
%type <node> select_no_parens select_with_parens select_clause
simple_select values_clause
***************
*** 276,282 ****
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate
%type <range> qualified_name OptConstrFromTable
--- 277,283 ----
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate opt_checker
%type <range> qualified_name OptConstrFromTable
***************
*** 701,706 ****
--- 702,708 ----
| AlterUserSetStmt
| AlterUserStmt
| AnalyzeStmt
+ | CheckFunctionStmt
| CheckPointStmt
| ClosePortalStmt
| ClusterStmt
***************
*** 3206,3216 ****
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
--- 3208,3219 ----
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
+ n->plchecker = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator opt_checker
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
***************
*** 3218,3223 ****
--- 3221,3227 ----
n->plhandler = $8;
n->plinline = $9;
n->plvalidator = $10;
+ n->plchecker = $11;
n->pltrusted = $3;
$$ = (Node *)n;
}
***************
*** 3252,3257 ****
--- 3256,3266 ----
| /*EMPTY*/ { $$ = NIL; }
;
+ opt_checker:
+ CHECK handler_name { $$ = $2; }
+ | /*EMPTY*/ { $$ = NIL; }
+ ;
+
DropPLangStmt:
DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
{
***************
*** 6282,6287 ****
--- 6291,6326 ----
/*****************************************************************************
*
+ * CHECK FUNCTION funcname(args)
+ * CHECK TRIGGER triggername ON table
+ *
+ *
+ *****************************************************************************/
+
+
+ CheckFunctionStmt:
+ CHECK FUNCTION func_name func_args
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = $3;
+ n->args = extractArgTypes($4);
+ n->trgname = NULL;
+ n->relation = NULL;
+ $$ = (Node *) n;
+ }
+ | CHECK TRIGGER name ON qualified_name
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = $3;
+ n->relation = $5;
+ $$ = (Node *) n;
+ }
+ ;
+
+ /*****************************************************************************
+ *
* DO <anonymous code block> [ LANGUAGE language ]
*
* We use a DefElem list for future extensibility, and to allow flexibility
*** ./src/backend/tcop/utility.c.orig 2011-11-24 12:51:44.929314198 +0100
--- ./src/backend/tcop/utility.c 2011-11-25 12:05:31.880784082 +0100
***************
*** 882,887 ****
--- 882,891 ----
AlterFunction((AlterFunctionStmt *) parsetree);
break;
+ case T_CheckFunctionStmt:
+ CheckFunction((CheckFunctionStmt *) parsetree);
+ break;
+
case T_IndexStmt: /* CREATE INDEX */
{
IndexStmt *stmt = (IndexStmt *) parsetree;
***************
*** 2125,2130 ****
--- 2129,2141 ----
}
break;
+ case T_CheckFunctionStmt:
+ if (((CheckFunctionStmt *) parsetree)->funcname != NULL)
+ tag = "CHECK FUNCTION";
+ else
+ tag = "CHECK TRIGGER";
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
***************
*** 2565,2570 ****
--- 2576,2585 ----
}
break;
+ case T_CheckFunctionStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
*** ./src/bin/psql/tab-complete.c.orig 2011-11-24 12:51:45.147311716 +0100
--- ./src/bin/psql/tab-complete.c 2011-11-25 12:05:31.882784060 +0100
***************
*** 1,4 ****
--- 1,5 ----
/*
+ *
* psql - the PostgreSQL interactive terminal
*
* Copyright (c) 2000-2011, PostgreSQL Global Development Group
***************
*** 727,733 ****
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
--- 728,734 ----
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECK", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
***************
*** 1524,1529 ****
--- 1525,1552 ----
COMPLETE_WITH_LIST(list_TRANS);
}
+
+ /* CHECK */
+ else if (pg_strcasecmp(prev_wd, "CHECK") == 0)
+ {
+ static const char *const list_CHECK[] =
+ {"FUNCTION", "TRIGGER", NULL};
+
+ COMPLETE_WITH_LIST(list_CHECK);
+ }
+ else if (pg_strcasecmp(prev3_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
+ {
+ COMPLETE_WITH_CONST("ON");
+ }
+ else if (pg_strcasecmp(prev4_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
+ pg_strcasecmp(prev_wd, "ON") == 0)
+ {
+ completion_info_charp = prev2_wd;
+ COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger);
+ }
+
/* CLUSTER */
/*
*** ./src/include/catalog/pg_language.h.orig 2011-11-25 12:04:21.610587514 +0100
--- ./src/include/catalog/pg_language.h 2011-11-25 12:05:31.883784048 +0100
***************
*** 37,42 ****
--- 37,43 ----
Oid lanplcallfoid; /* Call handler for PL */
Oid laninline; /* Optional anonymous-block handler function */
Oid lanvalidator; /* Optional validation function */
+ Oid lanchecker; /* Optional checker function */
aclitem lanacl[1]; /* Access privileges */
} FormData_pg_language;
***************
*** 51,57 ****
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 8
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
--- 52,58 ----
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 9
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
***************
*** 59,78 ****
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanacl 8
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
--- 60,80 ----
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanchecker 8
! #define Anum_pg_language_lanacl 9
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 0 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 0 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 0 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
*** ./src/include/catalog/pg_pltemplate.h.orig 2011-11-25 12:04:21.612587492 +0100
--- ./src/include/catalog/pg_pltemplate.h 2011-11-25 12:05:31.884784036 +0100
***************
*** 36,41 ****
--- 36,42 ----
text tmplhandler; /* name of call handler function */
text tmplinline; /* name of anonymous-block handler, or NULL */
text tmplvalidator; /* name of validator function, or NULL */
+ text tmplchecker; /* name of checker function, or NULL */
text tmpllibrary; /* path of shared library */
aclitem tmplacl[1]; /* access privileges for template */
} FormData_pg_pltemplate;
***************
*** 51,65 ****
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 8
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmpllibrary 7
! #define Anum_pg_pltemplate_tmplacl 8
/* ----------------
--- 52,67 ----
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 9
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmplchecker 7
! #define Anum_pg_pltemplate_tmpllibrary 8
! #define Anum_pg_pltemplate_tmplacl 9
/* ----------------
***************
*** 67,79 ****
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
--- 69,81 ----
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "plpgsql_checker" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" _null_ "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
*** ./src/include/commands/defrem.h.orig 2011-11-24 12:51:45.172311430 +0100
--- ./src/include/commands/defrem.h 2011-11-25 12:05:31.885784024 +0100
***************
*** 62,67 ****
--- 62,68 ----
/* commands/functioncmds.c */
extern void CreateFunction(CreateFunctionStmt *stmt, const char *queryString);
extern void RemoveFunctionById(Oid funcOid);
+ extern void CheckFunction(CheckFunctionStmt *stmt);
extern void SetFunctionReturnType(Oid funcOid, Oid newRetType);
extern void SetFunctionArgType(Oid funcOid, int argIndex, Oid newArgType);
extern void RenameFunction(List *name, List *argtypes, const char *newname);
*** ./src/include/nodes/nodes.h.orig 2011-11-24 12:51:45.000000000 +0100
--- ./src/include/nodes/nodes.h 2011-11-25 12:22:37.306374249 +0100
***************
*** 291,296 ****
--- 291,297 ----
T_IndexStmt,
T_CreateFunctionStmt,
T_AlterFunctionStmt,
+ T_CheckFunctionStmt,
T_DoStmt,
T_RenameStmt,
T_RuleStmt,
*** ./src/include/nodes/parsenodes.h.orig 2011-11-24 12:51:45.194311178 +0100
--- ./src/include/nodes/parsenodes.h 2011-11-25 12:05:31.888783991 +0100
***************
*** 1734,1739 ****
--- 1734,1740 ----
List *plhandler; /* PL call handler function (qual. name) */
List *plinline; /* optional inline function (qual. name) */
List *plvalidator; /* optional validator function (qual. name) */
+ List *plchecker; /* optional checker function (qual. name) */
bool pltrusted; /* PL is trusted */
} CreatePLangStmt;
***************
*** 2077,2082 ****
--- 2078,2096 ----
} AlterFunctionStmt;
/* ----------------------
+ * Check {Function|Trigger} Statement
+ * ----------------------
+ */
+ typedef struct CheckFunctionStmt
+ {
+ NodeTag type;
+ List *funcname; /* qualified name of checked object */
+ List *args; /* types of the arguments */
+ char *trgname; /* trigger's name */
+ RangeVar *relation; /* trigger's relation */
+ } CheckFunctionStmt;
+
+ /* ----------------------
* DO Statement
*
* DoStmt is the raw parser output, InlineCodeBlock is the execution-time API
*** ./src/pl/plpgsql/src/Makefile.orig 2011-11-24 12:51:45.000000000 +0100
--- ./src/pl/plpgsql/src/Makefile 2011-11-25 13:23:57.605922840 +0100
***************
*** 17,23 ****
SHLIB_LINK = $(filter -lintl, $(LIBS))
rpath =
! OBJS = pl_gram.o pl_handler.o pl_comp.o pl_exec.o pl_funcs.o pl_scanner.o
DATA = plpgsql.control plpgsql--1.0.sql plpgsql--unpackaged--1.0.sql
--- 17,23 ----
SHLIB_LINK = $(filter -lintl, $(LIBS))
rpath =
! OBJS = pl_gram.o pl_handler.o pl_comp.o pl_exec.o pl_funcs.o pl_scanner.o pl_check.o
DATA = plpgsql.control plpgsql--1.0.sql plpgsql--unpackaged--1.0.sql
***************
*** 44,50 ****
# Force these dependencies to be known even without dependency info built:
! pl_gram.o pl_handler.o pl_comp.o pl_exec.o pl_funcs.o pl_scanner.o: plpgsql.h pl_gram.h plerrcodes.h
# See notes in src/backend/parser/Makefile about the following two rules
--- 44,50 ----
# Force these dependencies to be known even without dependency info built:
! pl_gram.o pl_handler.o pl_comp.o pl_exec.o pl_funcs.o pl_scanner.o pl_check.o: plpgsql.h pl_gram.h plerrcodes.h
# See notes in src/backend/parser/Makefile about the following two rules
*** ./src/pl/plpgsql/src/pl_exec.c.orig 2011-11-24 12:51:45.000000000 +0100
--- ./src/pl/plpgsql/src/pl_exec.c 2011-11-25 22:19:41.398342164 +0100
***************
*** 80,86 ****
* Local function forward declarations
************************************************************/
static void plpgsql_exec_error_callback(void *arg);
- static PLpgSQL_datum *copy_plpgsql_datum(PLpgSQL_datum *datum);
static int exec_stmt_block(PLpgSQL_execstate *estate,
PLpgSQL_stmt_block *block);
--- 80,85 ----
***************
*** 133,141 ****
static int exec_stmt_dynfors(PLpgSQL_execstate *estate,
PLpgSQL_stmt_dynfors *stmt);
- static void plpgsql_estate_setup(PLpgSQL_execstate *estate,
- PLpgSQL_function *func,
- ReturnSetInfo *rsi);
static void exec_eval_cleanup(PLpgSQL_execstate *estate);
static void exec_prepare_plan(PLpgSQL_execstate *estate,
--- 132,137 ----
***************
*** 181,190 ****
static ParamListInfo setup_param_list(PLpgSQL_execstate *estate,
PLpgSQL_expr *expr);
static void plpgsql_param_fetch(ParamListInfo params, int paramid);
- static void exec_move_row(PLpgSQL_execstate *estate,
- PLpgSQL_rec *rec,
- PLpgSQL_row *row,
- HeapTuple tup, TupleDesc tupdesc);
static HeapTuple make_tuple_from_row(PLpgSQL_execstate *estate,
PLpgSQL_row *row,
TupleDesc tupdesc);
--- 177,182 ----
***************
*** 201,207 ****
static void exec_init_tuple_store(PLpgSQL_execstate *estate);
static void exec_set_found(PLpgSQL_execstate *estate, bool state);
static void plpgsql_create_econtext(PLpgSQL_execstate *estate);
- static void plpgsql_destroy_econtext(PLpgSQL_execstate *estate);
static void free_var(PLpgSQL_var *var);
static void assign_text_var(PLpgSQL_var *var, const char *str);
static PreparedParamsData *exec_eval_using_params(PLpgSQL_execstate *estate,
--- 193,198 ----
***************
*** 229,235 ****
* Setup the execution state
*/
plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo);
-
/*
* Setup error traceback support for ereport()
*/
--- 220,225 ----
***************
*** 243,249 ****
*/
estate.err_text = gettext_noop("during initialization of execution state");
for (i = 0; i < estate.ndatums; i++)
! estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
/*
* Store the actual call argument values into the appropriate variables
--- 233,239 ----
*/
estate.err_text = gettext_noop("during initialization of execution state");
for (i = 0; i < estate.ndatums; i++)
! estate.datums[i] = plpgsql_copy_datum(func->datums[i]);
/*
* Store the actual call argument values into the appropriate variables
***************
*** 287,299 ****
ItemPointerSetInvalid(&(tmptup.t_self));
tmptup.t_tableOid = InvalidOid;
tmptup.t_data = td;
! exec_move_row(&estate, NULL, row, &tmptup, tupdesc);
ReleaseTupleDesc(tupdesc);
}
else
{
/* If arg is null, treat it as an empty row */
! exec_move_row(&estate, NULL, row, NULL, NULL);
}
}
break;
--- 277,289 ----
ItemPointerSetInvalid(&(tmptup.t_self));
tmptup.t_tableOid = InvalidOid;
tmptup.t_data = td;
! plpgsql_exec_move_row(&estate, NULL, row, &tmptup, tupdesc);
ReleaseTupleDesc(tupdesc);
}
else
{
/* If arg is null, treat it as an empty row */
! plpgsql_exec_move_row(&estate, NULL, row, NULL, NULL);
}
}
break;
***************
*** 514,520 ****
*/
estate.err_text = gettext_noop("during initialization of execution state");
for (i = 0; i < estate.ndatums; i++)
! estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
/*
* Put the OLD and NEW tuples into record variables
--- 504,510 ----
*/
estate.err_text = gettext_noop("during initialization of execution state");
for (i = 0; i < estate.ndatums; i++)
! estate.datums[i] = plpgsql_copy_datum(func->datums[i]);
/*
* Put the OLD and NEW tuples into record variables
***************
*** 832,839 ****
* Support function for initializing local execution variables
* ----------
*/
! static PLpgSQL_datum *
! copy_plpgsql_datum(PLpgSQL_datum *datum)
{
PLpgSQL_datum *result;
--- 822,829 ----
* Support function for initializing local execution variables
* ----------
*/
! PLpgSQL_datum *
! plpgsql_copy_datum(PLpgSQL_datum *datum)
{
PLpgSQL_datum *result;
***************
*** 2854,2860 ****
* Initialize a mostly empty execution state
* ----------
*/
! static void
plpgsql_estate_setup(PLpgSQL_execstate *estate,
PLpgSQL_function *func,
ReturnSetInfo *rsi)
--- 2844,2850 ----
* Initialize a mostly empty execution state
* ----------
*/
! void
plpgsql_estate_setup(PLpgSQL_execstate *estate,
PLpgSQL_function *func,
ReturnSetInfo *rsi)
***************
*** 3170,3176 ****
(errcode(ERRCODE_NO_DATA_FOUND),
errmsg("query returned no rows")));
/* set the target to NULL(s) */
! exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
}
else
{
--- 3160,3166 ----
(errcode(ERRCODE_NO_DATA_FOUND),
errmsg("query returned no rows")));
/* set the target to NULL(s) */
! plpgsql_exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
}
else
{
***************
*** 3179,3185 ****
(errcode(ERRCODE_TOO_MANY_ROWS),
errmsg("query returned more than one row")));
/* Put the first result row into the target */
! exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
}
/* Clean up */
--- 3169,3175 ----
(errcode(ERRCODE_TOO_MANY_ROWS),
errmsg("query returned more than one row")));
/* Put the first result row into the target */
! plpgsql_exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
}
/* Clean up */
***************
*** 3350,3356 ****
(errcode(ERRCODE_NO_DATA_FOUND),
errmsg("query returned no rows")));
/* set the target to NULL(s) */
! exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
}
else
{
--- 3340,3346 ----
(errcode(ERRCODE_NO_DATA_FOUND),
errmsg("query returned no rows")));
/* set the target to NULL(s) */
! plpgsql_exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
}
else
{
***************
*** 3359,3365 ****
(errcode(ERRCODE_TOO_MANY_ROWS),
errmsg("query returned more than one row")));
/* Put the first result row into the target */
! exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
}
}
else
--- 3349,3355 ----
(errcode(ERRCODE_TOO_MANY_ROWS),
errmsg("query returned more than one row")));
/* Put the first result row into the target */
! plpgsql_exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
}
}
else
***************
*** 3630,3638 ****
* ----------
*/
if (n == 0)
! exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
else
! exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
SPI_freetuptable(tuptab);
}
--- 3620,3628 ----
* ----------
*/
if (n == 0)
! plpgsql_exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
else
! plpgsql_exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
SPI_freetuptable(tuptab);
}
***************
*** 3806,3812 ****
if (*isNull)
{
/* If source is null, just assign nulls to the row */
! exec_move_row(estate, NULL, row, NULL, NULL);
}
else
{
--- 3796,3802 ----
if (*isNull)
{
/* If source is null, just assign nulls to the row */
! plpgsql_exec_move_row(estate, NULL, row, NULL, NULL);
}
else
{
***************
*** 3832,3838 ****
ItemPointerSetInvalid(&(tmptup.t_self));
tmptup.t_tableOid = InvalidOid;
tmptup.t_data = td;
! exec_move_row(estate, NULL, row, &tmptup, tupdesc);
ReleaseTupleDesc(tupdesc);
}
break;
--- 3822,3828 ----
ItemPointerSetInvalid(&(tmptup.t_self));
tmptup.t_tableOid = InvalidOid;
tmptup.t_data = td;
! plpgsql_exec_move_row(estate, NULL, row, &tmptup, tupdesc);
ReleaseTupleDesc(tupdesc);
}
break;
***************
*** 3848,3854 ****
if (*isNull)
{
/* If source is null, just assign nulls to the record */
! exec_move_row(estate, rec, NULL, NULL, NULL);
}
else
{
--- 3838,3844 ----
if (*isNull)
{
/* If source is null, just assign nulls to the record */
! plpgsql_exec_move_row(estate, rec, NULL, NULL, NULL);
}
else
{
***************
*** 3875,3881 ****
ItemPointerSetInvalid(&(tmptup.t_self));
tmptup.t_tableOid = InvalidOid;
tmptup.t_data = td;
! exec_move_row(estate, rec, NULL, &tmptup, tupdesc);
ReleaseTupleDesc(tupdesc);
}
break;
--- 3865,3871 ----
ItemPointerSetInvalid(&(tmptup.t_self));
tmptup.t_tableOid = InvalidOid;
tmptup.t_data = td;
! plpgsql_exec_move_row(estate, rec, NULL, &tmptup, tupdesc);
ReleaseTupleDesc(tupdesc);
}
break;
***************
*** 4730,4736 ****
* through with found = false.
*/
if (n <= 0)
! exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
else
found = true; /* processed at least one tuple */
--- 4720,4726 ----
* through with found = false.
*/
if (n <= 0)
! plpgsql_exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
else
found = true; /* processed at least one tuple */
***************
*** 4746,4752 ****
/*
* Assign the tuple to the target
*/
! exec_move_row(estate, rec, row, tuptab->vals[i], tuptab->tupdesc);
/*
* Execute the statements
--- 4736,4742 ----
/*
* Assign the tuple to the target
*/
! plpgsql_exec_move_row(estate, rec, row, tuptab->vals[i], tuptab->tupdesc);
/*
* Execute the statements
***************
*** 5137,5147 ****
/* ----------
! * exec_move_row Move one tuple's values into a record or row
* ----------
*/
! static void
! exec_move_row(PLpgSQL_execstate *estate,
PLpgSQL_rec *rec,
PLpgSQL_row *row,
HeapTuple tup, TupleDesc tupdesc)
--- 5127,5137 ----
/* ----------
! * plpgsql_exec_move_row Move one tuple's values into a record or row
* ----------
*/
! void
! plpgsql_exec_move_row(PLpgSQL_execstate *estate,
PLpgSQL_rec *rec,
PLpgSQL_row *row,
HeapTuple tup, TupleDesc tupdesc)
***************
*** 5916,5922 ****
* We check that it matches the top stack entry, and destroy the stack
* entry along with the context.
*/
! static void
plpgsql_destroy_econtext(PLpgSQL_execstate *estate)
{
SimpleEcontextStackEntry *next;
--- 5906,5912 ----
* We check that it matches the top stack entry, and destroy the stack
* entry along with the context.
*/
! void
plpgsql_destroy_econtext(PLpgSQL_execstate *estate)
{
SimpleEcontextStackEntry *next;
*** ./src/pl/plpgsql/src/pl_handler.c.orig 2011-11-25 12:04:21.661586925 +0100
--- ./src/pl/plpgsql/src/pl_handler.c 2011-11-26 08:54:10.153356791 +0100
***************
*** 312,314 ****
--- 312,452 ----
PG_RETURN_VOID();
}
+
+ /* ----------
+ * plpgsql_checker
+ *
+ * This function attempts to check a embeded SQL inside a PL/pgSQL function at
+ * CHECK FUNCTION time. It should to have one or two parameters. Second
+ * parameter is a relation (used when function is trigger).
+ * ----------
+ */
+ PG_FUNCTION_INFO_V1(plpgsql_checker);
+
+ Datum
+ plpgsql_checker(PG_FUNCTION_ARGS)
+ {
+ Oid funcoid = PG_GETARG_OID(0);
+ Oid relid = PG_GETARG_OID(1);
+ HeapTuple tuple;
+ FunctionCallInfoData fake_fcinfo;
+ FmgrInfo flinfo;
+ TriggerData trigdata;
+ int rc;
+ PLpgSQL_function *function;
+ PLpgSQL_execstate *cur_estate;
+
+ Form_pg_proc proc;
+ char functyptype;
+ bool istrigger = false;
+
+ /* we don't need to repair a check done by validator */
+
+ tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
+ if (!HeapTupleIsValid(tuple))
+ elog(ERROR, "cache lookup failed for function %u", funcoid);
+ proc = (Form_pg_proc) GETSTRUCT(tuple);
+
+ functyptype = get_typtype(proc->prorettype);
+
+ if (functyptype == TYPTYPE_PSEUDO)
+ {
+ /* we assume OPAQUE with no arguments means a trigger */
+ if (proc->prorettype == TRIGGEROID ||
+ (proc->prorettype == OPAQUEOID && proc->pronargs == 0))
+ {
+ istrigger = true;
+ if (!OidIsValid(relid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("PL/pgSQL trigger functions cannot be checked directly"),
+ errhint("use CHECK TRIGGER statement instead")));
+ }
+ }
+
+ /*
+ * Connect to SPI manager
+ */
+ if ((rc = SPI_connect()) != SPI_OK_CONNECT)
+ elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
+
+ /*
+ * Set up a fake fcinfo with just enough info to satisfy
+ * plpgsql_compile().
+ *
+ * there should be a different real argtypes for polymorphic params
+ */
+ MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo));
+ MemSet(&flinfo, 0, sizeof(flinfo));
+ fake_fcinfo.flinfo = &flinfo;
+ flinfo.fn_oid = funcoid;
+ flinfo.fn_mcxt = CurrentMemoryContext;
+
+ if (istrigger)
+ {
+ MemSet(&trigdata, 0, sizeof(trigdata));
+ trigdata.type = T_TriggerData;
+ trigdata.tg_relation = relation_open(relid, AccessShareLock);
+ fake_fcinfo.context = (Node *) &trigdata;
+ }
+
+ /* Get a compiled function */
+ function = plpgsql_compile(&fake_fcinfo, false);
+
+ /* Must save and restore prior value of cur_estate */
+ cur_estate = function->cur_estate;
+
+ /* Mark the function as busy, so it can't be deleted from under us */
+ function->use_count++;
+
+
+ /* Create a fake runtime environment and prepare plans */
+ PG_TRY();
+ {
+ if (!istrigger)
+ plpgsql_check_function(function, &fake_fcinfo);
+ else
+ plpgsql_check_trigger(function, &trigdata);
+ }
+ PG_CATCH();
+ {
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ /*
+ * Disconnect from SPI manager
+ */
+ if ((rc = SPI_finish()) != SPI_OK_FINISH)
+ elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
+
+ ReleaseSysCache(tuple);
+
+ PG_RETURN_VOID();
+ }
*** ./src/pl/plpgsql/src/pl_check.c.orig 2011-11-25 13:07:08.000000000 +0100
--- ./src/pl/plpgsql/src/pl_check.c 2011-11-26 09:15:17.010967364 +0100
***************
*** 0 ****
--- 1,1091 ----
+ /*-------------------------------------------------------------------------
+ *
+ * pl_check.c - Checker part of the PL/pgSQL
+ * procedural language
+ *
+ * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/pl/plpgsql/src/pl_check.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+ #include "plpgsql.h"
+
+ #include "funcapi.h"
+ #include "catalog/pg_type.h"
+ #include "executor/spi_priv.h"
+ #include "utils/builtins.h"
+ #include "utils/lsyscache.h"
+ #include "utils/rel.h"
+ #include "utils/typcache.h"
+
+ static void check_target(PLpgSQL_execstate *estate, int varno);
+ static void check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec);
+ static void prepare_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr, int cursorOptions);
+ static void check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr);
+ static void assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
+ PLpgSQL_row *row, PLpgSQL_rec *rec,
+ TupleDesc tupdesc);
+ static void assign_tupdesc_dno(PLpgSQL_execstate *estate, int varno, TupleDesc tupdesc);
+ static TupleDesc
+ expr_get_desc(PLpgSQL_execstate *estate, PLpgSQL_expr *query,
+ bool use_element_type,
+ bool expand_record,
+ bool is_expression);
+
+ static void check_stmts(PLpgSQL_execstate *estate, List *stmts);
+ static void check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
+
+ static void var_init_to_null(PLpgSQL_execstate *estate, int varno);
+
+
+ /*
+ * append a CONTEXT to error message
+ */
+ static void
+ check_error_callback(void *arg)
+ {
+ PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;
+
+ if (estate->err_stmt != NULL)
+ {
+ /* translator: last %s is a plpgsql statement type name */
+ errcontext("checking of PL/pgSQL function \"%s\" line %d at %s",
+ estate->func->fn_name,
+ estate->err_stmt->lineno,
+ plpgsql_stmt_typename(estate->err_stmt));
+ }
+ else
+ errcontext("checking of PL/pgSQL function \"%s\"",
+ estate->func->fn_name);
+ }
+
+ /*
+ * Check function - it prepare variables and starts a prepare plan walker
+ * called by function checker
+ */
+ void
+ plpgsql_check_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = plpgsql_copy_datum(func->datums[i]);
+
+ /*
+ * Store the actual call argument values into the appropriate variables
+ */
+ for (i = 0; i < func->fn_nargs; i++)
+ {
+ int n = func->fn_argvarnos[i];
+
+ switch (estate.datums[n]->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ {
+ var_init_to_null(&estate, n);
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ {
+ PLpgSQL_row *row = (PLpgSQL_row *) estate.datums[n];
+
+ plpgsql_exec_move_row(&estate, NULL, row, NULL, NULL);
+ }
+ break;
+
+ default:
+ elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
+ }
+ }
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Check trigger - prepare fake environments for testing trigger
+ *
+ */
+ void
+ plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ PLpgSQL_rec *rec_new,
+ *rec_old;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, NULL);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = plpgsql_copy_datum(func->datums[i]);
+
+ /*
+ * Put the OLD and NEW tuples into record variables
+ *
+ * We make the tupdescs available in both records even though only one may
+ * have a value. This allows parsing of record references to succeed in
+ * functions that are used for multiple trigger types. For example, we
+ * might have a test like "if (TG_OP = 'INSERT' and NEW.foo = 'xyz')",
+ * which should parse regardless of the current trigger type.
+ */
+ rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
+ rec_new->freetup = false;
+ rec_new->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_new, trigdata->tg_relation->rd_att);
+
+ rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
+ rec_old->freetup = false;
+ rec_old->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_old, trigdata->tg_relation->rd_att);
+
+ /*
+ * Assign the special tg_ variables
+ */
+ var_init_to_null(&estate, func->tg_op_varno);
+ var_init_to_null(&estate, func->tg_name_varno);
+ var_init_to_null(&estate, func->tg_when_varno);
+ var_init_to_null(&estate, func->tg_level_varno);
+ var_init_to_null(&estate, func->tg_relid_varno);
+ var_init_to_null(&estate, func->tg_relname_varno);
+ var_init_to_null(&estate, func->tg_table_name_varno);
+ var_init_to_null(&estate, func->tg_table_schema_varno);
+ var_init_to_null(&estate, func->tg_nargs_varno);
+ var_init_to_null(&estate, func->tg_argv_varno);
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Verify lvalue
+ * It doesn't repeat a checks that are done.
+ * Checks a subscript expressions, verify a validity of record's fields
+ */
+ static void
+ check_target(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ switch (target->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ case PLPGSQL_DTYPE_REC:
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ check_row_or_rec(estate, (PLpgSQL_row *) target, NULL);
+ break;
+
+ case PLPGSQL_DTYPE_RECFIELD:
+ {
+ PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
+ PLpgSQL_rec *rec;
+ int fno;
+
+ rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
+
+ /*
+ * Check that there is already a tuple in the record. We need
+ * that because records don't have any predefined field
+ * structure.
+ */
+ if (!HeapTupleIsValid(rec->tup))
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("record \"%s\" is not assigned to tuple structure",
+ rec->refname)));
+
+ /*
+ * Get the number of the records field to change and the
+ * number of attributes in the tuple. Note: disallow system
+ * column names because the code below won't cope.
+ */
+ fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
+ if (fno <= 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("record \"%s\" has no field \"%s\"",
+ rec->refname, recfield->fieldname)));
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ARRAYELEM:
+ {
+ /*
+ * Target is an element of an array
+ */
+ int nsubscripts;
+ Oid arrayelemtypeid;
+ Oid arraytypeid;
+
+ /*
+ * To handle constructs like x[1][2] := something, we have to
+ * be prepared to deal with a chain of arrayelem datums. Chase
+ * back to find the base array datum, and save the subscript
+ * expressions as we go. (We are scanning right to left here,
+ * but want to evaluate the subscripts left-to-right to
+ * minimize surprises.)
+ */
+ nsubscripts = 0;
+ do
+ {
+ PLpgSQL_arrayelem *arrayelem = (PLpgSQL_arrayelem *) target;
+
+ if (nsubscripts++ >= MAXDIM)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
+ nsubscripts + 1, MAXDIM)));
+
+ check_expr(estate, arrayelem->subscript);
+
+ target = estate->datums[arrayelem->arrayparentno];
+ } while (target->dtype == PLPGSQL_DTYPE_ARRAYELEM);
+
+ /* If target is domain over array, reduce to base type */
+ arraytypeid = exec_get_datum_type(estate, target);
+ arraytypeid = getBaseType(arraytypeid);
+
+ arrayelemtypeid = get_element_type(arraytypeid);
+
+ if (!OidIsValid(arrayelemtypeid))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("subscripted object is not an array")));
+ }
+ break;
+ }
+ }
+
+ /*
+ * Check composed lvalue
+ * There is nothing to check on rec variables
+ */
+ static void
+ check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec)
+ {
+ int fnum;
+
+ /* there are nothing to check on rec now */
+ if (row != NULL)
+ {
+ for (fnum = 0; fnum < row->nfields; fnum++)
+ {
+ /* skip dropped columns */
+ if (row->varnos[fnum] < 0)
+ continue;
+
+ check_target(estate, row->varnos[fnum]);
+ }
+ }
+ }
+
+ /*
+ * Generate a prepared plan - this is simplyfied copy from pl_exec.c
+ * Is not necessary to check simple plan
+ */
+ static void
+ prepare_expr(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *expr, int cursorOptions)
+ {
+ SPIPlanPtr plan;
+
+ /* leave when there are not expression */
+ if (expr == NULL)
+ return;
+
+ /* leave when plan is created */
+ if (expr->plan != NULL)
+ return;
+
+ /*
+ * The grammar can't conveniently set expr->func while building the parse
+ * tree, so make sure it's set before parser hooks need it.
+ */
+ expr->func = estate->func;
+
+ /*
+ * Generate and save the plan
+ */
+ plan = SPI_prepare_params(expr->query,
+ (ParserSetupHook) plpgsql_parser_setup,
+ (void *) expr,
+ cursorOptions);
+ if (plan == NULL)
+ {
+ /* Some SPI errors deserve specific error messages */
+ switch (SPI_result)
+ {
+ case SPI_ERROR_COPY:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot COPY to/from client in PL/pgSQL")));
+ case SPI_ERROR_TRANSACTION:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot begin/end transactions in PL/pgSQL"),
+ errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
+ default:
+ elog(ERROR, "SPI_prepare_params failed for \"%s\": %s",
+ expr->query, SPI_result_code_string(SPI_result));
+ }
+ }
+
+ expr->plan = SPI_saveplan(plan);
+ SPI_freeplan(plan);
+ }
+
+ /*
+ * Verify a expression
+ */
+ static void
+ check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
+ {
+ TupleDesc tupdesc;
+
+ if (expr != NULL)
+ {
+ prepare_expr(estate, expr, 0);
+ tupdesc = expr_get_desc(estate, expr, false, false, true);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+
+ /*
+ * We have to assign TupleDesc to all used record variables step by step.
+ * We would to use a exec routines for query preprocessing, so we must
+ * to create a typed NULL value, and this value is assigned to record
+ * variable.
+ */
+ static void
+ assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
+ PLpgSQL_row *row, PLpgSQL_rec *rec,
+ TupleDesc tupdesc)
+ {
+ bool *nulls;
+ HeapTuple tup;
+
+ if (tupdesc == NULL)
+ elog(ERROR, "tuple descriptor is empty");
+
+ /*
+ * row variable has assigned TupleDesc already, so don't be processed
+ * here
+ */
+ if (rec != NULL)
+ {
+ PLpgSQL_rec *target = (PLpgSQL_rec *)(estate->datums[rec->dno]);
+
+ if (target->freetup)
+ heap_freetuple(target->tup);
+
+ if (rec->freetupdesc)
+ FreeTupleDesc(target->tupdesc);
+
+ /* initialize rec by NULLs */
+ nulls = (bool *) palloc(tupdesc->natts * sizeof(bool));
+ memset(nulls, true, tupdesc->natts * sizeof(bool));
+
+ target->tupdesc = CreateTupleDescCopy(tupdesc);
+ target->freetupdesc = true;
+
+ tup = heap_form_tuple(tupdesc, NULL, nulls);
+ if (HeapTupleIsValid(tup))
+ {
+ target->tup = tup;
+ target->freetup = true;
+ }
+ else
+ elog(ERROR, "cannot to build valid composite value");
+ }
+ }
+
+ /*
+ * Assign a tuple descriptor to variable specified by dno
+ */
+ static void
+ assign_tupdesc_dno(PLpgSQL_execstate *estate, int varno, TupleDesc tupdesc)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ if (target->dtype == PLPGSQL_DTYPE_REC)
+ assign_tupdesc_row_or_rec(estate, NULL, (PLpgSQL_rec *) target, tupdesc);
+ }
+
+ /*
+ * Returns a tuple descriptor based on existing plan
+ */
+ static TupleDesc
+ expr_get_desc(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *query,
+ bool use_element_type,
+ bool expand_record,
+ bool is_expression)
+ {
+ TupleDesc tupdesc = NULL;
+ CachedPlanSource *plansource = NULL;
+
+ if (query->plan != NULL)
+ {
+ SPIPlanPtr plan = query->plan;
+
+ if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
+ elog(ERROR, "cached plan is not valid plan");
+
+ if (list_length(plan->plancache_list) != 1)
+ elog(ERROR, "plan is not single execution plan");
+
+ plansource = (CachedPlanSource *) linitial(plan->plancache_list);
+
+ tupdesc = CreateTupleDescCopy(plansource->resultDesc);
+ }
+ else
+ elog(ERROR, "there are no plan for query: \"%s\"",
+ query->query);
+
+ /*
+ * try to get a element type, when result is a array (used with FOREACH ARRAY stmt)
+ */
+ if (use_element_type)
+ {
+ Oid elemtype;
+ TupleDesc elemtupdesc;
+
+ /* result should be a array */
+ if (tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /* check the type of the expression - must be an array */
+ elemtype = get_element_type(tupdesc->attrs[0]->atttypid);
+ if (!OidIsValid(elemtype))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("FOREACH expression must yield an array, not type %s",
+ format_type_be(tupdesc->attrs[0]->atttypid))));
+
+ /* we can't know typmod now */
+ elemtupdesc = lookup_rowtype_tupdesc_noerror(elemtype, -1, true);
+ if (elemtupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(elemtupdesc);
+ ReleaseTupleDesc(elemtupdesc);
+ }
+ else
+ elog(ERROR, "cannot to identify real type for record type variable");
+ }
+
+ if (is_expression && tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /*
+ * One spacial case is when record is assigned to composite type, then
+ * we should to unpack composite type.
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 && expand_record)
+ {
+ TupleDesc unpack_tupdesc;
+
+ unpack_tupdesc = lookup_rowtype_tupdesc_noerror(tupdesc->attrs[0]->atttypid,
+ tupdesc->attrs[0]->atttypmod,
+ true);
+ if (unpack_tupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(unpack_tupdesc);
+ ReleaseTupleDesc(unpack_tupdesc);
+ }
+ }
+
+ /*
+ * There is special case, when returned tupdesc contains only
+ * unpined record: rec := func_with_out_parameters(). IN this case
+ * we must to dig more deep - we have to find oid of function and
+ * get their parameters,
+ *
+ * This is support for assign statement
+ * recvar := func_with_out_parameters(..)
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 &&
+ tupdesc->attrs[0]->atttypid == RECORDOID &&
+ tupdesc->attrs[0]->atttypmod == -1 &&
+ expand_record)
+ {
+ PlannedStmt *_stmt;
+ Plan *_plan;
+ TargetEntry *tle;
+ CachedPlan *cplan;
+
+ /*
+ * When tupdesc is related to unpined record, we will try
+ * to check plan if it is just function call and if it is
+ * then we can try to derive a tupledes from function's
+ * description.
+ */
+ cplan = GetCachedPlan(plansource, NULL, true);
+ _stmt = (PlannedStmt *) linitial(cplan->stmt_list);
+
+ if (IsA(_stmt, PlannedStmt) && _stmt->commandType == CMD_SELECT)
+ {
+ _plan = _stmt->planTree;
+ if (IsA(_plan, Result) && list_length(_plan->targetlist) == 1)
+ {
+ tle = (TargetEntry *) linitial(_plan->targetlist);
+ if (((Node *) tle->expr)->type == T_FuncExpr)
+ {
+ FuncExpr *fn = (FuncExpr *) tle->expr;
+ FmgrInfo flinfo;
+ FunctionCallInfoData fcinfo;
+ TupleDesc rd;
+ Oid rt;
+
+ fmgr_info(fn->funcid, &flinfo);
+ flinfo.fn_expr = (Node *) fn;
+ fcinfo.flinfo = &flinfo;
+
+ get_call_result_type(&fcinfo, &rt, &rd);
+ if (rd == NULL)
+ elog(ERROR, "function does not return composite type is not possible to identify composite type");
+
+ FreeTupleDesc(tupdesc);
+ BlessTupleDesc(rd);
+
+ tupdesc = rd;
+ }
+ }
+ }
+
+ ReleaseCachedPlan(cplan, true);
+ }
+
+ return tupdesc;
+ }
+
+ /*
+ * Ensure check for all statements in list
+ */
+ static void
+ check_stmts(PLpgSQL_execstate *estate, List *stmts)
+ {
+ ListCell *lc;
+
+ foreach(lc, stmts)
+ {
+ check_stmt(estate, (PLpgSQL_stmt *) lfirst(lc));
+ }
+ }
+
+ /*
+ * walk over all statements
+ */
+ static void
+ check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
+ {
+ TupleDesc tupdesc = NULL;
+ PLpgSQL_function *func;
+ ListCell *l;
+
+ if (stmt == NULL)
+ return;
+
+ estate->err_stmt = stmt;
+ func = estate->func;
+
+ switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
+ {
+ case PLPGSQL_STMT_BLOCK:
+ {
+ PLpgSQL_stmt_block *stmt_block = (PLpgSQL_stmt_block *) stmt;
+ int i;
+ PLpgSQL_datum *d;
+
+ for (i = 0; i < stmt_block->n_initvars; i++)
+ {
+ d = func->datums[stmt_block->initvarnos[i]];
+
+ if (d->dtype == PLPGSQL_DTYPE_VAR)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) d;
+
+ check_expr(estate, var->default_val);
+ }
+ }
+
+ check_stmts(estate, stmt_block->body);
+
+ if (stmt_block->exceptions)
+ {
+ foreach(l, stmt_block->exceptions->exc_list)
+ {
+ check_stmts(estate, ((PLpgSQL_exception *) lfirst(l))->action);
+ }
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_ASSIGN:
+ {
+ PLpgSQL_stmt_assign *stmt_assign = (PLpgSQL_stmt_assign *) stmt;
+
+ /* prepare plan if desn't exist yet */
+ prepare_expr(estate, stmt_assign->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_assign->expr,
+ false, /* no element type */
+ true, /* expand record */
+ true); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_target(estate, stmt_assign->varno);
+
+ /* assign a tupdesc to record variable */
+ assign_tupdesc_dno(estate, stmt_assign->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_IF:
+ {
+ PLpgSQL_stmt_if *stmt_if = (PLpgSQL_stmt_if *) stmt;
+ ListCell *l;
+
+ check_expr(estate, stmt_if->cond);
+
+ check_stmts(estate, stmt_if->then_body);
+
+ foreach(l, stmt_if->elsif_list)
+ {
+ PLpgSQL_if_elsif *elif = (PLpgSQL_if_elsif *) lfirst(l);
+
+ check_expr(estate, elif->cond);
+ check_stmts(estate, elif->stmts);
+ }
+
+ check_stmts(estate, stmt_if->else_body);
+ }
+ break;
+
+ case PLPGSQL_STMT_CASE:
+ {
+ PLpgSQL_stmt_case *stmt_case = (PLpgSQL_stmt_case *) stmt;
+ Oid result_oid;
+
+ if (stmt_case->t_expr != NULL)
+ {
+ PLpgSQL_var *t_var = (PLpgSQL_var *) estate->datums[stmt_case->t_varno];
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_case->t_expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_case->t_expr,
+ false, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ result_oid = tupdesc->attrs[0]->atttypid;
+
+ /*
+ * When expected datatype is different from real, change it. Note that
+ * what we're modifying here is an execution copy of the datum, so
+ * this doesn't affect the originally stored function parse tree.
+ */
+
+ if (t_var->datatype->typoid != result_oid)
+ t_var->datatype = plpgsql_build_datatype(result_oid,
+ -1,
+ estate->func->fn_input_collation);
+
+ ReleaseTupleDesc(tupdesc);
+ }
+
+ foreach(l, stmt_case->case_when_list)
+ {
+ PLpgSQL_case_when *cwt = (PLpgSQL_case_when *) lfirst(l);
+
+ check_expr(estate, cwt->expr);
+ check_stmts(estate, cwt->stmts);
+ }
+
+ check_stmts(estate, stmt_case->else_stmts);
+ }
+ break;
+
+ case PLPGSQL_STMT_LOOP:
+ check_stmts(estate, ((PLpgSQL_stmt_loop *) stmt)->body);
+ break;
+
+ case PLPGSQL_STMT_WHILE:
+ {
+ PLpgSQL_stmt_while *stmt_while = (PLpgSQL_stmt_while *) stmt;
+
+ check_expr(estate, stmt_while->cond);
+ check_stmts(estate, stmt_while->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORI:
+ {
+ PLpgSQL_stmt_fori *stmt_fori = (PLpgSQL_stmt_fori *) stmt;
+
+ check_expr(estate, stmt_fori->lower);
+ check_expr(estate, stmt_fori->upper);
+ check_expr(estate, stmt_fori->step);
+
+ check_stmts(estate, stmt_fori->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORS:
+ {
+ PLpgSQL_stmt_fors *stmt_fors = (PLpgSQL_stmt_fors *) stmt;
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_fors->query, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_fors->query,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_fors->row, stmt_fors->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fors->row, stmt_fors->rec, tupdesc);
+
+ check_stmts(estate, stmt_fors->body);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORC:
+ {
+ PLpgSQL_stmt_forc *stmt_forc = (PLpgSQL_stmt_forc *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_forc->curvar];
+
+ prepare_expr(estate, stmt_forc->argquery, 0);
+
+ if (var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_forc->row, stmt_forc->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_forc->row, stmt_forc->rec, tupdesc);
+ }
+
+ check_stmts(estate, stmt_forc->body);
+ if (tupdesc != NULL)
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNFORS:
+ {
+ PLpgSQL_stmt_dynfors * stmt_dynfors = (PLpgSQL_stmt_dynfors *) stmt;
+
+ if (stmt_dynfors->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_expr(estate, stmt_dynfors->query);
+
+ foreach(l, stmt_dynfors->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ check_stmts(estate, stmt_dynfors->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FOREACH_A:
+ {
+ PLpgSQL_stmt_foreach_a *stmt_foreach_a = (PLpgSQL_stmt_foreach_a *) stmt;
+
+ prepare_expr(estate, stmt_foreach_a->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_foreach_a->expr,
+ true, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ check_target(estate, stmt_foreach_a->varno);
+ assign_tupdesc_dno(estate, stmt_foreach_a->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+
+ check_stmts(estate, stmt_foreach_a->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_EXIT:
+ check_expr(estate, ((PLpgSQL_stmt_exit *) stmt)->cond);
+ break;
+
+ case PLPGSQL_STMT_PERFORM:
+ prepare_expr(estate, ((PLpgSQL_stmt_perform *) stmt)->expr, 0);
+ break;
+
+ case PLPGSQL_STMT_RETURN:
+ check_expr(estate, ((PLpgSQL_stmt_return *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_NEXT:
+ check_expr(estate, ((PLpgSQL_stmt_return_next *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_QUERY:
+ {
+ PLpgSQL_stmt_return_query *stmt_rq = (PLpgSQL_stmt_return_query *) stmt;
+
+ check_expr(estate, stmt_rq->dynquery);
+ prepare_expr(estate, stmt_rq->query, 0);
+
+ foreach(l, stmt_rq->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_RAISE:
+ {
+ PLpgSQL_stmt_raise *stmt_raise = (PLpgSQL_stmt_raise *) stmt;
+ ListCell *current_param;
+ char *cp;
+
+ foreach(l, stmt_raise->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ foreach(l, stmt_raise->options)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ current_param = list_head(stmt_raise->params);
+
+ /* ensure any single % has a own parameter */
+ if (stmt_raise->message != NULL)
+ {
+ for (cp = stmt_raise->message; *cp; cp++)
+ {
+ if (cp[0] == '%')
+ {
+ if (cp[1] == '%')
+ {
+ cp++;
+ continue;
+ }
+
+ if (current_param == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too few parameters specified for RAISE")));
+
+ current_param = lnext(current_param);
+ }
+ }
+ }
+
+ if (current_param != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too many parameters specified for RAISE")));
+ }
+ break;
+
+ case PLPGSQL_STMT_EXECSQL:
+ {
+ PLpgSQL_stmt_execsql *stmt_execsql = (PLpgSQL_stmt_execsql *) stmt;
+
+ prepare_expr(estate, stmt_execsql->sqlstmt, 0);
+ if (stmt_execsql->into)
+ {
+ tupdesc = expr_get_desc(estate,
+ stmt_execsql->sqlstmt,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNEXECUTE:
+ {
+ PLpgSQL_stmt_dynexecute *stmt_dynexecute = (PLpgSQL_stmt_dynexecute *) stmt;
+
+ check_expr(estate, stmt_dynexecute->query);
+
+ foreach(l, stmt_dynexecute->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ if (stmt_dynexecute->into)
+ {
+ if (stmt_dynexecute->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_row_or_rec(estate, stmt_dynexecute->row, stmt_dynexecute->rec);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_OPEN:
+ {
+ PLpgSQL_stmt_open *stmt_open = (PLpgSQL_stmt_open *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_open->curvar];
+
+ if (var->cursor_explicit_expr)
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ prepare_expr(estate, stmt_open->query, 0);
+ prepare_expr(estate, stmt_open->argquery, 0);
+ check_expr(estate, stmt_open->dynquery);
+
+ foreach(l, stmt_open->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_GETDIAG:
+ {
+ PLpgSQL_stmt_getdiag *stmt_getdiag = (PLpgSQL_stmt_getdiag *) stmt;
+ ListCell *lc;
+
+ foreach(lc, stmt_getdiag->diag_items)
+ {
+ PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
+
+ check_target(estate, diag_item->target);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_FETCH:
+ {
+ PLpgSQL_stmt_fetch *stmt_fetch = (PLpgSQL_stmt_fetch *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *)(estate->datums[stmt_fetch->curvar]);
+
+ if (var != NULL && var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+ check_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_CLOSE:
+ break;
+
+ default:
+ elog(ERROR, "unrecognized cmd_type: %d", stmt->cmd_type);
+ return; /* be compiler quite */
+ }
+ }
+
+ /*
+ * Initialize variable to NULL
+ */
+ static void
+ var_init_to_null(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) estate->datums[varno];
+ var->value = (Datum) 0;
+ var->isnull = true;
+ var->freeval = false;
+ }
*** ./src/pl/plpgsql/src/plpgsql.h.orig 2011-11-24 12:51:45.000000000 +0100
--- ./src/pl/plpgsql/src/plpgsql.h 2011-11-25 22:45:14.167881352 +0100
***************
*** 901,906 ****
--- 901,907 ----
extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
extern void plpgsql_adddatum(PLpgSQL_datum *new);
extern int plpgsql_add_initdatums(int **varnos);
+ extern void plpgsql_delete_function(PLpgSQL_function *func);
extern void plpgsql_HashTableInit(void);
/* ----------
***************
*** 911,921 ****
--- 912,931 ----
extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_inline_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
+ extern Datum plpgsql_checker(PG_FUNCTION_ARGS);
+
+ /* ----------
+ * Functions in pl_check.c
+ * ----------
+ */
+ extern void plpgsql_check_function(PLpgSQL_function *func, FunctionCallInfo fcinfo);
+ extern void plpgsql_check_trigger(PLpgSQL_function *func, TriggerData *trigdata);
/* ----------
* Functions in pl_exec.c
* ----------
*/
+ extern PLpgSQL_datum *plpgsql_copy_datum(PLpgSQL_datum *datum);
extern Datum plpgsql_exec_function(PLpgSQL_function *func,
FunctionCallInfo fcinfo);
extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
***************
*** 928,933 ****
--- 938,952 ----
extern void exec_get_datum_type_info(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum,
Oid *typeid, int32 *typmod, Oid *collation);
+ extern bool plpgsql_expr_prepare_plan(PLpgSQL_stmt *stmt, PLpgSQL_expr *expr, void *context);
+ extern void plpgsql_estate_setup(PLpgSQL_execstate *estate,
+ PLpgSQL_function *func,
+ ReturnSetInfo *rsi);
+ extern void plpgsql_exec_move_row(PLpgSQL_execstate *estate,
+ PLpgSQL_rec *rec,
+ PLpgSQL_row *row,
+ HeapTuple tup, TupleDesc tupdesc);
+ extern void plpgsql_destroy_econtext(PLpgSQL_execstate *estate);
/* ----------
* Functions for namespace handling in pl_funcs.c
*** ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql.orig 2011-11-25 12:04:21.675586770 +0100
--- ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql 2011-11-25 12:05:31.895783910 +0100
***************
*** 5,7 ****
--- 5,8 ----
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_call_handler();
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_inline_handler(internal);
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_validator(oid);
+ ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_checker(oid, regclass);
*** ./src/test/regress/expected/plpgsql.out.orig 2011-11-24 12:51:45.000000000 +0100
--- ./src/test/regress/expected/plpgsql.out 2011-11-26 09:15:55.000000000 +0100
***************
*** 302,307 ****
--- 302,310 ----
' language plpgsql;
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
+ NOTICE: checking function "tg_hslot_biu()"
-- ************************************************************
-- * BEFORE DELETE on HSlot
-- * - prevent from manual manipulation
***************
*** 635,640 ****
--- 638,645 ----
raise exception ''illegal backlink beginning with %'', mytype;
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
-- ************************************************************
-- * Support function to clear out the backlink field if
-- * it still points to specific slot
***************
*** 2802,2807 ****
--- 2807,2840 ----
(1 row)
+ -- check function should not fail
+ check function for_vect();
+ -- recheck after check function
+ select for_vect();
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 4
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1 bb cc
+ NOTICE: 2 bb cc
+ NOTICE: 3 bb cc
+ NOTICE: 4 bb cc
+ for_vect
+ ----------
+
+ (1 row)
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 3283,3288 ****
--- 3316,3323 ----
return;
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
select forc01();
NOTICE: 5 from c
NOTICE: 6 from c
***************
*** 3716,3721 ****
--- 3751,3758 ----
end case;
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
select case_test(1);
case_test
-----------
***************
*** 4571,4573 ****
--- 4608,4942 ----
CONTEXT: PL/pgSQL function "testoa" line 5 at assignment
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+ --
+ -- check function statement tests
+ --
+ create table t1(a int, b int);
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: column "c" of relation "t1" does not exist
+ LINE 1: update t1 set c = 30
+ ^
+ QUERY: update t1 set c = 30
+ CONTEXT: checking of PL/pgSQL function "f1" line 4 at SQL statement
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ checking of PL/pgSQL function "f1" line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ checking of PL/pgSQL function "f1" line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: record "r" has no field "c"
+ CONTEXT: checking of PL/pgSQL function "f1" line 6 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: column "a" does not exist
+ LINE 1: SELECT a + b
+ ^
+ QUERY: SELECT a + b
+ CONTEXT: checking of PL/pgSQL function "f1" line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: too many parameters specified for RAISE
+ CONTEXT: checking of PL/pgSQL function "f1" line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: too few parameters specified for RAISE
+ CONTEXT: checking of PL/pgSQL function "f1" line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: column "c" does not exist
+ LINE 1: SELECT c+10
+ ^
+ QUERY: SELECT c+10
+ CONTEXT: checking of PL/pgSQL function "f1" line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: subscripted object is not an array
+ CONTEXT: checking of PL/pgSQL function "f1" line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: record "_exception" has no field "hint"
+ CONTEXT: checking of PL/pgSQL function "f1" line 7 at GET DIAGNOSTICS
+ drop function f1();
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ NOTICE: checking function "f1_trg()"
+ ERROR: record "new" has no field "c"
+ CONTEXT: SQL statement "SELECT new.c"
+ checking of PL/pgSQL function "f1_trg" line 5 at RAISE
+ insert into t1 values(6,30);
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- should to fail
+ check trigger t1_f1 on t1;
+ NOTICE: checking function "f1_trg()"
+ ERROR: record "new" has no field "c"
+ CONTEXT: checking of PL/pgSQL function "f1_trg" line 5 at assignment
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+ ERROR: record "new" has no field "c"
+ CONTEXT: PL/pgSQL function "f1_trg" line 5 at assignment
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- ok
+ check trigger t1_f1 on t1;
+ NOTICE: checking function "f1_trg()"
+ -- ok
+ insert into t1 values(6,30);
+ drop table t1;
+ drop type _exception_type;
+ drop function f1_trg();
*** ./src/test/regress/sql/plpgsql.sql.orig 2011-11-24 12:51:45.000000000 +0100
--- ./src/test/regress/sql/plpgsql.sql 2011-11-26 09:01:01.346078538 +0100
***************
*** 366,371 ****
--- 366,373 ----
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
-- ************************************************************
-- * BEFORE DELETE on HSlot
***************
*** 747,752 ****
--- 749,757 ----
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
+
-- ************************************************************
-- * Support function to clear out the backlink field if
***************
*** 2335,2340 ****
--- 2340,2352 ----
select for_vect();
+ -- check function should not fail
+ check function for_vect();
+
+ -- recheck after check function
+ select for_vect();
+
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 2714,2719 ****
--- 2726,2734 ----
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
+
select forc01();
-- try updating the cursor's current row
***************
*** 3048,3053 ****
--- 3063,3071 ----
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
+
select case_test(1);
select case_test(2);
select case_test(3);
***************
*** 3600,3602 ****
--- 3618,3862 ----
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+
+ --
+ -- check function statement tests
+ --
+
+ create table t1(a int, b int);
+
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+
+ drop function f1();
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- should to fail
+ check trigger t1_f1 on t1;
+
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- ok
+ check trigger t1_f1 on t1;
+
+ -- ok
+ insert into t1 values(6,30);
+
+ drop table t1;
+ drop type _exception_type;
+
+ drop function f1_trg();
+
Pavel Stehule wrote:
I am sending updated patch, that implements a CHECK FUNCTION and CHECK
TRIGGER statements.This patch is significantly redesigned to previous version (PL/pgSQL
part) - it is more readable, more accurate. There are new regress
tests.Please, can some English native speaker fix doc and comments?
ToDo:
CHECK FUNCTION search function according to function signature - it
should be changes for using a actual types - it can be solution for
polymorphic types and useful tool for work with overloaded functions -
when is not clean, that function was executed.check function foo(int, int);
NOTICE: checking function foo(variadic anyarray)
...and maybe some support for named parameters
check function foo(name text, surname text);
NOTICE: checking function foo(text, text, text, text)
...
I think that CHECK FUNCTION should work exactly like DROP FUNCTION
in these respects.
Submission review:
------------------
The patch is context diff, applies with some offsets, contains
regression tests and documentation.
The documentation should be expanded, the doc for CHECK FUNCTION
is only a stub. It should describe the procedure and what is checked.
That would also make reviewing easier.
I think that some documentation should be added to plhandler.sgml.
There is a spelling error (statemnt) in the docs.
Usability review:
-----------------
If I understand right, the goal of CHECK FUNCTION is to find errors in
the function definition without actually having to execute it.
The patch tries to provide this for PL/pgSQL.
There hasn't been any discussion on the list, the patch was just posted,
so I can't say that we want that. Tom added it to the commitfest page,
so there's one important voice against dismissing it right away :^)
I don't understand the functional difference between a "validator function"
and a "check function" as proposed by this patch. I am probably missing
something, but why couldn't these checks be added to function validation
when check_function_bodies is set?
A new "CHECK FUNCTION" statement could simply call the validator function.
I don't see any pg_dump support in this patch, and PL/pgSQL probably doesn't
need that, but I think pg_dump support for CREATE LANGUAGE would have to
be added for other PLs.
I can't test if the functionality is complete because I can't get it to
run (see below).
Feature test:
-------------
I can't really test the patch because initdb fails:
$ initdb -E UTF8 --locale=de_DE.UTF-8 --lc-messages=en_US.UTF-8 -U postgres /postgres/cvs/dbhome
The files belonging to this database system will be owned by user "laurenz".
This user must also own the server process.
The database cluster will be initialized with locales
COLLATE: de_DE.UTF-8
CTYPE: de_DE.UTF-8
MESSAGES: en_US.UTF-8
MONETARY: de_DE.UTF-8
NUMERIC: de_DE.UTF-8
TIME: de_DE.UTF-8
The default text search configuration will be set to "german".
creating directory /postgres/cvs/dbhome ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
creating configuration files ... ok
creating template1 database in /postgres/cvs/dbhome/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... FATAL: could not load library "/postgres/cvs/pg92/lib/plpgsql.so": /postgres/cvs/pg92/lib/plpgsql.so: undefined symbol: plpgsql_delete_function
STATEMENT: CREATE EXTENSION plpgsql;
child process exited with exit code 1
initdb: removing data directory "/postgres/cvs/dbhome"
Coding review:
--------------
The patch compiles without warnings.
The comments in the code should be revised, they are bad English.
I can't say if there should be more of them -- I don't know this part of
the code well enough to have a well-founded opinion.
I don't think there are any portability issues, but I could not test it.
There are a lot of small changes to pl/plpgsql/src/pl_exec.c, are they all
necessary? For example, why was copy_plpgsql_datum renamed to
plpgsql_copy_datum?
I'll mark the patch as "Waiting on Author".
Yours,
Laurenz Albe
Hello
2011/11/29 Albe Laurenz <laurenz.albe@wien.gv.at>:
Pavel Stehule wrote:
I am sending updated patch, that implements a CHECK FUNCTION and CHECK
TRIGGER statements.This patch is significantly redesigned to previous version (PL/pgSQL
part) - it is more readable, more accurate. There are new regress
tests.Please, can some English native speaker fix doc and comments?
ToDo:
CHECK FUNCTION search function according to function signature - it
should be changes for using a actual types - it can be solution for
polymorphic types and useful tool for work with overloaded functions -
when is not clean, that function was executed.check function foo(int, int);
NOTICE: checking function foo(variadic anyarray)
...and maybe some support for named parameters
check function foo(name text, surname text);
NOTICE: checking function foo(text, text, text, text)
...I think that CHECK FUNCTION should work exactly like DROP FUNCTION
in these respects.Submission review:
------------------The patch is context diff, applies with some offsets, contains
regression tests and documentation.The documentation should be expanded, the doc for CHECK FUNCTION
is only a stub. It should describe the procedure and what is checked.
That would also make reviewing easier.
I think that some documentation should be added to plhandler.sgml.
There is a spelling error (statemnt) in the docs.Usability review:
-----------------If I understand right, the goal of CHECK FUNCTION is to find errors in
the function definition without actually having to execute it.
The patch tries to provide this for PL/pgSQL.There hasn't been any discussion on the list, the patch was just posted,
so I can't say that we want that. Tom added it to the commitfest page,
so there's one important voice against dismissing it right away :^)
This feature was transformed from plpgsql_lint contrib module. So
there was a voises so this functionality should be in contrib module
as minimum
http://archives.postgresql.org/pgsql-hackers/2011-07/msg00900.php
http://archives.postgresql.org/pgsql-hackers/2011-07/msg01035.php
Contrib module has one disadvantage - it cannot be used in combination
with other plpgsql extensions like edb debugger or edb profiler. So I
rewrote it as core plpgsql patch. It was a plpgsql.prepare_plans
feature. This idea was rejected and replaced by CHECK FUNCTION
statement
Tom propossed a syntax
http://archives.postgresql.org/pgsql-hackers/2011-09/msg00549.php
http://archives.postgresql.org/pgsql-hackers/2011-09/msg00563.php
I don't understand the functional difference between a "validator function"
and a "check function" as proposed by this patch. I am probably missing
something, but why couldn't these checks be added to function validation
when check_function_bodies is set?
A new "CHECK FUNCTION" statement could simply call the validator function.
A validation function is not simple now - and check feature increase a
complexity. Other problem with validator function is their polymorphic
interface.
I don't see any pg_dump support in this patch, and PL/pgSQL probably doesn't
need that, but I think pg_dump support for CREATE LANGUAGE would have to
be added for other PLs.
I have to recheck it
I can't test if the functionality is complete because I can't get it to
run (see below).
sorry - I'll look on it
Feature test:
-------------I can't really test the patch because initdb fails:
$ initdb -E UTF8 --locale=de_DE.UTF-8 --lc-messages=en_US.UTF-8 -U postgres /postgres/cvs/dbhome
The files belonging to this database system will be owned by user "laurenz".
This user must also own the server process.The database cluster will be initialized with locales
COLLATE: de_DE.UTF-8
CTYPE: de_DE.UTF-8
MESSAGES: en_US.UTF-8
MONETARY: de_DE.UTF-8
NUMERIC: de_DE.UTF-8
TIME: de_DE.UTF-8
The default text search configuration will be set to "german".creating directory /postgres/cvs/dbhome ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
creating configuration files ... ok
creating template1 database in /postgres/cvs/dbhome/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... FATAL: could not load library "/postgres/cvs/pg92/lib/plpgsql.so": /postgres/cvs/pg92/lib/plpgsql.so: undefined symbol: plpgsql_delete_function
STATEMENT: CREATE EXTENSION plpgsql;child process exited with exit code 1
initdb: removing data directory "/postgres/cvs/dbhome"Coding review:
--------------The patch compiles without warnings.
The comments in the code should be revised, they are bad English.
I can't say if there should be more of them -- I don't know this part of
the code well enough to have a well-founded opinion.I don't think there are any portability issues, but I could not test it.
There are a lot of small changes to pl/plpgsql/src/pl_exec.c, are they all
necessary? For example, why was copy_plpgsql_datum renamed to
plpgsql_copy_datum?
yes, it's necessary - a implementation is in new file and there is
necessary call a functions from pg_compile and pg_exec files -
checking is between compilation and execution - so some functions
should not be static now. All plpgsql public functions should start
with plpgsql_ prefix. It is reason for renaming.
I'll mark the patch as "Waiting on Author".
I'll look on it this night
Regards
Pavel
Show quoted text
Yours,
Laurenz Albe
Pavel Stehule <pavel.stehule@gmail.com> writes:
2011/11/29 Albe Laurenz <laurenz.albe@wien.gv.at>:
There are a lot of small changes to pl/plpgsql/src/pl_exec.c, are they all
necessary? For example, why was copy_plpgsql_datum renamed to
plpgsql_copy_datum?
yes, it's necessary - a implementation is in new file and there is
necessary call a functions from pg_compile and pg_exec files -
checking is between compilation and execution - so some functions
should not be static now. All plpgsql public functions should start
with plpgsql_ prefix. It is reason for renaming.
I don't think renaming is necessary. plpgsql is a standalone shared
library and so its symbols don't matter to anybody but itself.
Possibly a larger question, though, is whether you really need a new
source file. If that results in having to export functions that
otherwise could stay static, maybe it's not the best choice.
regards, tom lane
2011/11/29 Tom Lane <tgl@sss.pgh.pa.us>:
Pavel Stehule <pavel.stehule@gmail.com> writes:
2011/11/29 Albe Laurenz <laurenz.albe@wien.gv.at>:
There are a lot of small changes to pl/plpgsql/src/pl_exec.c, are they all
necessary? For example, why was copy_plpgsql_datum renamed to
plpgsql_copy_datum?yes, it's necessary - a implementation is in new file and there is
necessary call a functions from pg_compile and pg_exec files -
checking is between compilation and execution - so some functions
should not be static now. All plpgsql public functions should start
with plpgsql_ prefix. It is reason for renaming.I don't think renaming is necessary. plpgsql is a standalone shared
library and so its symbols don't matter to anybody but itself.Possibly a larger question, though, is whether you really need a new
source file. If that results in having to export functions that
otherwise could stay static, maybe it's not the best choice.
This patch was originally in pl_exec.c but this file has a 6170 lines
and checking adds 1092 lines - so I moved it to new file
It has little bit different semantics, but it is true, so checking
hardly depends on routines from pl_exec - routines for variable's
management.
I have no problem to move it back. I reduces original patch little bit.
Some refactoring of pl_exec should be nice - a management of row,
record variables and array fields is part that can be shared with
SQL/PSM interpret. But I have not idea how it realize.
Regards
Pavel
Show quoted text
regards, tom lane
Excerpts from Pavel Stehule's message of mar nov 29 14:37:24 -0300 2011:
2011/11/29 Tom Lane <tgl@sss.pgh.pa.us>:
I don't think renaming is necessary. plpgsql is a standalone shared
library and so its symbols don't matter to anybody but itself.Possibly a larger question, though, is whether you really need a new
source file. If that results in having to export functions that
otherwise could stay static, maybe it's not the best choice.
Some refactoring of pl_exec should be nice - a management of row,
record variables and array fields is part that can be shared with
SQL/PSM interpret. But I have not idea how it realize.
I proposed at the PL summit that perhaps we should have some sort of PL
lib that would be shared by plpgsql and plpsm, to reduce code
duplication.
--
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support
Hello
updated patch:
* recheck compilation and initdb
* working routines moved to pl_exec.c
* add entry to catalog.sgml about lanchecker field
* add node's utils
Regards
Pavel Stehule
2011/11/29 Albe Laurenz <laurenz.albe@wien.gv.at>:
Show quoted text
Pavel Stehule wrote:
I am sending updated patch, that implements a CHECK FUNCTION and CHECK
TRIGGER statements.This patch is significantly redesigned to previous version (PL/pgSQL
part) - it is more readable, more accurate. There are new regress
tests.Please, can some English native speaker fix doc and comments?
ToDo:
CHECK FUNCTION search function according to function signature - it
should be changes for using a actual types - it can be solution for
polymorphic types and useful tool for work with overloaded functions -
when is not clean, that function was executed.check function foo(int, int);
NOTICE: checking function foo(variadic anyarray)
...and maybe some support for named parameters
check function foo(name text, surname text);
NOTICE: checking function foo(text, text, text, text)
...I think that CHECK FUNCTION should work exactly like DROP FUNCTION
in these respects.Submission review:
------------------The patch is context diff, applies with some offsets, contains
regression tests and documentation.The documentation should be expanded, the doc for CHECK FUNCTION
is only a stub. It should describe the procedure and what is checked.
That would also make reviewing easier.
I think that some documentation should be added to plhandler.sgml.
There is a spelling error (statemnt) in the docs.Usability review:
-----------------If I understand right, the goal of CHECK FUNCTION is to find errors in
the function definition without actually having to execute it.
The patch tries to provide this for PL/pgSQL.There hasn't been any discussion on the list, the patch was just posted,
so I can't say that we want that. Tom added it to the commitfest page,
so there's one important voice against dismissing it right away :^)I don't understand the functional difference between a "validator function"
and a "check function" as proposed by this patch. I am probably missing
something, but why couldn't these checks be added to function validation
when check_function_bodies is set?
A new "CHECK FUNCTION" statement could simply call the validator function.I don't see any pg_dump support in this patch, and PL/pgSQL probably doesn't
need that, but I think pg_dump support for CREATE LANGUAGE would have to
be added for other PLs.I can't test if the functionality is complete because I can't get it to
run (see below).Feature test:
-------------I can't really test the patch because initdb fails:
$ initdb -E UTF8 --locale=de_DE.UTF-8 --lc-messages=en_US.UTF-8 -U postgres /postgres/cvs/dbhome
The files belonging to this database system will be owned by user "laurenz".
This user must also own the server process.The database cluster will be initialized with locales
COLLATE: de_DE.UTF-8
CTYPE: de_DE.UTF-8
MESSAGES: en_US.UTF-8
MONETARY: de_DE.UTF-8
NUMERIC: de_DE.UTF-8
TIME: de_DE.UTF-8
The default text search configuration will be set to "german".creating directory /postgres/cvs/dbhome ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
creating configuration files ... ok
creating template1 database in /postgres/cvs/dbhome/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... FATAL: could not load library "/postgres/cvs/pg92/lib/plpgsql.so": /postgres/cvs/pg92/lib/plpgsql.so: undefined symbol: plpgsql_delete_function
STATEMENT: CREATE EXTENSION plpgsql;child process exited with exit code 1
initdb: removing data directory "/postgres/cvs/dbhome"Coding review:
--------------The patch compiles without warnings.
The comments in the code should be revised, they are bad English.
I can't say if there should be more of them -- I don't know this part of
the code well enough to have a well-founded opinion.I don't think there are any portability issues, but I could not test it.
There are a lot of small changes to pl/plpgsql/src/pl_exec.c, are they all
necessary? For example, why was copy_plpgsql_datum renamed to
plpgsql_copy_datum?I'll mark the patch as "Waiting on Author".
Yours,
Laurenz Albe
Attachments:
check_pl-2011-11-29.difftext/x-patch; charset=US-ASCII; name=check_pl-2011-11-29.diffDownload
*** ./doc/src/sgml/catalogs.sgml.orig 2011-11-29 19:09:02.000000000 +0100
--- ./doc/src/sgml/catalogs.sgml 2011-11-29 20:28:00.571246006 +0100
***************
*** 3652,3657 ****
--- 3652,3668 ----
</row>
<row>
+ <entry><structfield>lanchecker</structfield></entry>
+ <entry><type>oid</type></entry>
+ <entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
+ <entry>
+ This references a language checker function that is responsible
+ for checking a embedded SQL and can provide detailed checking.
+ Zero if no checker is provided.
+ </entry>
+ </row>
+
+ <row>
<entry><structfield>lanacl</structfield></entry>
<entry><type>aclitem[]</type></entry>
<entry></entry>
*** ./doc/src/sgml/ref/allfiles.sgml.orig 2011-11-29 19:20:59.468117093 +0100
--- ./doc/src/sgml/ref/allfiles.sgml 2011-11-29 19:21:24.487804955 +0100
***************
*** 40,45 ****
--- 40,46 ----
<!ENTITY alterView SYSTEM "alter_view.sgml">
<!ENTITY analyze SYSTEM "analyze.sgml">
<!ENTITY begin SYSTEM "begin.sgml">
+ <!ENTITY checkFunction SYSTEM "check_function.sgml">
<!ENTITY checkpoint SYSTEM "checkpoint.sgml">
<!ENTITY close SYSTEM "close.sgml">
<!ENTITY cluster SYSTEM "cluster.sgml">
*** ./doc/src/sgml/ref/create_language.sgml.orig 2011-11-29 19:20:59.470117069 +0100
--- ./doc/src/sgml/ref/create_language.sgml 2011-11-29 19:21:24.488804943 +0100
***************
*** 23,29 ****
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
--- 23,29 ----
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ] [ CHECK <replaceable>checkfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
***************
*** 217,222 ****
--- 217,236 ----
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><literal>CHECK</literal> <replaceable class="parameter">checkfunction</replaceable></term>
+
+ <listitem>
+ <para><replaceable class="parameter">checkfunction</replaceable> is the
+ name of a previously registered function that will be called
+ when a new function in the language is created, to check the
+ function by statemnt <command>CHECK FUNCTION</command> or
+ <command>CHECK TRIGGER</command>.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
*** ./doc/src/sgml/reference.sgml.orig 2011-11-29 19:20:59.471117057 +0100
--- ./doc/src/sgml/reference.sgml 2011-11-29 19:21:24.492804895 +0100
***************
*** 68,73 ****
--- 68,74 ----
&alterView;
&analyze;
&begin;
+ &checkFunction;
&checkpoint;
&close;
&cluster;
*** ./src/backend/catalog/pg_proc.c.orig 2011-11-29 19:20:59.474117021 +0100
--- ./src/backend/catalog/pg_proc.c 2011-11-29 19:21:24.494804869 +0100
***************
*** 1101,1103 ****
--- 1101,1104 ----
*newcursorpos = newcp;
return false;
}
+
*** ./src/backend/commands/functioncmds.c.orig 2011-11-29 19:20:59.475117009 +0100
--- ./src/backend/commands/functioncmds.c 2011-11-29 19:21:24.496804843 +0100
***************
*** 44,53 ****
--- 44,55 ----
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_proc_fn.h"
+ #include "catalog/pg_trigger.h"
#include "catalog/pg_type.h"
#include "catalog/pg_type_fn.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
+ #include "commands/trigger.h"
#include "miscadmin.h"
#include "optimizer/var.h"
#include "parser/parse_coerce.h"
***************
*** 60,65 ****
--- 62,68 ----
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
+ #include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
***************
*** 1009,1014 ****
--- 1012,1152 ----
}
}
+ /*
+ * CheckFunction
+ * call a PL checker function when this function exists.
+ */
+ void
+ CheckFunction(CheckFunctionStmt *stmt)
+ {
+ List *functionName = stmt->funcname;
+ List *argTypes = stmt->args; /* list of TypeName nodes */
+ Oid funcOid;
+
+ HeapTuple tup;
+ Form_pg_proc proc;
+
+ HeapTuple languageTuple;
+ Form_pg_language languageStruct;
+ Oid languageChecker;
+ Oid trgOid = InvalidOid;
+ Oid relid = InvalidOid;
+
+ /* when we should to check trigger, then we should to find a trigger handler */
+ if (functionName == NULL)
+ {
+ HeapTuple ht_trig;
+ Form_pg_trigger trigrec;
+ ScanKeyData skey[1];
+ Relation tgrel;
+ SysScanDesc tgscan;
+ char *fname;
+
+ relid = RangeVarGetRelid(stmt->relation, ShareLock, false, false);
+ trgOid = get_trigger_oid(relid, stmt->trgname, false);
+
+ /*
+ * Fetch the pg_trigger tuple by the Oid of the trigger
+ */
+ tgrel = heap_open(TriggerRelationId, AccessShareLock);
+
+ ScanKeyInit(&skey[0],
+ ObjectIdAttributeNumber,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(trgOid));
+
+ tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
+ SnapshotNow, 1, skey);
+
+ ht_trig = systable_getnext(tgscan);
+
+ if (!HeapTupleIsValid(ht_trig))
+ elog(ERROR, "could not find tuple for trigger %u", trgOid);
+
+ trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
+
+ /* we need to know trigger function to get PL checker function */
+ funcOid = trigrec->tgfoid;
+ fname = format_procedure(funcOid);
+ /* Clean up */
+ systable_endscan(tgscan);
+
+ elog(NOTICE, "checking function \"%s\"", fname);
+ pfree(fname);
+
+ heap_close(tgrel, AccessShareLock);
+ }
+ else
+ {
+ /*
+ * Find the function,
+ */
+ funcOid = LookupFuncNameTypeNames(functionName, argTypes, false);
+ }
+
+ tup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
+ if (!HeapTupleIsValid(tup)) /* should not happen */
+ elog(ERROR, "cache lookup failed for function %u", funcOid);
+
+ proc = (Form_pg_proc) GETSTRUCT(tup);
+
+ languageTuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(proc->prolang));
+ Assert(HeapTupleIsValid(languageTuple));
+
+ languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
+ languageChecker = languageStruct->lanchecker;
+
+ /* Check a function body */
+ if (OidIsValid(languageChecker))
+ {
+ ArrayType *set_items = NULL;
+ int save_nestlevel;
+ Datum datum;
+ bool isnull;
+ MemoryContext oldCxt;
+ MemoryContext checkCxt;
+
+ datum = SysCacheGetAttr(PROCOID, tup, Anum_pg_proc_proconfig, &isnull);
+
+ if (!isnull)
+ {
+ /* Set per-function configuration parameters */
+ set_items = (ArrayType *) DatumGetPointer(datum);
+ if (set_items) /* Need a new GUC nesting level */
+ {
+ save_nestlevel = NewGUCNestLevel();
+ ProcessGUCArray(set_items,
+ (superuser() ? PGC_SUSET : PGC_USERSET),
+ PGC_S_SESSION,
+ GUC_ACTION_SAVE);
+ }
+ else
+ save_nestlevel = 0; /* keep compiler quiet */
+ }
+
+ checkCxt = AllocSetContextCreate(CurrentMemoryContext,
+ "Check temporary context",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
+
+ oldCxt = MemoryContextSwitchTo(checkCxt);
+
+ OidFunctionCall2(languageChecker, ObjectIdGetDatum(funcOid),
+ ObjectIdGetDatum(relid));
+
+ MemoryContextSwitchTo(oldCxt);
+
+ if (set_items)
+ AtEOXact_GUC(true, save_nestlevel);
+ }
+ else
+ elog(WARNING, "language \"%s\" has no defined checker function",
+ NameStr(languageStruct->lanname));
+
+ ReleaseSysCache(languageTuple);
+ ReleaseSysCache(tup);
+ }
/*
* Rename function
*** ./src/backend/commands/proclang.c.orig 2011-11-29 19:20:59.477116983 +0100
--- ./src/backend/commands/proclang.c 2011-11-29 19:21:24.497804830 +0100
***************
*** 46,57 ****
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
--- 46,58 ----
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
+ char *tmplchecker; /* name of checker function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
***************
*** 67,75 ****
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid;
Oid funcrettype;
! Oid funcargtypes[1];
/*
* If we have template information for the language, ignore the supplied
--- 68,77 ----
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid,
! checkerOid;
Oid funcrettype;
! Oid funcargtypes[2];
/*
* If we have template information for the language, ignore the supplied
***************
*** 219,228 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, pltemplate->tmpltrusted);
}
else
{
--- 221,269 ----
else
valOid = InvalidOid;
+ /*
+ * Likewise for the checker, if required; but we don't care about
+ * its return type.
+ */
+ if (pltemplate->tmplchecker)
+ {
+ funcname = SystemFuncName(pltemplate->tmplchecker);
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(funcname, 2, funcargtypes, true);
+ if (!OidIsValid(checkerOid))
+ {
+ checkerOid = ProcedureCreate(pltemplate->tmplchecker,
+ PG_CATALOG_NAMESPACE,
+ false, /* replace */
+ false, /* returnsSet */
+ VOIDOID,
+ ClanguageId,
+ F_FMGR_C_VALIDATOR,
+ pltemplate->tmplchecker,
+ pltemplate->tmpllibrary,
+ false, /* isAgg */
+ false, /* isWindowFunc */
+ false, /* security_definer */
+ true, /* isStrict */
+ PROVOLATILE_VOLATILE,
+ buildoidvector(funcargtypes, 2),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ NIL,
+ PointerGetDatum(NULL),
+ 1,
+ 0);
+ }
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, pltemplate->tmpltrusted);
}
else
{
***************
*** 294,303 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, stmt->pltrusted);
}
}
--- 335,355 ----
else
valOid = InvalidOid;
+ /* validate the checker function */
+ if (stmt->plchecker)
+ {
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(stmt->plchecker, 2, funcargtypes, false);
+ /* return value is ignored, so we don't check the type */
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, stmt->pltrusted);
}
}
***************
*** 307,313 ****
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
--- 359,365 ----
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
***************
*** 337,342 ****
--- 389,395 ----
values[Anum_pg_language_lanplcallfoid - 1] = ObjectIdGetDatum(handlerOid);
values[Anum_pg_language_laninline - 1] = ObjectIdGetDatum(inlineOid);
values[Anum_pg_language_lanvalidator - 1] = ObjectIdGetDatum(valOid);
+ values[Anum_pg_language_lanchecker - 1] = ObjectIdGetDatum(checkerOid);
nulls[Anum_pg_language_lanacl - 1] = true;
/* Check for pre-existing definition */
***************
*** 423,428 ****
--- 476,490 ----
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
+ /* dependency on the checker function, if any */
+ if (OidIsValid(checkerOid))
+ {
+ referenced.classId = ProcedureRelationId;
+ referenced.objectId = checkerOid;
+ referenced.objectSubId = 0;
+ recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
+ }
+
/* Post creation hook for new procedural language */
InvokeObjectAccessHook(OAT_POST_CREATE,
LanguageRelationId, myself.objectId, 0);
***************
*** 478,483 ****
--- 540,550 ----
if (!isnull)
result->tmplvalidator = TextDatumGetCString(datum);
+ datum = heap_getattr(tup, Anum_pg_pltemplate_tmplchecker,
+ RelationGetDescr(rel), &isnull);
+ if (!isnull)
+ result->tmplchecker = TextDatumGetCString(datum);
+
datum = heap_getattr(tup, Anum_pg_pltemplate_tmpllibrary,
RelationGetDescr(rel), &isnull);
if (!isnull)
*** ./src/backend/nodes/copyfuncs.c.orig 2011-11-29 19:09:02.000000000 +0100
--- ./src/backend/nodes/copyfuncs.c 2011-11-29 20:17:01.339172458 +0100
***************
*** 2880,2885 ****
--- 2880,2898 ----
return newnode;
}
+ static CheckFunctionStmt *
+ _copyCheckFunctionStmt(CheckFunctionStmt *from)
+ {
+ CheckFunctionStmt *newnode = makeNode(CheckFunctionStmt);
+
+ COPY_NODE_FIELD(funcname);
+ COPY_NODE_FIELD(args);
+ COPY_STRING_FIELD(trgname);
+ COPY_NODE_FIELD(relation);
+
+ return newnode;
+ }
+
static DoStmt *
_copyDoStmt(DoStmt *from)
{
***************
*** 4165,4170 ****
--- 4178,4186 ----
case T_AlterFunctionStmt:
retval = _copyAlterFunctionStmt(from);
break;
+ case T_CheckFunctionStmt:
+ retval = _copyCheckFunctionStmt(from);
+ break;
case T_DoStmt:
retval = _copyDoStmt(from);
break;
*** ./src/backend/nodes/equalfuncs.c.orig 2011-11-29 20:19:55.045587471 +0100
--- ./src/backend/nodes/equalfuncs.c 2011-11-29 20:19:21.850082357 +0100
***************
*** 1292,1297 ****
--- 1292,1308 ----
}
static bool
+ _equalCheckFunctionStmt(CheckFunctionStmt *a, CheckFunctionStmt *b)
+ {
+ COMPARE_NODE_FIELD(funcname);
+ COMPARE_NODE_FIELD(args);
+ COMPARE_STRING_FIELD(trgname);
+ COMPARE_NODE_FIELD(relation);
+
+ return true;
+ }
+
+ static bool
_equalDoStmt(DoStmt *a, DoStmt *b)
{
COMPARE_NODE_FIELD(args);
***************
*** 2708,2713 ****
--- 2719,2727 ----
case T_AlterFunctionStmt:
retval = _equalAlterFunctionStmt(a, b);
break;
+ case T_CheckFunctionStmt:
+ retval = _equalCheckFunctionStmt(a, b);
+ break;
case T_DoStmt:
retval = _equalDoStmt(a, b);
break;
*** ./src/backend/parser/gram.y.orig 2011-11-29 19:09:02.876463248 +0100
--- ./src/backend/parser/gram.y 2011-11-29 19:21:24.502804769 +0100
***************
*** 227,232 ****
--- 227,233 ----
DeallocateStmt PrepareStmt ExecuteStmt
DropOwnedStmt ReassignOwnedStmt
AlterTSConfigurationStmt AlterTSDictionaryStmt
+ CheckFunctionStmt
%type <node> select_no_parens select_with_parens select_clause
simple_select values_clause
***************
*** 276,282 ****
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate
%type <range> qualified_name OptConstrFromTable
--- 277,283 ----
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate opt_checker
%type <range> qualified_name OptConstrFromTable
***************
*** 700,705 ****
--- 701,707 ----
| AlterUserSetStmt
| AlterUserStmt
| AnalyzeStmt
+ | CheckFunctionStmt
| CheckPointStmt
| ClosePortalStmt
| ClusterStmt
***************
*** 3174,3184 ****
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
--- 3176,3187 ----
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
+ n->plchecker = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator opt_checker
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
***************
*** 3186,3191 ****
--- 3189,3195 ----
n->plhandler = $8;
n->plinline = $9;
n->plvalidator = $10;
+ n->plchecker = $11;
n->pltrusted = $3;
$$ = (Node *)n;
}
***************
*** 3220,3225 ****
--- 3224,3234 ----
| /*EMPTY*/ { $$ = NIL; }
;
+ opt_checker:
+ CHECK handler_name { $$ = $2; }
+ | /*EMPTY*/ { $$ = NIL; }
+ ;
+
DropPLangStmt:
DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
{
***************
*** 6250,6255 ****
--- 6259,6294 ----
/*****************************************************************************
*
+ * CHECK FUNCTION funcname(args)
+ * CHECK TRIGGER triggername ON table
+ *
+ *
+ *****************************************************************************/
+
+
+ CheckFunctionStmt:
+ CHECK FUNCTION func_name func_args
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = $3;
+ n->args = extractArgTypes($4);
+ n->trgname = NULL;
+ n->relation = NULL;
+ $$ = (Node *) n;
+ }
+ | CHECK TRIGGER name ON qualified_name
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = $3;
+ n->relation = $5;
+ $$ = (Node *) n;
+ }
+ ;
+
+ /*****************************************************************************
+ *
* DO <anonymous code block> [ LANGUAGE language ]
*
* We use a DefElem list for future extensibility, and to allow flexibility
*** ./src/backend/tcop/utility.c.orig 2011-11-29 19:20:59.480116945 +0100
--- ./src/backend/tcop/utility.c 2011-11-29 19:21:24.513804628 +0100
***************
*** 882,887 ****
--- 882,891 ----
AlterFunction((AlterFunctionStmt *) parsetree);
break;
+ case T_CheckFunctionStmt:
+ CheckFunction((CheckFunctionStmt *) parsetree);
+ break;
+
case T_IndexStmt: /* CREATE INDEX */
{
IndexStmt *stmt = (IndexStmt *) parsetree;
***************
*** 2125,2130 ****
--- 2129,2141 ----
}
break;
+ case T_CheckFunctionStmt:
+ if (((CheckFunctionStmt *) parsetree)->funcname != NULL)
+ tag = "CHECK FUNCTION";
+ else
+ tag = "CHECK TRIGGER";
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
***************
*** 2565,2570 ****
--- 2576,2585 ----
}
break;
+ case T_CheckFunctionStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
*** ./src/bin/pg_dump/pg_dump.c.orig 2011-11-29 19:09:03.000000000 +0100
--- ./src/bin/pg_dump/pg_dump.c 2011-11-29 20:04:31.094156626 +0100
***************
*** 5326,5338 ****
int i_lanplcallfoid;
int i_laninline;
int i_lanvalidator;
int i_lanacl;
int i_lanowner;
/* Make sure we are in proper schema */
selectSourceSchema("pg_catalog");
! if (g_fout->remoteVersion >= 90000)
{
/* pg_language has a laninline column */
appendPQExpBuffer(query, "SELECT tableoid, oid, "
--- 5326,5351 ----
int i_lanplcallfoid;
int i_laninline;
int i_lanvalidator;
+ int i_lanchecker;
int i_lanacl;
int i_lanowner;
/* Make sure we are in proper schema */
selectSourceSchema("pg_catalog");
! if (g_fout->remoteVersion >= 90200)
! {
! /* pg_language has a lanchecker column */
! appendPQExpBuffer(query, "SELECT tableoid, oid, "
! "lanname, lanpltrusted, lanplcallfoid, "
! "laninline, lanvalidator, lanchecker, lanacl, "
! "(%s lanowner) AS lanowner "
! "FROM pg_language "
! "WHERE lanispl "
! "ORDER BY oid",
! username_subquery);
! }
! else if (g_fout->remoteVersion >= 90000)
{
/* pg_language has a laninline column */
appendPQExpBuffer(query, "SELECT tableoid, oid, "
***************
*** 5409,5414 ****
--- 5422,5428 ----
/* these may fail and return -1: */
i_laninline = PQfnumber(res, "laninline");
i_lanvalidator = PQfnumber(res, "lanvalidator");
+ i_lanchecker = PQfnumber(res, "lanchecker");
i_lanacl = PQfnumber(res, "lanacl");
i_lanowner = PQfnumber(res, "lanowner");
***************
*** 5422,5427 ****
--- 5436,5445 ----
planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
+ if (i_lanchecker >= 0)
+ planginfo[i].lanchecker = atooid(PQgetvalue(res, i, i_lanchecker));
+ else
+ planginfo[i].lanchecker = InvalidOid;
if (i_laninline >= 0)
planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
else
***************
*** 8597,8602 ****
--- 8615,8621 ----
char *qlanname;
char *lanschema;
FuncInfo *funcInfo;
+ FuncInfo *checkerInfo = NULL;
FuncInfo *inlineInfo = NULL;
FuncInfo *validatorInfo = NULL;
***************
*** 8616,8621 ****
--- 8635,8647 ----
if (funcInfo != NULL && !funcInfo->dobj.dump)
funcInfo = NULL; /* treat not-dumped same as not-found */
+ if (OidIsValid(plang->lanchecker))
+ {
+ checkerInfo = findFuncByOid(plang->lanchecker);
+ if (checkerInfo != NULL && !checkerInfo->dobj.dump)
+ checkerInfo = NULL;
+ }
+
if (OidIsValid(plang->laninline))
{
inlineInfo = findFuncByOid(plang->laninline);
***************
*** 8642,8647 ****
--- 8668,8674 ----
* don't, this might not work terribly nicely.
*/
useParams = (funcInfo != NULL &&
+ (checkerInfo != NULL || !OidIsValid(plang->lanchecker)) &&
(inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
(validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
***************
*** 8697,8702 ****
--- 8724,8739 ----
appendPQExpBuffer(defqry, "%s",
fmtId(validatorInfo->dobj.name));
}
+ if (OidIsValid(plang->lanchecker))
+ {
+ appendPQExpBuffer(defqry, " CHECK ");
+ /* Cope with possibility that checker is in different schema */
+ if (checkerInfo->dobj.namespace != funcInfo->dobj.namespace)
+ appendPQExpBuffer(defqry, "%s.",
+ fmtId(checkerInfo->dobj.namespace->dobj.name));
+ appendPQExpBuffer(defqry, "%s",
+ fmtId(checkerInfo->dobj.name));
+ }
}
else
{
*** ./src/bin/pg_dump/pg_dump.h.orig 2011-11-29 20:05:48.255044631 +0100
--- ./src/bin/pg_dump/pg_dump.h 2011-11-29 20:05:08.766614345 +0100
***************
*** 387,392 ****
--- 387,393 ----
Oid lanplcallfoid;
Oid laninline;
Oid lanvalidator;
+ Oid lanchecker;
char *lanacl;
char *lanowner; /* name of owner, or empty string */
} ProcLangInfo;
*** ./src/bin/psql/tab-complete.c.orig 2011-11-29 19:20:59.482116921 +0100
--- ./src/bin/psql/tab-complete.c 2011-11-29 19:21:24.516804592 +0100
***************
*** 1,4 ****
--- 1,5 ----
/*
+ *
* psql - the PostgreSQL interactive terminal
*
* Copyright (c) 2000-2011, PostgreSQL Global Development Group
***************
*** 727,733 ****
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
--- 728,734 ----
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECK", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
***************
*** 1524,1529 ****
--- 1525,1552 ----
COMPLETE_WITH_LIST(list_TRANS);
}
+
+ /* CHECK */
+ else if (pg_strcasecmp(prev_wd, "CHECK") == 0)
+ {
+ static const char *const list_CHECK[] =
+ {"FUNCTION", "TRIGGER", NULL};
+
+ COMPLETE_WITH_LIST(list_CHECK);
+ }
+ else if (pg_strcasecmp(prev3_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
+ {
+ COMPLETE_WITH_CONST("ON");
+ }
+ else if (pg_strcasecmp(prev4_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
+ pg_strcasecmp(prev_wd, "ON") == 0)
+ {
+ completion_info_charp = prev2_wd;
+ COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger);
+ }
+
/* CLUSTER */
/*
*** ./src/include/catalog/pg_language.h.orig 2011-11-29 19:20:59.483116909 +0100
--- ./src/include/catalog/pg_language.h 2011-11-29 19:21:24.518804568 +0100
***************
*** 37,42 ****
--- 37,43 ----
Oid lanplcallfoid; /* Call handler for PL */
Oid laninline; /* Optional anonymous-block handler function */
Oid lanvalidator; /* Optional validation function */
+ Oid lanchecker; /* Optional checker function */
aclitem lanacl[1]; /* Access privileges */
} FormData_pg_language;
***************
*** 51,57 ****
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 8
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
--- 52,58 ----
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 9
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
***************
*** 59,78 ****
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanacl 8
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
--- 60,80 ----
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanchecker 8
! #define Anum_pg_language_lanacl 9
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 0 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 0 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 0 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
*** ./src/include/catalog/pg_pltemplate.h.orig 2011-11-29 19:20:59.484116897 +0100
--- ./src/include/catalog/pg_pltemplate.h 2011-11-29 19:21:24.518804568 +0100
***************
*** 36,41 ****
--- 36,42 ----
text tmplhandler; /* name of call handler function */
text tmplinline; /* name of anonymous-block handler, or NULL */
text tmplvalidator; /* name of validator function, or NULL */
+ text tmplchecker; /* name of checker function, or NULL */
text tmpllibrary; /* path of shared library */
aclitem tmplacl[1]; /* access privileges for template */
} FormData_pg_pltemplate;
***************
*** 51,65 ****
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 8
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmpllibrary 7
! #define Anum_pg_pltemplate_tmplacl 8
/* ----------------
--- 52,67 ----
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 9
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmplchecker 7
! #define Anum_pg_pltemplate_tmpllibrary 8
! #define Anum_pg_pltemplate_tmplacl 9
/* ----------------
***************
*** 67,79 ****
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
--- 69,81 ----
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "plpgsql_checker" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" _null_ "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
*** ./src/include/commands/defrem.h.orig 2011-11-29 19:20:59.486116871 +0100
--- ./src/include/commands/defrem.h 2011-11-29 19:21:24.519804556 +0100
***************
*** 62,67 ****
--- 62,68 ----
/* commands/functioncmds.c */
extern void CreateFunction(CreateFunctionStmt *stmt, const char *queryString);
extern void RemoveFunctionById(Oid funcOid);
+ extern void CheckFunction(CheckFunctionStmt *stmt);
extern void SetFunctionReturnType(Oid funcOid, Oid newRetType);
extern void SetFunctionArgType(Oid funcOid, int argIndex, Oid newArgType);
extern void RenameFunction(List *name, List *argtypes, const char *newname);
*** ./src/include/nodes/nodes.h.orig 2011-11-29 19:20:59.487116858 +0100
--- ./src/include/nodes/nodes.h 2011-11-29 19:21:24.521804532 +0100
***************
*** 291,296 ****
--- 291,297 ----
T_IndexStmt,
T_CreateFunctionStmt,
T_AlterFunctionStmt,
+ T_CheckFunctionStmt,
T_DoStmt,
T_RenameStmt,
T_RuleStmt,
*** ./src/include/nodes/parsenodes.h.orig 2011-11-29 19:20:59.489116833 +0100
--- ./src/include/nodes/parsenodes.h 2011-11-29 19:21:24.523804506 +0100
***************
*** 1734,1739 ****
--- 1734,1740 ----
List *plhandler; /* PL call handler function (qual. name) */
List *plinline; /* optional inline function (qual. name) */
List *plvalidator; /* optional validator function (qual. name) */
+ List *plchecker; /* optional checker function (qual. name) */
bool pltrusted; /* PL is trusted */
} CreatePLangStmt;
***************
*** 2077,2082 ****
--- 2078,2096 ----
} AlterFunctionStmt;
/* ----------------------
+ * Check {Function|Trigger} Statement
+ * ----------------------
+ */
+ typedef struct CheckFunctionStmt
+ {
+ NodeTag type;
+ List *funcname; /* qualified name of checked object */
+ List *args; /* types of the arguments */
+ char *trgname; /* trigger's name */
+ RangeVar *relation; /* trigger's relation */
+ } CheckFunctionStmt;
+
+ /* ----------------------
* DO Statement
*
* DoStmt is the raw parser output, InlineCodeBlock is the execution-time API
*** ./src/pl/plpgsql/src/pl_comp.c.orig 2011-11-29 19:09:03.000000000 +0100
--- ./src/pl/plpgsql/src/pl_comp.c 2011-11-29 19:42:43.058753779 +0100
***************
*** 115,121 ****
static void plpgsql_HashTableInsert(PLpgSQL_function *function,
PLpgSQL_func_hashkey *func_key);
static void plpgsql_HashTableDelete(PLpgSQL_function *function);
- static void delete_function(PLpgSQL_function *func);
/* ----------
* plpgsql_compile Make an execution tree for a PL/pgSQL function.
--- 115,120 ----
***************
*** 175,181 ****
* Nope, so remove it from hashtable and try to drop associated
* storage (if not done already).
*/
! delete_function(function);
/*
* If the function isn't in active use then we can overwrite the
--- 174,180 ----
* Nope, so remove it from hashtable and try to drop associated
* storage (if not done already).
*/
! plpgsql_delete_function(function);
/*
* If the function isn't in active use then we can overwrite the
***************
*** 2426,2432 ****
}
/*
! * delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
--- 2425,2431 ----
}
/*
! * plpgsql_delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
***************
*** 2439,2446 ****
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
! static void
! delete_function(PLpgSQL_function *func)
{
/* remove function from hash table (might be done already) */
plpgsql_HashTableDelete(func);
--- 2438,2445 ----
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
! void
! plpgsql_delete_function(PLpgSQL_function *func)
{
/* remove function from hash table (might be done already) */
plpgsql_HashTableDelete(func);
*** ./src/pl/plpgsql/src/pl_exec.c.orig 2011-11-29 19:09:03.316459122 +0100
--- ./src/pl/plpgsql/src/pl_exec.c 2011-11-29 19:37:19.000000000 +0100
***************
*** 210,216 ****
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
PLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
!
/* ----------
* plpgsql_exec_function Called by the call handler for
--- 210,228 ----
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
PLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
! static void check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec);
! static void check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr);
! static void assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
! PLpgSQL_row *row, PLpgSQL_rec *rec,
! TupleDesc tupdesc);
! static TupleDesc expr_get_desc(PLpgSQL_execstate *estate,
! PLpgSQL_expr *query,
! bool use_element_type,
! bool expand_record,
! bool is_expression);
! static void var_init_to_null(PLpgSQL_execstate *estate, int varno);
! static void check_stmts(PLpgSQL_execstate *estate, List *stmts);
! static void check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
/* ----------
* plpgsql_exec_function Called by the call handler for
***************
*** 6176,6178 ****
--- 6188,7242 ----
return portal;
}
+
+ /*
+ * Following code ensures a CHECK FUNCTION and CHECK TRIGGER statements for PL/pgSQL
+ *
+ */
+
+ /*
+ * append a CONTEXT to error message
+ */
+ static void
+ check_error_callback(void *arg)
+ {
+ PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;
+
+ if (estate->err_stmt != NULL)
+ {
+ /* translator: last %s is a plpgsql statement type name */
+ errcontext("checking of PL/pgSQL function \"%s\" line %d at %s",
+ estate->func->fn_name,
+ estate->err_stmt->lineno,
+ plpgsql_stmt_typename(estate->err_stmt));
+ }
+ else
+ errcontext("checking of PL/pgSQL function \"%s\"",
+ estate->func->fn_name);
+ }
+
+ /*
+ * Check function - it prepare variables and starts a prepare plan walker
+ * called by function checker
+ */
+ void
+ plpgsql_check_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
+
+ /*
+ * Store the actual call argument values into the appropriate variables
+ */
+ for (i = 0; i < func->fn_nargs; i++)
+ {
+ int n = func->fn_argvarnos[i];
+
+ switch (estate.datums[n]->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ {
+ var_init_to_null(&estate, n);
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ {
+ PLpgSQL_row *row = (PLpgSQL_row *) estate.datums[n];
+
+ exec_move_row(&estate, NULL, row, NULL, NULL);
+ }
+ break;
+
+ default:
+ elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
+ }
+ }
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Check trigger - prepare fake environments for testing trigger
+ *
+ */
+ void
+ plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ PLpgSQL_rec *rec_new,
+ *rec_old;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, NULL);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
+
+ /*
+ * Put the OLD and NEW tuples into record variables
+ *
+ * We make the tupdescs available in both records even though only one may
+ * have a value. This allows parsing of record references to succeed in
+ * functions that are used for multiple trigger types. For example, we
+ * might have a test like "if (TG_OP = 'INSERT' and NEW.foo = 'xyz')",
+ * which should parse regardless of the current trigger type.
+ */
+ rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
+ rec_new->freetup = false;
+ rec_new->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_new, trigdata->tg_relation->rd_att);
+
+ rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
+ rec_old->freetup = false;
+ rec_old->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_old, trigdata->tg_relation->rd_att);
+
+ /*
+ * Assign the special tg_ variables
+ */
+ var_init_to_null(&estate, func->tg_op_varno);
+ var_init_to_null(&estate, func->tg_name_varno);
+ var_init_to_null(&estate, func->tg_when_varno);
+ var_init_to_null(&estate, func->tg_level_varno);
+ var_init_to_null(&estate, func->tg_relid_varno);
+ var_init_to_null(&estate, func->tg_relname_varno);
+ var_init_to_null(&estate, func->tg_table_name_varno);
+ var_init_to_null(&estate, func->tg_table_schema_varno);
+ var_init_to_null(&estate, func->tg_nargs_varno);
+ var_init_to_null(&estate, func->tg_argv_varno);
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Verify lvalue
+ * It doesn't repeat a checks that are done.
+ * Checks a subscript expressions, verify a validity of record's fields
+ */
+ static void
+ check_target(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ switch (target->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ case PLPGSQL_DTYPE_REC:
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ check_row_or_rec(estate, (PLpgSQL_row *) target, NULL);
+ break;
+
+ case PLPGSQL_DTYPE_RECFIELD:
+ {
+ PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
+ PLpgSQL_rec *rec;
+ int fno;
+
+ rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
+
+ /*
+ * Check that there is already a tuple in the record. We need
+ * that because records don't have any predefined field
+ * structure.
+ */
+ if (!HeapTupleIsValid(rec->tup))
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("record \"%s\" is not assigned to tuple structure",
+ rec->refname)));
+
+ /*
+ * Get the number of the records field to change and the
+ * number of attributes in the tuple. Note: disallow system
+ * column names because the code below won't cope.
+ */
+ fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
+ if (fno <= 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("record \"%s\" has no field \"%s\"",
+ rec->refname, recfield->fieldname)));
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ARRAYELEM:
+ {
+ /*
+ * Target is an element of an array
+ */
+ int nsubscripts;
+ Oid arrayelemtypeid;
+ Oid arraytypeid;
+
+ /*
+ * To handle constructs like x[1][2] := something, we have to
+ * be prepared to deal with a chain of arrayelem datums. Chase
+ * back to find the base array datum, and save the subscript
+ * expressions as we go. (We are scanning right to left here,
+ * but want to evaluate the subscripts left-to-right to
+ * minimize surprises.)
+ */
+ nsubscripts = 0;
+ do
+ {
+ PLpgSQL_arrayelem *arrayelem = (PLpgSQL_arrayelem *) target;
+
+ if (nsubscripts++ >= MAXDIM)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
+ nsubscripts + 1, MAXDIM)));
+
+ check_expr(estate, arrayelem->subscript);
+
+ target = estate->datums[arrayelem->arrayparentno];
+ } while (target->dtype == PLPGSQL_DTYPE_ARRAYELEM);
+
+ /* If target is domain over array, reduce to base type */
+ arraytypeid = exec_get_datum_type(estate, target);
+ arraytypeid = getBaseType(arraytypeid);
+
+ arrayelemtypeid = get_element_type(arraytypeid);
+
+ if (!OidIsValid(arrayelemtypeid))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("subscripted object is not an array")));
+ }
+ break;
+ }
+ }
+
+ /*
+ * Check composed lvalue
+ * There is nothing to check on rec variables
+ */
+ static void
+ check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec)
+ {
+ int fnum;
+
+ /* there are nothing to check on rec now */
+ if (row != NULL)
+ {
+ for (fnum = 0; fnum < row->nfields; fnum++)
+ {
+ /* skip dropped columns */
+ if (row->varnos[fnum] < 0)
+ continue;
+
+ check_target(estate, row->varnos[fnum]);
+ }
+ }
+ }
+
+ /*
+ * Generate a prepared plan - this is simplyfied copy from pl_exec.c
+ * Is not necessary to check simple plan
+ */
+ static void
+ prepare_expr(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *expr, int cursorOptions)
+ {
+ SPIPlanPtr plan;
+
+ /* leave when there are not expression */
+ if (expr == NULL)
+ return;
+
+ /* leave when plan is created */
+ if (expr->plan != NULL)
+ return;
+
+ /*
+ * The grammar can't conveniently set expr->func while building the parse
+ * tree, so make sure it's set before parser hooks need it.
+ */
+ expr->func = estate->func;
+
+ /*
+ * Generate and save the plan
+ */
+ plan = SPI_prepare_params(expr->query,
+ (ParserSetupHook) plpgsql_parser_setup,
+ (void *) expr,
+ cursorOptions);
+ if (plan == NULL)
+ {
+ /* Some SPI errors deserve specific error messages */
+ switch (SPI_result)
+ {
+ case SPI_ERROR_COPY:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot COPY to/from client in PL/pgSQL")));
+ case SPI_ERROR_TRANSACTION:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot begin/end transactions in PL/pgSQL"),
+ errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
+ default:
+ elog(ERROR, "SPI_prepare_params failed for \"%s\": %s",
+ expr->query, SPI_result_code_string(SPI_result));
+ }
+ }
+
+ expr->plan = SPI_saveplan(plan);
+ SPI_freeplan(plan);
+ }
+
+ /*
+ * Verify a expression
+ */
+ static void
+ check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
+ {
+ TupleDesc tupdesc;
+
+ if (expr != NULL)
+ {
+ prepare_expr(estate, expr, 0);
+ tupdesc = expr_get_desc(estate, expr, false, false, true);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+
+ /*
+ * We have to assign TupleDesc to all used record variables step by step.
+ * We would to use a exec routines for query preprocessing, so we must
+ * to create a typed NULL value, and this value is assigned to record
+ * variable.
+ */
+ static void
+ assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
+ PLpgSQL_row *row, PLpgSQL_rec *rec,
+ TupleDesc tupdesc)
+ {
+ bool *nulls;
+ HeapTuple tup;
+
+ if (tupdesc == NULL)
+ elog(ERROR, "tuple descriptor is empty");
+
+ /*
+ * row variable has assigned TupleDesc already, so don't be processed
+ * here
+ */
+ if (rec != NULL)
+ {
+ PLpgSQL_rec *target = (PLpgSQL_rec *)(estate->datums[rec->dno]);
+
+ if (target->freetup)
+ heap_freetuple(target->tup);
+
+ if (rec->freetupdesc)
+ FreeTupleDesc(target->tupdesc);
+
+ /* initialize rec by NULLs */
+ nulls = (bool *) palloc(tupdesc->natts * sizeof(bool));
+ memset(nulls, true, tupdesc->natts * sizeof(bool));
+
+ target->tupdesc = CreateTupleDescCopy(tupdesc);
+ target->freetupdesc = true;
+
+ tup = heap_form_tuple(tupdesc, NULL, nulls);
+ if (HeapTupleIsValid(tup))
+ {
+ target->tup = tup;
+ target->freetup = true;
+ }
+ else
+ elog(ERROR, "cannot to build valid composite value");
+ }
+ }
+
+ /*
+ * Assign a tuple descriptor to variable specified by dno
+ */
+ static void
+ assign_tupdesc_dno(PLpgSQL_execstate *estate, int varno, TupleDesc tupdesc)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ if (target->dtype == PLPGSQL_DTYPE_REC)
+ assign_tupdesc_row_or_rec(estate, NULL, (PLpgSQL_rec *) target, tupdesc);
+ }
+
+ /*
+ * Returns a tuple descriptor based on existing plan
+ */
+ static TupleDesc
+ expr_get_desc(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *query,
+ bool use_element_type,
+ bool expand_record,
+ bool is_expression)
+ {
+ TupleDesc tupdesc = NULL;
+ CachedPlanSource *plansource = NULL;
+
+ if (query->plan != NULL)
+ {
+ SPIPlanPtr plan = query->plan;
+
+ if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
+ elog(ERROR, "cached plan is not valid plan");
+
+ if (list_length(plan->plancache_list) != 1)
+ elog(ERROR, "plan is not single execution plan");
+
+ plansource = (CachedPlanSource *) linitial(plan->plancache_list);
+
+ tupdesc = CreateTupleDescCopy(plansource->resultDesc);
+ }
+ else
+ elog(ERROR, "there are no plan for query: \"%s\"",
+ query->query);
+
+ /*
+ * try to get a element type, when result is a array (used with FOREACH ARRAY stmt)
+ */
+ if (use_element_type)
+ {
+ Oid elemtype;
+ TupleDesc elemtupdesc;
+
+ /* result should be a array */
+ if (tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /* check the type of the expression - must be an array */
+ elemtype = get_element_type(tupdesc->attrs[0]->atttypid);
+ if (!OidIsValid(elemtype))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("FOREACH expression must yield an array, not type %s",
+ format_type_be(tupdesc->attrs[0]->atttypid))));
+
+ /* we can't know typmod now */
+ elemtupdesc = lookup_rowtype_tupdesc_noerror(elemtype, -1, true);
+ if (elemtupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(elemtupdesc);
+ ReleaseTupleDesc(elemtupdesc);
+ }
+ else
+ elog(ERROR, "cannot to identify real type for record type variable");
+ }
+
+ if (is_expression && tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /*
+ * One spacial case is when record is assigned to composite type, then
+ * we should to unpack composite type.
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 && expand_record)
+ {
+ TupleDesc unpack_tupdesc;
+
+ unpack_tupdesc = lookup_rowtype_tupdesc_noerror(tupdesc->attrs[0]->atttypid,
+ tupdesc->attrs[0]->atttypmod,
+ true);
+ if (unpack_tupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(unpack_tupdesc);
+ ReleaseTupleDesc(unpack_tupdesc);
+ }
+ }
+
+ /*
+ * There is special case, when returned tupdesc contains only
+ * unpined record: rec := func_with_out_parameters(). IN this case
+ * we must to dig more deep - we have to find oid of function and
+ * get their parameters,
+ *
+ * This is support for assign statement
+ * recvar := func_with_out_parameters(..)
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 &&
+ tupdesc->attrs[0]->atttypid == RECORDOID &&
+ tupdesc->attrs[0]->atttypmod == -1 &&
+ expand_record)
+ {
+ PlannedStmt *_stmt;
+ Plan *_plan;
+ TargetEntry *tle;
+ CachedPlan *cplan;
+
+ /*
+ * When tupdesc is related to unpined record, we will try
+ * to check plan if it is just function call and if it is
+ * then we can try to derive a tupledes from function's
+ * description.
+ */
+ cplan = GetCachedPlan(plansource, NULL, true);
+ _stmt = (PlannedStmt *) linitial(cplan->stmt_list);
+
+ if (IsA(_stmt, PlannedStmt) && _stmt->commandType == CMD_SELECT)
+ {
+ _plan = _stmt->planTree;
+ if (IsA(_plan, Result) && list_length(_plan->targetlist) == 1)
+ {
+ tle = (TargetEntry *) linitial(_plan->targetlist);
+ if (((Node *) tle->expr)->type == T_FuncExpr)
+ {
+ FuncExpr *fn = (FuncExpr *) tle->expr;
+ FmgrInfo flinfo;
+ FunctionCallInfoData fcinfo;
+ TupleDesc rd;
+ Oid rt;
+
+ fmgr_info(fn->funcid, &flinfo);
+ flinfo.fn_expr = (Node *) fn;
+ fcinfo.flinfo = &flinfo;
+
+ get_call_result_type(&fcinfo, &rt, &rd);
+ if (rd == NULL)
+ elog(ERROR, "function does not return composite type is not possible to identify composite type");
+
+ FreeTupleDesc(tupdesc);
+ BlessTupleDesc(rd);
+
+ tupdesc = rd;
+ }
+ }
+ }
+
+ ReleaseCachedPlan(cplan, true);
+ }
+
+ return tupdesc;
+ }
+
+ /*
+ * Ensure check for all statements in list
+ */
+ static void
+ check_stmts(PLpgSQL_execstate *estate, List *stmts)
+ {
+ ListCell *lc;
+
+ foreach(lc, stmts)
+ {
+ check_stmt(estate, (PLpgSQL_stmt *) lfirst(lc));
+ }
+ }
+
+ /*
+ * walk over all statements
+ */
+ static void
+ check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
+ {
+ TupleDesc tupdesc = NULL;
+ PLpgSQL_function *func;
+ ListCell *l;
+
+ if (stmt == NULL)
+ return;
+
+ estate->err_stmt = stmt;
+ func = estate->func;
+
+ switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
+ {
+ case PLPGSQL_STMT_BLOCK:
+ {
+ PLpgSQL_stmt_block *stmt_block = (PLpgSQL_stmt_block *) stmt;
+ int i;
+ PLpgSQL_datum *d;
+
+ for (i = 0; i < stmt_block->n_initvars; i++)
+ {
+ d = func->datums[stmt_block->initvarnos[i]];
+
+ if (d->dtype == PLPGSQL_DTYPE_VAR)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) d;
+
+ check_expr(estate, var->default_val);
+ }
+ }
+
+ check_stmts(estate, stmt_block->body);
+
+ if (stmt_block->exceptions)
+ {
+ foreach(l, stmt_block->exceptions->exc_list)
+ {
+ check_stmts(estate, ((PLpgSQL_exception *) lfirst(l))->action);
+ }
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_ASSIGN:
+ {
+ PLpgSQL_stmt_assign *stmt_assign = (PLpgSQL_stmt_assign *) stmt;
+
+ /* prepare plan if desn't exist yet */
+ prepare_expr(estate, stmt_assign->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_assign->expr,
+ false, /* no element type */
+ true, /* expand record */
+ true); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_target(estate, stmt_assign->varno);
+
+ /* assign a tupdesc to record variable */
+ assign_tupdesc_dno(estate, stmt_assign->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_IF:
+ {
+ PLpgSQL_stmt_if *stmt_if = (PLpgSQL_stmt_if *) stmt;
+ ListCell *l;
+
+ check_expr(estate, stmt_if->cond);
+
+ check_stmts(estate, stmt_if->then_body);
+
+ foreach(l, stmt_if->elsif_list)
+ {
+ PLpgSQL_if_elsif *elif = (PLpgSQL_if_elsif *) lfirst(l);
+
+ check_expr(estate, elif->cond);
+ check_stmts(estate, elif->stmts);
+ }
+
+ check_stmts(estate, stmt_if->else_body);
+ }
+ break;
+
+ case PLPGSQL_STMT_CASE:
+ {
+ PLpgSQL_stmt_case *stmt_case = (PLpgSQL_stmt_case *) stmt;
+ Oid result_oid;
+
+ if (stmt_case->t_expr != NULL)
+ {
+ PLpgSQL_var *t_var = (PLpgSQL_var *) estate->datums[stmt_case->t_varno];
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_case->t_expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_case->t_expr,
+ false, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ result_oid = tupdesc->attrs[0]->atttypid;
+
+ /*
+ * When expected datatype is different from real, change it. Note that
+ * what we're modifying here is an execution copy of the datum, so
+ * this doesn't affect the originally stored function parse tree.
+ */
+
+ if (t_var->datatype->typoid != result_oid)
+ t_var->datatype = plpgsql_build_datatype(result_oid,
+ -1,
+ estate->func->fn_input_collation);
+
+ ReleaseTupleDesc(tupdesc);
+ }
+
+ foreach(l, stmt_case->case_when_list)
+ {
+ PLpgSQL_case_when *cwt = (PLpgSQL_case_when *) lfirst(l);
+
+ check_expr(estate, cwt->expr);
+ check_stmts(estate, cwt->stmts);
+ }
+
+ check_stmts(estate, stmt_case->else_stmts);
+ }
+ break;
+
+ case PLPGSQL_STMT_LOOP:
+ check_stmts(estate, ((PLpgSQL_stmt_loop *) stmt)->body);
+ break;
+
+ case PLPGSQL_STMT_WHILE:
+ {
+ PLpgSQL_stmt_while *stmt_while = (PLpgSQL_stmt_while *) stmt;
+
+ check_expr(estate, stmt_while->cond);
+ check_stmts(estate, stmt_while->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORI:
+ {
+ PLpgSQL_stmt_fori *stmt_fori = (PLpgSQL_stmt_fori *) stmt;
+
+ check_expr(estate, stmt_fori->lower);
+ check_expr(estate, stmt_fori->upper);
+ check_expr(estate, stmt_fori->step);
+
+ check_stmts(estate, stmt_fori->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORS:
+ {
+ PLpgSQL_stmt_fors *stmt_fors = (PLpgSQL_stmt_fors *) stmt;
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_fors->query, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_fors->query,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_fors->row, stmt_fors->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fors->row, stmt_fors->rec, tupdesc);
+
+ check_stmts(estate, stmt_fors->body);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORC:
+ {
+ PLpgSQL_stmt_forc *stmt_forc = (PLpgSQL_stmt_forc *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_forc->curvar];
+
+ prepare_expr(estate, stmt_forc->argquery, 0);
+
+ if (var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_forc->row, stmt_forc->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_forc->row, stmt_forc->rec, tupdesc);
+ }
+
+ check_stmts(estate, stmt_forc->body);
+ if (tupdesc != NULL)
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNFORS:
+ {
+ PLpgSQL_stmt_dynfors * stmt_dynfors = (PLpgSQL_stmt_dynfors *) stmt;
+
+ if (stmt_dynfors->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_expr(estate, stmt_dynfors->query);
+
+ foreach(l, stmt_dynfors->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ check_stmts(estate, stmt_dynfors->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FOREACH_A:
+ {
+ PLpgSQL_stmt_foreach_a *stmt_foreach_a = (PLpgSQL_stmt_foreach_a *) stmt;
+
+ prepare_expr(estate, stmt_foreach_a->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_foreach_a->expr,
+ true, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ check_target(estate, stmt_foreach_a->varno);
+ assign_tupdesc_dno(estate, stmt_foreach_a->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+
+ check_stmts(estate, stmt_foreach_a->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_EXIT:
+ check_expr(estate, ((PLpgSQL_stmt_exit *) stmt)->cond);
+ break;
+
+ case PLPGSQL_STMT_PERFORM:
+ prepare_expr(estate, ((PLpgSQL_stmt_perform *) stmt)->expr, 0);
+ break;
+
+ case PLPGSQL_STMT_RETURN:
+ check_expr(estate, ((PLpgSQL_stmt_return *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_NEXT:
+ check_expr(estate, ((PLpgSQL_stmt_return_next *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_QUERY:
+ {
+ PLpgSQL_stmt_return_query *stmt_rq = (PLpgSQL_stmt_return_query *) stmt;
+
+ check_expr(estate, stmt_rq->dynquery);
+ prepare_expr(estate, stmt_rq->query, 0);
+
+ foreach(l, stmt_rq->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_RAISE:
+ {
+ PLpgSQL_stmt_raise *stmt_raise = (PLpgSQL_stmt_raise *) stmt;
+ ListCell *current_param;
+ char *cp;
+
+ foreach(l, stmt_raise->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ foreach(l, stmt_raise->options)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ current_param = list_head(stmt_raise->params);
+
+ /* ensure any single % has a own parameter */
+ if (stmt_raise->message != NULL)
+ {
+ for (cp = stmt_raise->message; *cp; cp++)
+ {
+ if (cp[0] == '%')
+ {
+ if (cp[1] == '%')
+ {
+ cp++;
+ continue;
+ }
+
+ if (current_param == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too few parameters specified for RAISE")));
+
+ current_param = lnext(current_param);
+ }
+ }
+ }
+
+ if (current_param != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too many parameters specified for RAISE")));
+ }
+ break;
+
+ case PLPGSQL_STMT_EXECSQL:
+ {
+ PLpgSQL_stmt_execsql *stmt_execsql = (PLpgSQL_stmt_execsql *) stmt;
+
+ prepare_expr(estate, stmt_execsql->sqlstmt, 0);
+ if (stmt_execsql->into)
+ {
+ tupdesc = expr_get_desc(estate,
+ stmt_execsql->sqlstmt,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNEXECUTE:
+ {
+ PLpgSQL_stmt_dynexecute *stmt_dynexecute = (PLpgSQL_stmt_dynexecute *) stmt;
+
+ check_expr(estate, stmt_dynexecute->query);
+
+ foreach(l, stmt_dynexecute->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ if (stmt_dynexecute->into)
+ {
+ if (stmt_dynexecute->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_row_or_rec(estate, stmt_dynexecute->row, stmt_dynexecute->rec);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_OPEN:
+ {
+ PLpgSQL_stmt_open *stmt_open = (PLpgSQL_stmt_open *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_open->curvar];
+
+ if (var->cursor_explicit_expr)
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ prepare_expr(estate, stmt_open->query, 0);
+ prepare_expr(estate, stmt_open->argquery, 0);
+ check_expr(estate, stmt_open->dynquery);
+
+ foreach(l, stmt_open->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_GETDIAG:
+ {
+ PLpgSQL_stmt_getdiag *stmt_getdiag = (PLpgSQL_stmt_getdiag *) stmt;
+ ListCell *lc;
+
+ foreach(lc, stmt_getdiag->diag_items)
+ {
+ PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
+
+ check_target(estate, diag_item->target);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_FETCH:
+ {
+ PLpgSQL_stmt_fetch *stmt_fetch = (PLpgSQL_stmt_fetch *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *)(estate->datums[stmt_fetch->curvar]);
+
+ if (var != NULL && var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+ check_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_CLOSE:
+ break;
+
+ default:
+ elog(ERROR, "unrecognized cmd_type: %d", stmt->cmd_type);
+ return; /* be compiler quite */
+ }
+ }
+
+ /*
+ * Initialize variable to NULL
+ */
+ static void
+ var_init_to_null(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) estate->datums[varno];
+ var->value = (Datum) 0;
+ var->isnull = true;
+ var->freeval = false;
+ }
*** ./src/pl/plpgsql/src/pl_handler.c.orig 2011-11-29 19:20:59.494116771 +0100
--- ./src/pl/plpgsql/src/pl_handler.c 2011-11-29 19:21:24.529804431 +0100
***************
*** 312,314 ****
--- 312,452 ----
PG_RETURN_VOID();
}
+
+ /* ----------
+ * plpgsql_checker
+ *
+ * This function attempts to check a embeded SQL inside a PL/pgSQL function at
+ * CHECK FUNCTION time. It should to have one or two parameters. Second
+ * parameter is a relation (used when function is trigger).
+ * ----------
+ */
+ PG_FUNCTION_INFO_V1(plpgsql_checker);
+
+ Datum
+ plpgsql_checker(PG_FUNCTION_ARGS)
+ {
+ Oid funcoid = PG_GETARG_OID(0);
+ Oid relid = PG_GETARG_OID(1);
+ HeapTuple tuple;
+ FunctionCallInfoData fake_fcinfo;
+ FmgrInfo flinfo;
+ TriggerData trigdata;
+ int rc;
+ PLpgSQL_function *function;
+ PLpgSQL_execstate *cur_estate;
+
+ Form_pg_proc proc;
+ char functyptype;
+ bool istrigger = false;
+
+ /* we don't need to repair a check done by validator */
+
+ tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
+ if (!HeapTupleIsValid(tuple))
+ elog(ERROR, "cache lookup failed for function %u", funcoid);
+ proc = (Form_pg_proc) GETSTRUCT(tuple);
+
+ functyptype = get_typtype(proc->prorettype);
+
+ if (functyptype == TYPTYPE_PSEUDO)
+ {
+ /* we assume OPAQUE with no arguments means a trigger */
+ if (proc->prorettype == TRIGGEROID ||
+ (proc->prorettype == OPAQUEOID && proc->pronargs == 0))
+ {
+ istrigger = true;
+ if (!OidIsValid(relid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("PL/pgSQL trigger functions cannot be checked directly"),
+ errhint("use CHECK TRIGGER statement instead")));
+ }
+ }
+
+ /*
+ * Connect to SPI manager
+ */
+ if ((rc = SPI_connect()) != SPI_OK_CONNECT)
+ elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
+
+ /*
+ * Set up a fake fcinfo with just enough info to satisfy
+ * plpgsql_compile().
+ *
+ * there should be a different real argtypes for polymorphic params
+ */
+ MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo));
+ MemSet(&flinfo, 0, sizeof(flinfo));
+ fake_fcinfo.flinfo = &flinfo;
+ flinfo.fn_oid = funcoid;
+ flinfo.fn_mcxt = CurrentMemoryContext;
+
+ if (istrigger)
+ {
+ MemSet(&trigdata, 0, sizeof(trigdata));
+ trigdata.type = T_TriggerData;
+ trigdata.tg_relation = relation_open(relid, AccessShareLock);
+ fake_fcinfo.context = (Node *) &trigdata;
+ }
+
+ /* Get a compiled function */
+ function = plpgsql_compile(&fake_fcinfo, false);
+
+ /* Must save and restore prior value of cur_estate */
+ cur_estate = function->cur_estate;
+
+ /* Mark the function as busy, so it can't be deleted from under us */
+ function->use_count++;
+
+
+ /* Create a fake runtime environment and prepare plans */
+ PG_TRY();
+ {
+ if (!istrigger)
+ plpgsql_check_function(function, &fake_fcinfo);
+ else
+ plpgsql_check_trigger(function, &trigdata);
+ }
+ PG_CATCH();
+ {
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ /*
+ * Disconnect from SPI manager
+ */
+ if ((rc = SPI_finish()) != SPI_OK_FINISH)
+ elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
+
+ ReleaseSysCache(tuple);
+
+ PG_RETURN_VOID();
+ }
*** ./src/pl/plpgsql/src/plpgsql.h.orig 2011-11-29 19:20:59.500116698 +0100
--- ./src/pl/plpgsql/src/plpgsql.h 2011-11-29 20:22:19.423516596 +0100
***************
*** 902,907 ****
--- 902,908 ----
extern void plpgsql_adddatum(PLpgSQL_datum *new);
extern int plpgsql_add_initdatums(int **varnos);
extern void plpgsql_HashTableInit(void);
+ extern void plpgsql_delete_function(PLpgSQL_function *func);
/* ----------
* Functions in pl_handler.c
***************
*** 911,916 ****
--- 912,918 ----
extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_inline_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
+ extern Datum plpgsql_checker(PG_FUNCTION_ARGS);
/* ----------
* Functions in pl_exec.c
***************
*** 928,933 ****
--- 930,939 ----
extern void exec_get_datum_type_info(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum,
Oid *typeid, int32 *typmod, Oid *collation);
+ extern void plpgsql_check_function(PLpgSQL_function *func,
+ FunctionCallInfo fcinfo);
+ extern void plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata);
/* ----------
* Functions for namespace handling in pl_funcs.c
*** ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql.orig 2011-11-29 19:20:59.502116672 +0100
--- ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql 2011-11-29 19:21:24.533804381 +0100
***************
*** 5,7 ****
--- 5,8 ----
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_call_handler();
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_inline_handler(internal);
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_validator(oid);
+ ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_checker(oid, regclass);
*** ./src/test/regress/expected/plpgsql.out.orig 2011-11-29 19:20:59.505116634 +0100
--- ./src/test/regress/expected/plpgsql.out 2011-11-29 19:21:24.536804342 +0100
***************
*** 302,307 ****
--- 302,310 ----
' language plpgsql;
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
+ NOTICE: checking function "tg_hslot_biu()"
-- ************************************************************
-- * BEFORE DELETE on HSlot
-- * - prevent from manual manipulation
***************
*** 635,640 ****
--- 638,645 ----
raise exception ''illegal backlink beginning with %'', mytype;
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
-- ************************************************************
-- * Support function to clear out the backlink field if
-- * it still points to specific slot
***************
*** 2802,2807 ****
--- 2807,2840 ----
(1 row)
+ -- check function should not fail
+ check function for_vect();
+ -- recheck after check function
+ select for_vect();
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 4
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1 bb cc
+ NOTICE: 2 bb cc
+ NOTICE: 3 bb cc
+ NOTICE: 4 bb cc
+ for_vect
+ ----------
+
+ (1 row)
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 3283,3288 ****
--- 3316,3323 ----
return;
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
select forc01();
NOTICE: 5 from c
NOTICE: 6 from c
***************
*** 3716,3721 ****
--- 3751,3758 ----
end case;
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
select case_test(1);
case_test
-----------
***************
*** 4571,4573 ****
--- 4608,4942 ----
CONTEXT: PL/pgSQL function "testoa" line 5 at assignment
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+ --
+ -- check function statement tests
+ --
+ create table t1(a int, b int);
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: column "c" of relation "t1" does not exist
+ LINE 1: update t1 set c = 30
+ ^
+ QUERY: update t1 set c = 30
+ CONTEXT: checking of PL/pgSQL function "f1" line 4 at SQL statement
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ checking of PL/pgSQL function "f1" line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ checking of PL/pgSQL function "f1" line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: record "r" has no field "c"
+ CONTEXT: checking of PL/pgSQL function "f1" line 6 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: column "a" does not exist
+ LINE 1: SELECT a + b
+ ^
+ QUERY: SELECT a + b
+ CONTEXT: checking of PL/pgSQL function "f1" line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: too many parameters specified for RAISE
+ CONTEXT: checking of PL/pgSQL function "f1" line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: too few parameters specified for RAISE
+ CONTEXT: checking of PL/pgSQL function "f1" line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: column "c" does not exist
+ LINE 1: SELECT c+10
+ ^
+ QUERY: SELECT c+10
+ CONTEXT: checking of PL/pgSQL function "f1" line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: subscripted object is not an array
+ CONTEXT: checking of PL/pgSQL function "f1" line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: record "_exception" has no field "hint"
+ CONTEXT: checking of PL/pgSQL function "f1" line 7 at GET DIAGNOSTICS
+ drop function f1();
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ NOTICE: checking function "f1_trg()"
+ ERROR: record "new" has no field "c"
+ CONTEXT: SQL statement "SELECT new.c"
+ checking of PL/pgSQL function "f1_trg" line 5 at RAISE
+ insert into t1 values(6,30);
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- should to fail
+ check trigger t1_f1 on t1;
+ NOTICE: checking function "f1_trg()"
+ ERROR: record "new" has no field "c"
+ CONTEXT: checking of PL/pgSQL function "f1_trg" line 5 at assignment
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+ ERROR: record "new" has no field "c"
+ CONTEXT: PL/pgSQL function "f1_trg" line 5 at assignment
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- ok
+ check trigger t1_f1 on t1;
+ NOTICE: checking function "f1_trg()"
+ -- ok
+ insert into t1 values(6,30);
+ drop table t1;
+ drop type _exception_type;
+ drop function f1_trg();
*** ./src/test/regress/sql/plpgsql.sql.orig 2011-11-29 19:20:59.508116598 +0100
--- ./src/test/regress/sql/plpgsql.sql 2011-11-29 19:21:24.538804318 +0100
***************
*** 366,371 ****
--- 366,373 ----
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
-- ************************************************************
-- * BEFORE DELETE on HSlot
***************
*** 747,752 ****
--- 749,757 ----
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
+
-- ************************************************************
-- * Support function to clear out the backlink field if
***************
*** 2335,2340 ****
--- 2340,2352 ----
select for_vect();
+ -- check function should not fail
+ check function for_vect();
+
+ -- recheck after check function
+ select for_vect();
+
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 2714,2719 ****
--- 2726,2734 ----
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
+
select forc01();
-- try updating the cursor's current row
***************
*** 3048,3053 ****
--- 3063,3071 ----
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
+
select case_test(1);
select case_test(2);
select case_test(3);
***************
*** 3600,3602 ****
--- 3618,3862 ----
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+
+ --
+ -- check function statement tests
+ --
+
+ create table t1(a int, b int);
+
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+
+ drop function f1();
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- should to fail
+ check trigger t1_f1 on t1;
+
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- ok
+ check trigger t1_f1 on t1;
+
+ -- ok
+ insert into t1 values(6,30);
+
+ drop table t1;
+ drop type _exception_type;
+
+ drop function f1_trg();
+
2011/11/29 Pavel Stehule <pavel.stehule@gmail.com>:
Hello
updated patch:
* recheck compilation and initdb
* working routines moved to pl_exec.c
* add entry to catalog.sgml about lanchecker field
* add node's utils
+ pg_dump support
Pavel
Show quoted text
Regards
Pavel Stehule
2011/11/29 Albe Laurenz <laurenz.albe@wien.gv.at>:
Pavel Stehule wrote:
I am sending updated patch, that implements a CHECK FUNCTION and CHECK
TRIGGER statements.This patch is significantly redesigned to previous version (PL/pgSQL
part) - it is more readable, more accurate. There are new regress
tests.Please, can some English native speaker fix doc and comments?
ToDo:
CHECK FUNCTION search function according to function signature - it
should be changes for using a actual types - it can be solution for
polymorphic types and useful tool for work with overloaded functions -
when is not clean, that function was executed.check function foo(int, int);
NOTICE: checking function foo(variadic anyarray)
...and maybe some support for named parameters
check function foo(name text, surname text);
NOTICE: checking function foo(text, text, text, text)
...I think that CHECK FUNCTION should work exactly like DROP FUNCTION
in these respects.Submission review:
------------------The patch is context diff, applies with some offsets, contains
regression tests and documentation.The documentation should be expanded, the doc for CHECK FUNCTION
is only a stub. It should describe the procedure and what is checked.
That would also make reviewing easier.
I think that some documentation should be added to plhandler.sgml.
There is a spelling error (statemnt) in the docs.Usability review:
-----------------If I understand right, the goal of CHECK FUNCTION is to find errors in
the function definition without actually having to execute it.
The patch tries to provide this for PL/pgSQL.There hasn't been any discussion on the list, the patch was just posted,
so I can't say that we want that. Tom added it to the commitfest page,
so there's one important voice against dismissing it right away :^)I don't understand the functional difference between a "validator function"
and a "check function" as proposed by this patch. I am probably missing
something, but why couldn't these checks be added to function validation
when check_function_bodies is set?
A new "CHECK FUNCTION" statement could simply call the validator function.I don't see any pg_dump support in this patch, and PL/pgSQL probably doesn't
need that, but I think pg_dump support for CREATE LANGUAGE would have to
be added for other PLs.I can't test if the functionality is complete because I can't get it to
run (see below).Feature test:
-------------I can't really test the patch because initdb fails:
$ initdb -E UTF8 --locale=de_DE.UTF-8 --lc-messages=en_US.UTF-8 -U postgres /postgres/cvs/dbhome
The files belonging to this database system will be owned by user "laurenz".
This user must also own the server process.The database cluster will be initialized with locales
COLLATE: de_DE.UTF-8
CTYPE: de_DE.UTF-8
MESSAGES: en_US.UTF-8
MONETARY: de_DE.UTF-8
NUMERIC: de_DE.UTF-8
TIME: de_DE.UTF-8
The default text search configuration will be set to "german".creating directory /postgres/cvs/dbhome ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
creating configuration files ... ok
creating template1 database in /postgres/cvs/dbhome/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... FATAL: could not load library "/postgres/cvs/pg92/lib/plpgsql.so": /postgres/cvs/pg92/lib/plpgsql.so: undefined symbol: plpgsql_delete_function
STATEMENT: CREATE EXTENSION plpgsql;child process exited with exit code 1
initdb: removing data directory "/postgres/cvs/dbhome"Coding review:
--------------The patch compiles without warnings.
The comments in the code should be revised, they are bad English.
I can't say if there should be more of them -- I don't know this part of
the code well enough to have a well-founded opinion.I don't think there are any portability issues, but I could not test it.
There are a lot of small changes to pl/plpgsql/src/pl_exec.c, are they all
necessary? For example, why was copy_plpgsql_datum renamed to
plpgsql_copy_datum?I'll mark the patch as "Waiting on Author".
Yours,
Laurenz Albe
Pavel Stehule wrote:
updated patch:
* recheck compilation and initdb
* working routines moved to pl_exec.c
* add entry to catalog.sgml about lanchecker field
* add node's utils
Documentation:
--------------
This patch has no documentation for CHECK FUNCTION or CHECK TRIGGER.
The last patch had at least something.
"\h check function" in psql does not show anything.
The patch should also add documentation about the handler function
to plhandler.sgml. In particular, the difference between the
validator function and the check function should be pointed out.
Usability:
----------
Do I understand right that the reason why the check function is
different from the validator function is that it would be more difficult
to add the checks to the validator function?
Is that a good enough argument? From a user's perspective it is
difficult to see why some checks are performed at function creation
time, while others have to be explicitly checked with CHECK FUNCTION.
I think it would be much more intuitive if CHECK FUNCTION does
the same as function validation with check_function_bodies on.
Submission, Compilation, Regression tests:
------------------------------------------
The patch applies and compiles fine and passes regression tests.
The tests cover the functionality.
"initdb" succeeds.
pg_dump:
--------
pg_dump support does not work right.
If I create a language like this:
CREATE LANGUAGE mylang HANDLER plpgsql_call_handler
INLINE plpgsql_inline_handler
VALIDATOR plpgsql_validator
CHECK plpgsql_checker;
the dump will contain:
CREATE OR REPLACE PROCEDURAL LANGUAGE mylang;
This is not a problem of this patch though (same in 9.1);
it seems that pg_dump support for languages without definition
in pg_pltemplate is broken in general.
Feature test:
-------------
CHECK FUNCTION and CHECK TRIGGER work, I couldn't crash it.
Error messages could be better:
CHECK TRIGGER atrigger;
ERROR: syntax error at or near ";"
LINE 1: CHECK TRIGGER atrigger;
^
Something like "expected keyword 'ON'" might be nice.
There are a lot of things that CHECK FUNCTION does not check, some
examples:
1)
CREATE OR REPLACE FUNCTION t(i integer) RETURNS integer
LANGUAGE plpgsql STRICT AS
$$DECLARE j integer;
BEGIN
IF i=1 THEN
FOR I IN 1..4 BY -1 LOOP
RAISE NOTICE '%', i;
END LOOP;
RETURN -1;
ELSE
RETURN 2*i;
END IF;
END;$$;
CHECK FUNCTION t(integer); -- no error
SELECT t(1);
ERROR: BY value of FOR loop must be greater than zero
CONTEXT: PL/pgSQL function "t" line 4 at FOR with integer loop variable
2)
CREATE OR REPLACE FUNCTION t(i integer) RETURNS integer
LANGUAGE plpgsql STRICT AS
$$DECLARE j integer;
BEGIN
IF i=1 THEN
j=9999999999999999999;
RETURN j;
ELSE
RETURN 2*i;
END IF;
END;$$;
CHECK FUNCTION t(integer); -- no error
SELECT t(1);
ERROR: value "9999999999999999999" is out of range for type integer
CONTEXT: PL/pgSQL function "t" line 4 at assignment
3)
CREATE OR REPLACE FUNCTION t(i integer) RETURNS integer
LANGUAGE plpgsql STRICT AS
$$DECLARE j atable;
BEGIN IF i=1 THEN
j=12;
RETURN j;
ELSE
RETURN 2*i;
END IF;
END;$$;
CHECK FUNCTION t(integer); -- no error
SELECT t(1);
ERROR: cannot assign non-composite value to a row variable
CONTEXT: PL/pgSQL function "t" line 3 at assignment
4)
CREATE TABLE atable(
id integer PRIMARY KEY,
val text NOT NULL
);
INSERT INTO atable VALUES (1, 'eins');
CREATE OR REPLACE FUNCTION atrigger() RETURNS trigger
LANGUAGE plpgsql STRICT AS
$$BEGIN
NEW.id=22;
RETURN NEW;
END;$$;
CREATE TRIGGER atrigger AFTER DELETE ON atable FOR EACH ROW
EXECUTE PROCEDURE atrigger();
CHECK TRIGGER atrigger ON atable; -- no error
NOTICE: checking function "atrigger()"
DELETE FROM atable;
ERROR: record "new" is not assigned yet
DETAIL: The tuple structure of a not-yet-assigned record is indeterminate.
CONTEXT: PL/pgSQL function "atrigger" line 2 at assignment
I can try and come up with more if desired.
Maybe case 2) and 4) cannot reasonably be covered.
It is probably very hard to check everything possible, but the
usefulness of CHECK FUNCTION is proportional to the number of
cases covered.
I'll mark the patch as "Waiting on Author" until there is more
documentation, I understand the answers to the questions
raised in "usability" above, and until it is agreed that the things
checked are sufficient.
Yours,
Laurenz Albe
"Albe Laurenz" <laurenz.albe@wien.gv.at> writes:
Do I understand right that the reason why the check function is
different from the validator function is that it would be more difficult
to add the checks to the validator function?
Is that a good enough argument? From a user's perspective it is
difficult to see why some checks are performed at function creation
time, while others have to be explicitly checked with CHECK FUNCTION.
I think it would be much more intuitive if CHECK FUNCTION does
the same as function validation with check_function_bodies on.
I think the important point here is that we need to support more than
one level of validation, and that the higher levels can't really be
applied by default in CREATE FUNCTION because they may fail on perfectly
valid code.
The real reason why we need a separate check function is that the API
for validators doesn't include any parameter about check level.
It's conceivable that instead of adding a check-function entry point,
we could generalizee check_function_bodies into a more-than-2-level
setting, and expect validators to pay attention to the GUC's value
when deciding how aggressively to validate. However, it's far from
clear to me that that's a more usable definition than having a separate
CHECK FUNCTION command. An example of where a separate CHECK command
could come in handy is: you just did some ALTER TABLE commands, and now
you would like to check if your functions all still work with the
modified table schemas.
[ snip examples of some additional checks that could be made ]
It is probably very hard to check everything possible, but the
usefulness of CHECK FUNCTION is proportional to the number of
cases covered.
I don't think that the initial patch needs to check everything that
could conceivably be checked. We can always add more checking later.
The initial patch obviously has to create the infrastructure for
optional checking, and the specific check that Pavel wants to add
is to run parse analysis on each SQL statement in a plpgsql function.
That seems to me to be a well-defined and useful check, so I think the
scope of the patch is entirely adequate for now.
A bigger issue is that once you think about more than one kind of check,
it becomes apparent that we might need some user-specifiable options to
control which checks are applied. And I see no provision for that here.
This is not something we can add later, at least not without breaking
the API for the check function --- and if we're willing to break API,
why not just add some more parameters to the validator and avoid having
a second function?
On the whole, it might not be a bad idea to have two allowed signatures
for the validator function, rather than inventing an additional column
in pg_language. But the fundamental point IMHO is that there needs to
be a provision to pass language-dependent validation options to the
function, whether it's the existing validator or a separate checker
entry point.
regards, tom lane
On Wed, Nov 30, 2011 at 10:53 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
On the whole, it might not be a bad idea to have two allowed signatures
for the validator function, rather than inventing an additional column
in pg_language. But the fundamental point IMHO is that there needs to
be a provision to pass language-dependent validation options to the
function, whether it's the existing validator or a separate checker
entry point.
Something like:
CHECK FUNCTION proname(proargs) WITH (...fdw-style elastic options...)
?
--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
Hello
CREATE OR REPLACE FUNCTION t(i integer) RETURNS integer
LANGUAGE plpgsql STRICT AS
$$DECLARE j integer;
BEGIN
IF i=1 THEN
FOR I IN 1..4 BY -1 LOOP
RAISE NOTICE '%', i;
END LOOP;
RETURN -1;
ELSE
RETURN 2*i;
END IF;
END;$$;CHECK FUNCTION t(integer); -- no error
SELECT t(1);
ERROR: BY value of FOR loop must be greater than zero
CONTEXT: PL/pgSQL function "t" line 4 at FOR with integer loop variable2)
CREATE OR REPLACE FUNCTION t(i integer) RETURNS integer
LANGUAGE plpgsql STRICT AS
$$DECLARE j integer;
BEGIN
IF i=1 THEN
j=9999999999999999999;
RETURN j;
ELSE
RETURN 2*i;
END IF;
END;$$;CHECK FUNCTION t(integer); -- no error
SELECT t(1);
ERROR: value "9999999999999999999" is out of range for type integer
CONTEXT: PL/pgSQL function "t" line 4 at assignment
This kind of check are little bit difficult. It is solveable but I
would to have a skelet in core, and then this skelet can be enhanced
step by step.
Where is problem? PL/pgSQL usually don't work with numeric constant.
Almost all numbers are expressions - and checking function ensure only
semantic validity of expression, but don't try to evaluate expression.
So isn't possible to check runtime errors now.
Regards
Pavel
Excerpts from Tom Lane's message of mié nov 30 12:53:42 -0300 2011:
A bigger issue is that once you think about more than one kind of check,
it becomes apparent that we might need some user-specifiable options to
control which checks are applied. And I see no provision for that here.
This is not something we can add later, at least not without breaking
the API for the check function --- and if we're willing to break API,
why not just add some more parameters to the validator and avoid having
a second function?
How about
CHECK (parse, names=off) FUNCTION foobar(a, b, c)
--
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support
Robert Haas <robertmhaas@gmail.com> writes:
On Wed, Nov 30, 2011 at 10:53 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
On the whole, it might not be a bad idea to have two allowed signatures
for the validator function, rather than inventing an additional column
in pg_language. �But the fundamental point IMHO is that there needs to
be a provision to pass language-dependent validation options to the
function, whether it's the existing validator or a separate checker
entry point.
Something like:
CHECK FUNCTION proname(proargs) WITH (...fdw-style elastic options...)
Great minds think alike ... that was pretty much exactly the syntax that
was in the back of my mind.
regards, tom lane
2011/11/30 Alvaro Herrera <alvherre@commandprompt.com>:
Excerpts from Tom Lane's message of mié nov 30 12:53:42 -0300 2011:
A bigger issue is that once you think about more than one kind of check,
it becomes apparent that we might need some user-specifiable options to
control which checks are applied. And I see no provision for that here.
This is not something we can add later, at least not without breaking
the API for the check function --- and if we're willing to break API,
why not just add some more parameters to the validator and avoid having
a second function?How about
CHECK (parse, names=off) FUNCTION foobar(a, b, c)
this syntax is relative consistent with EXPLAIN, is it ok for all?
Pavel
Show quoted text
--
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support
Pavel Stehule <pavel.stehule@gmail.com> writes:
2011/11/30 Alvaro Herrera <alvherre@commandprompt.com>:
How about
CHECK (parse, names=off) FUNCTION foobar(a, b, c)
this syntax is relative consistent with EXPLAIN, is it ok for all?
It seems pretty awkward to me, particularly putting the options before
the second keyword of the command --- that could bite us if we ever want
some other flavors of CHECK command. I prefer Robert's suggestion of a
WITH clause at the end.
regards, tom lane
2011/11/30 Tom Lane <tgl@sss.pgh.pa.us>:
Pavel Stehule <pavel.stehule@gmail.com> writes:
2011/11/30 Alvaro Herrera <alvherre@commandprompt.com>:
How about
CHECK (parse, names=off) FUNCTION foobar(a, b, c)this syntax is relative consistent with EXPLAIN, is it ok for all?
It seems pretty awkward to me, particularly putting the options before
the second keyword of the command --- that could bite us if we ever want
some other flavors of CHECK command. I prefer Robert's suggestion of a
WITH clause at the end.
we can provide both versions - as can be fine for people. Is is simple
in parser. I like both variants and I am thinking so much more
important is a API of checker function and behave of CHECK FUNCTION
statement.
Just idea - don't kill me :). Because CHECK FUNCTION is not
destructive , then complete signature is not necessary, and when
function name is unique, then parameters should be optional - it can
be comfortable for manual work, so just CHECK FUNCTION name; can work.
I see a usage for option - a entering parameter's values instead
signature. When I started with overloaded functions, sometimes I had a
problem with identification of function that was executed - CHECK
FUNCTION can help
CHECK FUNCTION name(10,20) WITH (values);
Notice: checking function name(int, int, int default 20)
I would to design API of checker function be friendly to direct call.
There was some ideas to design CHECK FUNCTION for possibility to check
all functions in schema or language. It should be, but we have a
inline statement and system catalog, so anybody can write own scripts
per your requests. And It was one point to decision for separate
checker function from validate function.
Regards
Pavel
Show quoted text
regards, tom lane
Pavel Stehule <pavel.stehule@gmail.com> writes:
2011/11/30 Tom Lane <tgl@sss.pgh.pa.us>:
It seems pretty awkward to me, particularly putting the options before
the second keyword of the command --- that could bite us if we ever want
some other flavors of CHECK command. I prefer Robert's suggestion of a
WITH clause at the end.
we can provide both versions - as can be fine for people. Is is simple
in parser. I like both variants and I am thinking so much more
important is a API of checker function and behave of CHECK FUNCTION
statement.
I think you missed my point: I don't want the options list at the front
because I'm afraid it will prevent us from making good extensions in the
future. Offering both syntaxes does not fix that.
Just idea - don't kill me :). Because CHECK FUNCTION is not
destructive , then complete signature is not necessary, and when
function name is unique, then parameters should be optional - it can
be comfortable for manual work, so just CHECK FUNCTION name; can work.
Well, there was some discussion of having a "bulk check" or wildcard
capability in the CHECK command, but this seems like an awfully
constricted version of that.
The thing I'd prefer to see in the first cut is some notation for "check
all functions owned by me that are in language FOO". The reason for the
language restriction is that if we think the options are
language-specific, there's no reason to believe that different
validators would accept the same options.
regards, tom lane
Hello
I rechecked a possibility to use a validator function together with
checker function.
The main issue is a different interface of both functions. Validator
needs just function oid and uses global variable
check_function_bodies. Checker function needs function oid and
relation oid (possible some other params). When we mix these two
functions together we need a
validator(oid) or validator(oid, oid, variadic "any")
one parameter function is old validator, three parameters function is checker.
Question:
What is a correct signature for this function? We cannot use a
overloading, because we can have only one validator function per
language. We can change a validator to support both forms, and we have
to be carefully and correct if we will work with our validators(3 and
more params) or foreign validators (1 param). We should to support
both (compatibility reasons). We are not careful about validators now
- there are some places, where one parameter is hardly expected - this
should be changed. So using validator for checking doesn't mean
smaller patch, but is true, so these functions has similar semantic -
validator is usually "low level" checker.
What is your opinion?
Regards
Pavel
Pavel Stehule <pavel.stehule@gmail.com> writes:
I rechecked a possibility to use a validator function together with
checker function.
The main issue is a different interface of both functions. Validator
needs just function oid and uses global variable
check_function_bodies. Checker function needs function oid and
relation oid (possible some other params). When we mix these two
functions together we need a
validator(oid) or validator(oid, oid, variadic "any")
Right, although if you want it to be callable from SQL I think that
variadic "any" is too loose.
What is a correct signature for this function? We cannot use a
overloading, because we can have only one validator function per
language.
So? You have one validator function, it has either signature;
if it has the old signature then CHECK isn't supported by the language.
We have plenty of examples of this sort of thing already.
One issue that would need to be considered is how the validator tells
the difference between a CREATE FUNCTION call and a CHECK call with
default parameters (no WITH clause). Those shouldn't do exactly the
same thing, presumably. Maybe that's a sufficient reason to have two
entry points.
regards, tom lane
Hello
updated patch with documentation
Regards
Pavel
Attachments:
check_pl-2011-11-30.difftext/x-patch; charset=US-ASCII; name=check_pl-2011-11-30.diffDownload
*** ./doc/src/sgml/catalogs.sgml.orig 2011-11-29 19:09:02.000000000 +0100
--- ./doc/src/sgml/catalogs.sgml 2011-11-29 20:28:00.571246006 +0100
***************
*** 3652,3657 ****
--- 3652,3668 ----
</row>
<row>
+ <entry><structfield>lanchecker</structfield></entry>
+ <entry><type>oid</type></entry>
+ <entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
+ <entry>
+ This references a language checker function that is responsible
+ for checking a embedded SQL and can provide detailed checking.
+ Zero if no checker is provided.
+ </entry>
+ </row>
+
+ <row>
<entry><structfield>lanacl</structfield></entry>
<entry><type>aclitem[]</type></entry>
<entry></entry>
*** ./doc/src/sgml/ref/allfiles.sgml.orig 2011-11-29 19:20:59.468117093 +0100
--- ./doc/src/sgml/ref/allfiles.sgml 2011-11-29 19:21:24.487804955 +0100
***************
*** 40,45 ****
--- 40,46 ----
<!ENTITY alterView SYSTEM "alter_view.sgml">
<!ENTITY analyze SYSTEM "analyze.sgml">
<!ENTITY begin SYSTEM "begin.sgml">
+ <!ENTITY checkFunction SYSTEM "check_function.sgml">
<!ENTITY checkpoint SYSTEM "checkpoint.sgml">
<!ENTITY close SYSTEM "close.sgml">
<!ENTITY cluster SYSTEM "cluster.sgml">
*** ./doc/src/sgml/ref/create_language.sgml.orig 2011-11-29 19:20:59.470117069 +0100
--- ./doc/src/sgml/ref/create_language.sgml 2011-11-29 19:21:24.488804943 +0100
***************
*** 23,29 ****
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
--- 23,29 ----
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ] [ CHECK <replaceable>checkfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
***************
*** 217,222 ****
--- 217,236 ----
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><literal>CHECK</literal> <replaceable class="parameter">checkfunction</replaceable></term>
+
+ <listitem>
+ <para><replaceable class="parameter">checkfunction</replaceable> is the
+ name of a previously registered function that will be called
+ when a new function in the language is created, to check the
+ function by statemnt <command>CHECK FUNCTION</command> or
+ <command>CHECK TRIGGER</command>.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
*** ./doc/src/sgml/reference.sgml.orig 2011-11-29 19:20:59.471117057 +0100
--- ./doc/src/sgml/reference.sgml 2011-11-29 19:21:24.492804895 +0100
***************
*** 68,73 ****
--- 68,74 ----
&alterView;
&analyze;
&begin;
+ &checkFunction;
&checkpoint;
&close;
&cluster;
*** ./doc/src/sgml/ref/check_function.sgml.orig 2011-11-30 19:31:09.021947003 +0100
--- ./doc/src/sgml/ref/check_function.sgml 2011-11-29 19:21:24.493804882 +0100
***************
*** 0 ****
--- 1,37 ----
+ <!--
+ doc/src/sgml/ref/check_function.sgml
+ -->
+
+ <refentry id="SQL-CHECKFUNCTION">
+ <refmeta>
+ <refentrytitle>CHECK FUNCTION</refentrytitle>
+ <manvolnum>7</manvolnum>
+ <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+ <refname>CHECK FUNCTION</refname>
+ <refpurpose>ensure a deep checking of existing function</refpurpose>
+ </refnamediv>
+
+ <indexterm zone="sql-checkfunction">
+ <primary>CHECK FUNCTION</primary>
+ </indexterm>
+
+ <refsynopsisdiv>
+ <synopsis>
+ CREATE FUNCTION <replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] )
+ | CREATE TRIGGER <replaceable class="parameter">name</replaceable> ON <replaceable class="parameter">tablename</replaceable>
+ </synopsis>
+ </refsynopsisdiv>
+
+ <refsect1 id="sql-checkfunction-description">
+ <title>Description</title>
+
+ <para>
+ <command>CHECK FUNCTION</command> check a existing function.
+ <command>CHECK TRIGGER</command> check a trigger function.
+ </para>
+ </refsect1>
+
+ </refentry>
*** ./src/backend/catalog/pg_proc.c.orig 2011-11-29 19:20:59.474117021 +0100
--- ./src/backend/catalog/pg_proc.c 2011-11-29 19:21:24.494804869 +0100
***************
*** 1101,1103 ****
--- 1101,1104 ----
*newcursorpos = newcp;
return false;
}
+
*** ./src/backend/commands/functioncmds.c.orig 2011-11-29 19:20:59.475117009 +0100
--- ./src/backend/commands/functioncmds.c 2011-11-29 19:21:24.496804843 +0100
***************
*** 44,53 ****
--- 44,55 ----
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_proc_fn.h"
+ #include "catalog/pg_trigger.h"
#include "catalog/pg_type.h"
#include "catalog/pg_type_fn.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
+ #include "commands/trigger.h"
#include "miscadmin.h"
#include "optimizer/var.h"
#include "parser/parse_coerce.h"
***************
*** 60,65 ****
--- 62,68 ----
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
+ #include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
***************
*** 1009,1014 ****
--- 1012,1152 ----
}
}
+ /*
+ * CheckFunction
+ * call a PL checker function when this function exists.
+ */
+ void
+ CheckFunction(CheckFunctionStmt *stmt)
+ {
+ List *functionName = stmt->funcname;
+ List *argTypes = stmt->args; /* list of TypeName nodes */
+ Oid funcOid;
+
+ HeapTuple tup;
+ Form_pg_proc proc;
+
+ HeapTuple languageTuple;
+ Form_pg_language languageStruct;
+ Oid languageChecker;
+ Oid trgOid = InvalidOid;
+ Oid relid = InvalidOid;
+
+ /* when we should to check trigger, then we should to find a trigger handler */
+ if (functionName == NULL)
+ {
+ HeapTuple ht_trig;
+ Form_pg_trigger trigrec;
+ ScanKeyData skey[1];
+ Relation tgrel;
+ SysScanDesc tgscan;
+ char *fname;
+
+ relid = RangeVarGetRelid(stmt->relation, ShareLock, false, false);
+ trgOid = get_trigger_oid(relid, stmt->trgname, false);
+
+ /*
+ * Fetch the pg_trigger tuple by the Oid of the trigger
+ */
+ tgrel = heap_open(TriggerRelationId, AccessShareLock);
+
+ ScanKeyInit(&skey[0],
+ ObjectIdAttributeNumber,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(trgOid));
+
+ tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
+ SnapshotNow, 1, skey);
+
+ ht_trig = systable_getnext(tgscan);
+
+ if (!HeapTupleIsValid(ht_trig))
+ elog(ERROR, "could not find tuple for trigger %u", trgOid);
+
+ trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
+
+ /* we need to know trigger function to get PL checker function */
+ funcOid = trigrec->tgfoid;
+ fname = format_procedure(funcOid);
+ /* Clean up */
+ systable_endscan(tgscan);
+
+ elog(NOTICE, "checking function \"%s\"", fname);
+ pfree(fname);
+
+ heap_close(tgrel, AccessShareLock);
+ }
+ else
+ {
+ /*
+ * Find the function,
+ */
+ funcOid = LookupFuncNameTypeNames(functionName, argTypes, false);
+ }
+
+ tup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
+ if (!HeapTupleIsValid(tup)) /* should not happen */
+ elog(ERROR, "cache lookup failed for function %u", funcOid);
+
+ proc = (Form_pg_proc) GETSTRUCT(tup);
+
+ languageTuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(proc->prolang));
+ Assert(HeapTupleIsValid(languageTuple));
+
+ languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
+ languageChecker = languageStruct->lanchecker;
+
+ /* Check a function body */
+ if (OidIsValid(languageChecker))
+ {
+ ArrayType *set_items = NULL;
+ int save_nestlevel;
+ Datum datum;
+ bool isnull;
+ MemoryContext oldCxt;
+ MemoryContext checkCxt;
+
+ datum = SysCacheGetAttr(PROCOID, tup, Anum_pg_proc_proconfig, &isnull);
+
+ if (!isnull)
+ {
+ /* Set per-function configuration parameters */
+ set_items = (ArrayType *) DatumGetPointer(datum);
+ if (set_items) /* Need a new GUC nesting level */
+ {
+ save_nestlevel = NewGUCNestLevel();
+ ProcessGUCArray(set_items,
+ (superuser() ? PGC_SUSET : PGC_USERSET),
+ PGC_S_SESSION,
+ GUC_ACTION_SAVE);
+ }
+ else
+ save_nestlevel = 0; /* keep compiler quiet */
+ }
+
+ checkCxt = AllocSetContextCreate(CurrentMemoryContext,
+ "Check temporary context",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
+
+ oldCxt = MemoryContextSwitchTo(checkCxt);
+
+ OidFunctionCall2(languageChecker, ObjectIdGetDatum(funcOid),
+ ObjectIdGetDatum(relid));
+
+ MemoryContextSwitchTo(oldCxt);
+
+ if (set_items)
+ AtEOXact_GUC(true, save_nestlevel);
+ }
+ else
+ elog(WARNING, "language \"%s\" has no defined checker function",
+ NameStr(languageStruct->lanname));
+
+ ReleaseSysCache(languageTuple);
+ ReleaseSysCache(tup);
+ }
/*
* Rename function
*** ./src/backend/commands/proclang.c.orig 2011-11-29 19:20:59.477116983 +0100
--- ./src/backend/commands/proclang.c 2011-11-29 19:21:24.497804830 +0100
***************
*** 46,57 ****
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
--- 46,58 ----
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
+ char *tmplchecker; /* name of checker function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
***************
*** 67,75 ****
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid;
Oid funcrettype;
! Oid funcargtypes[1];
/*
* If we have template information for the language, ignore the supplied
--- 68,77 ----
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid,
! checkerOid;
Oid funcrettype;
! Oid funcargtypes[2];
/*
* If we have template information for the language, ignore the supplied
***************
*** 219,228 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, pltemplate->tmpltrusted);
}
else
{
--- 221,269 ----
else
valOid = InvalidOid;
+ /*
+ * Likewise for the checker, if required; but we don't care about
+ * its return type.
+ */
+ if (pltemplate->tmplchecker)
+ {
+ funcname = SystemFuncName(pltemplate->tmplchecker);
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(funcname, 2, funcargtypes, true);
+ if (!OidIsValid(checkerOid))
+ {
+ checkerOid = ProcedureCreate(pltemplate->tmplchecker,
+ PG_CATALOG_NAMESPACE,
+ false, /* replace */
+ false, /* returnsSet */
+ VOIDOID,
+ ClanguageId,
+ F_FMGR_C_VALIDATOR,
+ pltemplate->tmplchecker,
+ pltemplate->tmpllibrary,
+ false, /* isAgg */
+ false, /* isWindowFunc */
+ false, /* security_definer */
+ true, /* isStrict */
+ PROVOLATILE_VOLATILE,
+ buildoidvector(funcargtypes, 2),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ NIL,
+ PointerGetDatum(NULL),
+ 1,
+ 0);
+ }
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, pltemplate->tmpltrusted);
}
else
{
***************
*** 294,303 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, stmt->pltrusted);
}
}
--- 335,355 ----
else
valOid = InvalidOid;
+ /* validate the checker function */
+ if (stmt->plchecker)
+ {
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(stmt->plchecker, 2, funcargtypes, false);
+ /* return value is ignored, so we don't check the type */
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, stmt->pltrusted);
}
}
***************
*** 307,313 ****
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
--- 359,365 ----
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
***************
*** 337,342 ****
--- 389,395 ----
values[Anum_pg_language_lanplcallfoid - 1] = ObjectIdGetDatum(handlerOid);
values[Anum_pg_language_laninline - 1] = ObjectIdGetDatum(inlineOid);
values[Anum_pg_language_lanvalidator - 1] = ObjectIdGetDatum(valOid);
+ values[Anum_pg_language_lanchecker - 1] = ObjectIdGetDatum(checkerOid);
nulls[Anum_pg_language_lanacl - 1] = true;
/* Check for pre-existing definition */
***************
*** 423,428 ****
--- 476,490 ----
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
+ /* dependency on the checker function, if any */
+ if (OidIsValid(checkerOid))
+ {
+ referenced.classId = ProcedureRelationId;
+ referenced.objectId = checkerOid;
+ referenced.objectSubId = 0;
+ recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
+ }
+
/* Post creation hook for new procedural language */
InvokeObjectAccessHook(OAT_POST_CREATE,
LanguageRelationId, myself.objectId, 0);
***************
*** 478,483 ****
--- 540,550 ----
if (!isnull)
result->tmplvalidator = TextDatumGetCString(datum);
+ datum = heap_getattr(tup, Anum_pg_pltemplate_tmplchecker,
+ RelationGetDescr(rel), &isnull);
+ if (!isnull)
+ result->tmplchecker = TextDatumGetCString(datum);
+
datum = heap_getattr(tup, Anum_pg_pltemplate_tmpllibrary,
RelationGetDescr(rel), &isnull);
if (!isnull)
*** ./src/backend/nodes/copyfuncs.c.orig 2011-11-29 19:09:02.000000000 +0100
--- ./src/backend/nodes/copyfuncs.c 2011-11-29 20:17:01.339172458 +0100
***************
*** 2880,2885 ****
--- 2880,2898 ----
return newnode;
}
+ static CheckFunctionStmt *
+ _copyCheckFunctionStmt(CheckFunctionStmt *from)
+ {
+ CheckFunctionStmt *newnode = makeNode(CheckFunctionStmt);
+
+ COPY_NODE_FIELD(funcname);
+ COPY_NODE_FIELD(args);
+ COPY_STRING_FIELD(trgname);
+ COPY_NODE_FIELD(relation);
+
+ return newnode;
+ }
+
static DoStmt *
_copyDoStmt(DoStmt *from)
{
***************
*** 4165,4170 ****
--- 4178,4186 ----
case T_AlterFunctionStmt:
retval = _copyAlterFunctionStmt(from);
break;
+ case T_CheckFunctionStmt:
+ retval = _copyCheckFunctionStmt(from);
+ break;
case T_DoStmt:
retval = _copyDoStmt(from);
break;
*** ./src/backend/nodes/equalfuncs.c.orig 2011-11-29 20:19:55.045587471 +0100
--- ./src/backend/nodes/equalfuncs.c 2011-11-29 20:19:21.850082357 +0100
***************
*** 1292,1297 ****
--- 1292,1308 ----
}
static bool
+ _equalCheckFunctionStmt(CheckFunctionStmt *a, CheckFunctionStmt *b)
+ {
+ COMPARE_NODE_FIELD(funcname);
+ COMPARE_NODE_FIELD(args);
+ COMPARE_STRING_FIELD(trgname);
+ COMPARE_NODE_FIELD(relation);
+
+ return true;
+ }
+
+ static bool
_equalDoStmt(DoStmt *a, DoStmt *b)
{
COMPARE_NODE_FIELD(args);
***************
*** 2708,2713 ****
--- 2719,2727 ----
case T_AlterFunctionStmt:
retval = _equalAlterFunctionStmt(a, b);
break;
+ case T_CheckFunctionStmt:
+ retval = _equalCheckFunctionStmt(a, b);
+ break;
case T_DoStmt:
retval = _equalDoStmt(a, b);
break;
*** ./src/backend/parser/gram.y.orig 2011-11-29 19:09:02.876463248 +0100
--- ./src/backend/parser/gram.y 2011-11-29 19:21:24.502804769 +0100
***************
*** 227,232 ****
--- 227,233 ----
DeallocateStmt PrepareStmt ExecuteStmt
DropOwnedStmt ReassignOwnedStmt
AlterTSConfigurationStmt AlterTSDictionaryStmt
+ CheckFunctionStmt
%type <node> select_no_parens select_with_parens select_clause
simple_select values_clause
***************
*** 276,282 ****
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate
%type <range> qualified_name OptConstrFromTable
--- 277,283 ----
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate opt_checker
%type <range> qualified_name OptConstrFromTable
***************
*** 700,705 ****
--- 701,707 ----
| AlterUserSetStmt
| AlterUserStmt
| AnalyzeStmt
+ | CheckFunctionStmt
| CheckPointStmt
| ClosePortalStmt
| ClusterStmt
***************
*** 3174,3184 ****
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
--- 3176,3187 ----
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
+ n->plchecker = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator opt_checker
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
***************
*** 3186,3191 ****
--- 3189,3195 ----
n->plhandler = $8;
n->plinline = $9;
n->plvalidator = $10;
+ n->plchecker = $11;
n->pltrusted = $3;
$$ = (Node *)n;
}
***************
*** 3220,3225 ****
--- 3224,3234 ----
| /*EMPTY*/ { $$ = NIL; }
;
+ opt_checker:
+ CHECK handler_name { $$ = $2; }
+ | /*EMPTY*/ { $$ = NIL; }
+ ;
+
DropPLangStmt:
DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
{
***************
*** 6250,6255 ****
--- 6259,6294 ----
/*****************************************************************************
*
+ * CHECK FUNCTION funcname(args)
+ * CHECK TRIGGER triggername ON table
+ *
+ *
+ *****************************************************************************/
+
+
+ CheckFunctionStmt:
+ CHECK FUNCTION func_name func_args
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = $3;
+ n->args = extractArgTypes($4);
+ n->trgname = NULL;
+ n->relation = NULL;
+ $$ = (Node *) n;
+ }
+ | CHECK TRIGGER name ON qualified_name
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = $3;
+ n->relation = $5;
+ $$ = (Node *) n;
+ }
+ ;
+
+ /*****************************************************************************
+ *
* DO <anonymous code block> [ LANGUAGE language ]
*
* We use a DefElem list for future extensibility, and to allow flexibility
*** ./src/backend/tcop/utility.c.orig 2011-11-29 19:20:59.480116945 +0100
--- ./src/backend/tcop/utility.c 2011-11-29 19:21:24.513804628 +0100
***************
*** 882,887 ****
--- 882,891 ----
AlterFunction((AlterFunctionStmt *) parsetree);
break;
+ case T_CheckFunctionStmt:
+ CheckFunction((CheckFunctionStmt *) parsetree);
+ break;
+
case T_IndexStmt: /* CREATE INDEX */
{
IndexStmt *stmt = (IndexStmt *) parsetree;
***************
*** 2125,2130 ****
--- 2129,2141 ----
}
break;
+ case T_CheckFunctionStmt:
+ if (((CheckFunctionStmt *) parsetree)->funcname != NULL)
+ tag = "CHECK FUNCTION";
+ else
+ tag = "CHECK TRIGGER";
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
***************
*** 2565,2570 ****
--- 2576,2585 ----
}
break;
+ case T_CheckFunctionStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
*** ./src/bin/pg_dump/pg_dump.c.orig 2011-11-29 19:09:03.000000000 +0100
--- ./src/bin/pg_dump/pg_dump.c 2011-11-29 20:04:31.094156626 +0100
***************
*** 5326,5338 ****
int i_lanplcallfoid;
int i_laninline;
int i_lanvalidator;
int i_lanacl;
int i_lanowner;
/* Make sure we are in proper schema */
selectSourceSchema("pg_catalog");
! if (g_fout->remoteVersion >= 90000)
{
/* pg_language has a laninline column */
appendPQExpBuffer(query, "SELECT tableoid, oid, "
--- 5326,5351 ----
int i_lanplcallfoid;
int i_laninline;
int i_lanvalidator;
+ int i_lanchecker;
int i_lanacl;
int i_lanowner;
/* Make sure we are in proper schema */
selectSourceSchema("pg_catalog");
! if (g_fout->remoteVersion >= 90200)
! {
! /* pg_language has a lanchecker column */
! appendPQExpBuffer(query, "SELECT tableoid, oid, "
! "lanname, lanpltrusted, lanplcallfoid, "
! "laninline, lanvalidator, lanchecker, lanacl, "
! "(%s lanowner) AS lanowner "
! "FROM pg_language "
! "WHERE lanispl "
! "ORDER BY oid",
! username_subquery);
! }
! else if (g_fout->remoteVersion >= 90000)
{
/* pg_language has a laninline column */
appendPQExpBuffer(query, "SELECT tableoid, oid, "
***************
*** 5409,5414 ****
--- 5422,5428 ----
/* these may fail and return -1: */
i_laninline = PQfnumber(res, "laninline");
i_lanvalidator = PQfnumber(res, "lanvalidator");
+ i_lanchecker = PQfnumber(res, "lanchecker");
i_lanacl = PQfnumber(res, "lanacl");
i_lanowner = PQfnumber(res, "lanowner");
***************
*** 5422,5427 ****
--- 5436,5445 ----
planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
+ if (i_lanchecker >= 0)
+ planginfo[i].lanchecker = atooid(PQgetvalue(res, i, i_lanchecker));
+ else
+ planginfo[i].lanchecker = InvalidOid;
if (i_laninline >= 0)
planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
else
***************
*** 8597,8602 ****
--- 8615,8621 ----
char *qlanname;
char *lanschema;
FuncInfo *funcInfo;
+ FuncInfo *checkerInfo = NULL;
FuncInfo *inlineInfo = NULL;
FuncInfo *validatorInfo = NULL;
***************
*** 8616,8621 ****
--- 8635,8647 ----
if (funcInfo != NULL && !funcInfo->dobj.dump)
funcInfo = NULL; /* treat not-dumped same as not-found */
+ if (OidIsValid(plang->lanchecker))
+ {
+ checkerInfo = findFuncByOid(plang->lanchecker);
+ if (checkerInfo != NULL && !checkerInfo->dobj.dump)
+ checkerInfo = NULL;
+ }
+
if (OidIsValid(plang->laninline))
{
inlineInfo = findFuncByOid(plang->laninline);
***************
*** 8642,8647 ****
--- 8668,8674 ----
* don't, this might not work terribly nicely.
*/
useParams = (funcInfo != NULL &&
+ (checkerInfo != NULL || !OidIsValid(plang->lanchecker)) &&
(inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
(validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
***************
*** 8697,8702 ****
--- 8724,8739 ----
appendPQExpBuffer(defqry, "%s",
fmtId(validatorInfo->dobj.name));
}
+ if (OidIsValid(plang->lanchecker))
+ {
+ appendPQExpBuffer(defqry, " CHECK ");
+ /* Cope with possibility that checker is in different schema */
+ if (checkerInfo->dobj.namespace != funcInfo->dobj.namespace)
+ appendPQExpBuffer(defqry, "%s.",
+ fmtId(checkerInfo->dobj.namespace->dobj.name));
+ appendPQExpBuffer(defqry, "%s",
+ fmtId(checkerInfo->dobj.name));
+ }
}
else
{
*** ./src/bin/pg_dump/pg_dump.h.orig 2011-11-29 20:05:48.255044631 +0100
--- ./src/bin/pg_dump/pg_dump.h 2011-11-29 20:05:08.766614345 +0100
***************
*** 387,392 ****
--- 387,393 ----
Oid lanplcallfoid;
Oid laninline;
Oid lanvalidator;
+ Oid lanchecker;
char *lanacl;
char *lanowner; /* name of owner, or empty string */
} ProcLangInfo;
*** ./src/bin/psql/tab-complete.c.orig 2011-11-29 19:20:59.482116921 +0100
--- ./src/bin/psql/tab-complete.c 2011-11-29 19:21:24.516804592 +0100
***************
*** 1,4 ****
--- 1,5 ----
/*
+ *
* psql - the PostgreSQL interactive terminal
*
* Copyright (c) 2000-2011, PostgreSQL Global Development Group
***************
*** 727,733 ****
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
--- 728,734 ----
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECK", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
***************
*** 1524,1529 ****
--- 1525,1552 ----
COMPLETE_WITH_LIST(list_TRANS);
}
+
+ /* CHECK */
+ else if (pg_strcasecmp(prev_wd, "CHECK") == 0)
+ {
+ static const char *const list_CHECK[] =
+ {"FUNCTION", "TRIGGER", NULL};
+
+ COMPLETE_WITH_LIST(list_CHECK);
+ }
+ else if (pg_strcasecmp(prev3_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
+ {
+ COMPLETE_WITH_CONST("ON");
+ }
+ else if (pg_strcasecmp(prev4_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
+ pg_strcasecmp(prev_wd, "ON") == 0)
+ {
+ completion_info_charp = prev2_wd;
+ COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger);
+ }
+
/* CLUSTER */
/*
*** ./src/include/catalog/pg_language.h.orig 2011-11-29 19:20:59.483116909 +0100
--- ./src/include/catalog/pg_language.h 2011-11-29 19:21:24.518804568 +0100
***************
*** 37,42 ****
--- 37,43 ----
Oid lanplcallfoid; /* Call handler for PL */
Oid laninline; /* Optional anonymous-block handler function */
Oid lanvalidator; /* Optional validation function */
+ Oid lanchecker; /* Optional checker function */
aclitem lanacl[1]; /* Access privileges */
} FormData_pg_language;
***************
*** 51,57 ****
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 8
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
--- 52,58 ----
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 9
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
***************
*** 59,78 ****
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanacl 8
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
--- 60,80 ----
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanchecker 8
! #define Anum_pg_language_lanacl 9
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 0 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 0 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 0 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
*** ./src/include/catalog/pg_pltemplate.h.orig 2011-11-29 19:20:59.484116897 +0100
--- ./src/include/catalog/pg_pltemplate.h 2011-11-29 19:21:24.518804568 +0100
***************
*** 36,41 ****
--- 36,42 ----
text tmplhandler; /* name of call handler function */
text tmplinline; /* name of anonymous-block handler, or NULL */
text tmplvalidator; /* name of validator function, or NULL */
+ text tmplchecker; /* name of checker function, or NULL */
text tmpllibrary; /* path of shared library */
aclitem tmplacl[1]; /* access privileges for template */
} FormData_pg_pltemplate;
***************
*** 51,65 ****
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 8
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmpllibrary 7
! #define Anum_pg_pltemplate_tmplacl 8
/* ----------------
--- 52,67 ----
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 9
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmplchecker 7
! #define Anum_pg_pltemplate_tmpllibrary 8
! #define Anum_pg_pltemplate_tmplacl 9
/* ----------------
***************
*** 67,79 ****
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
--- 69,81 ----
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "plpgsql_checker" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" _null_ "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
*** ./src/include/commands/defrem.h.orig 2011-11-29 19:20:59.486116871 +0100
--- ./src/include/commands/defrem.h 2011-11-29 19:21:24.519804556 +0100
***************
*** 62,67 ****
--- 62,68 ----
/* commands/functioncmds.c */
extern void CreateFunction(CreateFunctionStmt *stmt, const char *queryString);
extern void RemoveFunctionById(Oid funcOid);
+ extern void CheckFunction(CheckFunctionStmt *stmt);
extern void SetFunctionReturnType(Oid funcOid, Oid newRetType);
extern void SetFunctionArgType(Oid funcOid, int argIndex, Oid newArgType);
extern void RenameFunction(List *name, List *argtypes, const char *newname);
*** ./src/include/nodes/nodes.h.orig 2011-11-29 19:20:59.487116858 +0100
--- ./src/include/nodes/nodes.h 2011-11-29 19:21:24.521804532 +0100
***************
*** 291,296 ****
--- 291,297 ----
T_IndexStmt,
T_CreateFunctionStmt,
T_AlterFunctionStmt,
+ T_CheckFunctionStmt,
T_DoStmt,
T_RenameStmt,
T_RuleStmt,
*** ./src/include/nodes/parsenodes.h.orig 2011-11-29 19:20:59.489116833 +0100
--- ./src/include/nodes/parsenodes.h 2011-11-29 19:21:24.523804506 +0100
***************
*** 1734,1739 ****
--- 1734,1740 ----
List *plhandler; /* PL call handler function (qual. name) */
List *plinline; /* optional inline function (qual. name) */
List *plvalidator; /* optional validator function (qual. name) */
+ List *plchecker; /* optional checker function (qual. name) */
bool pltrusted; /* PL is trusted */
} CreatePLangStmt;
***************
*** 2077,2082 ****
--- 2078,2096 ----
} AlterFunctionStmt;
/* ----------------------
+ * Check {Function|Trigger} Statement
+ * ----------------------
+ */
+ typedef struct CheckFunctionStmt
+ {
+ NodeTag type;
+ List *funcname; /* qualified name of checked object */
+ List *args; /* types of the arguments */
+ char *trgname; /* trigger's name */
+ RangeVar *relation; /* trigger's relation */
+ } CheckFunctionStmt;
+
+ /* ----------------------
* DO Statement
*
* DoStmt is the raw parser output, InlineCodeBlock is the execution-time API
*** ./src/pl/plpgsql/src/pl_comp.c.orig 2011-11-29 19:09:03.000000000 +0100
--- ./src/pl/plpgsql/src/pl_comp.c 2011-11-29 19:42:43.058753779 +0100
***************
*** 115,121 ****
static void plpgsql_HashTableInsert(PLpgSQL_function *function,
PLpgSQL_func_hashkey *func_key);
static void plpgsql_HashTableDelete(PLpgSQL_function *function);
- static void delete_function(PLpgSQL_function *func);
/* ----------
* plpgsql_compile Make an execution tree for a PL/pgSQL function.
--- 115,120 ----
***************
*** 175,181 ****
* Nope, so remove it from hashtable and try to drop associated
* storage (if not done already).
*/
! delete_function(function);
/*
* If the function isn't in active use then we can overwrite the
--- 174,180 ----
* Nope, so remove it from hashtable and try to drop associated
* storage (if not done already).
*/
! plpgsql_delete_function(function);
/*
* If the function isn't in active use then we can overwrite the
***************
*** 2426,2432 ****
}
/*
! * delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
--- 2425,2431 ----
}
/*
! * plpgsql_delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
***************
*** 2439,2446 ****
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
! static void
! delete_function(PLpgSQL_function *func)
{
/* remove function from hash table (might be done already) */
plpgsql_HashTableDelete(func);
--- 2438,2445 ----
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
! void
! plpgsql_delete_function(PLpgSQL_function *func)
{
/* remove function from hash table (might be done already) */
plpgsql_HashTableDelete(func);
*** ./src/pl/plpgsql/src/pl_exec.c.orig 2011-11-29 19:09:03.316459122 +0100
--- ./src/pl/plpgsql/src/pl_exec.c 2011-11-29 19:37:19.000000000 +0100
***************
*** 210,216 ****
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
PLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
!
/* ----------
* plpgsql_exec_function Called by the call handler for
--- 210,228 ----
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
PLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
! static void check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec);
! static void check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr);
! static void assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
! PLpgSQL_row *row, PLpgSQL_rec *rec,
! TupleDesc tupdesc);
! static TupleDesc expr_get_desc(PLpgSQL_execstate *estate,
! PLpgSQL_expr *query,
! bool use_element_type,
! bool expand_record,
! bool is_expression);
! static void var_init_to_null(PLpgSQL_execstate *estate, int varno);
! static void check_stmts(PLpgSQL_execstate *estate, List *stmts);
! static void check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
/* ----------
* plpgsql_exec_function Called by the call handler for
***************
*** 6176,6178 ****
--- 6188,7242 ----
return portal;
}
+
+ /*
+ * Following code ensures a CHECK FUNCTION and CHECK TRIGGER statements for PL/pgSQL
+ *
+ */
+
+ /*
+ * append a CONTEXT to error message
+ */
+ static void
+ check_error_callback(void *arg)
+ {
+ PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;
+
+ if (estate->err_stmt != NULL)
+ {
+ /* translator: last %s is a plpgsql statement type name */
+ errcontext("checking of PL/pgSQL function \"%s\" line %d at %s",
+ estate->func->fn_name,
+ estate->err_stmt->lineno,
+ plpgsql_stmt_typename(estate->err_stmt));
+ }
+ else
+ errcontext("checking of PL/pgSQL function \"%s\"",
+ estate->func->fn_name);
+ }
+
+ /*
+ * Check function - it prepare variables and starts a prepare plan walker
+ * called by function checker
+ */
+ void
+ plpgsql_check_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
+
+ /*
+ * Store the actual call argument values into the appropriate variables
+ */
+ for (i = 0; i < func->fn_nargs; i++)
+ {
+ int n = func->fn_argvarnos[i];
+
+ switch (estate.datums[n]->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ {
+ var_init_to_null(&estate, n);
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ {
+ PLpgSQL_row *row = (PLpgSQL_row *) estate.datums[n];
+
+ exec_move_row(&estate, NULL, row, NULL, NULL);
+ }
+ break;
+
+ default:
+ elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
+ }
+ }
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Check trigger - prepare fake environments for testing trigger
+ *
+ */
+ void
+ plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ PLpgSQL_rec *rec_new,
+ *rec_old;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, NULL);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
+
+ /*
+ * Put the OLD and NEW tuples into record variables
+ *
+ * We make the tupdescs available in both records even though only one may
+ * have a value. This allows parsing of record references to succeed in
+ * functions that are used for multiple trigger types. For example, we
+ * might have a test like "if (TG_OP = 'INSERT' and NEW.foo = 'xyz')",
+ * which should parse regardless of the current trigger type.
+ */
+ rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
+ rec_new->freetup = false;
+ rec_new->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_new, trigdata->tg_relation->rd_att);
+
+ rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
+ rec_old->freetup = false;
+ rec_old->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_old, trigdata->tg_relation->rd_att);
+
+ /*
+ * Assign the special tg_ variables
+ */
+ var_init_to_null(&estate, func->tg_op_varno);
+ var_init_to_null(&estate, func->tg_name_varno);
+ var_init_to_null(&estate, func->tg_when_varno);
+ var_init_to_null(&estate, func->tg_level_varno);
+ var_init_to_null(&estate, func->tg_relid_varno);
+ var_init_to_null(&estate, func->tg_relname_varno);
+ var_init_to_null(&estate, func->tg_table_name_varno);
+ var_init_to_null(&estate, func->tg_table_schema_varno);
+ var_init_to_null(&estate, func->tg_nargs_varno);
+ var_init_to_null(&estate, func->tg_argv_varno);
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Verify lvalue
+ * It doesn't repeat a checks that are done.
+ * Checks a subscript expressions, verify a validity of record's fields
+ */
+ static void
+ check_target(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ switch (target->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ case PLPGSQL_DTYPE_REC:
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ check_row_or_rec(estate, (PLpgSQL_row *) target, NULL);
+ break;
+
+ case PLPGSQL_DTYPE_RECFIELD:
+ {
+ PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
+ PLpgSQL_rec *rec;
+ int fno;
+
+ rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
+
+ /*
+ * Check that there is already a tuple in the record. We need
+ * that because records don't have any predefined field
+ * structure.
+ */
+ if (!HeapTupleIsValid(rec->tup))
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("record \"%s\" is not assigned to tuple structure",
+ rec->refname)));
+
+ /*
+ * Get the number of the records field to change and the
+ * number of attributes in the tuple. Note: disallow system
+ * column names because the code below won't cope.
+ */
+ fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
+ if (fno <= 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("record \"%s\" has no field \"%s\"",
+ rec->refname, recfield->fieldname)));
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ARRAYELEM:
+ {
+ /*
+ * Target is an element of an array
+ */
+ int nsubscripts;
+ Oid arrayelemtypeid;
+ Oid arraytypeid;
+
+ /*
+ * To handle constructs like x[1][2] := something, we have to
+ * be prepared to deal with a chain of arrayelem datums. Chase
+ * back to find the base array datum, and save the subscript
+ * expressions as we go. (We are scanning right to left here,
+ * but want to evaluate the subscripts left-to-right to
+ * minimize surprises.)
+ */
+ nsubscripts = 0;
+ do
+ {
+ PLpgSQL_arrayelem *arrayelem = (PLpgSQL_arrayelem *) target;
+
+ if (nsubscripts++ >= MAXDIM)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
+ nsubscripts + 1, MAXDIM)));
+
+ check_expr(estate, arrayelem->subscript);
+
+ target = estate->datums[arrayelem->arrayparentno];
+ } while (target->dtype == PLPGSQL_DTYPE_ARRAYELEM);
+
+ /* If target is domain over array, reduce to base type */
+ arraytypeid = exec_get_datum_type(estate, target);
+ arraytypeid = getBaseType(arraytypeid);
+
+ arrayelemtypeid = get_element_type(arraytypeid);
+
+ if (!OidIsValid(arrayelemtypeid))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("subscripted object is not an array")));
+ }
+ break;
+ }
+ }
+
+ /*
+ * Check composed lvalue
+ * There is nothing to check on rec variables
+ */
+ static void
+ check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec)
+ {
+ int fnum;
+
+ /* there are nothing to check on rec now */
+ if (row != NULL)
+ {
+ for (fnum = 0; fnum < row->nfields; fnum++)
+ {
+ /* skip dropped columns */
+ if (row->varnos[fnum] < 0)
+ continue;
+
+ check_target(estate, row->varnos[fnum]);
+ }
+ }
+ }
+
+ /*
+ * Generate a prepared plan - this is simplyfied copy from pl_exec.c
+ * Is not necessary to check simple plan
+ */
+ static void
+ prepare_expr(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *expr, int cursorOptions)
+ {
+ SPIPlanPtr plan;
+
+ /* leave when there are not expression */
+ if (expr == NULL)
+ return;
+
+ /* leave when plan is created */
+ if (expr->plan != NULL)
+ return;
+
+ /*
+ * The grammar can't conveniently set expr->func while building the parse
+ * tree, so make sure it's set before parser hooks need it.
+ */
+ expr->func = estate->func;
+
+ /*
+ * Generate and save the plan
+ */
+ plan = SPI_prepare_params(expr->query,
+ (ParserSetupHook) plpgsql_parser_setup,
+ (void *) expr,
+ cursorOptions);
+ if (plan == NULL)
+ {
+ /* Some SPI errors deserve specific error messages */
+ switch (SPI_result)
+ {
+ case SPI_ERROR_COPY:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot COPY to/from client in PL/pgSQL")));
+ case SPI_ERROR_TRANSACTION:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot begin/end transactions in PL/pgSQL"),
+ errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
+ default:
+ elog(ERROR, "SPI_prepare_params failed for \"%s\": %s",
+ expr->query, SPI_result_code_string(SPI_result));
+ }
+ }
+
+ expr->plan = SPI_saveplan(plan);
+ SPI_freeplan(plan);
+ }
+
+ /*
+ * Verify a expression
+ */
+ static void
+ check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
+ {
+ TupleDesc tupdesc;
+
+ if (expr != NULL)
+ {
+ prepare_expr(estate, expr, 0);
+ tupdesc = expr_get_desc(estate, expr, false, false, true);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+
+ /*
+ * We have to assign TupleDesc to all used record variables step by step.
+ * We would to use a exec routines for query preprocessing, so we must
+ * to create a typed NULL value, and this value is assigned to record
+ * variable.
+ */
+ static void
+ assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
+ PLpgSQL_row *row, PLpgSQL_rec *rec,
+ TupleDesc tupdesc)
+ {
+ bool *nulls;
+ HeapTuple tup;
+
+ if (tupdesc == NULL)
+ elog(ERROR, "tuple descriptor is empty");
+
+ /*
+ * row variable has assigned TupleDesc already, so don't be processed
+ * here
+ */
+ if (rec != NULL)
+ {
+ PLpgSQL_rec *target = (PLpgSQL_rec *)(estate->datums[rec->dno]);
+
+ if (target->freetup)
+ heap_freetuple(target->tup);
+
+ if (rec->freetupdesc)
+ FreeTupleDesc(target->tupdesc);
+
+ /* initialize rec by NULLs */
+ nulls = (bool *) palloc(tupdesc->natts * sizeof(bool));
+ memset(nulls, true, tupdesc->natts * sizeof(bool));
+
+ target->tupdesc = CreateTupleDescCopy(tupdesc);
+ target->freetupdesc = true;
+
+ tup = heap_form_tuple(tupdesc, NULL, nulls);
+ if (HeapTupleIsValid(tup))
+ {
+ target->tup = tup;
+ target->freetup = true;
+ }
+ else
+ elog(ERROR, "cannot to build valid composite value");
+ }
+ }
+
+ /*
+ * Assign a tuple descriptor to variable specified by dno
+ */
+ static void
+ assign_tupdesc_dno(PLpgSQL_execstate *estate, int varno, TupleDesc tupdesc)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ if (target->dtype == PLPGSQL_DTYPE_REC)
+ assign_tupdesc_row_or_rec(estate, NULL, (PLpgSQL_rec *) target, tupdesc);
+ }
+
+ /*
+ * Returns a tuple descriptor based on existing plan
+ */
+ static TupleDesc
+ expr_get_desc(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *query,
+ bool use_element_type,
+ bool expand_record,
+ bool is_expression)
+ {
+ TupleDesc tupdesc = NULL;
+ CachedPlanSource *plansource = NULL;
+
+ if (query->plan != NULL)
+ {
+ SPIPlanPtr plan = query->plan;
+
+ if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
+ elog(ERROR, "cached plan is not valid plan");
+
+ if (list_length(plan->plancache_list) != 1)
+ elog(ERROR, "plan is not single execution plan");
+
+ plansource = (CachedPlanSource *) linitial(plan->plancache_list);
+
+ tupdesc = CreateTupleDescCopy(plansource->resultDesc);
+ }
+ else
+ elog(ERROR, "there are no plan for query: \"%s\"",
+ query->query);
+
+ /*
+ * try to get a element type, when result is a array (used with FOREACH ARRAY stmt)
+ */
+ if (use_element_type)
+ {
+ Oid elemtype;
+ TupleDesc elemtupdesc;
+
+ /* result should be a array */
+ if (tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /* check the type of the expression - must be an array */
+ elemtype = get_element_type(tupdesc->attrs[0]->atttypid);
+ if (!OidIsValid(elemtype))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("FOREACH expression must yield an array, not type %s",
+ format_type_be(tupdesc->attrs[0]->atttypid))));
+
+ /* we can't know typmod now */
+ elemtupdesc = lookup_rowtype_tupdesc_noerror(elemtype, -1, true);
+ if (elemtupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(elemtupdesc);
+ ReleaseTupleDesc(elemtupdesc);
+ }
+ else
+ elog(ERROR, "cannot to identify real type for record type variable");
+ }
+
+ if (is_expression && tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /*
+ * One spacial case is when record is assigned to composite type, then
+ * we should to unpack composite type.
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 && expand_record)
+ {
+ TupleDesc unpack_tupdesc;
+
+ unpack_tupdesc = lookup_rowtype_tupdesc_noerror(tupdesc->attrs[0]->atttypid,
+ tupdesc->attrs[0]->atttypmod,
+ true);
+ if (unpack_tupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(unpack_tupdesc);
+ ReleaseTupleDesc(unpack_tupdesc);
+ }
+ }
+
+ /*
+ * There is special case, when returned tupdesc contains only
+ * unpined record: rec := func_with_out_parameters(). IN this case
+ * we must to dig more deep - we have to find oid of function and
+ * get their parameters,
+ *
+ * This is support for assign statement
+ * recvar := func_with_out_parameters(..)
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 &&
+ tupdesc->attrs[0]->atttypid == RECORDOID &&
+ tupdesc->attrs[0]->atttypmod == -1 &&
+ expand_record)
+ {
+ PlannedStmt *_stmt;
+ Plan *_plan;
+ TargetEntry *tle;
+ CachedPlan *cplan;
+
+ /*
+ * When tupdesc is related to unpined record, we will try
+ * to check plan if it is just function call and if it is
+ * then we can try to derive a tupledes from function's
+ * description.
+ */
+ cplan = GetCachedPlan(plansource, NULL, true);
+ _stmt = (PlannedStmt *) linitial(cplan->stmt_list);
+
+ if (IsA(_stmt, PlannedStmt) && _stmt->commandType == CMD_SELECT)
+ {
+ _plan = _stmt->planTree;
+ if (IsA(_plan, Result) && list_length(_plan->targetlist) == 1)
+ {
+ tle = (TargetEntry *) linitial(_plan->targetlist);
+ if (((Node *) tle->expr)->type == T_FuncExpr)
+ {
+ FuncExpr *fn = (FuncExpr *) tle->expr;
+ FmgrInfo flinfo;
+ FunctionCallInfoData fcinfo;
+ TupleDesc rd;
+ Oid rt;
+
+ fmgr_info(fn->funcid, &flinfo);
+ flinfo.fn_expr = (Node *) fn;
+ fcinfo.flinfo = &flinfo;
+
+ get_call_result_type(&fcinfo, &rt, &rd);
+ if (rd == NULL)
+ elog(ERROR, "function does not return composite type is not possible to identify composite type");
+
+ FreeTupleDesc(tupdesc);
+ BlessTupleDesc(rd);
+
+ tupdesc = rd;
+ }
+ }
+ }
+
+ ReleaseCachedPlan(cplan, true);
+ }
+
+ return tupdesc;
+ }
+
+ /*
+ * Ensure check for all statements in list
+ */
+ static void
+ check_stmts(PLpgSQL_execstate *estate, List *stmts)
+ {
+ ListCell *lc;
+
+ foreach(lc, stmts)
+ {
+ check_stmt(estate, (PLpgSQL_stmt *) lfirst(lc));
+ }
+ }
+
+ /*
+ * walk over all statements
+ */
+ static void
+ check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
+ {
+ TupleDesc tupdesc = NULL;
+ PLpgSQL_function *func;
+ ListCell *l;
+
+ if (stmt == NULL)
+ return;
+
+ estate->err_stmt = stmt;
+ func = estate->func;
+
+ switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
+ {
+ case PLPGSQL_STMT_BLOCK:
+ {
+ PLpgSQL_stmt_block *stmt_block = (PLpgSQL_stmt_block *) stmt;
+ int i;
+ PLpgSQL_datum *d;
+
+ for (i = 0; i < stmt_block->n_initvars; i++)
+ {
+ d = func->datums[stmt_block->initvarnos[i]];
+
+ if (d->dtype == PLPGSQL_DTYPE_VAR)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) d;
+
+ check_expr(estate, var->default_val);
+ }
+ }
+
+ check_stmts(estate, stmt_block->body);
+
+ if (stmt_block->exceptions)
+ {
+ foreach(l, stmt_block->exceptions->exc_list)
+ {
+ check_stmts(estate, ((PLpgSQL_exception *) lfirst(l))->action);
+ }
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_ASSIGN:
+ {
+ PLpgSQL_stmt_assign *stmt_assign = (PLpgSQL_stmt_assign *) stmt;
+
+ /* prepare plan if desn't exist yet */
+ prepare_expr(estate, stmt_assign->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_assign->expr,
+ false, /* no element type */
+ true, /* expand record */
+ true); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_target(estate, stmt_assign->varno);
+
+ /* assign a tupdesc to record variable */
+ assign_tupdesc_dno(estate, stmt_assign->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_IF:
+ {
+ PLpgSQL_stmt_if *stmt_if = (PLpgSQL_stmt_if *) stmt;
+ ListCell *l;
+
+ check_expr(estate, stmt_if->cond);
+
+ check_stmts(estate, stmt_if->then_body);
+
+ foreach(l, stmt_if->elsif_list)
+ {
+ PLpgSQL_if_elsif *elif = (PLpgSQL_if_elsif *) lfirst(l);
+
+ check_expr(estate, elif->cond);
+ check_stmts(estate, elif->stmts);
+ }
+
+ check_stmts(estate, stmt_if->else_body);
+ }
+ break;
+
+ case PLPGSQL_STMT_CASE:
+ {
+ PLpgSQL_stmt_case *stmt_case = (PLpgSQL_stmt_case *) stmt;
+ Oid result_oid;
+
+ if (stmt_case->t_expr != NULL)
+ {
+ PLpgSQL_var *t_var = (PLpgSQL_var *) estate->datums[stmt_case->t_varno];
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_case->t_expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_case->t_expr,
+ false, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ result_oid = tupdesc->attrs[0]->atttypid;
+
+ /*
+ * When expected datatype is different from real, change it. Note that
+ * what we're modifying here is an execution copy of the datum, so
+ * this doesn't affect the originally stored function parse tree.
+ */
+
+ if (t_var->datatype->typoid != result_oid)
+ t_var->datatype = plpgsql_build_datatype(result_oid,
+ -1,
+ estate->func->fn_input_collation);
+
+ ReleaseTupleDesc(tupdesc);
+ }
+
+ foreach(l, stmt_case->case_when_list)
+ {
+ PLpgSQL_case_when *cwt = (PLpgSQL_case_when *) lfirst(l);
+
+ check_expr(estate, cwt->expr);
+ check_stmts(estate, cwt->stmts);
+ }
+
+ check_stmts(estate, stmt_case->else_stmts);
+ }
+ break;
+
+ case PLPGSQL_STMT_LOOP:
+ check_stmts(estate, ((PLpgSQL_stmt_loop *) stmt)->body);
+ break;
+
+ case PLPGSQL_STMT_WHILE:
+ {
+ PLpgSQL_stmt_while *stmt_while = (PLpgSQL_stmt_while *) stmt;
+
+ check_expr(estate, stmt_while->cond);
+ check_stmts(estate, stmt_while->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORI:
+ {
+ PLpgSQL_stmt_fori *stmt_fori = (PLpgSQL_stmt_fori *) stmt;
+
+ check_expr(estate, stmt_fori->lower);
+ check_expr(estate, stmt_fori->upper);
+ check_expr(estate, stmt_fori->step);
+
+ check_stmts(estate, stmt_fori->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORS:
+ {
+ PLpgSQL_stmt_fors *stmt_fors = (PLpgSQL_stmt_fors *) stmt;
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_fors->query, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_fors->query,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_fors->row, stmt_fors->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fors->row, stmt_fors->rec, tupdesc);
+
+ check_stmts(estate, stmt_fors->body);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORC:
+ {
+ PLpgSQL_stmt_forc *stmt_forc = (PLpgSQL_stmt_forc *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_forc->curvar];
+
+ prepare_expr(estate, stmt_forc->argquery, 0);
+
+ if (var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_forc->row, stmt_forc->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_forc->row, stmt_forc->rec, tupdesc);
+ }
+
+ check_stmts(estate, stmt_forc->body);
+ if (tupdesc != NULL)
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNFORS:
+ {
+ PLpgSQL_stmt_dynfors * stmt_dynfors = (PLpgSQL_stmt_dynfors *) stmt;
+
+ if (stmt_dynfors->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_expr(estate, stmt_dynfors->query);
+
+ foreach(l, stmt_dynfors->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ check_stmts(estate, stmt_dynfors->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FOREACH_A:
+ {
+ PLpgSQL_stmt_foreach_a *stmt_foreach_a = (PLpgSQL_stmt_foreach_a *) stmt;
+
+ prepare_expr(estate, stmt_foreach_a->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_foreach_a->expr,
+ true, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ check_target(estate, stmt_foreach_a->varno);
+ assign_tupdesc_dno(estate, stmt_foreach_a->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+
+ check_stmts(estate, stmt_foreach_a->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_EXIT:
+ check_expr(estate, ((PLpgSQL_stmt_exit *) stmt)->cond);
+ break;
+
+ case PLPGSQL_STMT_PERFORM:
+ prepare_expr(estate, ((PLpgSQL_stmt_perform *) stmt)->expr, 0);
+ break;
+
+ case PLPGSQL_STMT_RETURN:
+ check_expr(estate, ((PLpgSQL_stmt_return *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_NEXT:
+ check_expr(estate, ((PLpgSQL_stmt_return_next *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_QUERY:
+ {
+ PLpgSQL_stmt_return_query *stmt_rq = (PLpgSQL_stmt_return_query *) stmt;
+
+ check_expr(estate, stmt_rq->dynquery);
+ prepare_expr(estate, stmt_rq->query, 0);
+
+ foreach(l, stmt_rq->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_RAISE:
+ {
+ PLpgSQL_stmt_raise *stmt_raise = (PLpgSQL_stmt_raise *) stmt;
+ ListCell *current_param;
+ char *cp;
+
+ foreach(l, stmt_raise->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ foreach(l, stmt_raise->options)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ current_param = list_head(stmt_raise->params);
+
+ /* ensure any single % has a own parameter */
+ if (stmt_raise->message != NULL)
+ {
+ for (cp = stmt_raise->message; *cp; cp++)
+ {
+ if (cp[0] == '%')
+ {
+ if (cp[1] == '%')
+ {
+ cp++;
+ continue;
+ }
+
+ if (current_param == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too few parameters specified for RAISE")));
+
+ current_param = lnext(current_param);
+ }
+ }
+ }
+
+ if (current_param != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too many parameters specified for RAISE")));
+ }
+ break;
+
+ case PLPGSQL_STMT_EXECSQL:
+ {
+ PLpgSQL_stmt_execsql *stmt_execsql = (PLpgSQL_stmt_execsql *) stmt;
+
+ prepare_expr(estate, stmt_execsql->sqlstmt, 0);
+ if (stmt_execsql->into)
+ {
+ tupdesc = expr_get_desc(estate,
+ stmt_execsql->sqlstmt,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNEXECUTE:
+ {
+ PLpgSQL_stmt_dynexecute *stmt_dynexecute = (PLpgSQL_stmt_dynexecute *) stmt;
+
+ check_expr(estate, stmt_dynexecute->query);
+
+ foreach(l, stmt_dynexecute->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ if (stmt_dynexecute->into)
+ {
+ if (stmt_dynexecute->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_row_or_rec(estate, stmt_dynexecute->row, stmt_dynexecute->rec);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_OPEN:
+ {
+ PLpgSQL_stmt_open *stmt_open = (PLpgSQL_stmt_open *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_open->curvar];
+
+ if (var->cursor_explicit_expr)
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ prepare_expr(estate, stmt_open->query, 0);
+ prepare_expr(estate, stmt_open->argquery, 0);
+ check_expr(estate, stmt_open->dynquery);
+
+ foreach(l, stmt_open->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_GETDIAG:
+ {
+ PLpgSQL_stmt_getdiag *stmt_getdiag = (PLpgSQL_stmt_getdiag *) stmt;
+ ListCell *lc;
+
+ foreach(lc, stmt_getdiag->diag_items)
+ {
+ PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
+
+ check_target(estate, diag_item->target);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_FETCH:
+ {
+ PLpgSQL_stmt_fetch *stmt_fetch = (PLpgSQL_stmt_fetch *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *)(estate->datums[stmt_fetch->curvar]);
+
+ if (var != NULL && var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+ check_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_CLOSE:
+ break;
+
+ default:
+ elog(ERROR, "unrecognized cmd_type: %d", stmt->cmd_type);
+ return; /* be compiler quite */
+ }
+ }
+
+ /*
+ * Initialize variable to NULL
+ */
+ static void
+ var_init_to_null(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) estate->datums[varno];
+ var->value = (Datum) 0;
+ var->isnull = true;
+ var->freeval = false;
+ }
*** ./src/pl/plpgsql/src/pl_handler.c.orig 2011-11-29 19:20:59.494116771 +0100
--- ./src/pl/plpgsql/src/pl_handler.c 2011-11-29 19:21:24.529804431 +0100
***************
*** 312,314 ****
--- 312,452 ----
PG_RETURN_VOID();
}
+
+ /* ----------
+ * plpgsql_checker
+ *
+ * This function attempts to check a embeded SQL inside a PL/pgSQL function at
+ * CHECK FUNCTION time. It should to have one or two parameters. Second
+ * parameter is a relation (used when function is trigger).
+ * ----------
+ */
+ PG_FUNCTION_INFO_V1(plpgsql_checker);
+
+ Datum
+ plpgsql_checker(PG_FUNCTION_ARGS)
+ {
+ Oid funcoid = PG_GETARG_OID(0);
+ Oid relid = PG_GETARG_OID(1);
+ HeapTuple tuple;
+ FunctionCallInfoData fake_fcinfo;
+ FmgrInfo flinfo;
+ TriggerData trigdata;
+ int rc;
+ PLpgSQL_function *function;
+ PLpgSQL_execstate *cur_estate;
+
+ Form_pg_proc proc;
+ char functyptype;
+ bool istrigger = false;
+
+ /* we don't need to repair a check done by validator */
+
+ tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
+ if (!HeapTupleIsValid(tuple))
+ elog(ERROR, "cache lookup failed for function %u", funcoid);
+ proc = (Form_pg_proc) GETSTRUCT(tuple);
+
+ functyptype = get_typtype(proc->prorettype);
+
+ if (functyptype == TYPTYPE_PSEUDO)
+ {
+ /* we assume OPAQUE with no arguments means a trigger */
+ if (proc->prorettype == TRIGGEROID ||
+ (proc->prorettype == OPAQUEOID && proc->pronargs == 0))
+ {
+ istrigger = true;
+ if (!OidIsValid(relid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("PL/pgSQL trigger functions cannot be checked directly"),
+ errhint("use CHECK TRIGGER statement instead")));
+ }
+ }
+
+ /*
+ * Connect to SPI manager
+ */
+ if ((rc = SPI_connect()) != SPI_OK_CONNECT)
+ elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
+
+ /*
+ * Set up a fake fcinfo with just enough info to satisfy
+ * plpgsql_compile().
+ *
+ * there should be a different real argtypes for polymorphic params
+ */
+ MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo));
+ MemSet(&flinfo, 0, sizeof(flinfo));
+ fake_fcinfo.flinfo = &flinfo;
+ flinfo.fn_oid = funcoid;
+ flinfo.fn_mcxt = CurrentMemoryContext;
+
+ if (istrigger)
+ {
+ MemSet(&trigdata, 0, sizeof(trigdata));
+ trigdata.type = T_TriggerData;
+ trigdata.tg_relation = relation_open(relid, AccessShareLock);
+ fake_fcinfo.context = (Node *) &trigdata;
+ }
+
+ /* Get a compiled function */
+ function = plpgsql_compile(&fake_fcinfo, false);
+
+ /* Must save and restore prior value of cur_estate */
+ cur_estate = function->cur_estate;
+
+ /* Mark the function as busy, so it can't be deleted from under us */
+ function->use_count++;
+
+
+ /* Create a fake runtime environment and prepare plans */
+ PG_TRY();
+ {
+ if (!istrigger)
+ plpgsql_check_function(function, &fake_fcinfo);
+ else
+ plpgsql_check_trigger(function, &trigdata);
+ }
+ PG_CATCH();
+ {
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ /*
+ * Disconnect from SPI manager
+ */
+ if ((rc = SPI_finish()) != SPI_OK_FINISH)
+ elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
+
+ ReleaseSysCache(tuple);
+
+ PG_RETURN_VOID();
+ }
*** ./src/pl/plpgsql/src/plpgsql.h.orig 2011-11-29 19:20:59.500116698 +0100
--- ./src/pl/plpgsql/src/plpgsql.h 2011-11-29 20:22:19.423516596 +0100
***************
*** 902,907 ****
--- 902,908 ----
extern void plpgsql_adddatum(PLpgSQL_datum *new);
extern int plpgsql_add_initdatums(int **varnos);
extern void plpgsql_HashTableInit(void);
+ extern void plpgsql_delete_function(PLpgSQL_function *func);
/* ----------
* Functions in pl_handler.c
***************
*** 911,916 ****
--- 912,918 ----
extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_inline_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
+ extern Datum plpgsql_checker(PG_FUNCTION_ARGS);
/* ----------
* Functions in pl_exec.c
***************
*** 928,933 ****
--- 930,939 ----
extern void exec_get_datum_type_info(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum,
Oid *typeid, int32 *typmod, Oid *collation);
+ extern void plpgsql_check_function(PLpgSQL_function *func,
+ FunctionCallInfo fcinfo);
+ extern void plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata);
/* ----------
* Functions for namespace handling in pl_funcs.c
*** ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql.orig 2011-11-29 19:20:59.502116672 +0100
--- ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql 2011-11-29 19:21:24.533804381 +0100
***************
*** 5,7 ****
--- 5,8 ----
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_call_handler();
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_inline_handler(internal);
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_validator(oid);
+ ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_checker(oid, regclass);
*** ./src/test/regress/expected/plpgsql.out.orig 2011-11-29 19:20:59.505116634 +0100
--- ./src/test/regress/expected/plpgsql.out 2011-11-29 19:21:24.536804342 +0100
***************
*** 302,307 ****
--- 302,310 ----
' language plpgsql;
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
+ NOTICE: checking function "tg_hslot_biu()"
-- ************************************************************
-- * BEFORE DELETE on HSlot
-- * - prevent from manual manipulation
***************
*** 635,640 ****
--- 638,645 ----
raise exception ''illegal backlink beginning with %'', mytype;
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
-- ************************************************************
-- * Support function to clear out the backlink field if
-- * it still points to specific slot
***************
*** 2802,2807 ****
--- 2807,2840 ----
(1 row)
+ -- check function should not fail
+ check function for_vect();
+ -- recheck after check function
+ select for_vect();
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 4
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1 bb cc
+ NOTICE: 2 bb cc
+ NOTICE: 3 bb cc
+ NOTICE: 4 bb cc
+ for_vect
+ ----------
+
+ (1 row)
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 3283,3288 ****
--- 3316,3323 ----
return;
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
select forc01();
NOTICE: 5 from c
NOTICE: 6 from c
***************
*** 3716,3721 ****
--- 3751,3758 ----
end case;
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
select case_test(1);
case_test
-----------
***************
*** 4571,4573 ****
--- 4608,4942 ----
CONTEXT: PL/pgSQL function "testoa" line 5 at assignment
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+ --
+ -- check function statement tests
+ --
+ create table t1(a int, b int);
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: column "c" of relation "t1" does not exist
+ LINE 1: update t1 set c = 30
+ ^
+ QUERY: update t1 set c = 30
+ CONTEXT: checking of PL/pgSQL function "f1" line 4 at SQL statement
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ checking of PL/pgSQL function "f1" line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ checking of PL/pgSQL function "f1" line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: record "r" has no field "c"
+ CONTEXT: checking of PL/pgSQL function "f1" line 6 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: column "a" does not exist
+ LINE 1: SELECT a + b
+ ^
+ QUERY: SELECT a + b
+ CONTEXT: checking of PL/pgSQL function "f1" line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: too many parameters specified for RAISE
+ CONTEXT: checking of PL/pgSQL function "f1" line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: too few parameters specified for RAISE
+ CONTEXT: checking of PL/pgSQL function "f1" line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: column "c" does not exist
+ LINE 1: SELECT c+10
+ ^
+ QUERY: SELECT c+10
+ CONTEXT: checking of PL/pgSQL function "f1" line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: subscripted object is not an array
+ CONTEXT: checking of PL/pgSQL function "f1" line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ ERROR: record "_exception" has no field "hint"
+ CONTEXT: checking of PL/pgSQL function "f1" line 7 at GET DIAGNOSTICS
+ drop function f1();
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ NOTICE: checking function "f1_trg()"
+ ERROR: record "new" has no field "c"
+ CONTEXT: SQL statement "SELECT new.c"
+ checking of PL/pgSQL function "f1_trg" line 5 at RAISE
+ insert into t1 values(6,30);
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- should to fail
+ check trigger t1_f1 on t1;
+ NOTICE: checking function "f1_trg()"
+ ERROR: record "new" has no field "c"
+ CONTEXT: checking of PL/pgSQL function "f1_trg" line 5 at assignment
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+ ERROR: record "new" has no field "c"
+ CONTEXT: PL/pgSQL function "f1_trg" line 5 at assignment
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- ok
+ check trigger t1_f1 on t1;
+ NOTICE: checking function "f1_trg()"
+ -- ok
+ insert into t1 values(6,30);
+ drop table t1;
+ drop type _exception_type;
+ drop function f1_trg();
*** ./src/test/regress/sql/plpgsql.sql.orig 2011-11-29 19:20:59.508116598 +0100
--- ./src/test/regress/sql/plpgsql.sql 2011-11-29 19:21:24.538804318 +0100
***************
*** 366,371 ****
--- 366,373 ----
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
-- ************************************************************
-- * BEFORE DELETE on HSlot
***************
*** 747,752 ****
--- 749,757 ----
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
+
-- ************************************************************
-- * Support function to clear out the backlink field if
***************
*** 2335,2340 ****
--- 2340,2352 ----
select for_vect();
+ -- check function should not fail
+ check function for_vect();
+
+ -- recheck after check function
+ select for_vect();
+
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 2714,2719 ****
--- 2726,2734 ----
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
+
select forc01();
-- try updating the cursor's current row
***************
*** 3048,3053 ****
--- 3063,3071 ----
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
+
select case_test(1);
select case_test(2);
select case_test(3);
***************
*** 3600,3602 ****
--- 3618,3862 ----
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+
+ --
+ -- check function statement tests
+ --
+
+ create table t1(a int, b int);
+
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+
+ drop function f1();
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- should to fail
+ check trigger t1_f1 on t1;
+
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- ok
+ check trigger t1_f1 on t1;
+
+ -- ok
+ insert into t1 values(6,30);
+
+ drop table t1;
+ drop type _exception_type;
+
+ drop function f1_trg();
+
On ons, 2011-11-30 at 10:53 -0500, Tom Lane wrote:
I think the important point here is that we need to support more than
one level of validation, and that the higher levels can't really be
applied by default in CREATE FUNCTION because they may fail on perfectly
valid code.
How would this work with anything other than PL/pgSQL in practice?
Here is an additional use case: There are a bunch of syntax and style
checkers for Python: pylint, pyflakes, pep8, pychecker, and maybe more.
I would like to have a way to use these for PL/Python. Right now I use
a tool I wrote called plpylint (https://github.com/petere/plpylint),
which pulls the source code out of the database and runs pylint on the
client, which works well enough, but what is being discussed here could
lead to a better solution.
So what I'd like to have is some way to say
check all plpythonu functions [in this schema or whatever] using
checker "pylint"
where "pylint" was previously defined as a checker associated with the
plpythonu language that actually invokes some user-defined function.
Also, what kind of report does this generate?
Tom Lane wrote:
Do I understand right that the reason why the check function is
different from the validator function is that it would be more
difficult
to add the checks to the validator function?
Is that a good enough argument? From a user's perspective it is
difficult to see why some checks are performed at function creation
time, while others have to be explicitly checked with CHECK FUNCTION.
I think it would be much more intuitive if CHECK FUNCTION does
the same as function validation with check_function_bodies on.I think the important point here is that we need to support more than
one level of validation, and that the higher levels can't really be
applied by default in CREATE FUNCTION because they may fail on
perfectly
valid code.
I understand now.
There are three levels of checking:
1) Validation with check_function_bodies = off (checks nothing).
2) Validation with check_function_bodies = on (checks syntax).
3) CHECK FUNCTION (checks RAISE and objects referenced in the function).
As long as 3) implies 2) (which I think it does), that makes sense.
I guess I was led astray by the documentation in plhandler.sgml:
Validator functions should typically honor the check_function_bodies
parameter: [...] this parameter is turned off by pg_dump so that it
can load procedural language functions without worrying about possible
dependencies of the function bodies on other database objects.
"Dependencyies on other database objects" seems more like a description
of CHECK FUNCTION.
But I guess that this documentation should be changed anyway to describe
the check function.
A bigger issue is that once you think about more than one kind of
check,
it becomes apparent that we might need some user-specifiable options
to
control which checks are applied. And I see no provision for that
here.
My attempt at a syntax that could also cover Peter's wish for multiple
checker functions:
CHECK FUNCTION { func(args) | ALL [IN SCHEMA schema] [FOR ROLE user] }
[ USING check_function ] OPTIONS (optname optarg [, ...])
Yours,
Laurenz Albe
Peter Eisentraut <peter_e@gmx.net> writes:
On ons, 2011-11-30 at 10:53 -0500, Tom Lane wrote:
I think the important point here is that we need to support more than
one level of validation, and that the higher levels can't really be
applied by default in CREATE FUNCTION because they may fail on perfectly
valid code.
How would this work with anything other than PL/pgSQL in practice?
Well, that's TBD by the individual PL authors, but it hardly seems
implausible that there might be lint-like checks applicable in many
PLs. As long as we have the functionality pushed out to a PL-specific
checker function, the details can be worked out later.
So what I'd like to have is some way to say
check all plpythonu functions [in this schema or whatever] using
checker "pylint"
where "pylint" was previously defined as a checker associated with the
plpythonu language that actually invokes some user-defined function.
That sounds like a language-specific option to me.
Also, what kind of report does this generate?
Good question. I suspect what Pavel has now will raise errors, but that
doesn't scale very nicely to checking more than one function, or even to
finding more than one bug in a single function.
My first instinct is to say that it should work like plain EXPLAIN, ie,
deliver a textual report that we send as if it were a query result.
regards, tom lane
Hello
Also, what kind of report does this generate?
Good question. I suspect what Pavel has now will raise errors, but that
doesn't scale very nicely to checking more than one function, or even to
finding more than one bug in a single function.
I stop on first error now. Reason is reuse of functionality that can
to mark a critical point (bug) of embedded query in console.
Continuous checking is possible (plpgsql), but there are a few
critical bugs, that does stop. For example - any buggy assign to
record var causes uninitialized variable and any later checks are
affected. This is possible when ASSIGN, FOR SELECT, SELECT INTO
statements are used. It is small part of possible bugs but relative
often pattern. So I didn't worry about it yet.
My first instinct is to say that it should work like plain EXPLAIN, ie,
deliver a textual report that we send as if it were a query result.
can be - but EXPLAIN raises exception too, when there some error.
There should be a some possibility to simply identify result. I am not
sure if checking on empty result is good way. A raising exception
should be option. When we return text, then we have to think about
some structured form result - line, position, message. A advance of
exception on first issue is fact, so these questions are solved.
so CHECK can returns lines - like EXPLAIN, but can be nice and helpful
for this moment a GUC - some like check_raises_exception = on|off.
Default should be "off". And I have to think about some FORMAT option.
is it good plan?
Pavel
Show quoted text
regards, tom lane
Hello
My attempt at a syntax that could also cover Peter's wish for multiple
checker functions:CHECK FUNCTION { func(args) | ALL [IN SCHEMA schema] [FOR ROLE user] }
[ USING check_function ] OPTIONS (optname optarg [, ...])
check_function should be related to one language, so you have to
specify language if you would to specify check_function (if we would
to have more check functions for one language).
Regards
Pavel Stehule
2011/12/2 Pavel Stehule <pavel.stehule@gmail.com>:
Hello
My attempt at a syntax that could also cover Peter's wish for multiple
checker functions:CHECK FUNCTION { func(args) | ALL [IN SCHEMA schema] [FOR ROLE user] }
[ USING check_function ] OPTIONS (optname optarg [, ...])
some other idea about other using CHECK FUNCTION
CHECK FUNCTION func(args)
RETURNS ... AS $$
$$ LANGUAGE xxx
This should to do check of function body without affect on registered
function. This is addition to previous defined syntax.
Nice a day
Pavel
Pavel Stehule wrote:
My attempt at a syntax that could also cover Peter's wish for multiple
checker functions:CHECK FUNCTION { func(args) | ALL [IN SCHEMA schema] [FOR ROLE user] }
[ USING check_function ] OPTIONS (optname optarg [, ...])
check_function should be related to one language, so you have to
specify language if you would to specify check_function (if we would
to have more check functions for one language).
Right, I forgot LANGUAGE:
CHECK FUNCTION { func(args) | ALL IN LANGUAGE pl [IN SCHEMA schema] [FOR ROLE user] }
[ USING check_function ] OPTIONS (optname optarg [, ...])
If func(args) is given, the language can be inferred.
Yours,
Laurenz Albe
Hello
there is a updated patch.
it support multi check, options and custom check functions are not
supported yet. I don't plan to implement custom check functions in
this round - I has not any example of usage - but we have agreement on
syntax and behave, so this should not be problem. I changed reporting
- from exception to warnings.
postgres=# check function all in schema public;
NOTICE: skip check function "hello()", it use C or internal language
NOTICE: skip check function "tri()", it is trigger function
NOTICE: skip check function "vloz_do_foo(integer)", language "sql"
hasn't checker function
NOTICE: skip check function "mojefunc(integer)", language "sql"
hasn't checker function
NOTICE: skip check function "myleft(text,integer)", language "sql"
hasn't checker function
NOTICE: checked function "array_random(integer,integer)"
NOTICE: skip check function "array_random1(integer,integer)",
language "sql" hasn't checker function
NOTICE: checked function "vrattab(integer)"
NOTICE: checked function "yyy()"
NOTICE: checked function "xxx()"
WARNING: error in function "zpracuj(integer)"
LINE 1: select h from hodnoty
^
DETAIL: column "h" does not exist
QUERY: select h from hodnoty
CONTEXT: line 4 at FOR over SELECT rows
WARNING: error in function "ii(integer)"
LINE 1: SELECT (select aa from i)
^
DETAIL: column "aa" does not exist
QUERY: SELECT (select aa from i)
CONTEXT: line 3 at RETURN
CHECK FUNCTION
postgres=# check function all in schema public in language plpgsql;
NOTICE: skip check function "tri()", it is trigger function
NOTICE: checked function "array_random(integer,integer)"
NOTICE: checked function "vrattab(integer)"
NOTICE: checked function "yyy()"
NOTICE: checked function "xxx()"
WARNING: error in function "zpracuj(integer)"
LINE 1: select h from hodnoty
^
DETAIL: column "h" does not exist
QUERY: select h from hodnoty
CONTEXT: line 4 at FOR over SELECT rows
WARNING: error in function "ii(integer)"
LINE 1: SELECT (select aa from i)
^
DETAIL: column "aa" does not exist
QUERY: SELECT (select aa from i)
CONTEXT: line 3 at RETURN
CHECK FUNCTION
postgres=# check function all in schema public in language plpgsql for role www;
NOTICE: nothing to check
CHECK FUNCTION
please, try it
Regards
Pavel Stehule
2011/12/3 Albe Laurenz <laurenz.albe@wien.gv.at>:
Show quoted text
Pavel Stehule wrote:
My attempt at a syntax that could also cover Peter's wish for multiple
checker functions:CHECK FUNCTION { func(args) | ALL [IN SCHEMA schema] [FOR ROLE user] }
[ USING check_function ] OPTIONS (optname optarg [, ...])check_function should be related to one language, so you have to
specify language if you would to specify check_function (if we would
to have more check functions for one language).Right, I forgot LANGUAGE:
CHECK FUNCTION { func(args) | ALL IN LANGUAGE pl [IN SCHEMA schema] [FOR ROLE user] }
[ USING check_function ] OPTIONS (optname optarg [, ...])If func(args) is given, the language can be inferred.
Yours,
Laurenz Albe
Attachments:
check_function-2011-12-07-4.difftext/x-patch; charset=US-ASCII; name=check_function-2011-12-07-4.diffDownload
*** ./doc/src/sgml/catalogs.sgml.orig 2011-12-07 05:59:37.123674706 +0100
--- ./doc/src/sgml/catalogs.sgml 2011-12-07 06:00:07.253332903 +0100
***************
*** 3652,3657 ****
--- 3652,3668 ----
</row>
<row>
+ <entry><structfield>lanchecker</structfield></entry>
+ <entry><type>oid</type></entry>
+ <entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
+ <entry>
+ This references a language checker function that is responsible
+ for checking a embedded SQL and can provide detailed checking.
+ Zero if no checker is provided.
+ </entry>
+ </row>
+
+ <row>
<entry><structfield>lanacl</structfield></entry>
<entry><type>aclitem[]</type></entry>
<entry></entry>
*** ./doc/src/sgml/ref/allfiles.sgml.orig 2011-12-07 05:59:37.125674684 +0100
--- ./doc/src/sgml/ref/allfiles.sgml 2011-12-07 06:00:07.254332891 +0100
***************
*** 40,45 ****
--- 40,46 ----
<!ENTITY alterView SYSTEM "alter_view.sgml">
<!ENTITY analyze SYSTEM "analyze.sgml">
<!ENTITY begin SYSTEM "begin.sgml">
+ <!ENTITY checkFunction SYSTEM "check_function.sgml">
<!ENTITY checkpoint SYSTEM "checkpoint.sgml">
<!ENTITY close SYSTEM "close.sgml">
<!ENTITY cluster SYSTEM "cluster.sgml">
*** ./doc/src/sgml/ref/create_language.sgml.orig 2011-12-07 05:59:37.127674661 +0100
--- ./doc/src/sgml/ref/create_language.sgml 2011-12-07 06:00:07.256332868 +0100
***************
*** 23,29 ****
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
--- 23,29 ----
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ] [ CHECK <replaceable>checkfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
***************
*** 217,222 ****
--- 217,236 ----
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><literal>CHECK</literal> <replaceable class="parameter">checkfunction</replaceable></term>
+
+ <listitem>
+ <para><replaceable class="parameter">checkfunction</replaceable> is the
+ name of a previously registered function that will be called
+ when a new function in the language is created, to check the
+ function by statemnt <command>CHECK FUNCTION</command> or
+ <command>CHECK TRIGGER</command>.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
*** ./doc/src/sgml/reference.sgml.orig 2011-12-07 05:59:37.128674649 +0100
--- ./doc/src/sgml/reference.sgml 2011-12-07 06:00:07.257332857 +0100
***************
*** 68,73 ****
--- 68,74 ----
&alterView;
&analyze;
&begin;
+ &checkFunction;
&checkpoint;
&close;
&cluster;
*** ./doc/src/sgml/ref/check_function.sgml.orig 2011-12-07 05:59:58.965426923 +0100
--- ./doc/src/sgml/ref/check_function.sgml 2011-12-06 17:54:28.000000000 +0100
***************
*** 0 ****
--- 1,38 ----
+ <!--
+ doc/src/sgml/ref/check_function.sgml
+ -->
+
+ <refentry id="SQL-CHECKFUNCTION">
+ <refmeta>
+ <refentrytitle>CHECK FUNCTION</refentrytitle>
+ <manvolnum>7</manvolnum>
+ <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+ <refname>CHECK FUNCTION</refname>
+ <refpurpose>ensure a deep checking of existing function</refpurpose>
+ </refnamediv>
+
+ <indexterm zone="sql-checkfunction">
+ <primary>CHECK FUNCTION</primary>
+ </indexterm>
+
+ <refsynopsisdiv>
+ <synopsis>
+ CHECK FUNCTION <replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] )
+ | CHECK FUNCTION ALL [ IN LANGUAGE <replaceable class="parameter">langname</replaceable> ] [ IN SCHEMA <replaceable class="parameter">schemaname</replaceable> ] [ FOR ROLE <replaceable class="parameter">ownername</replaceable> ]
+ | CHECK TRIGGER <replaceable class="parameter">name</replaceable> ON <replaceable class="parameter">tablename</replaceable>
+ </synopsis>
+ </refsynopsisdiv>
+
+ <refsect1 id="sql-checkfunction-description">
+ <title>Description</title>
+
+ <para>
+ <command>CHECK FUNCTION</command> check a existing function.
+ <command>CHECK TRIGGER</command> check a trigger function.
+ </para>
+ </refsect1>
+
+ </refentry>
*** ./src/backend/catalog/pg_proc.c.orig 2011-12-07 05:59:37.131674614 +0100
--- ./src/backend/catalog/pg_proc.c 2011-12-07 06:00:07.260332824 +0100
***************
*** 1101,1103 ****
--- 1101,1104 ----
*newcursorpos = newcp;
return false;
}
+
*** ./src/backend/commands/functioncmds.c.orig 2011-12-07 05:59:37.132674603 +0100
--- ./src/backend/commands/functioncmds.c 2011-12-07 07:58:31.520772351 +0100
***************
*** 35,53 ****
--- 35,58 ----
#include "access/genam.h"
#include "access/heapam.h"
#include "access/sysattr.h"
+ #include "access/xact.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_language.h"
+ #include "catalog/namespace.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_proc_fn.h"
+ #include "catalog/pg_trigger.h"
#include "catalog/pg_type.h"
#include "catalog/pg_type_fn.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
+ #include "commands/trigger.h"
+ #include "executor/spi_priv.h"
#include "miscadmin.h"
#include "optimizer/var.h"
#include "parser/parse_coerce.h"
***************
*** 60,66 ****
--- 65,73 ----
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
+ #include "utils/memutils.h"
#include "utils/rel.h"
+ #include "utils/resowner.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
***************
*** 1009,1014 ****
--- 1016,1365 ----
}
}
+ /*
+ * Search and execute related checker function
+ */
+ static void
+ CheckFunctionById(Oid funcOid, Oid relid)
+ {
+ HeapTuple tup;
+ Form_pg_proc proc;
+ HeapTuple languageTuple;
+ Form_pg_language languageStruct;
+ Oid languageChecker;
+ bool found_bug;
+ char *funcname;
+
+ tup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
+ if (!HeapTupleIsValid(tup)) /* should not happen */
+ elog(ERROR, "cache lookup failed for function %u", funcOid);
+
+ proc = (Form_pg_proc) GETSTRUCT(tup);
+
+ languageTuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(proc->prolang));
+ Assert(HeapTupleIsValid(languageTuple));
+
+ languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
+ languageChecker = languageStruct->lanchecker;
+
+ funcname = format_procedure(funcOid);
+
+ /* Check a function body */
+ if (OidIsValid(languageChecker))
+ {
+ ArrayType *set_items = NULL;
+ int save_nestlevel = 0;
+ Datum datum;
+ bool isnull;
+ MemoryContext oldCxt;
+ MemoryContext checkCxt;
+ ResourceOwner oldowner;
+
+ datum = SysCacheGetAttr(PROCOID, tup, Anum_pg_proc_proconfig, &isnull);
+
+ if (!isnull)
+ {
+ /* Set per-function configuration parameters */
+ set_items = (ArrayType *) DatumGetPointer(datum);
+ if (set_items) /* Need a new GUC nesting level */
+ {
+ save_nestlevel = NewGUCNestLevel();
+ ProcessGUCArray(set_items,
+ (superuser() ? PGC_SUSET : PGC_USERSET),
+ PGC_S_SESSION,
+ GUC_ACTION_SAVE);
+ }
+ else
+ save_nestlevel = 0; /* keep compiler quiet */
+ }
+
+ checkCxt = AllocSetContextCreate(CurrentMemoryContext,
+ "Check temporary context",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
+
+ oldCxt = MemoryContextSwitchTo(checkCxt);
+
+ oldowner = CurrentResourceOwner;
+ BeginInternalSubTransaction(NULL);
+ MemoryContextSwitchTo(checkCxt);
+
+ /*
+ * forward exception to warning
+ */
+ PG_TRY();
+ {
+ OidFunctionCall2(languageChecker, ObjectIdGetDatum(funcOid),
+ ObjectIdGetDatum(relid));
+
+ RollbackAndReleaseCurrentSubTransaction();
+ MemoryContextSwitchTo(checkCxt);
+ CurrentResourceOwner = oldowner;
+
+ found_bug = false;
+ }
+ PG_CATCH();
+ {
+ ErrorData *edata;
+
+ MemoryContextSwitchTo(checkCxt);
+ edata = CopyErrorData();
+ FlushErrorState();
+
+ RollbackAndReleaseCurrentSubTransaction();
+ MemoryContextSwitchTo(checkCxt);
+ CurrentResourceOwner = oldowner;
+
+ edata->elevel = WARNING;
+
+ ereport(WARNING,
+ (errmsg("error in function \"%s\"", funcname),
+ edata->message != NULL ? errdetail_internal("%s", edata->message) : 0,
+ edata->context != NULL ? errcontext("%s",edata->context) : 0,
+ edata->internalquery != NULL ? internalerrquery(edata->internalquery) : 0,
+ internalerrposition(edata->internalpos)));
+
+ FreeErrorData(edata);
+
+ found_bug = true;
+ }
+ PG_END_TRY();
+
+ MemoryContextSwitchTo(oldCxt);
+
+ if (set_items)
+ AtEOXact_GUC(true, save_nestlevel);
+
+ if (!found_bug)
+ elog(NOTICE, "checked function \"%s\"",
+ funcname);
+ }
+ else
+ elog(NOTICE, "skip check function \"%s\", language \"%s\" hasn't checker function",
+ funcname,
+ NameStr(languageStruct->lanname));
+
+ pfree(funcname);
+
+ ReleaseSysCache(languageTuple);
+ ReleaseSysCache(tup);
+ }
+
+ /*
+ * CheckFunction
+ * call a PL checker function when this function exists.
+ */
+ void
+ CheckFunction(CheckFunctionStmt *stmt)
+ {
+ List *functionName = stmt->funcname;
+ List *argTypes = stmt->args; /* list of TypeName nodes */
+ Oid funcOid = InvalidOid;
+ HeapTuple tup;
+ Oid trgOid = InvalidOid;
+ Oid relid = InvalidOid;
+ Oid languageId;
+ int nkeys = 0;
+ List *objects = NIL;
+
+ /*
+ * when Check stmt is multiple statement, then
+ * prepare list of processed functions.
+ */
+ if (stmt->options != NIL)
+ {
+ ScanKeyData key[3];
+ Relation rel;
+ HeapScanDesc scan;
+ bool immutable_language = false;
+ ListCell *option;
+ bool language_opt = false;
+ bool schema_opt = false;
+ bool owner_opt = false;
+
+ foreach(option, stmt->options)
+ {
+ DefElem *defel = (DefElem *) lfirst(option);
+
+ if (strcmp(defel->defname, "language") == 0)
+ {
+ HeapTuple languageTuple;
+ Form_pg_language languageStruct;
+ Oid languageChecker;
+ char *language = defGetString(defel);
+
+ if (language_opt)
+ elog(ERROR, "multiple usage of IN LANGUAGE option");
+ language_opt = true;
+
+ languageTuple = SearchSysCache1(LANGNAME, PointerGetDatum(language));
+ if (!HeapTupleIsValid(languageTuple))
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_OBJECT),
+ errmsg("language \"%s\" does not exist", language),
+ (PLTemplateExists(language) ?
+ errhint("Use CREATE LANGUAGE to load the language into the database.") : 0)));
+
+ languageId = HeapTupleGetOid(languageTuple);
+ if (languageId == INTERNALlanguageId || languageId == ClanguageId)
+ elog(ERROR, "cannot to check functions in C or internal languages");
+
+ languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
+ languageChecker = languageStruct->lanchecker;
+ if (!OidIsValid(languageChecker))
+ elog(ERROR, "language \"%s\" has no defined checker function",
+ language);
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_prolang,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(languageId));
+
+ ReleaseSysCache(languageTuple);
+
+ immutable_language = true;
+ }
+ else if (strcmp(defel->defname, "schema") == 0)
+ {
+ Oid namespaceId = LookupExplicitNamespace(defGetString(defel));
+
+ if (schema_opt)
+ elog(ERROR, "multiple usage of IN SCHEMA option");
+ schema_opt = true;
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_pronamespace,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(namespaceId));
+
+ }
+ else if (strcmp(defel->defname, "owner") == 0)
+ {
+ Oid ownerId = get_role_oid(defGetString(defel), false);
+
+ if (owner_opt)
+ elog(ERROR, "multiple usage of FOR ROLE option");
+ owner_opt = true;
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_proowner,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(ownerId));
+
+ }
+ else
+ elog(ERROR, "option \"%s\" not recognized",
+ defel->defname);
+ }
+
+ rel = heap_open(ProcedureRelationId,AccessShareLock);
+ scan = heap_beginscan(rel, SnapshotNow, nkeys, key);
+
+ while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
+ {
+ Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(tup);
+
+ funcOid = HeapTupleGetOid(tup);
+
+ if (!immutable_language)
+ {
+ Oid prolang = proc->prolang;
+
+ /* skip C or internal */
+ if (prolang == INTERNALlanguageId || prolang == ClanguageId)
+ {
+ elog(NOTICE, "skip check function \"%s\", it use C or internal language",
+ format_procedure(funcOid));
+ continue;
+ }
+ }
+
+ if (proc->prorettype == TRIGGEROID)
+ {
+ elog(NOTICE, "skip check function \"%s\", it is trigger function",
+ format_procedure(funcOid));
+ continue;
+ }
+
+ objects = lappend_oid(objects, funcOid);
+ }
+
+ heap_endscan(scan);
+ heap_close(rel, AccessShareLock);
+ }
+
+ /* when we should to check trigger, then we should to find a trigger handler */
+ else if (stmt->trgname != NULL)
+ {
+ HeapTuple ht_trig;
+ Form_pg_trigger trigrec;
+ ScanKeyData skey[1];
+ Relation tgrel;
+ SysScanDesc tgscan;
+
+ relid = RangeVarGetRelid(stmt->relation, ShareLock, false);
+ trgOid = get_trigger_oid(relid, stmt->trgname, false);
+
+ /*
+ * Fetch the pg_trigger tuple by the Oid of the trigger
+ */
+ tgrel = heap_open(TriggerRelationId, AccessShareLock);
+
+ ScanKeyInit(&skey[0],
+ ObjectIdAttributeNumber,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(trgOid));
+
+ tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
+ SnapshotNow, 1, skey);
+
+ ht_trig = systable_getnext(tgscan);
+
+ if (!HeapTupleIsValid(ht_trig))
+ elog(ERROR, "could not find tuple for trigger %u", trgOid);
+
+ trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
+
+ /* we need to know trigger function to get PL checker function */
+ funcOid = trigrec->tgfoid;
+
+ /* Clean up */
+ systable_endscan(tgscan);
+
+ heap_close(tgrel, AccessShareLock);
+ }
+ else if (stmt->funcname != NULL)
+ {
+ /*
+ * Find the function,
+ */
+ funcOid = LookupFuncNameTypeNames(functionName, argTypes, false);
+ }
+
+ if (funcOid == InvalidOid && objects == NIL)
+ {
+ elog(NOTICE, "nothing to check");
+ return;
+ }
+
+ if (objects != NIL)
+ {
+ ListCell *object;
+
+ foreach(object, objects)
+ {
+ CHECK_FOR_INTERRUPTS();
+
+ funcOid = lfirst_oid(object);
+ CheckFunctionById(funcOid, InvalidOid);
+ }
+ }
+ else
+ {
+ CheckFunctionById(funcOid, relid);
+ }
+ }
/*
* Rename function
*** ./src/backend/commands/proclang.c.orig 2011-12-07 05:59:37.134674581 +0100
--- ./src/backend/commands/proclang.c 2011-12-07 06:00:07.264332779 +0100
***************
*** 46,57 ****
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
--- 46,58 ----
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
+ char *tmplchecker; /* name of checker function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
***************
*** 67,75 ****
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid;
Oid funcrettype;
! Oid funcargtypes[1];
/*
* If we have template information for the language, ignore the supplied
--- 68,77 ----
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid,
! checkerOid;
Oid funcrettype;
! Oid funcargtypes[2];
/*
* If we have template information for the language, ignore the supplied
***************
*** 219,228 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, pltemplate->tmpltrusted);
}
else
{
--- 221,269 ----
else
valOid = InvalidOid;
+ /*
+ * Likewise for the checker, if required; but we don't care about
+ * its return type.
+ */
+ if (pltemplate->tmplchecker)
+ {
+ funcname = SystemFuncName(pltemplate->tmplchecker);
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(funcname, 2, funcargtypes, true);
+ if (!OidIsValid(checkerOid))
+ {
+ checkerOid = ProcedureCreate(pltemplate->tmplchecker,
+ PG_CATALOG_NAMESPACE,
+ false, /* replace */
+ false, /* returnsSet */
+ VOIDOID,
+ ClanguageId,
+ F_FMGR_C_VALIDATOR,
+ pltemplate->tmplchecker,
+ pltemplate->tmpllibrary,
+ false, /* isAgg */
+ false, /* isWindowFunc */
+ false, /* security_definer */
+ true, /* isStrict */
+ PROVOLATILE_VOLATILE,
+ buildoidvector(funcargtypes, 2),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ NIL,
+ PointerGetDatum(NULL),
+ 1,
+ 0);
+ }
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, pltemplate->tmpltrusted);
}
else
{
***************
*** 294,303 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, stmt->pltrusted);
}
}
--- 335,355 ----
else
valOid = InvalidOid;
+ /* validate the checker function */
+ if (stmt->plchecker)
+ {
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(stmt->plchecker, 2, funcargtypes, false);
+ /* return value is ignored, so we don't check the type */
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, stmt->pltrusted);
}
}
***************
*** 307,313 ****
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
--- 359,365 ----
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
***************
*** 337,342 ****
--- 389,395 ----
values[Anum_pg_language_lanplcallfoid - 1] = ObjectIdGetDatum(handlerOid);
values[Anum_pg_language_laninline - 1] = ObjectIdGetDatum(inlineOid);
values[Anum_pg_language_lanvalidator - 1] = ObjectIdGetDatum(valOid);
+ values[Anum_pg_language_lanchecker - 1] = ObjectIdGetDatum(checkerOid);
nulls[Anum_pg_language_lanacl - 1] = true;
/* Check for pre-existing definition */
***************
*** 423,428 ****
--- 476,490 ----
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
+ /* dependency on the checker function, if any */
+ if (OidIsValid(checkerOid))
+ {
+ referenced.classId = ProcedureRelationId;
+ referenced.objectId = checkerOid;
+ referenced.objectSubId = 0;
+ recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
+ }
+
/* Post creation hook for new procedural language */
InvokeObjectAccessHook(OAT_POST_CREATE,
LanguageRelationId, myself.objectId, 0);
***************
*** 478,483 ****
--- 540,550 ----
if (!isnull)
result->tmplvalidator = TextDatumGetCString(datum);
+ datum = heap_getattr(tup, Anum_pg_pltemplate_tmplchecker,
+ RelationGetDescr(rel), &isnull);
+ if (!isnull)
+ result->tmplchecker = TextDatumGetCString(datum);
+
datum = heap_getattr(tup, Anum_pg_pltemplate_tmpllibrary,
RelationGetDescr(rel), &isnull);
if (!isnull)
*** ./src/backend/nodes/copyfuncs.c.orig 2011-12-07 05:59:37.135674570 +0100
--- ./src/backend/nodes/copyfuncs.c 2011-12-07 06:38:24.420227380 +0100
***************
*** 2880,2885 ****
--- 2880,2900 ----
return newnode;
}
+ static CheckFunctionStmt *
+ _copyCheckFunctionStmt(CheckFunctionStmt *from)
+ {
+ CheckFunctionStmt *newnode = makeNode(CheckFunctionStmt);
+
+ COPY_NODE_FIELD(funcname);
+ COPY_NODE_FIELD(args);
+ COPY_STRING_FIELD(trgname);
+ COPY_NODE_FIELD(relation);
+ COPY_SCALAR_FIELD(is_function);
+ COPY_NODE_FIELD(options);
+
+ return newnode;
+ }
+
static DoStmt *
_copyDoStmt(DoStmt *from)
{
***************
*** 4165,4170 ****
--- 4180,4188 ----
case T_AlterFunctionStmt:
retval = _copyAlterFunctionStmt(from);
break;
+ case T_CheckFunctionStmt:
+ retval = _copyCheckFunctionStmt(from);
+ break;
case T_DoStmt:
retval = _copyDoStmt(from);
break;
*** ./src/backend/nodes/equalfuncs.c.orig 2011-12-07 05:59:37.137674548 +0100
--- ./src/backend/nodes/equalfuncs.c 2011-12-07 06:38:56.187881989 +0100
***************
*** 1292,1297 ****
--- 1292,1310 ----
}
static bool
+ _equalCheckFunctionStmt(CheckFunctionStmt *a, CheckFunctionStmt *b)
+ {
+ COMPARE_NODE_FIELD(funcname);
+ COMPARE_NODE_FIELD(args);
+ COMPARE_STRING_FIELD(trgname);
+ COMPARE_NODE_FIELD(relation);
+ COMPARE_SCALAR_FIELD(is_function);
+ COMPARE_NODE_FIELD(options);
+
+ return true;
+ }
+
+ static bool
_equalDoStmt(DoStmt *a, DoStmt *b)
{
COMPARE_NODE_FIELD(args);
***************
*** 2708,2713 ****
--- 2721,2729 ----
case T_AlterFunctionStmt:
retval = _equalAlterFunctionStmt(a, b);
break;
+ case T_CheckFunctionStmt:
+ retval = _equalCheckFunctionStmt(a, b);
+ break;
case T_DoStmt:
retval = _equalDoStmt(a, b);
break;
*** ./src/backend/parser/gram.y.orig 2011-12-07 05:59:37.138674536 +0100
--- ./src/backend/parser/gram.y 2011-12-07 06:33:34.230177891 +0100
***************
*** 227,232 ****
--- 227,233 ----
DeallocateStmt PrepareStmt ExecuteStmt
DropOwnedStmt ReassignOwnedStmt
AlterTSConfigurationStmt AlterTSDictionaryStmt
+ CheckFunctionStmt
%type <node> select_no_parens select_with_parens select_clause
simple_select values_clause
***************
*** 276,282 ****
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate
%type <range> qualified_name OptConstrFromTable
--- 277,283 ----
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate opt_checker
%type <range> qualified_name OptConstrFromTable
***************
*** 463,469 ****
%type <windef> window_definition over_clause window_specification
opt_frame_clause frame_extent frame_bound
%type <str> opt_existing_window_name
!
/*
* Non-keyword token types. These are hard-wired into the "flex" lexer.
--- 464,471 ----
%type <windef> window_definition over_clause window_specification
opt_frame_clause frame_extent frame_bound
%type <str> opt_existing_window_name
! %type <defelt> CheckFunctionOptElem
! %type <list> CheckFunctionOpts
/*
* Non-keyword token types. These are hard-wired into the "flex" lexer.
***************
*** 700,705 ****
--- 702,708 ----
| AlterUserSetStmt
| AlterUserStmt
| AnalyzeStmt
+ | CheckFunctionStmt
| CheckPointStmt
| ClosePortalStmt
| ClusterStmt
***************
*** 3174,3184 ****
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
--- 3177,3188 ----
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
+ n->plchecker = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator opt_checker
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
***************
*** 3186,3191 ****
--- 3190,3196 ----
n->plhandler = $8;
n->plinline = $9;
n->plvalidator = $10;
+ n->plchecker = $11;
n->pltrusted = $3;
$$ = (Node *)n;
}
***************
*** 3220,3225 ****
--- 3225,3235 ----
| /*EMPTY*/ { $$ = NIL; }
;
+ opt_checker:
+ CHECK handler_name { $$ = $2; }
+ | /*EMPTY*/ { $$ = NIL; }
+ ;
+
DropPLangStmt:
DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
{
***************
*** 6250,6255 ****
--- 6260,6333 ----
/*****************************************************************************
*
+ * CHECK FUNCTION funcname(args)
+ * CHECK TRIGGER triggername ON table
+ *
+ *
+ *****************************************************************************/
+
+
+ CheckFunctionStmt:
+ CHECK FUNCTION func_name func_args
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = $3;
+ n->args = extractArgTypes($4);
+ n->trgname = NULL;
+ n->relation = NULL;
+ n->is_function = true;
+ n->options = NIL;
+ $$ = (Node *) n;
+ }
+ | CHECK TRIGGER name ON qualified_name
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = $3;
+ n->relation = $5;
+ n->is_function = false;
+ n->options = NIL;
+ $$ = (Node *) n;
+ }
+ | CHECK FUNCTION ALL CheckFunctionOpts
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = NULL;
+ n->relation = NULL;
+ n->is_function = true;
+ n->options = $4;
+ $$ = (Node *) n;
+ }
+ ;
+
+ CheckFunctionOpts:
+ CheckFunctionOptElem { $$ = list_make1($1); }
+ | CheckFunctionOpts CheckFunctionOptElem { $$ = lappend($1, $2); }
+ ;
+
+ CheckFunctionOptElem:
+ IN_P LANGUAGE ColId
+ {
+ $$ = makeDefElem("language",
+ (Node *) makeString($3));
+ }
+ | IN_P SCHEMA ColId
+ {
+ $$ = makeDefElem("schema",
+ (Node *) makeString($3));
+ }
+ | FOR ROLE ColId
+ {
+ $$ = makeDefElem("owner",
+ (Node *) makeString($3));
+ }
+ ;
+
+ /*****************************************************************************
+ *
* DO <anonymous code block> [ LANGUAGE language ]
*
* We use a DefElem list for future extensibility, and to allow flexibility
*** ./src/backend/tcop/utility.c.orig 2011-12-07 05:59:37.140674512 +0100
--- ./src/backend/tcop/utility.c 2011-12-07 06:35:57.836744377 +0100
***************
*** 882,887 ****
--- 882,891 ----
AlterFunction((AlterFunctionStmt *) parsetree);
break;
+ case T_CheckFunctionStmt:
+ CheckFunction((CheckFunctionStmt *) parsetree);
+ break;
+
case T_IndexStmt: /* CREATE INDEX */
{
IndexStmt *stmt = (IndexStmt *) parsetree;
***************
*** 2125,2130 ****
--- 2129,2141 ----
}
break;
+ case T_CheckFunctionStmt:
+ if (((CheckFunctionStmt *) parsetree)->is_function)
+ tag = "CHECK FUNCTION";
+ else
+ tag = "CHECK TRIGGER";
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
***************
*** 2565,2570 ****
--- 2576,2585 ----
}
break;
+ case T_CheckFunctionStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
*** ./src/bin/pg_dump/pg_dump.c.orig 2011-12-07 05:59:37.142674489 +0100
--- ./src/bin/pg_dump/pg_dump.c 2011-12-07 06:00:07.312332234 +0100
***************
*** 5326,5338 ****
int i_lanplcallfoid;
int i_laninline;
int i_lanvalidator;
int i_lanacl;
int i_lanowner;
/* Make sure we are in proper schema */
selectSourceSchema("pg_catalog");
! if (g_fout->remoteVersion >= 90000)
{
/* pg_language has a laninline column */
appendPQExpBuffer(query, "SELECT tableoid, oid, "
--- 5326,5351 ----
int i_lanplcallfoid;
int i_laninline;
int i_lanvalidator;
+ int i_lanchecker;
int i_lanacl;
int i_lanowner;
/* Make sure we are in proper schema */
selectSourceSchema("pg_catalog");
! if (g_fout->remoteVersion >= 90200)
! {
! /* pg_language has a lanchecker column */
! appendPQExpBuffer(query, "SELECT tableoid, oid, "
! "lanname, lanpltrusted, lanplcallfoid, "
! "laninline, lanvalidator, lanchecker, lanacl, "
! "(%s lanowner) AS lanowner "
! "FROM pg_language "
! "WHERE lanispl "
! "ORDER BY oid",
! username_subquery);
! }
! else if (g_fout->remoteVersion >= 90000)
{
/* pg_language has a laninline column */
appendPQExpBuffer(query, "SELECT tableoid, oid, "
***************
*** 5409,5414 ****
--- 5422,5428 ----
/* these may fail and return -1: */
i_laninline = PQfnumber(res, "laninline");
i_lanvalidator = PQfnumber(res, "lanvalidator");
+ i_lanchecker = PQfnumber(res, "lanchecker");
i_lanacl = PQfnumber(res, "lanacl");
i_lanowner = PQfnumber(res, "lanowner");
***************
*** 5422,5427 ****
--- 5436,5445 ----
planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
+ if (i_lanchecker >= 0)
+ planginfo[i].lanchecker = atooid(PQgetvalue(res, i, i_lanchecker));
+ else
+ planginfo[i].lanchecker = InvalidOid;
if (i_laninline >= 0)
planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
else
***************
*** 8597,8602 ****
--- 8615,8621 ----
char *qlanname;
char *lanschema;
FuncInfo *funcInfo;
+ FuncInfo *checkerInfo = NULL;
FuncInfo *inlineInfo = NULL;
FuncInfo *validatorInfo = NULL;
***************
*** 8616,8621 ****
--- 8635,8647 ----
if (funcInfo != NULL && !funcInfo->dobj.dump)
funcInfo = NULL; /* treat not-dumped same as not-found */
+ if (OidIsValid(plang->lanchecker))
+ {
+ checkerInfo = findFuncByOid(plang->lanchecker);
+ if (checkerInfo != NULL && !checkerInfo->dobj.dump)
+ checkerInfo = NULL;
+ }
+
if (OidIsValid(plang->laninline))
{
inlineInfo = findFuncByOid(plang->laninline);
***************
*** 8642,8647 ****
--- 8668,8674 ----
* don't, this might not work terribly nicely.
*/
useParams = (funcInfo != NULL &&
+ (checkerInfo != NULL || !OidIsValid(plang->lanchecker)) &&
(inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
(validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
***************
*** 8697,8702 ****
--- 8724,8739 ----
appendPQExpBuffer(defqry, "%s",
fmtId(validatorInfo->dobj.name));
}
+ if (OidIsValid(plang->lanchecker))
+ {
+ appendPQExpBuffer(defqry, " CHECK ");
+ /* Cope with possibility that checker is in different schema */
+ if (checkerInfo->dobj.namespace != funcInfo->dobj.namespace)
+ appendPQExpBuffer(defqry, "%s.",
+ fmtId(checkerInfo->dobj.namespace->dobj.name));
+ appendPQExpBuffer(defqry, "%s",
+ fmtId(checkerInfo->dobj.name));
+ }
}
else
{
*** ./src/bin/pg_dump/pg_dump.h.orig 2011-12-07 05:59:37.144674467 +0100
--- ./src/bin/pg_dump/pg_dump.h 2011-12-07 06:00:07.348331825 +0100
***************
*** 387,392 ****
--- 387,393 ----
Oid lanplcallfoid;
Oid laninline;
Oid lanvalidator;
+ Oid lanchecker;
char *lanacl;
char *lanowner; /* name of owner, or empty string */
} ProcLangInfo;
*** ./src/bin/psql/tab-complete.c.orig 2011-12-07 05:59:37.146674445 +0100
--- ./src/bin/psql/tab-complete.c 2011-12-07 06:00:07.350331803 +0100
***************
*** 1,4 ****
--- 1,5 ----
/*
+ *
* psql - the PostgreSQL interactive terminal
*
* Copyright (c) 2000-2011, PostgreSQL Global Development Group
***************
*** 727,733 ****
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
--- 728,734 ----
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECK", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
***************
*** 1524,1529 ****
--- 1525,1552 ----
COMPLETE_WITH_LIST(list_TRANS);
}
+
+ /* CHECK */
+ else if (pg_strcasecmp(prev_wd, "CHECK") == 0)
+ {
+ static const char *const list_CHECK[] =
+ {"FUNCTION", "TRIGGER", NULL};
+
+ COMPLETE_WITH_LIST(list_CHECK);
+ }
+ else if (pg_strcasecmp(prev3_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
+ {
+ COMPLETE_WITH_CONST("ON");
+ }
+ else if (pg_strcasecmp(prev4_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
+ pg_strcasecmp(prev_wd, "ON") == 0)
+ {
+ completion_info_charp = prev2_wd;
+ COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger);
+ }
+
/* CLUSTER */
/*
*** ./src/include/catalog/pg_language.h.orig 2011-12-07 05:59:37.147674434 +0100
--- ./src/include/catalog/pg_language.h 2011-12-07 06:00:07.351331792 +0100
***************
*** 37,42 ****
--- 37,43 ----
Oid lanplcallfoid; /* Call handler for PL */
Oid laninline; /* Optional anonymous-block handler function */
Oid lanvalidator; /* Optional validation function */
+ Oid lanchecker; /* Optional checker function */
aclitem lanacl[1]; /* Access privileges */
} FormData_pg_language;
***************
*** 51,57 ****
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 8
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
--- 52,58 ----
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 9
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
***************
*** 59,78 ****
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanacl 8
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
--- 60,80 ----
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanchecker 8
! #define Anum_pg_language_lanacl 9
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 0 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 0 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 0 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
*** ./src/include/catalog/pg_pltemplate.h.orig 2011-12-07 05:59:37.149674412 +0100
--- ./src/include/catalog/pg_pltemplate.h 2011-12-07 06:00:07.352331780 +0100
***************
*** 36,41 ****
--- 36,42 ----
text tmplhandler; /* name of call handler function */
text tmplinline; /* name of anonymous-block handler, or NULL */
text tmplvalidator; /* name of validator function, or NULL */
+ text tmplchecker; /* name of checker function, or NULL */
text tmpllibrary; /* path of shared library */
aclitem tmplacl[1]; /* access privileges for template */
} FormData_pg_pltemplate;
***************
*** 51,65 ****
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 8
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmpllibrary 7
! #define Anum_pg_pltemplate_tmplacl 8
/* ----------------
--- 52,67 ----
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 9
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmplchecker 7
! #define Anum_pg_pltemplate_tmpllibrary 8
! #define Anum_pg_pltemplate_tmplacl 9
/* ----------------
***************
*** 67,79 ****
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
--- 69,81 ----
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "plpgsql_checker" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" _null_ "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
*** ./src/include/commands/defrem.h.orig 2011-12-07 05:59:37.150674400 +0100
--- ./src/include/commands/defrem.h 2011-12-07 06:00:07.352331780 +0100
***************
*** 62,67 ****
--- 62,68 ----
/* commands/functioncmds.c */
extern void CreateFunction(CreateFunctionStmt *stmt, const char *queryString);
extern void RemoveFunctionById(Oid funcOid);
+ extern void CheckFunction(CheckFunctionStmt *stmt);
extern void SetFunctionReturnType(Oid funcOid, Oid newRetType);
extern void SetFunctionArgType(Oid funcOid, int argIndex, Oid newArgType);
extern void RenameFunction(List *name, List *argtypes, const char *newname);
*** ./src/include/nodes/nodes.h.orig 2011-12-07 05:59:37.151674388 +0100
--- ./src/include/nodes/nodes.h 2011-12-07 06:00:07.353331768 +0100
***************
*** 291,296 ****
--- 291,297 ----
T_IndexStmt,
T_CreateFunctionStmt,
T_AlterFunctionStmt,
+ T_CheckFunctionStmt,
T_DoStmt,
T_RenameStmt,
T_RuleStmt,
*** ./src/include/nodes/parsenodes.h.orig 2011-12-07 05:59:37.153674364 +0100
--- ./src/include/nodes/parsenodes.h 2011-12-07 06:16:44.502121440 +0100
***************
*** 1734,1739 ****
--- 1734,1740 ----
List *plhandler; /* PL call handler function (qual. name) */
List *plinline; /* optional inline function (qual. name) */
List *plvalidator; /* optional validator function (qual. name) */
+ List *plchecker; /* optional checker function (qual. name) */
bool pltrusted; /* PL is trusted */
} CreatePLangStmt;
***************
*** 2077,2082 ****
--- 2078,2098 ----
} AlterFunctionStmt;
/* ----------------------
+ * Check {Function|Trigger} Statement
+ * ----------------------
+ */
+ typedef struct CheckFunctionStmt
+ {
+ NodeTag type;
+ List *funcname; /* qualified name of checked object */
+ List *args; /* types of the arguments */
+ char *trgname; /* trigger's name */
+ RangeVar *relation; /* trigger's relation */
+ bool is_function; /* true for CHECK FUNCTION statement */
+ List *options; /* other DefElem options */
+ } CheckFunctionStmt;
+
+ /* ----------------------
* DO Statement
*
* DoStmt is the raw parser output, InlineCodeBlock is the execution-time API
*** ./src/pl/plpgsql/src/pl_comp.c.orig 2011-12-07 05:59:37.155674342 +0100
--- ./src/pl/plpgsql/src/pl_comp.c 2011-12-07 06:00:07.360331689 +0100
***************
*** 115,121 ****
static void plpgsql_HashTableInsert(PLpgSQL_function *function,
PLpgSQL_func_hashkey *func_key);
static void plpgsql_HashTableDelete(PLpgSQL_function *function);
- static void delete_function(PLpgSQL_function *func);
/* ----------
* plpgsql_compile Make an execution tree for a PL/pgSQL function.
--- 115,120 ----
***************
*** 175,181 ****
* Nope, so remove it from hashtable and try to drop associated
* storage (if not done already).
*/
! delete_function(function);
/*
* If the function isn't in active use then we can overwrite the
--- 174,180 ----
* Nope, so remove it from hashtable and try to drop associated
* storage (if not done already).
*/
! plpgsql_delete_function(function);
/*
* If the function isn't in active use then we can overwrite the
***************
*** 2426,2432 ****
}
/*
! * delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
--- 2425,2431 ----
}
/*
! * plpgsql_delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
***************
*** 2439,2446 ****
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
! static void
! delete_function(PLpgSQL_function *func)
{
/* remove function from hash table (might be done already) */
plpgsql_HashTableDelete(func);
--- 2438,2445 ----
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
! void
! plpgsql_delete_function(PLpgSQL_function *func)
{
/* remove function from hash table (might be done already) */
plpgsql_HashTableDelete(func);
*** ./src/pl/plpgsql/src/pl_exec.c.orig 2011-12-07 05:59:37.157674320 +0100
--- ./src/pl/plpgsql/src/pl_exec.c 2011-12-07 07:36:23.112366630 +0100
***************
*** 210,216 ****
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
PLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
!
/* ----------
* plpgsql_exec_function Called by the call handler for
--- 210,228 ----
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
PLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
! static void check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec);
! static void check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr);
! static void assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
! PLpgSQL_row *row, PLpgSQL_rec *rec,
! TupleDesc tupdesc);
! static TupleDesc expr_get_desc(PLpgSQL_execstate *estate,
! PLpgSQL_expr *query,
! bool use_element_type,
! bool expand_record,
! bool is_expression);
! static void var_init_to_null(PLpgSQL_execstate *estate, int varno);
! static void check_stmts(PLpgSQL_execstate *estate, List *stmts);
! static void check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
/* ----------
* plpgsql_exec_function Called by the call handler for
***************
*** 6176,6178 ****
--- 6188,7240 ----
return portal;
}
+
+ /*
+ * Following code ensures a CHECK FUNCTION and CHECK TRIGGER statements for PL/pgSQL
+ *
+ */
+
+ /*
+ * append a CONTEXT to error message
+ */
+ static void
+ check_error_callback(void *arg)
+ {
+ PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;
+
+ if (estate->err_stmt != NULL)
+ {
+ /* translator: last %s is a plpgsql statement type name */
+ errcontext("line %d at %s",
+ estate->err_stmt->lineno,
+ plpgsql_stmt_typename(estate->err_stmt));
+ }
+ }
+
+ /*
+ * Check function - it prepare variables and starts a prepare plan walker
+ * called by function checker
+ */
+ void
+ plpgsql_check_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
+
+ /*
+ * Store the actual call argument values into the appropriate variables
+ */
+ for (i = 0; i < func->fn_nargs; i++)
+ {
+ int n = func->fn_argvarnos[i];
+
+ switch (estate.datums[n]->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ {
+ var_init_to_null(&estate, n);
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ {
+ PLpgSQL_row *row = (PLpgSQL_row *) estate.datums[n];
+
+ exec_move_row(&estate, NULL, row, NULL, NULL);
+ }
+ break;
+
+ default:
+ elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
+ }
+ }
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Check trigger - prepare fake environments for testing trigger
+ *
+ */
+ void
+ plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ PLpgSQL_rec *rec_new,
+ *rec_old;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, NULL);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
+
+ /*
+ * Put the OLD and NEW tuples into record variables
+ *
+ * We make the tupdescs available in both records even though only one may
+ * have a value. This allows parsing of record references to succeed in
+ * functions that are used for multiple trigger types. For example, we
+ * might have a test like "if (TG_OP = 'INSERT' and NEW.foo = 'xyz')",
+ * which should parse regardless of the current trigger type.
+ */
+ rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
+ rec_new->freetup = false;
+ rec_new->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_new, trigdata->tg_relation->rd_att);
+
+ rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
+ rec_old->freetup = false;
+ rec_old->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_old, trigdata->tg_relation->rd_att);
+
+ /*
+ * Assign the special tg_ variables
+ */
+ var_init_to_null(&estate, func->tg_op_varno);
+ var_init_to_null(&estate, func->tg_name_varno);
+ var_init_to_null(&estate, func->tg_when_varno);
+ var_init_to_null(&estate, func->tg_level_varno);
+ var_init_to_null(&estate, func->tg_relid_varno);
+ var_init_to_null(&estate, func->tg_relname_varno);
+ var_init_to_null(&estate, func->tg_table_name_varno);
+ var_init_to_null(&estate, func->tg_table_schema_varno);
+ var_init_to_null(&estate, func->tg_nargs_varno);
+ var_init_to_null(&estate, func->tg_argv_varno);
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Verify lvalue
+ * It doesn't repeat a checks that are done.
+ * Checks a subscript expressions, verify a validity of record's fields
+ */
+ static void
+ check_target(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ switch (target->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ case PLPGSQL_DTYPE_REC:
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ check_row_or_rec(estate, (PLpgSQL_row *) target, NULL);
+ break;
+
+ case PLPGSQL_DTYPE_RECFIELD:
+ {
+ PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
+ PLpgSQL_rec *rec;
+ int fno;
+
+ rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
+
+ /*
+ * Check that there is already a tuple in the record. We need
+ * that because records don't have any predefined field
+ * structure.
+ */
+ if (!HeapTupleIsValid(rec->tup))
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("record \"%s\" is not assigned to tuple structure",
+ rec->refname)));
+
+ /*
+ * Get the number of the records field to change and the
+ * number of attributes in the tuple. Note: disallow system
+ * column names because the code below won't cope.
+ */
+ fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
+ if (fno <= 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("record \"%s\" has no field \"%s\"",
+ rec->refname, recfield->fieldname)));
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ARRAYELEM:
+ {
+ /*
+ * Target is an element of an array
+ */
+ int nsubscripts;
+ Oid arrayelemtypeid;
+ Oid arraytypeid;
+
+ /*
+ * To handle constructs like x[1][2] := something, we have to
+ * be prepared to deal with a chain of arrayelem datums. Chase
+ * back to find the base array datum, and save the subscript
+ * expressions as we go. (We are scanning right to left here,
+ * but want to evaluate the subscripts left-to-right to
+ * minimize surprises.)
+ */
+ nsubscripts = 0;
+ do
+ {
+ PLpgSQL_arrayelem *arrayelem = (PLpgSQL_arrayelem *) target;
+
+ if (nsubscripts++ >= MAXDIM)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
+ nsubscripts + 1, MAXDIM)));
+
+ check_expr(estate, arrayelem->subscript);
+
+ target = estate->datums[arrayelem->arrayparentno];
+ } while (target->dtype == PLPGSQL_DTYPE_ARRAYELEM);
+
+ /* If target is domain over array, reduce to base type */
+ arraytypeid = exec_get_datum_type(estate, target);
+ arraytypeid = getBaseType(arraytypeid);
+
+ arrayelemtypeid = get_element_type(arraytypeid);
+
+ if (!OidIsValid(arrayelemtypeid))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("subscripted object is not an array")));
+ }
+ break;
+ }
+ }
+
+ /*
+ * Check composed lvalue
+ * There is nothing to check on rec variables
+ */
+ static void
+ check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec)
+ {
+ int fnum;
+
+ /* there are nothing to check on rec now */
+ if (row != NULL)
+ {
+ for (fnum = 0; fnum < row->nfields; fnum++)
+ {
+ /* skip dropped columns */
+ if (row->varnos[fnum] < 0)
+ continue;
+
+ check_target(estate, row->varnos[fnum]);
+ }
+ }
+ }
+
+ /*
+ * Generate a prepared plan - this is simplyfied copy from pl_exec.c
+ * Is not necessary to check simple plan
+ */
+ static void
+ prepare_expr(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *expr, int cursorOptions)
+ {
+ SPIPlanPtr plan;
+
+ /* leave when there are not expression */
+ if (expr == NULL)
+ return;
+
+ /* leave when plan is created */
+ if (expr->plan != NULL)
+ return;
+
+ /*
+ * The grammar can't conveniently set expr->func while building the parse
+ * tree, so make sure it's set before parser hooks need it.
+ */
+ expr->func = estate->func;
+
+ /*
+ * Generate and save the plan
+ */
+ plan = SPI_prepare_params(expr->query,
+ (ParserSetupHook) plpgsql_parser_setup,
+ (void *) expr,
+ cursorOptions);
+ if (plan == NULL)
+ {
+ /* Some SPI errors deserve specific error messages */
+ switch (SPI_result)
+ {
+ case SPI_ERROR_COPY:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot COPY to/from client in PL/pgSQL")));
+ case SPI_ERROR_TRANSACTION:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot begin/end transactions in PL/pgSQL"),
+ errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
+ default:
+ elog(ERROR, "SPI_prepare_params failed for \"%s\": %s",
+ expr->query, SPI_result_code_string(SPI_result));
+ }
+ }
+
+ expr->plan = SPI_saveplan(plan);
+ SPI_freeplan(plan);
+ }
+
+ /*
+ * Verify a expression
+ */
+ static void
+ check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
+ {
+ TupleDesc tupdesc;
+
+ if (expr != NULL)
+ {
+ prepare_expr(estate, expr, 0);
+ tupdesc = expr_get_desc(estate, expr, false, false, true);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+
+ /*
+ * We have to assign TupleDesc to all used record variables step by step.
+ * We would to use a exec routines for query preprocessing, so we must
+ * to create a typed NULL value, and this value is assigned to record
+ * variable.
+ */
+ static void
+ assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
+ PLpgSQL_row *row, PLpgSQL_rec *rec,
+ TupleDesc tupdesc)
+ {
+ bool *nulls;
+ HeapTuple tup;
+
+ if (tupdesc == NULL)
+ elog(ERROR, "tuple descriptor is empty");
+
+ /*
+ * row variable has assigned TupleDesc already, so don't be processed
+ * here
+ */
+ if (rec != NULL)
+ {
+ PLpgSQL_rec *target = (PLpgSQL_rec *)(estate->datums[rec->dno]);
+
+ if (target->freetup)
+ heap_freetuple(target->tup);
+
+ if (rec->freetupdesc)
+ FreeTupleDesc(target->tupdesc);
+
+ /* initialize rec by NULLs */
+ nulls = (bool *) palloc(tupdesc->natts * sizeof(bool));
+ memset(nulls, true, tupdesc->natts * sizeof(bool));
+
+ target->tupdesc = CreateTupleDescCopy(tupdesc);
+ target->freetupdesc = true;
+
+ tup = heap_form_tuple(tupdesc, NULL, nulls);
+ if (HeapTupleIsValid(tup))
+ {
+ target->tup = tup;
+ target->freetup = true;
+ }
+ else
+ elog(ERROR, "cannot to build valid composite value");
+ }
+ }
+
+ /*
+ * Assign a tuple descriptor to variable specified by dno
+ */
+ static void
+ assign_tupdesc_dno(PLpgSQL_execstate *estate, int varno, TupleDesc tupdesc)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ if (target->dtype == PLPGSQL_DTYPE_REC)
+ assign_tupdesc_row_or_rec(estate, NULL, (PLpgSQL_rec *) target, tupdesc);
+ }
+
+ /*
+ * Returns a tuple descriptor based on existing plan
+ */
+ static TupleDesc
+ expr_get_desc(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *query,
+ bool use_element_type,
+ bool expand_record,
+ bool is_expression)
+ {
+ TupleDesc tupdesc = NULL;
+ CachedPlanSource *plansource = NULL;
+
+ if (query->plan != NULL)
+ {
+ SPIPlanPtr plan = query->plan;
+
+ if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
+ elog(ERROR, "cached plan is not valid plan");
+
+ if (list_length(plan->plancache_list) != 1)
+ elog(ERROR, "plan is not single execution plan");
+
+ plansource = (CachedPlanSource *) linitial(plan->plancache_list);
+
+ tupdesc = CreateTupleDescCopy(plansource->resultDesc);
+ }
+ else
+ elog(ERROR, "there are no plan for query: \"%s\"",
+ query->query);
+
+ /*
+ * try to get a element type, when result is a array (used with FOREACH ARRAY stmt)
+ */
+ if (use_element_type)
+ {
+ Oid elemtype;
+ TupleDesc elemtupdesc;
+
+ /* result should be a array */
+ if (tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /* check the type of the expression - must be an array */
+ elemtype = get_element_type(tupdesc->attrs[0]->atttypid);
+ if (!OidIsValid(elemtype))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("FOREACH expression must yield an array, not type %s",
+ format_type_be(tupdesc->attrs[0]->atttypid))));
+
+ /* we can't know typmod now */
+ elemtupdesc = lookup_rowtype_tupdesc_noerror(elemtype, -1, true);
+ if (elemtupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(elemtupdesc);
+ ReleaseTupleDesc(elemtupdesc);
+ }
+ else
+ elog(ERROR, "cannot to identify real type for record type variable");
+ }
+
+ if (is_expression && tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /*
+ * One spacial case is when record is assigned to composite type, then
+ * we should to unpack composite type.
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 && expand_record)
+ {
+ TupleDesc unpack_tupdesc;
+
+ unpack_tupdesc = lookup_rowtype_tupdesc_noerror(tupdesc->attrs[0]->atttypid,
+ tupdesc->attrs[0]->atttypmod,
+ true);
+ if (unpack_tupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(unpack_tupdesc);
+ ReleaseTupleDesc(unpack_tupdesc);
+ }
+ }
+
+ /*
+ * There is special case, when returned tupdesc contains only
+ * unpined record: rec := func_with_out_parameters(). IN this case
+ * we must to dig more deep - we have to find oid of function and
+ * get their parameters,
+ *
+ * This is support for assign statement
+ * recvar := func_with_out_parameters(..)
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 &&
+ tupdesc->attrs[0]->atttypid == RECORDOID &&
+ tupdesc->attrs[0]->atttypmod == -1 &&
+ expand_record)
+ {
+ PlannedStmt *_stmt;
+ Plan *_plan;
+ TargetEntry *tle;
+ CachedPlan *cplan;
+
+ /*
+ * When tupdesc is related to unpined record, we will try
+ * to check plan if it is just function call and if it is
+ * then we can try to derive a tupledes from function's
+ * description.
+ */
+ cplan = GetCachedPlan(plansource, NULL, true);
+ _stmt = (PlannedStmt *) linitial(cplan->stmt_list);
+
+ if (IsA(_stmt, PlannedStmt) && _stmt->commandType == CMD_SELECT)
+ {
+ _plan = _stmt->planTree;
+ if (IsA(_plan, Result) && list_length(_plan->targetlist) == 1)
+ {
+ tle = (TargetEntry *) linitial(_plan->targetlist);
+ if (((Node *) tle->expr)->type == T_FuncExpr)
+ {
+ FuncExpr *fn = (FuncExpr *) tle->expr;
+ FmgrInfo flinfo;
+ FunctionCallInfoData fcinfo;
+ TupleDesc rd;
+ Oid rt;
+
+ fmgr_info(fn->funcid, &flinfo);
+ flinfo.fn_expr = (Node *) fn;
+ fcinfo.flinfo = &flinfo;
+
+ get_call_result_type(&fcinfo, &rt, &rd);
+ if (rd == NULL)
+ elog(ERROR, "function does not return composite type is not possible to identify composite type");
+
+ FreeTupleDesc(tupdesc);
+ BlessTupleDesc(rd);
+
+ tupdesc = rd;
+ }
+ }
+ }
+
+ ReleaseCachedPlan(cplan, true);
+ }
+
+ return tupdesc;
+ }
+
+ /*
+ * Ensure check for all statements in list
+ */
+ static void
+ check_stmts(PLpgSQL_execstate *estate, List *stmts)
+ {
+ ListCell *lc;
+
+ foreach(lc, stmts)
+ {
+ check_stmt(estate, (PLpgSQL_stmt *) lfirst(lc));
+ }
+ }
+
+ /*
+ * walk over all statements
+ */
+ static void
+ check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
+ {
+ TupleDesc tupdesc = NULL;
+ PLpgSQL_function *func;
+ ListCell *l;
+
+ if (stmt == NULL)
+ return;
+
+ estate->err_stmt = stmt;
+ func = estate->func;
+
+ switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
+ {
+ case PLPGSQL_STMT_BLOCK:
+ {
+ PLpgSQL_stmt_block *stmt_block = (PLpgSQL_stmt_block *) stmt;
+ int i;
+ PLpgSQL_datum *d;
+
+ for (i = 0; i < stmt_block->n_initvars; i++)
+ {
+ d = func->datums[stmt_block->initvarnos[i]];
+
+ if (d->dtype == PLPGSQL_DTYPE_VAR)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) d;
+
+ check_expr(estate, var->default_val);
+ }
+ }
+
+ check_stmts(estate, stmt_block->body);
+
+ if (stmt_block->exceptions)
+ {
+ foreach(l, stmt_block->exceptions->exc_list)
+ {
+ check_stmts(estate, ((PLpgSQL_exception *) lfirst(l))->action);
+ }
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_ASSIGN:
+ {
+ PLpgSQL_stmt_assign *stmt_assign = (PLpgSQL_stmt_assign *) stmt;
+
+ /* prepare plan if desn't exist yet */
+ prepare_expr(estate, stmt_assign->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_assign->expr,
+ false, /* no element type */
+ true, /* expand record */
+ true); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_target(estate, stmt_assign->varno);
+
+ /* assign a tupdesc to record variable */
+ assign_tupdesc_dno(estate, stmt_assign->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_IF:
+ {
+ PLpgSQL_stmt_if *stmt_if = (PLpgSQL_stmt_if *) stmt;
+ ListCell *l;
+
+ check_expr(estate, stmt_if->cond);
+
+ check_stmts(estate, stmt_if->then_body);
+
+ foreach(l, stmt_if->elsif_list)
+ {
+ PLpgSQL_if_elsif *elif = (PLpgSQL_if_elsif *) lfirst(l);
+
+ check_expr(estate, elif->cond);
+ check_stmts(estate, elif->stmts);
+ }
+
+ check_stmts(estate, stmt_if->else_body);
+ }
+ break;
+
+ case PLPGSQL_STMT_CASE:
+ {
+ PLpgSQL_stmt_case *stmt_case = (PLpgSQL_stmt_case *) stmt;
+ Oid result_oid;
+
+ if (stmt_case->t_expr != NULL)
+ {
+ PLpgSQL_var *t_var = (PLpgSQL_var *) estate->datums[stmt_case->t_varno];
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_case->t_expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_case->t_expr,
+ false, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ result_oid = tupdesc->attrs[0]->atttypid;
+
+ /*
+ * When expected datatype is different from real, change it. Note that
+ * what we're modifying here is an execution copy of the datum, so
+ * this doesn't affect the originally stored function parse tree.
+ */
+
+ if (t_var->datatype->typoid != result_oid)
+ t_var->datatype = plpgsql_build_datatype(result_oid,
+ -1,
+ estate->func->fn_input_collation);
+
+ ReleaseTupleDesc(tupdesc);
+ }
+
+ foreach(l, stmt_case->case_when_list)
+ {
+ PLpgSQL_case_when *cwt = (PLpgSQL_case_when *) lfirst(l);
+
+ check_expr(estate, cwt->expr);
+ check_stmts(estate, cwt->stmts);
+ }
+
+ check_stmts(estate, stmt_case->else_stmts);
+ }
+ break;
+
+ case PLPGSQL_STMT_LOOP:
+ check_stmts(estate, ((PLpgSQL_stmt_loop *) stmt)->body);
+ break;
+
+ case PLPGSQL_STMT_WHILE:
+ {
+ PLpgSQL_stmt_while *stmt_while = (PLpgSQL_stmt_while *) stmt;
+
+ check_expr(estate, stmt_while->cond);
+ check_stmts(estate, stmt_while->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORI:
+ {
+ PLpgSQL_stmt_fori *stmt_fori = (PLpgSQL_stmt_fori *) stmt;
+
+ check_expr(estate, stmt_fori->lower);
+ check_expr(estate, stmt_fori->upper);
+ check_expr(estate, stmt_fori->step);
+
+ check_stmts(estate, stmt_fori->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORS:
+ {
+ PLpgSQL_stmt_fors *stmt_fors = (PLpgSQL_stmt_fors *) stmt;
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_fors->query, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_fors->query,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_fors->row, stmt_fors->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fors->row, stmt_fors->rec, tupdesc);
+
+ check_stmts(estate, stmt_fors->body);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORC:
+ {
+ PLpgSQL_stmt_forc *stmt_forc = (PLpgSQL_stmt_forc *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_forc->curvar];
+
+ prepare_expr(estate, stmt_forc->argquery, 0);
+
+ if (var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_forc->row, stmt_forc->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_forc->row, stmt_forc->rec, tupdesc);
+ }
+
+ check_stmts(estate, stmt_forc->body);
+ if (tupdesc != NULL)
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNFORS:
+ {
+ PLpgSQL_stmt_dynfors * stmt_dynfors = (PLpgSQL_stmt_dynfors *) stmt;
+
+ if (stmt_dynfors->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_expr(estate, stmt_dynfors->query);
+
+ foreach(l, stmt_dynfors->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ check_stmts(estate, stmt_dynfors->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FOREACH_A:
+ {
+ PLpgSQL_stmt_foreach_a *stmt_foreach_a = (PLpgSQL_stmt_foreach_a *) stmt;
+
+ prepare_expr(estate, stmt_foreach_a->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_foreach_a->expr,
+ true, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ check_target(estate, stmt_foreach_a->varno);
+ assign_tupdesc_dno(estate, stmt_foreach_a->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+
+ check_stmts(estate, stmt_foreach_a->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_EXIT:
+ check_expr(estate, ((PLpgSQL_stmt_exit *) stmt)->cond);
+ break;
+
+ case PLPGSQL_STMT_PERFORM:
+ prepare_expr(estate, ((PLpgSQL_stmt_perform *) stmt)->expr, 0);
+ break;
+
+ case PLPGSQL_STMT_RETURN:
+ check_expr(estate, ((PLpgSQL_stmt_return *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_NEXT:
+ check_expr(estate, ((PLpgSQL_stmt_return_next *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_QUERY:
+ {
+ PLpgSQL_stmt_return_query *stmt_rq = (PLpgSQL_stmt_return_query *) stmt;
+
+ check_expr(estate, stmt_rq->dynquery);
+ prepare_expr(estate, stmt_rq->query, 0);
+
+ foreach(l, stmt_rq->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_RAISE:
+ {
+ PLpgSQL_stmt_raise *stmt_raise = (PLpgSQL_stmt_raise *) stmt;
+ ListCell *current_param;
+ char *cp;
+
+ foreach(l, stmt_raise->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ foreach(l, stmt_raise->options)
+ {
+ PLpgSQL_raise_option *opt = (PLpgSQL_raise_option *) lfirst(l);
+
+ check_expr(estate, opt->expr);
+ }
+
+ current_param = list_head(stmt_raise->params);
+
+ /* ensure any single % has a own parameter */
+ if (stmt_raise->message != NULL)
+ {
+ for (cp = stmt_raise->message; *cp; cp++)
+ {
+ if (cp[0] == '%')
+ {
+ if (cp[1] == '%')
+ {
+ cp++;
+ continue;
+ }
+
+ if (current_param == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too few parameters specified for RAISE")));
+
+ current_param = lnext(current_param);
+ }
+ }
+ }
+
+ if (current_param != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too many parameters specified for RAISE")));
+ }
+ break;
+
+ case PLPGSQL_STMT_EXECSQL:
+ {
+ PLpgSQL_stmt_execsql *stmt_execsql = (PLpgSQL_stmt_execsql *) stmt;
+
+ prepare_expr(estate, stmt_execsql->sqlstmt, 0);
+ if (stmt_execsql->into)
+ {
+ tupdesc = expr_get_desc(estate,
+ stmt_execsql->sqlstmt,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNEXECUTE:
+ {
+ PLpgSQL_stmt_dynexecute *stmt_dynexecute = (PLpgSQL_stmt_dynexecute *) stmt;
+
+ check_expr(estate, stmt_dynexecute->query);
+
+ foreach(l, stmt_dynexecute->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ if (stmt_dynexecute->into)
+ {
+ if (stmt_dynexecute->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_row_or_rec(estate, stmt_dynexecute->row, stmt_dynexecute->rec);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_OPEN:
+ {
+ PLpgSQL_stmt_open *stmt_open = (PLpgSQL_stmt_open *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_open->curvar];
+
+ if (var->cursor_explicit_expr)
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ prepare_expr(estate, stmt_open->query, 0);
+ prepare_expr(estate, stmt_open->argquery, 0);
+ check_expr(estate, stmt_open->dynquery);
+
+ foreach(l, stmt_open->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_GETDIAG:
+ {
+ PLpgSQL_stmt_getdiag *stmt_getdiag = (PLpgSQL_stmt_getdiag *) stmt;
+ ListCell *lc;
+
+ foreach(lc, stmt_getdiag->diag_items)
+ {
+ PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
+
+ check_target(estate, diag_item->target);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_FETCH:
+ {
+ PLpgSQL_stmt_fetch *stmt_fetch = (PLpgSQL_stmt_fetch *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *)(estate->datums[stmt_fetch->curvar]);
+
+ if (var != NULL && var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+ check_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_CLOSE:
+ break;
+
+ default:
+ elog(ERROR, "unrecognized cmd_type: %d", stmt->cmd_type);
+ return; /* be compiler quite */
+ }
+ }
+
+ /*
+ * Initialize variable to NULL
+ */
+ static void
+ var_init_to_null(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) estate->datums[varno];
+ var->value = (Datum) 0;
+ var->isnull = true;
+ var->freeval = false;
+ }
*** ./src/pl/plpgsql/src/pl_handler.c.orig 2011-12-07 05:59:37.158674309 +0100
--- ./src/pl/plpgsql/src/pl_handler.c 2011-12-07 06:00:07.365331632 +0100
***************
*** 312,314 ****
--- 312,452 ----
PG_RETURN_VOID();
}
+
+ /* ----------
+ * plpgsql_checker
+ *
+ * This function attempts to check a embeded SQL inside a PL/pgSQL function at
+ * CHECK FUNCTION time. It should to have one or two parameters. Second
+ * parameter is a relation (used when function is trigger).
+ * ----------
+ */
+ PG_FUNCTION_INFO_V1(plpgsql_checker);
+
+ Datum
+ plpgsql_checker(PG_FUNCTION_ARGS)
+ {
+ Oid funcoid = PG_GETARG_OID(0);
+ Oid relid = PG_GETARG_OID(1);
+ HeapTuple tuple;
+ FunctionCallInfoData fake_fcinfo;
+ FmgrInfo flinfo;
+ TriggerData trigdata;
+ int rc;
+ PLpgSQL_function *function;
+ PLpgSQL_execstate *cur_estate;
+
+ Form_pg_proc proc;
+ char functyptype;
+ bool istrigger = false;
+
+ /* we don't need to repair a check done by validator */
+
+ tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
+ if (!HeapTupleIsValid(tuple))
+ elog(ERROR, "cache lookup failed for function %u", funcoid);
+ proc = (Form_pg_proc) GETSTRUCT(tuple);
+
+ functyptype = get_typtype(proc->prorettype);
+
+ if (functyptype == TYPTYPE_PSEUDO)
+ {
+ /* we assume OPAQUE with no arguments means a trigger */
+ if (proc->prorettype == TRIGGEROID ||
+ (proc->prorettype == OPAQUEOID && proc->pronargs == 0))
+ {
+ istrigger = true;
+ if (!OidIsValid(relid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("PL/pgSQL trigger functions cannot be checked directly"),
+ errhint("use CHECK TRIGGER statement instead")));
+ }
+ }
+
+ /*
+ * Connect to SPI manager
+ */
+ if ((rc = SPI_connect()) != SPI_OK_CONNECT)
+ elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
+
+ /*
+ * Set up a fake fcinfo with just enough info to satisfy
+ * plpgsql_compile().
+ *
+ * there should be a different real argtypes for polymorphic params
+ */
+ MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo));
+ MemSet(&flinfo, 0, sizeof(flinfo));
+ fake_fcinfo.flinfo = &flinfo;
+ flinfo.fn_oid = funcoid;
+ flinfo.fn_mcxt = CurrentMemoryContext;
+
+ if (istrigger)
+ {
+ MemSet(&trigdata, 0, sizeof(trigdata));
+ trigdata.type = T_TriggerData;
+ trigdata.tg_relation = relation_open(relid, AccessShareLock);
+ fake_fcinfo.context = (Node *) &trigdata;
+ }
+
+ /* Get a compiled function */
+ function = plpgsql_compile(&fake_fcinfo, false);
+
+ /* Must save and restore prior value of cur_estate */
+ cur_estate = function->cur_estate;
+
+ /* Mark the function as busy, so it can't be deleted from under us */
+ function->use_count++;
+
+
+ /* Create a fake runtime environment and prepare plans */
+ PG_TRY();
+ {
+ if (!istrigger)
+ plpgsql_check_function(function, &fake_fcinfo);
+ else
+ plpgsql_check_trigger(function, &trigdata);
+ }
+ PG_CATCH();
+ {
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ /*
+ * Disconnect from SPI manager
+ */
+ if ((rc = SPI_finish()) != SPI_OK_FINISH)
+ elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
+
+ ReleaseSysCache(tuple);
+
+ PG_RETURN_VOID();
+ }
*** ./src/pl/plpgsql/src/plpgsql.h.orig 2011-12-07 05:59:37.160674287 +0100
--- ./src/pl/plpgsql/src/plpgsql.h 2011-12-07 06:00:07.366331620 +0100
***************
*** 902,907 ****
--- 902,908 ----
extern void plpgsql_adddatum(PLpgSQL_datum *new);
extern int plpgsql_add_initdatums(int **varnos);
extern void plpgsql_HashTableInit(void);
+ extern void plpgsql_delete_function(PLpgSQL_function *func);
/* ----------
* Functions in pl_handler.c
***************
*** 911,916 ****
--- 912,918 ----
extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_inline_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
+ extern Datum plpgsql_checker(PG_FUNCTION_ARGS);
/* ----------
* Functions in pl_exec.c
***************
*** 928,933 ****
--- 930,939 ----
extern void exec_get_datum_type_info(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum,
Oid *typeid, int32 *typmod, Oid *collation);
+ extern void plpgsql_check_function(PLpgSQL_function *func,
+ FunctionCallInfo fcinfo);
+ extern void plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata);
/* ----------
* Functions for namespace handling in pl_funcs.c
*** ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql.orig 2011-12-07 05:59:37.162674263 +0100
--- ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql 2011-12-07 06:00:07.366331620 +0100
***************
*** 5,7 ****
--- 5,8 ----
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_call_handler();
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_inline_handler(internal);
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_validator(oid);
+ ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_checker(oid, regclass);
*** ./src/test/regress/expected/plpgsql.out.orig 2011-12-07 05:59:37.164674240 +0100
--- ./src/test/regress/expected/plpgsql.out 2011-12-07 08:00:04.000000000 +0100
***************
*** 302,307 ****
--- 302,310 ----
' language plpgsql;
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
+ NOTICE: checked function "tg_hslot_biu()"
-- ************************************************************
-- * BEFORE DELETE on HSlot
-- * - prevent from manual manipulation
***************
*** 635,640 ****
--- 638,646 ----
raise exception ''illegal backlink beginning with %'', mytype;
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
+ NOTICE: checked function "tg_backlink_set(character,character)"
-- ************************************************************
-- * Support function to clear out the backlink field if
-- * it still points to specific slot
***************
*** 2802,2807 ****
--- 2808,2842 ----
(1 row)
+ -- check function should not fail
+ check function for_vect();
+ NOTICE: checked function "for_vect()"
+ -- recheck after check function
+ select for_vect();
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 4
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1 bb cc
+ NOTICE: 2 bb cc
+ NOTICE: 3 bb cc
+ NOTICE: 4 bb cc
+ for_vect
+ ----------
+
+ (1 row)
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 3283,3288 ****
--- 3318,3326 ----
return;
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
+ NOTICE: checked function "forc01()"
select forc01();
NOTICE: 5 from c
NOTICE: 6 from c
***************
*** 3716,3721 ****
--- 3754,3762 ----
end case;
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
+ NOTICE: checked function "case_test(bigint)"
select case_test(1);
case_test
-----------
***************
*** 4571,4573 ****
--- 4612,4956 ----
CONTEXT: PL/pgSQL function "testoa" line 5 at assignment
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+ --
+ -- check function statement tests
+ --
+ create table t1(a int, b int);
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ LINE 1: update t1 set c = 30
+ ^
+ DETAIL: column "c" of relation "t1" does not exist
+ QUERY: update t1 set c = 30
+ CONTEXT: line 4 at SQL statement
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "r" has no field "c"
+ CONTEXT: line 6 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ LINE 1: SELECT a + b
+ ^
+ DETAIL: column "a" does not exist
+ QUERY: SELECT a + b
+ CONTEXT: line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: too many parameters specified for RAISE
+ CONTEXT: line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: too few parameters specified for RAISE
+ CONTEXT: line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ LINE 1: SELECT c+10
+ ^
+ DETAIL: column "c" does not exist
+ QUERY: SELECT c+10
+ CONTEXT: line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: subscripted object is not an array
+ CONTEXT: line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "_exception" has no field "hint"
+ CONTEXT: line 7 at GET DIAGNOSTICS
+ drop function f1();
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ WARNING: error in function "f1_trg()"
+ DETAIL: record "new" has no field "c"
+ CONTEXT: SQL statement "SELECT new.c"
+ line 5 at RAISE
+ insert into t1 values(6,30);
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- should to fail
+ check trigger t1_f1 on t1;
+ WARNING: error in function "f1_trg()"
+ DETAIL: record "new" has no field "c"
+ CONTEXT: line 5 at assignment
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+ ERROR: record "new" has no field "c"
+ CONTEXT: PL/pgSQL function "f1_trg" line 5 at assignment
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- ok
+ check trigger t1_f1 on t1;
+ NOTICE: checked function "f1_trg()"
+ -- ok
+ insert into t1 values(6,30);
+ drop table t1;
+ drop type _exception_type;
+ drop function f1_trg();
*** ./src/test/regress/sql/plpgsql.sql.orig 2011-12-07 05:59:37.166674218 +0100
--- ./src/test/regress/sql/plpgsql.sql 2011-12-07 06:00:07.371331565 +0100
***************
*** 366,371 ****
--- 366,373 ----
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
-- ************************************************************
-- * BEFORE DELETE on HSlot
***************
*** 747,752 ****
--- 749,757 ----
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
+
-- ************************************************************
-- * Support function to clear out the backlink field if
***************
*** 2335,2340 ****
--- 2340,2352 ----
select for_vect();
+ -- check function should not fail
+ check function for_vect();
+
+ -- recheck after check function
+ select for_vect();
+
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 2714,2719 ****
--- 2726,2734 ----
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
+
select forc01();
-- try updating the cursor's current row
***************
*** 3048,3053 ****
--- 3063,3071 ----
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
+
select case_test(1);
select case_test(2);
select case_test(3);
***************
*** 3600,3602 ****
--- 3618,3862 ----
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+
+ --
+ -- check function statement tests
+ --
+
+ create table t1(a int, b int);
+
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+
+ drop function f1();
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- should to fail
+ check trigger t1_f1 on t1;
+
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- ok
+ check trigger t1_f1 on t1;
+
+ -- ok
+ insert into t1 values(6,30);
+
+ drop table t1;
+ drop type _exception_type;
+
+ drop function f1_trg();
+
Pavel Stehule wrote:
there is a updated patch.
it support multi check, options and custom check functions are not
supported yet. I don't plan to implement custom check functions in
this round - I has not any example of usage - but we have agreement on
syntax and behave, so this should not be problem. I changed reporting
- from exception to warnings.
The patch applies and builds cleanly.
The syntax error messages are still inadequate; all I can get is
'syntax error at or near "%s"'. They should be more detailed.
Many other messages and code comments are in bad English.
It might be a good idea to add some regression tests for the
CHECK FUNCTION ALL variants.
Functionality:
--------------
I noticed an oddity:
postgres=# CHECK FUNCTION ALL;
ERROR: syntax error at or near ";"
LINE 1: CHECK FUNCTION ALL;
^
postgres=# CHECK FUNCTION ALL IN LANGUAGE plpgsql;
NOTICE: nothing to check
postgres=# CHECK FUNCTION ALL IN SCHEMA pg_catalog;
[prints lots of NOTICEs]
According to the syntax diagram and my intuition CHECK FUNCTION ALL
without additional clauses should work.
Regarding the syntax: I know I suggested it myself, but after several
times of typing "IN LANGUAGE plpgsql" I think that omitting the "IN"
would be better and more like other commands (e.g. CREATE FUNCTION).
It is a pity that the CHECK FUNCTION ALL variants will not check
trigger functions, but I understand the difficulty -- it would
require checking all trigger functions on all tables where they
occur in a trigger.
I think that the checker function should be shown in psql's
\dL+ output.
Barring these little gripes, the functionality seems "ready for
committer" from my point of view.
Code review:
------------
I do not feel competent for a thorough code review.
Documentation:
--------------
This is where I see the greatest shortcomings.
- The documentation for the system catalog pg_pltemplate should be
extended to include tmplchecker.
- The documentation patch for CREATE LANGUAGE is plain wrong and
contains a syntax error.
- CHECK FUNCTION and CHECK TRIGGER should be treated as different
SQL statements. It is misleading to have CHECK TRIGGER listed
under CHECK FUNCTION. If they have to be together, the statement
should be called "CHECK" and not "CHECK TRIGGER", but I think
they should be separate.
- There is still no documentation patch for plhandler.sgml.
I think that at least the documentation should be improved before
I am ready to set this as "ready for committer".
Yours,
Laurenz Albe
2011/12/7 Albe Laurenz <laurenz.albe@wien.gv.at>:
Pavel Stehule wrote:
there is a updated patch.
it support multi check, options and custom check functions are not
supported yet. I don't plan to implement custom check functions in
this round - I has not any example of usage - but we have agreement on
syntax and behave, so this should not be problem. I changed reporting
- from exception to warnings.The patch applies and builds cleanly.
The syntax error messages are still inadequate; all I can get is
'syntax error at or near "%s"'. They should be more detailed.
this system is based on error messages that generates a plpgsql engine
or bison engine. I can correct only a few percent from these messages
:(
internally I didn't wrote a compiler or plpgsql checker - this is just
tool that can emit some plpgsql interpret subprocess - and when these
subprocesses raises exceptions, then takes their messages.
Many other messages and code comments are in bad English.
It might be a good idea to add some regression tests for the
CHECK FUNCTION ALL variants.Functionality:
--------------I noticed an oddity:
postgres=# CHECK FUNCTION ALL;
ERROR: syntax error at or near ";"
LINE 1: CHECK FUNCTION ALL;
^
postgres=# CHECK FUNCTION ALL IN LANGUAGE plpgsql;
NOTICE: nothing to check
postgres=# CHECK FUNCTION ALL IN SCHEMA pg_catalog;
[prints lots of NOTICEs]According to the syntax diagram and my intuition CHECK FUNCTION ALL
without additional clauses should work.
this is question - this will check all functions in postgres.It's 2421
function, so one criterium as minimum should be good idea.
We can remove buildin functions from list - so it will check all
function in database.
Regarding the syntax: I know I suggested it myself, but after several
times of typing "IN LANGUAGE plpgsql" I think that omitting the "IN"
would be better and more like other commands (e.g. CREATE FUNCTION).
IN should be syntactic sugar
It is a pity that the CHECK FUNCTION ALL variants will not check
trigger functions, but I understand the difficulty -- it would
require checking all trigger functions on all tables where they
occur in a trigger.I think that the checker function should be shown in psql's
\dL+ output.Barring these little gripes, the functionality seems "ready for
committer" from my point of view.Code review:
------------I do not feel competent for a thorough code review.
Documentation:
--------------This is where I see the greatest shortcomings.
- The documentation for the system catalog pg_pltemplate should be
extended to include tmplchecker.
- The documentation patch for CREATE LANGUAGE is plain wrong and
contains a syntax error.
- CHECK FUNCTION and CHECK TRIGGER should be treated as different
SQL statements. It is misleading to have CHECK TRIGGER listed
under CHECK FUNCTION. If they have to be together, the statement
should be called "CHECK" and not "CHECK TRIGGER", but I think
they should be separate.
- There is still no documentation patch for plhandler.sgml.I think that at least the documentation should be improved before
I am ready to set this as "ready for committer".
please, can you send a correction to documentation or error messages?
I am not able to write documentation
Regards
Pavel
Show quoted text
Yours,
Laurenz Albe
Pavel Stehule wrote:
The syntax error messages are still inadequate; all I can get is
'syntax error at or near "%s"'. They should be more detailed.this system is based on error messages that generates a plpgsql engine
or bison engine. I can correct only a few percent from these messages
:(internally I didn't wrote a compiler or plpgsql checker - this is just
tool that can emit some plpgsql interpret subprocess - and when these
subprocesses raises exceptions, then takes their messages.
I see.
I think that at least the documentation should be improved before
I am ready to set this as "ready for committer".please, can you send a correction to documentation or error messages?
I am not able to write documentation
I'll give it a try.
Yours,
Laurenz Albe
Hello
updated version
changes:
* CHECK FUNCTION ALL; is enabled - in this case functions from
pg_catalog schema are ignored
I looked on parser, and I didn't other changes there - IN SCHEMA, FOR
ROLE are used more time there, so our usage will be consistent
Regards
Pavel
2011/12/7 Albe Laurenz <laurenz.albe@wien.gv.at>:
Show quoted text
Pavel Stehule wrote:
The syntax error messages are still inadequate; all I can get is
'syntax error at or near "%s"'. They should be more detailed.this system is based on error messages that generates a plpgsql engine
or bison engine. I can correct only a few percent from these messages
:(internally I didn't wrote a compiler or plpgsql checker - this is just
tool that can emit some plpgsql interpret subprocess - and when these
subprocesses raises exceptions, then takes their messages.I see.
I think that at least the documentation should be improved before
I am ready to set this as "ready for committer".please, can you send a correction to documentation or error messages?
I am not able to write documentation
I'll give it a try.
Yours,
Laurenz Albe
Attachments:
check_function-2011-12-08-1.difftext/x-patch; charset=US-ASCII; name=check_function-2011-12-08-1.diffDownload
*** ./doc/src/sgml/catalogs.sgml.orig 2011-12-07 05:59:37.123674706 +0100
--- ./doc/src/sgml/catalogs.sgml 2011-12-07 06:00:07.253332903 +0100
***************
*** 3652,3657 ****
--- 3652,3668 ----
</row>
<row>
+ <entry><structfield>lanchecker</structfield></entry>
+ <entry><type>oid</type></entry>
+ <entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
+ <entry>
+ This references a language checker function that is responsible
+ for checking a embedded SQL and can provide detailed checking.
+ Zero if no checker is provided.
+ </entry>
+ </row>
+
+ <row>
<entry><structfield>lanacl</structfield></entry>
<entry><type>aclitem[]</type></entry>
<entry></entry>
*** ./doc/src/sgml/ref/allfiles.sgml.orig 2011-12-07 05:59:37.125674684 +0100
--- ./doc/src/sgml/ref/allfiles.sgml 2011-12-07 06:00:07.254332891 +0100
***************
*** 40,45 ****
--- 40,46 ----
<!ENTITY alterView SYSTEM "alter_view.sgml">
<!ENTITY analyze SYSTEM "analyze.sgml">
<!ENTITY begin SYSTEM "begin.sgml">
+ <!ENTITY checkFunction SYSTEM "check_function.sgml">
<!ENTITY checkpoint SYSTEM "checkpoint.sgml">
<!ENTITY close SYSTEM "close.sgml">
<!ENTITY cluster SYSTEM "cluster.sgml">
*** ./doc/src/sgml/ref/create_language.sgml.orig 2011-12-07 05:59:37.127674661 +0100
--- ./doc/src/sgml/ref/create_language.sgml 2011-12-07 06:00:07.256332868 +0100
***************
*** 23,29 ****
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
--- 23,29 ----
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ] [ CHECK <replaceable>checkfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
***************
*** 217,222 ****
--- 217,236 ----
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><literal>CHECK</literal> <replaceable class="parameter">checkfunction</replaceable></term>
+
+ <listitem>
+ <para><replaceable class="parameter">checkfunction</replaceable> is the
+ name of a previously registered function that will be called
+ when a new function in the language is created, to check the
+ function by statemnt <command>CHECK FUNCTION</command> or
+ <command>CHECK TRIGGER</command>.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
*** ./doc/src/sgml/reference.sgml.orig 2011-12-07 05:59:37.128674649 +0100
--- ./doc/src/sgml/reference.sgml 2011-12-07 06:00:07.257332857 +0100
***************
*** 68,73 ****
--- 68,74 ----
&alterView;
&analyze;
&begin;
+ &checkFunction;
&checkpoint;
&close;
&cluster;
*** ./doc/src/sgml/ref/check_function.sgml.orig 2011-12-07 05:59:58.965426923 +0100
--- ./doc/src/sgml/ref/check_function.sgml 2011-12-06 17:54:28.000000000 +0100
***************
*** 0 ****
--- 1,38 ----
+ <!--
+ doc/src/sgml/ref/check_function.sgml
+ -->
+
+ <refentry id="SQL-CHECKFUNCTION">
+ <refmeta>
+ <refentrytitle>CHECK FUNCTION</refentrytitle>
+ <manvolnum>7</manvolnum>
+ <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+ <refname>CHECK FUNCTION</refname>
+ <refpurpose>ensure a deep checking of existing function</refpurpose>
+ </refnamediv>
+
+ <indexterm zone="sql-checkfunction">
+ <primary>CHECK FUNCTION</primary>
+ </indexterm>
+
+ <refsynopsisdiv>
+ <synopsis>
+ CHECK FUNCTION <replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] )
+ | CHECK FUNCTION ALL [ IN LANGUAGE <replaceable class="parameter">langname</replaceable> ] [ IN SCHEMA <replaceable class="parameter">schemaname</replaceable> ] [ FOR ROLE <replaceable class="parameter">ownername</replaceable> ]
+ | CHECK TRIGGER <replaceable class="parameter">name</replaceable> ON <replaceable class="parameter">tablename</replaceable>
+ </synopsis>
+ </refsynopsisdiv>
+
+ <refsect1 id="sql-checkfunction-description">
+ <title>Description</title>
+
+ <para>
+ <command>CHECK FUNCTION</command> check a existing function.
+ <command>CHECK TRIGGER</command> check a trigger function.
+ </para>
+ </refsect1>
+
+ </refentry>
*** ./src/backend/catalog/pg_proc.c.orig 2011-12-07 05:59:37.131674614 +0100
--- ./src/backend/catalog/pg_proc.c 2011-12-07 06:00:07.260332824 +0100
***************
*** 1101,1103 ****
--- 1101,1104 ----
*newcursorpos = newcp;
return false;
}
+
*** ./src/backend/commands/functioncmds.c.orig 2011-12-07 05:59:37.132674603 +0100
--- ./src/backend/commands/functioncmds.c 2011-12-08 14:57:35.755579256 +0100
***************
*** 35,53 ****
--- 35,58 ----
#include "access/genam.h"
#include "access/heapam.h"
#include "access/sysattr.h"
+ #include "access/xact.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_language.h"
+ #include "catalog/namespace.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_proc_fn.h"
+ #include "catalog/pg_trigger.h"
#include "catalog/pg_type.h"
#include "catalog/pg_type_fn.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
+ #include "commands/trigger.h"
+ #include "executor/spi_priv.h"
#include "miscadmin.h"
#include "optimizer/var.h"
#include "parser/parse_coerce.h"
***************
*** 60,66 ****
--- 65,73 ----
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
+ #include "utils/memutils.h"
#include "utils/rel.h"
+ #include "utils/resowner.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
***************
*** 1009,1014 ****
--- 1016,1374 ----
}
}
+ /*
+ * Search and execute related checker function
+ */
+ static void
+ CheckFunctionById(Oid funcOid, Oid relid)
+ {
+ HeapTuple tup;
+ Form_pg_proc proc;
+ HeapTuple languageTuple;
+ Form_pg_language languageStruct;
+ Oid languageChecker;
+ bool found_bug;
+ char *funcname;
+
+ tup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
+ if (!HeapTupleIsValid(tup)) /* should not happen */
+ elog(ERROR, "cache lookup failed for function %u", funcOid);
+
+ proc = (Form_pg_proc) GETSTRUCT(tup);
+
+ languageTuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(proc->prolang));
+ Assert(HeapTupleIsValid(languageTuple));
+
+ languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
+ languageChecker = languageStruct->lanchecker;
+
+ funcname = format_procedure(funcOid);
+
+ /* Check a function body */
+ if (OidIsValid(languageChecker))
+ {
+ ArrayType *set_items = NULL;
+ int save_nestlevel = 0;
+ Datum datum;
+ bool isnull;
+ MemoryContext oldCxt;
+ MemoryContext checkCxt;
+ ResourceOwner oldowner;
+
+ datum = SysCacheGetAttr(PROCOID, tup, Anum_pg_proc_proconfig, &isnull);
+
+ if (!isnull)
+ {
+ /* Set per-function configuration parameters */
+ set_items = (ArrayType *) DatumGetPointer(datum);
+ if (set_items) /* Need a new GUC nesting level */
+ {
+ save_nestlevel = NewGUCNestLevel();
+ ProcessGUCArray(set_items,
+ (superuser() ? PGC_SUSET : PGC_USERSET),
+ PGC_S_SESSION,
+ GUC_ACTION_SAVE);
+ }
+ else
+ save_nestlevel = 0; /* keep compiler quiet */
+ }
+
+ checkCxt = AllocSetContextCreate(CurrentMemoryContext,
+ "Check temporary context",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
+
+ oldCxt = MemoryContextSwitchTo(checkCxt);
+
+ oldowner = CurrentResourceOwner;
+ BeginInternalSubTransaction(NULL);
+ MemoryContextSwitchTo(checkCxt);
+
+ /*
+ * forward exception to warning
+ */
+ PG_TRY();
+ {
+ OidFunctionCall2(languageChecker, ObjectIdGetDatum(funcOid),
+ ObjectIdGetDatum(relid));
+
+ RollbackAndReleaseCurrentSubTransaction();
+ MemoryContextSwitchTo(checkCxt);
+ CurrentResourceOwner = oldowner;
+
+ found_bug = false;
+ }
+ PG_CATCH();
+ {
+ ErrorData *edata;
+
+ MemoryContextSwitchTo(checkCxt);
+ edata = CopyErrorData();
+ FlushErrorState();
+
+ RollbackAndReleaseCurrentSubTransaction();
+ MemoryContextSwitchTo(checkCxt);
+ CurrentResourceOwner = oldowner;
+
+ edata->elevel = WARNING;
+
+ ereport(WARNING,
+ (errmsg("error in function \"%s\"", funcname),
+ edata->message != NULL ? errdetail_internal("%s", edata->message) : 0,
+ edata->context != NULL ? errcontext("%s",edata->context) : 0,
+ edata->internalquery != NULL ? internalerrquery(edata->internalquery) : 0,
+ internalerrposition(edata->internalpos)));
+
+ FreeErrorData(edata);
+
+ found_bug = true;
+ }
+ PG_END_TRY();
+
+ MemoryContextSwitchTo(oldCxt);
+
+ if (set_items)
+ AtEOXact_GUC(true, save_nestlevel);
+
+ if (!found_bug)
+ elog(NOTICE, "checked function \"%s\"",
+ funcname);
+ }
+ else
+ elog(NOTICE, "skip check function \"%s\", language \"%s\" hasn't checker function",
+ funcname,
+ NameStr(languageStruct->lanname));
+
+ pfree(funcname);
+
+ ReleaseSysCache(languageTuple);
+ ReleaseSysCache(tup);
+ }
+
+ /*
+ * CheckFunction
+ * call a PL checker function when this function exists.
+ */
+ void
+ CheckFunction(CheckFunctionStmt *stmt)
+ {
+ List *functionName = stmt->funcname;
+ List *argTypes = stmt->args; /* list of TypeName nodes */
+ Oid funcOid = InvalidOid;
+ HeapTuple tup;
+ Oid trgOid = InvalidOid;
+ Oid relid = InvalidOid;
+ Oid languageId;
+ int nkeys = 0;
+ List *objects = NIL;
+
+
+ if (stmt->funcname != NULL)
+ {
+ /*
+ * Find the function,
+ */
+ funcOid = LookupFuncNameTypeNames(functionName, argTypes, false);
+ }
+ else if (stmt->trgname != NULL)
+ {
+ HeapTuple ht_trig;
+ Form_pg_trigger trigrec;
+ ScanKeyData skey[1];
+ Relation tgrel;
+ SysScanDesc tgscan;
+
+ /* find a trigger function */
+ relid = RangeVarGetRelid(stmt->relation, ShareLock, false);
+ trgOid = get_trigger_oid(relid, stmt->trgname, false);
+
+ /*
+ * Fetch the pg_trigger tuple by the Oid of the trigger
+ */
+ tgrel = heap_open(TriggerRelationId, AccessShareLock);
+
+ ScanKeyInit(&skey[0],
+ ObjectIdAttributeNumber,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(trgOid));
+
+ tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
+ SnapshotNow, 1, skey);
+
+ ht_trig = systable_getnext(tgscan);
+
+ if (!HeapTupleIsValid(ht_trig))
+ elog(ERROR, "could not find tuple for trigger %u", trgOid);
+
+ trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
+
+ /* we need to know trigger function to get PL checker function */
+ funcOid = trigrec->tgfoid;
+
+ /* Clean up */
+ systable_endscan(tgscan);
+
+ heap_close(tgrel, AccessShareLock);
+ }
+ else
+ {
+ ScanKeyData key[4];
+ Relation rel;
+ HeapScanDesc scan;
+ ListCell *option;
+ bool language_opt = false;
+ bool schema_opt = false;
+ bool owner_opt = false;
+
+ /*
+ * when Check stmt is multiple statement, then
+ * prepare list of processed functions.
+ */
+ foreach(option, stmt->options)
+ {
+ DefElem *defel = (DefElem *) lfirst(option);
+
+ if (strcmp(defel->defname, "language") == 0)
+ {
+ HeapTuple languageTuple;
+ Form_pg_language languageStruct;
+ Oid languageChecker;
+ char *language = defGetString(defel);
+
+ if (language_opt)
+ elog(ERROR, "multiple usage of IN LANGUAGE option");
+ language_opt = true;
+
+ languageTuple = SearchSysCache1(LANGNAME, PointerGetDatum(language));
+ if (!HeapTupleIsValid(languageTuple))
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_OBJECT),
+ errmsg("language \"%s\" does not exist", language),
+ (PLTemplateExists(language) ?
+ errhint("Use CREATE LANGUAGE to load the language into the database.") : 0)));
+
+ languageId = HeapTupleGetOid(languageTuple);
+ if (languageId == INTERNALlanguageId || languageId == ClanguageId)
+ elog(ERROR, "cannot to check functions in C or internal languages");
+
+ languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
+ languageChecker = languageStruct->lanchecker;
+ if (!OidIsValid(languageChecker))
+ elog(ERROR, "language \"%s\" has no defined checker function",
+ language);
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_prolang,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(languageId));
+
+ ReleaseSysCache(languageTuple);
+ }
+ else if (strcmp(defel->defname, "schema") == 0)
+ {
+ Oid namespaceId = LookupExplicitNamespace(defGetString(defel));
+
+ if (schema_opt)
+ elog(ERROR, "multiple usage of IN SCHEMA option");
+ schema_opt = true;
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_pronamespace,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(namespaceId));
+
+ }
+ else if (strcmp(defel->defname, "owner") == 0)
+ {
+ Oid ownerId = get_role_oid(defGetString(defel), false);
+
+ if (owner_opt)
+ elog(ERROR, "multiple usage of FOR ROLE option");
+ owner_opt = true;
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_proowner,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(ownerId));
+ }
+ else
+ elog(ERROR, "option \"%s\" not recognized",
+ defel->defname);
+ }
+
+ /*
+ * We don't would to iterate over pg_catalog functions without
+ * explicit request.
+ */
+ if (!schema_opt)
+ {
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_pronamespace,
+ BTEqualStrategyNumber, F_OIDNE,
+ ObjectIdGetDatum(PG_CATALOG_NAMESPACE));
+ }
+
+ rel = heap_open(ProcedureRelationId,AccessShareLock);
+ scan = heap_beginscan(rel, SnapshotNow, nkeys, key);
+
+ while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
+ {
+ Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(tup);
+
+ funcOid = HeapTupleGetOid(tup);
+
+ /* when language is not specified, then check checker */
+ if (!language_opt)
+ {
+ Oid prolang = proc->prolang;
+
+ /* skip C or internal */
+ if (prolang == INTERNALlanguageId || prolang == ClanguageId)
+ {
+ elog(NOTICE, "skip check function \"%s\", it use C or internal language",
+ format_procedure(funcOid));
+ continue;
+ }
+ }
+
+ if (proc->prorettype == TRIGGEROID)
+ {
+ elog(NOTICE, "skip check function \"%s\", it is trigger function",
+ format_procedure(funcOid));
+ continue;
+ }
+
+ objects = lappend_oid(objects, funcOid);
+ }
+
+ heap_endscan(scan);
+ heap_close(rel, AccessShareLock);
+ }
+
+ if (funcOid == InvalidOid && objects == NIL)
+ {
+ elog(NOTICE, "nothing to check");
+ return;
+ }
+
+ if (objects != NIL)
+ {
+ ListCell *object;
+
+ foreach(object, objects)
+ {
+ CHECK_FOR_INTERRUPTS();
+
+ funcOid = lfirst_oid(object);
+ CheckFunctionById(funcOid, InvalidOid);
+ }
+ }
+ else
+ {
+ CheckFunctionById(funcOid, relid);
+ }
+ }
/*
* Rename function
*** ./src/backend/commands/proclang.c.orig 2011-12-07 05:59:37.134674581 +0100
--- ./src/backend/commands/proclang.c 2011-12-07 06:00:07.264332779 +0100
***************
*** 46,57 ****
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
--- 46,58 ----
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
+ char *tmplchecker; /* name of checker function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
***************
*** 67,75 ****
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid;
Oid funcrettype;
! Oid funcargtypes[1];
/*
* If we have template information for the language, ignore the supplied
--- 68,77 ----
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid,
! checkerOid;
Oid funcrettype;
! Oid funcargtypes[2];
/*
* If we have template information for the language, ignore the supplied
***************
*** 219,228 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, pltemplate->tmpltrusted);
}
else
{
--- 221,269 ----
else
valOid = InvalidOid;
+ /*
+ * Likewise for the checker, if required; but we don't care about
+ * its return type.
+ */
+ if (pltemplate->tmplchecker)
+ {
+ funcname = SystemFuncName(pltemplate->tmplchecker);
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(funcname, 2, funcargtypes, true);
+ if (!OidIsValid(checkerOid))
+ {
+ checkerOid = ProcedureCreate(pltemplate->tmplchecker,
+ PG_CATALOG_NAMESPACE,
+ false, /* replace */
+ false, /* returnsSet */
+ VOIDOID,
+ ClanguageId,
+ F_FMGR_C_VALIDATOR,
+ pltemplate->tmplchecker,
+ pltemplate->tmpllibrary,
+ false, /* isAgg */
+ false, /* isWindowFunc */
+ false, /* security_definer */
+ true, /* isStrict */
+ PROVOLATILE_VOLATILE,
+ buildoidvector(funcargtypes, 2),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ NIL,
+ PointerGetDatum(NULL),
+ 1,
+ 0);
+ }
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, pltemplate->tmpltrusted);
}
else
{
***************
*** 294,303 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, stmt->pltrusted);
}
}
--- 335,355 ----
else
valOid = InvalidOid;
+ /* validate the checker function */
+ if (stmt->plchecker)
+ {
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(stmt->plchecker, 2, funcargtypes, false);
+ /* return value is ignored, so we don't check the type */
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, stmt->pltrusted);
}
}
***************
*** 307,313 ****
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
--- 359,365 ----
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
***************
*** 337,342 ****
--- 389,395 ----
values[Anum_pg_language_lanplcallfoid - 1] = ObjectIdGetDatum(handlerOid);
values[Anum_pg_language_laninline - 1] = ObjectIdGetDatum(inlineOid);
values[Anum_pg_language_lanvalidator - 1] = ObjectIdGetDatum(valOid);
+ values[Anum_pg_language_lanchecker - 1] = ObjectIdGetDatum(checkerOid);
nulls[Anum_pg_language_lanacl - 1] = true;
/* Check for pre-existing definition */
***************
*** 423,428 ****
--- 476,490 ----
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
+ /* dependency on the checker function, if any */
+ if (OidIsValid(checkerOid))
+ {
+ referenced.classId = ProcedureRelationId;
+ referenced.objectId = checkerOid;
+ referenced.objectSubId = 0;
+ recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
+ }
+
/* Post creation hook for new procedural language */
InvokeObjectAccessHook(OAT_POST_CREATE,
LanguageRelationId, myself.objectId, 0);
***************
*** 478,483 ****
--- 540,550 ----
if (!isnull)
result->tmplvalidator = TextDatumGetCString(datum);
+ datum = heap_getattr(tup, Anum_pg_pltemplate_tmplchecker,
+ RelationGetDescr(rel), &isnull);
+ if (!isnull)
+ result->tmplchecker = TextDatumGetCString(datum);
+
datum = heap_getattr(tup, Anum_pg_pltemplate_tmpllibrary,
RelationGetDescr(rel), &isnull);
if (!isnull)
*** ./src/backend/nodes/copyfuncs.c.orig 2011-12-07 05:59:37.135674570 +0100
--- ./src/backend/nodes/copyfuncs.c 2011-12-07 06:38:24.420227380 +0100
***************
*** 2880,2885 ****
--- 2880,2900 ----
return newnode;
}
+ static CheckFunctionStmt *
+ _copyCheckFunctionStmt(CheckFunctionStmt *from)
+ {
+ CheckFunctionStmt *newnode = makeNode(CheckFunctionStmt);
+
+ COPY_NODE_FIELD(funcname);
+ COPY_NODE_FIELD(args);
+ COPY_STRING_FIELD(trgname);
+ COPY_NODE_FIELD(relation);
+ COPY_SCALAR_FIELD(is_function);
+ COPY_NODE_FIELD(options);
+
+ return newnode;
+ }
+
static DoStmt *
_copyDoStmt(DoStmt *from)
{
***************
*** 4165,4170 ****
--- 4180,4188 ----
case T_AlterFunctionStmt:
retval = _copyAlterFunctionStmt(from);
break;
+ case T_CheckFunctionStmt:
+ retval = _copyCheckFunctionStmt(from);
+ break;
case T_DoStmt:
retval = _copyDoStmt(from);
break;
*** ./src/backend/nodes/equalfuncs.c.orig 2011-12-07 05:59:37.137674548 +0100
--- ./src/backend/nodes/equalfuncs.c 2011-12-07 06:38:56.187881989 +0100
***************
*** 1292,1297 ****
--- 1292,1310 ----
}
static bool
+ _equalCheckFunctionStmt(CheckFunctionStmt *a, CheckFunctionStmt *b)
+ {
+ COMPARE_NODE_FIELD(funcname);
+ COMPARE_NODE_FIELD(args);
+ COMPARE_STRING_FIELD(trgname);
+ COMPARE_NODE_FIELD(relation);
+ COMPARE_SCALAR_FIELD(is_function);
+ COMPARE_NODE_FIELD(options);
+
+ return true;
+ }
+
+ static bool
_equalDoStmt(DoStmt *a, DoStmt *b)
{
COMPARE_NODE_FIELD(args);
***************
*** 2708,2713 ****
--- 2721,2729 ----
case T_AlterFunctionStmt:
retval = _equalAlterFunctionStmt(a, b);
break;
+ case T_CheckFunctionStmt:
+ retval = _equalCheckFunctionStmt(a, b);
+ break;
case T_DoStmt:
retval = _equalDoStmt(a, b);
break;
*** ./src/backend/parser/gram.y.orig 2011-12-07 05:59:37.138674536 +0100
--- ./src/backend/parser/gram.y 2011-12-08 14:48:01.858729545 +0100
***************
*** 227,232 ****
--- 227,233 ----
DeallocateStmt PrepareStmt ExecuteStmt
DropOwnedStmt ReassignOwnedStmt
AlterTSConfigurationStmt AlterTSDictionaryStmt
+ CheckFunctionStmt
%type <node> select_no_parens select_with_parens select_clause
simple_select values_clause
***************
*** 276,282 ****
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate
%type <range> qualified_name OptConstrFromTable
--- 277,283 ----
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate opt_checker
%type <range> qualified_name OptConstrFromTable
***************
*** 463,469 ****
%type <windef> window_definition over_clause window_specification
opt_frame_clause frame_extent frame_bound
%type <str> opt_existing_window_name
!
/*
* Non-keyword token types. These are hard-wired into the "flex" lexer.
--- 464,471 ----
%type <windef> window_definition over_clause window_specification
opt_frame_clause frame_extent frame_bound
%type <str> opt_existing_window_name
! %type <defelt> CheckFunctionOptElem
! %type <list> CheckFunctionOpts
/*
* Non-keyword token types. These are hard-wired into the "flex" lexer.
***************
*** 700,705 ****
--- 702,708 ----
| AlterUserSetStmt
| AlterUserStmt
| AnalyzeStmt
+ | CheckFunctionStmt
| CheckPointStmt
| ClosePortalStmt
| ClusterStmt
***************
*** 3174,3184 ****
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
--- 3177,3188 ----
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
+ n->plchecker = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator opt_checker
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
***************
*** 3186,3191 ****
--- 3190,3196 ----
n->plhandler = $8;
n->plinline = $9;
n->plvalidator = $10;
+ n->plchecker = $11;
n->pltrusted = $3;
$$ = (Node *)n;
}
***************
*** 3220,3225 ****
--- 3225,3235 ----
| /*EMPTY*/ { $$ = NIL; }
;
+ opt_checker:
+ CHECK handler_name { $$ = $2; }
+ | /*EMPTY*/ { $$ = NIL; }
+ ;
+
DropPLangStmt:
DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
{
***************
*** 6250,6255 ****
--- 6260,6344 ----
/*****************************************************************************
*
+ * CHECK FUNCTION funcname(args)
+ * CHECK TRIGGER triggername ON table
+ *
+ *
+ *****************************************************************************/
+
+
+ CheckFunctionStmt:
+ CHECK FUNCTION func_name func_args
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = $3;
+ n->args = extractArgTypes($4);
+ n->trgname = NULL;
+ n->relation = NULL;
+ n->is_function = true;
+ n->options = NIL;
+ $$ = (Node *) n;
+ }
+ | CHECK TRIGGER name ON qualified_name
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = $3;
+ n->relation = $5;
+ n->is_function = false;
+ n->options = NIL;
+ $$ = (Node *) n;
+ }
+ | CHECK FUNCTION ALL
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = NULL;
+ n->relation = NULL;
+ n->is_function = true;
+ n->options = NIL;
+ $$ = (Node *) n;
+ }
+ | CHECK FUNCTION ALL CheckFunctionOpts
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = NULL;
+ n->relation = NULL;
+ n->is_function = true;
+ n->options = $4;
+ $$ = (Node *) n;
+ }
+ ;
+
+ CheckFunctionOpts:
+ CheckFunctionOptElem { $$ = list_make1($1); }
+ | CheckFunctionOpts CheckFunctionOptElem { $$ = lappend($1, $2); }
+ ;
+
+ CheckFunctionOptElem:
+ IN_P LANGUAGE ColId
+ {
+ $$ = makeDefElem("language",
+ (Node *) makeString($2));
+ }
+ | IN_P SCHEMA ColId
+ {
+ $$ = makeDefElem("schema",
+ (Node *) makeString($2));
+ }
+ | FOR ROLE ColId
+ {
+ $$ = makeDefElem("owner",
+ (Node *) makeString($3));
+ }
+ ;
+
+ /*****************************************************************************
+ *
* DO <anonymous code block> [ LANGUAGE language ]
*
* We use a DefElem list for future extensibility, and to allow flexibility
*** ./src/backend/tcop/utility.c.orig 2011-12-07 05:59:37.140674512 +0100
--- ./src/backend/tcop/utility.c 2011-12-07 06:35:57.836744377 +0100
***************
*** 882,887 ****
--- 882,891 ----
AlterFunction((AlterFunctionStmt *) parsetree);
break;
+ case T_CheckFunctionStmt:
+ CheckFunction((CheckFunctionStmt *) parsetree);
+ break;
+
case T_IndexStmt: /* CREATE INDEX */
{
IndexStmt *stmt = (IndexStmt *) parsetree;
***************
*** 2125,2130 ****
--- 2129,2141 ----
}
break;
+ case T_CheckFunctionStmt:
+ if (((CheckFunctionStmt *) parsetree)->is_function)
+ tag = "CHECK FUNCTION";
+ else
+ tag = "CHECK TRIGGER";
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
***************
*** 2565,2570 ****
--- 2576,2585 ----
}
break;
+ case T_CheckFunctionStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
*** ./src/bin/pg_dump/pg_dump.c.orig 2011-12-07 05:59:37.142674489 +0100
--- ./src/bin/pg_dump/pg_dump.c 2011-12-07 06:00:07.312332234 +0100
***************
*** 5326,5338 ****
int i_lanplcallfoid;
int i_laninline;
int i_lanvalidator;
int i_lanacl;
int i_lanowner;
/* Make sure we are in proper schema */
selectSourceSchema("pg_catalog");
! if (g_fout->remoteVersion >= 90000)
{
/* pg_language has a laninline column */
appendPQExpBuffer(query, "SELECT tableoid, oid, "
--- 5326,5351 ----
int i_lanplcallfoid;
int i_laninline;
int i_lanvalidator;
+ int i_lanchecker;
int i_lanacl;
int i_lanowner;
/* Make sure we are in proper schema */
selectSourceSchema("pg_catalog");
! if (g_fout->remoteVersion >= 90200)
! {
! /* pg_language has a lanchecker column */
! appendPQExpBuffer(query, "SELECT tableoid, oid, "
! "lanname, lanpltrusted, lanplcallfoid, "
! "laninline, lanvalidator, lanchecker, lanacl, "
! "(%s lanowner) AS lanowner "
! "FROM pg_language "
! "WHERE lanispl "
! "ORDER BY oid",
! username_subquery);
! }
! else if (g_fout->remoteVersion >= 90000)
{
/* pg_language has a laninline column */
appendPQExpBuffer(query, "SELECT tableoid, oid, "
***************
*** 5409,5414 ****
--- 5422,5428 ----
/* these may fail and return -1: */
i_laninline = PQfnumber(res, "laninline");
i_lanvalidator = PQfnumber(res, "lanvalidator");
+ i_lanchecker = PQfnumber(res, "lanchecker");
i_lanacl = PQfnumber(res, "lanacl");
i_lanowner = PQfnumber(res, "lanowner");
***************
*** 5422,5427 ****
--- 5436,5445 ----
planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
+ if (i_lanchecker >= 0)
+ planginfo[i].lanchecker = atooid(PQgetvalue(res, i, i_lanchecker));
+ else
+ planginfo[i].lanchecker = InvalidOid;
if (i_laninline >= 0)
planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
else
***************
*** 8597,8602 ****
--- 8615,8621 ----
char *qlanname;
char *lanschema;
FuncInfo *funcInfo;
+ FuncInfo *checkerInfo = NULL;
FuncInfo *inlineInfo = NULL;
FuncInfo *validatorInfo = NULL;
***************
*** 8616,8621 ****
--- 8635,8647 ----
if (funcInfo != NULL && !funcInfo->dobj.dump)
funcInfo = NULL; /* treat not-dumped same as not-found */
+ if (OidIsValid(plang->lanchecker))
+ {
+ checkerInfo = findFuncByOid(plang->lanchecker);
+ if (checkerInfo != NULL && !checkerInfo->dobj.dump)
+ checkerInfo = NULL;
+ }
+
if (OidIsValid(plang->laninline))
{
inlineInfo = findFuncByOid(plang->laninline);
***************
*** 8642,8647 ****
--- 8668,8674 ----
* don't, this might not work terribly nicely.
*/
useParams = (funcInfo != NULL &&
+ (checkerInfo != NULL || !OidIsValid(plang->lanchecker)) &&
(inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
(validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
***************
*** 8697,8702 ****
--- 8724,8739 ----
appendPQExpBuffer(defqry, "%s",
fmtId(validatorInfo->dobj.name));
}
+ if (OidIsValid(plang->lanchecker))
+ {
+ appendPQExpBuffer(defqry, " CHECK ");
+ /* Cope with possibility that checker is in different schema */
+ if (checkerInfo->dobj.namespace != funcInfo->dobj.namespace)
+ appendPQExpBuffer(defqry, "%s.",
+ fmtId(checkerInfo->dobj.namespace->dobj.name));
+ appendPQExpBuffer(defqry, "%s",
+ fmtId(checkerInfo->dobj.name));
+ }
}
else
{
*** ./src/bin/pg_dump/pg_dump.h.orig 2011-12-07 05:59:37.144674467 +0100
--- ./src/bin/pg_dump/pg_dump.h 2011-12-07 06:00:07.348331825 +0100
***************
*** 387,392 ****
--- 387,393 ----
Oid lanplcallfoid;
Oid laninline;
Oid lanvalidator;
+ Oid lanchecker;
char *lanacl;
char *lanowner; /* name of owner, or empty string */
} ProcLangInfo;
*** ./src/bin/psql/tab-complete.c.orig 2011-12-07 05:59:37.146674445 +0100
--- ./src/bin/psql/tab-complete.c 2011-12-07 06:00:07.350331803 +0100
***************
*** 1,4 ****
--- 1,5 ----
/*
+ *
* psql - the PostgreSQL interactive terminal
*
* Copyright (c) 2000-2011, PostgreSQL Global Development Group
***************
*** 727,733 ****
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
--- 728,734 ----
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECK", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
***************
*** 1524,1529 ****
--- 1525,1552 ----
COMPLETE_WITH_LIST(list_TRANS);
}
+
+ /* CHECK */
+ else if (pg_strcasecmp(prev_wd, "CHECK") == 0)
+ {
+ static const char *const list_CHECK[] =
+ {"FUNCTION", "TRIGGER", NULL};
+
+ COMPLETE_WITH_LIST(list_CHECK);
+ }
+ else if (pg_strcasecmp(prev3_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
+ {
+ COMPLETE_WITH_CONST("ON");
+ }
+ else if (pg_strcasecmp(prev4_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
+ pg_strcasecmp(prev_wd, "ON") == 0)
+ {
+ completion_info_charp = prev2_wd;
+ COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger);
+ }
+
/* CLUSTER */
/*
*** ./src/include/catalog/pg_language.h.orig 2011-12-07 05:59:37.147674434 +0100
--- ./src/include/catalog/pg_language.h 2011-12-07 06:00:07.351331792 +0100
***************
*** 37,42 ****
--- 37,43 ----
Oid lanplcallfoid; /* Call handler for PL */
Oid laninline; /* Optional anonymous-block handler function */
Oid lanvalidator; /* Optional validation function */
+ Oid lanchecker; /* Optional checker function */
aclitem lanacl[1]; /* Access privileges */
} FormData_pg_language;
***************
*** 51,57 ****
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 8
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
--- 52,58 ----
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 9
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
***************
*** 59,78 ****
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanacl 8
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
--- 60,80 ----
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanchecker 8
! #define Anum_pg_language_lanacl 9
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 0 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 0 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 0 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
*** ./src/include/catalog/pg_pltemplate.h.orig 2011-12-07 05:59:37.149674412 +0100
--- ./src/include/catalog/pg_pltemplate.h 2011-12-07 06:00:07.352331780 +0100
***************
*** 36,41 ****
--- 36,42 ----
text tmplhandler; /* name of call handler function */
text tmplinline; /* name of anonymous-block handler, or NULL */
text tmplvalidator; /* name of validator function, or NULL */
+ text tmplchecker; /* name of checker function, or NULL */
text tmpllibrary; /* path of shared library */
aclitem tmplacl[1]; /* access privileges for template */
} FormData_pg_pltemplate;
***************
*** 51,65 ****
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 8
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmpllibrary 7
! #define Anum_pg_pltemplate_tmplacl 8
/* ----------------
--- 52,67 ----
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 9
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmplchecker 7
! #define Anum_pg_pltemplate_tmpllibrary 8
! #define Anum_pg_pltemplate_tmplacl 9
/* ----------------
***************
*** 67,79 ****
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
--- 69,81 ----
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "plpgsql_checker" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" _null_ "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
*** ./src/include/commands/defrem.h.orig 2011-12-07 05:59:37.150674400 +0100
--- ./src/include/commands/defrem.h 2011-12-07 06:00:07.352331780 +0100
***************
*** 62,67 ****
--- 62,68 ----
/* commands/functioncmds.c */
extern void CreateFunction(CreateFunctionStmt *stmt, const char *queryString);
extern void RemoveFunctionById(Oid funcOid);
+ extern void CheckFunction(CheckFunctionStmt *stmt);
extern void SetFunctionReturnType(Oid funcOid, Oid newRetType);
extern void SetFunctionArgType(Oid funcOid, int argIndex, Oid newArgType);
extern void RenameFunction(List *name, List *argtypes, const char *newname);
*** ./src/include/nodes/nodes.h.orig 2011-12-07 05:59:37.151674388 +0100
--- ./src/include/nodes/nodes.h 2011-12-07 06:00:07.353331768 +0100
***************
*** 291,296 ****
--- 291,297 ----
T_IndexStmt,
T_CreateFunctionStmt,
T_AlterFunctionStmt,
+ T_CheckFunctionStmt,
T_DoStmt,
T_RenameStmt,
T_RuleStmt,
*** ./src/include/nodes/parsenodes.h.orig 2011-12-07 05:59:37.153674364 +0100
--- ./src/include/nodes/parsenodes.h 2011-12-07 06:16:44.502121440 +0100
***************
*** 1734,1739 ****
--- 1734,1740 ----
List *plhandler; /* PL call handler function (qual. name) */
List *plinline; /* optional inline function (qual. name) */
List *plvalidator; /* optional validator function (qual. name) */
+ List *plchecker; /* optional checker function (qual. name) */
bool pltrusted; /* PL is trusted */
} CreatePLangStmt;
***************
*** 2077,2082 ****
--- 2078,2098 ----
} AlterFunctionStmt;
/* ----------------------
+ * Check {Function|Trigger} Statement
+ * ----------------------
+ */
+ typedef struct CheckFunctionStmt
+ {
+ NodeTag type;
+ List *funcname; /* qualified name of checked object */
+ List *args; /* types of the arguments */
+ char *trgname; /* trigger's name */
+ RangeVar *relation; /* trigger's relation */
+ bool is_function; /* true for CHECK FUNCTION statement */
+ List *options; /* other DefElem options */
+ } CheckFunctionStmt;
+
+ /* ----------------------
* DO Statement
*
* DoStmt is the raw parser output, InlineCodeBlock is the execution-time API
*** ./src/pl/plpgsql/src/pl_comp.c.orig 2011-12-07 05:59:37.155674342 +0100
--- ./src/pl/plpgsql/src/pl_comp.c 2011-12-07 06:00:07.360331689 +0100
***************
*** 115,121 ****
static void plpgsql_HashTableInsert(PLpgSQL_function *function,
PLpgSQL_func_hashkey *func_key);
static void plpgsql_HashTableDelete(PLpgSQL_function *function);
- static void delete_function(PLpgSQL_function *func);
/* ----------
* plpgsql_compile Make an execution tree for a PL/pgSQL function.
--- 115,120 ----
***************
*** 175,181 ****
* Nope, so remove it from hashtable and try to drop associated
* storage (if not done already).
*/
! delete_function(function);
/*
* If the function isn't in active use then we can overwrite the
--- 174,180 ----
* Nope, so remove it from hashtable and try to drop associated
* storage (if not done already).
*/
! plpgsql_delete_function(function);
/*
* If the function isn't in active use then we can overwrite the
***************
*** 2426,2432 ****
}
/*
! * delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
--- 2425,2431 ----
}
/*
! * plpgsql_delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
***************
*** 2439,2446 ****
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
! static void
! delete_function(PLpgSQL_function *func)
{
/* remove function from hash table (might be done already) */
plpgsql_HashTableDelete(func);
--- 2438,2445 ----
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
! void
! plpgsql_delete_function(PLpgSQL_function *func)
{
/* remove function from hash table (might be done already) */
plpgsql_HashTableDelete(func);
*** ./src/pl/plpgsql/src/pl_exec.c.orig 2011-12-07 05:59:37.157674320 +0100
--- ./src/pl/plpgsql/src/pl_exec.c 2011-12-07 07:36:23.112366630 +0100
***************
*** 210,216 ****
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
PLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
!
/* ----------
* plpgsql_exec_function Called by the call handler for
--- 210,228 ----
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
PLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
! static void check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec);
! static void check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr);
! static void assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
! PLpgSQL_row *row, PLpgSQL_rec *rec,
! TupleDesc tupdesc);
! static TupleDesc expr_get_desc(PLpgSQL_execstate *estate,
! PLpgSQL_expr *query,
! bool use_element_type,
! bool expand_record,
! bool is_expression);
! static void var_init_to_null(PLpgSQL_execstate *estate, int varno);
! static void check_stmts(PLpgSQL_execstate *estate, List *stmts);
! static void check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
/* ----------
* plpgsql_exec_function Called by the call handler for
***************
*** 6176,6178 ****
--- 6188,7240 ----
return portal;
}
+
+ /*
+ * Following code ensures a CHECK FUNCTION and CHECK TRIGGER statements for PL/pgSQL
+ *
+ */
+
+ /*
+ * append a CONTEXT to error message
+ */
+ static void
+ check_error_callback(void *arg)
+ {
+ PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;
+
+ if (estate->err_stmt != NULL)
+ {
+ /* translator: last %s is a plpgsql statement type name */
+ errcontext("line %d at %s",
+ estate->err_stmt->lineno,
+ plpgsql_stmt_typename(estate->err_stmt));
+ }
+ }
+
+ /*
+ * Check function - it prepare variables and starts a prepare plan walker
+ * called by function checker
+ */
+ void
+ plpgsql_check_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
+
+ /*
+ * Store the actual call argument values into the appropriate variables
+ */
+ for (i = 0; i < func->fn_nargs; i++)
+ {
+ int n = func->fn_argvarnos[i];
+
+ switch (estate.datums[n]->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ {
+ var_init_to_null(&estate, n);
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ {
+ PLpgSQL_row *row = (PLpgSQL_row *) estate.datums[n];
+
+ exec_move_row(&estate, NULL, row, NULL, NULL);
+ }
+ break;
+
+ default:
+ elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
+ }
+ }
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Check trigger - prepare fake environments for testing trigger
+ *
+ */
+ void
+ plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ PLpgSQL_rec *rec_new,
+ *rec_old;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, NULL);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
+
+ /*
+ * Put the OLD and NEW tuples into record variables
+ *
+ * We make the tupdescs available in both records even though only one may
+ * have a value. This allows parsing of record references to succeed in
+ * functions that are used for multiple trigger types. For example, we
+ * might have a test like "if (TG_OP = 'INSERT' and NEW.foo = 'xyz')",
+ * which should parse regardless of the current trigger type.
+ */
+ rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
+ rec_new->freetup = false;
+ rec_new->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_new, trigdata->tg_relation->rd_att);
+
+ rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
+ rec_old->freetup = false;
+ rec_old->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_old, trigdata->tg_relation->rd_att);
+
+ /*
+ * Assign the special tg_ variables
+ */
+ var_init_to_null(&estate, func->tg_op_varno);
+ var_init_to_null(&estate, func->tg_name_varno);
+ var_init_to_null(&estate, func->tg_when_varno);
+ var_init_to_null(&estate, func->tg_level_varno);
+ var_init_to_null(&estate, func->tg_relid_varno);
+ var_init_to_null(&estate, func->tg_relname_varno);
+ var_init_to_null(&estate, func->tg_table_name_varno);
+ var_init_to_null(&estate, func->tg_table_schema_varno);
+ var_init_to_null(&estate, func->tg_nargs_varno);
+ var_init_to_null(&estate, func->tg_argv_varno);
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Verify lvalue
+ * It doesn't repeat a checks that are done.
+ * Checks a subscript expressions, verify a validity of record's fields
+ */
+ static void
+ check_target(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ switch (target->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ case PLPGSQL_DTYPE_REC:
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ check_row_or_rec(estate, (PLpgSQL_row *) target, NULL);
+ break;
+
+ case PLPGSQL_DTYPE_RECFIELD:
+ {
+ PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
+ PLpgSQL_rec *rec;
+ int fno;
+
+ rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
+
+ /*
+ * Check that there is already a tuple in the record. We need
+ * that because records don't have any predefined field
+ * structure.
+ */
+ if (!HeapTupleIsValid(rec->tup))
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("record \"%s\" is not assigned to tuple structure",
+ rec->refname)));
+
+ /*
+ * Get the number of the records field to change and the
+ * number of attributes in the tuple. Note: disallow system
+ * column names because the code below won't cope.
+ */
+ fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
+ if (fno <= 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("record \"%s\" has no field \"%s\"",
+ rec->refname, recfield->fieldname)));
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ARRAYELEM:
+ {
+ /*
+ * Target is an element of an array
+ */
+ int nsubscripts;
+ Oid arrayelemtypeid;
+ Oid arraytypeid;
+
+ /*
+ * To handle constructs like x[1][2] := something, we have to
+ * be prepared to deal with a chain of arrayelem datums. Chase
+ * back to find the base array datum, and save the subscript
+ * expressions as we go. (We are scanning right to left here,
+ * but want to evaluate the subscripts left-to-right to
+ * minimize surprises.)
+ */
+ nsubscripts = 0;
+ do
+ {
+ PLpgSQL_arrayelem *arrayelem = (PLpgSQL_arrayelem *) target;
+
+ if (nsubscripts++ >= MAXDIM)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
+ nsubscripts + 1, MAXDIM)));
+
+ check_expr(estate, arrayelem->subscript);
+
+ target = estate->datums[arrayelem->arrayparentno];
+ } while (target->dtype == PLPGSQL_DTYPE_ARRAYELEM);
+
+ /* If target is domain over array, reduce to base type */
+ arraytypeid = exec_get_datum_type(estate, target);
+ arraytypeid = getBaseType(arraytypeid);
+
+ arrayelemtypeid = get_element_type(arraytypeid);
+
+ if (!OidIsValid(arrayelemtypeid))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("subscripted object is not an array")));
+ }
+ break;
+ }
+ }
+
+ /*
+ * Check composed lvalue
+ * There is nothing to check on rec variables
+ */
+ static void
+ check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec)
+ {
+ int fnum;
+
+ /* there are nothing to check on rec now */
+ if (row != NULL)
+ {
+ for (fnum = 0; fnum < row->nfields; fnum++)
+ {
+ /* skip dropped columns */
+ if (row->varnos[fnum] < 0)
+ continue;
+
+ check_target(estate, row->varnos[fnum]);
+ }
+ }
+ }
+
+ /*
+ * Generate a prepared plan - this is simplyfied copy from pl_exec.c
+ * Is not necessary to check simple plan
+ */
+ static void
+ prepare_expr(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *expr, int cursorOptions)
+ {
+ SPIPlanPtr plan;
+
+ /* leave when there are not expression */
+ if (expr == NULL)
+ return;
+
+ /* leave when plan is created */
+ if (expr->plan != NULL)
+ return;
+
+ /*
+ * The grammar can't conveniently set expr->func while building the parse
+ * tree, so make sure it's set before parser hooks need it.
+ */
+ expr->func = estate->func;
+
+ /*
+ * Generate and save the plan
+ */
+ plan = SPI_prepare_params(expr->query,
+ (ParserSetupHook) plpgsql_parser_setup,
+ (void *) expr,
+ cursorOptions);
+ if (plan == NULL)
+ {
+ /* Some SPI errors deserve specific error messages */
+ switch (SPI_result)
+ {
+ case SPI_ERROR_COPY:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot COPY to/from client in PL/pgSQL")));
+ case SPI_ERROR_TRANSACTION:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot begin/end transactions in PL/pgSQL"),
+ errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
+ default:
+ elog(ERROR, "SPI_prepare_params failed for \"%s\": %s",
+ expr->query, SPI_result_code_string(SPI_result));
+ }
+ }
+
+ expr->plan = SPI_saveplan(plan);
+ SPI_freeplan(plan);
+ }
+
+ /*
+ * Verify a expression
+ */
+ static void
+ check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
+ {
+ TupleDesc tupdesc;
+
+ if (expr != NULL)
+ {
+ prepare_expr(estate, expr, 0);
+ tupdesc = expr_get_desc(estate, expr, false, false, true);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+
+ /*
+ * We have to assign TupleDesc to all used record variables step by step.
+ * We would to use a exec routines for query preprocessing, so we must
+ * to create a typed NULL value, and this value is assigned to record
+ * variable.
+ */
+ static void
+ assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
+ PLpgSQL_row *row, PLpgSQL_rec *rec,
+ TupleDesc tupdesc)
+ {
+ bool *nulls;
+ HeapTuple tup;
+
+ if (tupdesc == NULL)
+ elog(ERROR, "tuple descriptor is empty");
+
+ /*
+ * row variable has assigned TupleDesc already, so don't be processed
+ * here
+ */
+ if (rec != NULL)
+ {
+ PLpgSQL_rec *target = (PLpgSQL_rec *)(estate->datums[rec->dno]);
+
+ if (target->freetup)
+ heap_freetuple(target->tup);
+
+ if (rec->freetupdesc)
+ FreeTupleDesc(target->tupdesc);
+
+ /* initialize rec by NULLs */
+ nulls = (bool *) palloc(tupdesc->natts * sizeof(bool));
+ memset(nulls, true, tupdesc->natts * sizeof(bool));
+
+ target->tupdesc = CreateTupleDescCopy(tupdesc);
+ target->freetupdesc = true;
+
+ tup = heap_form_tuple(tupdesc, NULL, nulls);
+ if (HeapTupleIsValid(tup))
+ {
+ target->tup = tup;
+ target->freetup = true;
+ }
+ else
+ elog(ERROR, "cannot to build valid composite value");
+ }
+ }
+
+ /*
+ * Assign a tuple descriptor to variable specified by dno
+ */
+ static void
+ assign_tupdesc_dno(PLpgSQL_execstate *estate, int varno, TupleDesc tupdesc)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ if (target->dtype == PLPGSQL_DTYPE_REC)
+ assign_tupdesc_row_or_rec(estate, NULL, (PLpgSQL_rec *) target, tupdesc);
+ }
+
+ /*
+ * Returns a tuple descriptor based on existing plan
+ */
+ static TupleDesc
+ expr_get_desc(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *query,
+ bool use_element_type,
+ bool expand_record,
+ bool is_expression)
+ {
+ TupleDesc tupdesc = NULL;
+ CachedPlanSource *plansource = NULL;
+
+ if (query->plan != NULL)
+ {
+ SPIPlanPtr plan = query->plan;
+
+ if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
+ elog(ERROR, "cached plan is not valid plan");
+
+ if (list_length(plan->plancache_list) != 1)
+ elog(ERROR, "plan is not single execution plan");
+
+ plansource = (CachedPlanSource *) linitial(plan->plancache_list);
+
+ tupdesc = CreateTupleDescCopy(plansource->resultDesc);
+ }
+ else
+ elog(ERROR, "there are no plan for query: \"%s\"",
+ query->query);
+
+ /*
+ * try to get a element type, when result is a array (used with FOREACH ARRAY stmt)
+ */
+ if (use_element_type)
+ {
+ Oid elemtype;
+ TupleDesc elemtupdesc;
+
+ /* result should be a array */
+ if (tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /* check the type of the expression - must be an array */
+ elemtype = get_element_type(tupdesc->attrs[0]->atttypid);
+ if (!OidIsValid(elemtype))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("FOREACH expression must yield an array, not type %s",
+ format_type_be(tupdesc->attrs[0]->atttypid))));
+
+ /* we can't know typmod now */
+ elemtupdesc = lookup_rowtype_tupdesc_noerror(elemtype, -1, true);
+ if (elemtupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(elemtupdesc);
+ ReleaseTupleDesc(elemtupdesc);
+ }
+ else
+ elog(ERROR, "cannot to identify real type for record type variable");
+ }
+
+ if (is_expression && tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /*
+ * One spacial case is when record is assigned to composite type, then
+ * we should to unpack composite type.
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 && expand_record)
+ {
+ TupleDesc unpack_tupdesc;
+
+ unpack_tupdesc = lookup_rowtype_tupdesc_noerror(tupdesc->attrs[0]->atttypid,
+ tupdesc->attrs[0]->atttypmod,
+ true);
+ if (unpack_tupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(unpack_tupdesc);
+ ReleaseTupleDesc(unpack_tupdesc);
+ }
+ }
+
+ /*
+ * There is special case, when returned tupdesc contains only
+ * unpined record: rec := func_with_out_parameters(). IN this case
+ * we must to dig more deep - we have to find oid of function and
+ * get their parameters,
+ *
+ * This is support for assign statement
+ * recvar := func_with_out_parameters(..)
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 &&
+ tupdesc->attrs[0]->atttypid == RECORDOID &&
+ tupdesc->attrs[0]->atttypmod == -1 &&
+ expand_record)
+ {
+ PlannedStmt *_stmt;
+ Plan *_plan;
+ TargetEntry *tle;
+ CachedPlan *cplan;
+
+ /*
+ * When tupdesc is related to unpined record, we will try
+ * to check plan if it is just function call and if it is
+ * then we can try to derive a tupledes from function's
+ * description.
+ */
+ cplan = GetCachedPlan(plansource, NULL, true);
+ _stmt = (PlannedStmt *) linitial(cplan->stmt_list);
+
+ if (IsA(_stmt, PlannedStmt) && _stmt->commandType == CMD_SELECT)
+ {
+ _plan = _stmt->planTree;
+ if (IsA(_plan, Result) && list_length(_plan->targetlist) == 1)
+ {
+ tle = (TargetEntry *) linitial(_plan->targetlist);
+ if (((Node *) tle->expr)->type == T_FuncExpr)
+ {
+ FuncExpr *fn = (FuncExpr *) tle->expr;
+ FmgrInfo flinfo;
+ FunctionCallInfoData fcinfo;
+ TupleDesc rd;
+ Oid rt;
+
+ fmgr_info(fn->funcid, &flinfo);
+ flinfo.fn_expr = (Node *) fn;
+ fcinfo.flinfo = &flinfo;
+
+ get_call_result_type(&fcinfo, &rt, &rd);
+ if (rd == NULL)
+ elog(ERROR, "function does not return composite type is not possible to identify composite type");
+
+ FreeTupleDesc(tupdesc);
+ BlessTupleDesc(rd);
+
+ tupdesc = rd;
+ }
+ }
+ }
+
+ ReleaseCachedPlan(cplan, true);
+ }
+
+ return tupdesc;
+ }
+
+ /*
+ * Ensure check for all statements in list
+ */
+ static void
+ check_stmts(PLpgSQL_execstate *estate, List *stmts)
+ {
+ ListCell *lc;
+
+ foreach(lc, stmts)
+ {
+ check_stmt(estate, (PLpgSQL_stmt *) lfirst(lc));
+ }
+ }
+
+ /*
+ * walk over all statements
+ */
+ static void
+ check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
+ {
+ TupleDesc tupdesc = NULL;
+ PLpgSQL_function *func;
+ ListCell *l;
+
+ if (stmt == NULL)
+ return;
+
+ estate->err_stmt = stmt;
+ func = estate->func;
+
+ switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
+ {
+ case PLPGSQL_STMT_BLOCK:
+ {
+ PLpgSQL_stmt_block *stmt_block = (PLpgSQL_stmt_block *) stmt;
+ int i;
+ PLpgSQL_datum *d;
+
+ for (i = 0; i < stmt_block->n_initvars; i++)
+ {
+ d = func->datums[stmt_block->initvarnos[i]];
+
+ if (d->dtype == PLPGSQL_DTYPE_VAR)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) d;
+
+ check_expr(estate, var->default_val);
+ }
+ }
+
+ check_stmts(estate, stmt_block->body);
+
+ if (stmt_block->exceptions)
+ {
+ foreach(l, stmt_block->exceptions->exc_list)
+ {
+ check_stmts(estate, ((PLpgSQL_exception *) lfirst(l))->action);
+ }
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_ASSIGN:
+ {
+ PLpgSQL_stmt_assign *stmt_assign = (PLpgSQL_stmt_assign *) stmt;
+
+ /* prepare plan if desn't exist yet */
+ prepare_expr(estate, stmt_assign->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_assign->expr,
+ false, /* no element type */
+ true, /* expand record */
+ true); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_target(estate, stmt_assign->varno);
+
+ /* assign a tupdesc to record variable */
+ assign_tupdesc_dno(estate, stmt_assign->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_IF:
+ {
+ PLpgSQL_stmt_if *stmt_if = (PLpgSQL_stmt_if *) stmt;
+ ListCell *l;
+
+ check_expr(estate, stmt_if->cond);
+
+ check_stmts(estate, stmt_if->then_body);
+
+ foreach(l, stmt_if->elsif_list)
+ {
+ PLpgSQL_if_elsif *elif = (PLpgSQL_if_elsif *) lfirst(l);
+
+ check_expr(estate, elif->cond);
+ check_stmts(estate, elif->stmts);
+ }
+
+ check_stmts(estate, stmt_if->else_body);
+ }
+ break;
+
+ case PLPGSQL_STMT_CASE:
+ {
+ PLpgSQL_stmt_case *stmt_case = (PLpgSQL_stmt_case *) stmt;
+ Oid result_oid;
+
+ if (stmt_case->t_expr != NULL)
+ {
+ PLpgSQL_var *t_var = (PLpgSQL_var *) estate->datums[stmt_case->t_varno];
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_case->t_expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_case->t_expr,
+ false, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ result_oid = tupdesc->attrs[0]->atttypid;
+
+ /*
+ * When expected datatype is different from real, change it. Note that
+ * what we're modifying here is an execution copy of the datum, so
+ * this doesn't affect the originally stored function parse tree.
+ */
+
+ if (t_var->datatype->typoid != result_oid)
+ t_var->datatype = plpgsql_build_datatype(result_oid,
+ -1,
+ estate->func->fn_input_collation);
+
+ ReleaseTupleDesc(tupdesc);
+ }
+
+ foreach(l, stmt_case->case_when_list)
+ {
+ PLpgSQL_case_when *cwt = (PLpgSQL_case_when *) lfirst(l);
+
+ check_expr(estate, cwt->expr);
+ check_stmts(estate, cwt->stmts);
+ }
+
+ check_stmts(estate, stmt_case->else_stmts);
+ }
+ break;
+
+ case PLPGSQL_STMT_LOOP:
+ check_stmts(estate, ((PLpgSQL_stmt_loop *) stmt)->body);
+ break;
+
+ case PLPGSQL_STMT_WHILE:
+ {
+ PLpgSQL_stmt_while *stmt_while = (PLpgSQL_stmt_while *) stmt;
+
+ check_expr(estate, stmt_while->cond);
+ check_stmts(estate, stmt_while->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORI:
+ {
+ PLpgSQL_stmt_fori *stmt_fori = (PLpgSQL_stmt_fori *) stmt;
+
+ check_expr(estate, stmt_fori->lower);
+ check_expr(estate, stmt_fori->upper);
+ check_expr(estate, stmt_fori->step);
+
+ check_stmts(estate, stmt_fori->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORS:
+ {
+ PLpgSQL_stmt_fors *stmt_fors = (PLpgSQL_stmt_fors *) stmt;
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_fors->query, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_fors->query,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_fors->row, stmt_fors->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fors->row, stmt_fors->rec, tupdesc);
+
+ check_stmts(estate, stmt_fors->body);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORC:
+ {
+ PLpgSQL_stmt_forc *stmt_forc = (PLpgSQL_stmt_forc *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_forc->curvar];
+
+ prepare_expr(estate, stmt_forc->argquery, 0);
+
+ if (var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_forc->row, stmt_forc->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_forc->row, stmt_forc->rec, tupdesc);
+ }
+
+ check_stmts(estate, stmt_forc->body);
+ if (tupdesc != NULL)
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNFORS:
+ {
+ PLpgSQL_stmt_dynfors * stmt_dynfors = (PLpgSQL_stmt_dynfors *) stmt;
+
+ if (stmt_dynfors->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_expr(estate, stmt_dynfors->query);
+
+ foreach(l, stmt_dynfors->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ check_stmts(estate, stmt_dynfors->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FOREACH_A:
+ {
+ PLpgSQL_stmt_foreach_a *stmt_foreach_a = (PLpgSQL_stmt_foreach_a *) stmt;
+
+ prepare_expr(estate, stmt_foreach_a->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_foreach_a->expr,
+ true, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ check_target(estate, stmt_foreach_a->varno);
+ assign_tupdesc_dno(estate, stmt_foreach_a->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+
+ check_stmts(estate, stmt_foreach_a->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_EXIT:
+ check_expr(estate, ((PLpgSQL_stmt_exit *) stmt)->cond);
+ break;
+
+ case PLPGSQL_STMT_PERFORM:
+ prepare_expr(estate, ((PLpgSQL_stmt_perform *) stmt)->expr, 0);
+ break;
+
+ case PLPGSQL_STMT_RETURN:
+ check_expr(estate, ((PLpgSQL_stmt_return *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_NEXT:
+ check_expr(estate, ((PLpgSQL_stmt_return_next *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_QUERY:
+ {
+ PLpgSQL_stmt_return_query *stmt_rq = (PLpgSQL_stmt_return_query *) stmt;
+
+ check_expr(estate, stmt_rq->dynquery);
+ prepare_expr(estate, stmt_rq->query, 0);
+
+ foreach(l, stmt_rq->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_RAISE:
+ {
+ PLpgSQL_stmt_raise *stmt_raise = (PLpgSQL_stmt_raise *) stmt;
+ ListCell *current_param;
+ char *cp;
+
+ foreach(l, stmt_raise->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ foreach(l, stmt_raise->options)
+ {
+ PLpgSQL_raise_option *opt = (PLpgSQL_raise_option *) lfirst(l);
+
+ check_expr(estate, opt->expr);
+ }
+
+ current_param = list_head(stmt_raise->params);
+
+ /* ensure any single % has a own parameter */
+ if (stmt_raise->message != NULL)
+ {
+ for (cp = stmt_raise->message; *cp; cp++)
+ {
+ if (cp[0] == '%')
+ {
+ if (cp[1] == '%')
+ {
+ cp++;
+ continue;
+ }
+
+ if (current_param == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too few parameters specified for RAISE")));
+
+ current_param = lnext(current_param);
+ }
+ }
+ }
+
+ if (current_param != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too many parameters specified for RAISE")));
+ }
+ break;
+
+ case PLPGSQL_STMT_EXECSQL:
+ {
+ PLpgSQL_stmt_execsql *stmt_execsql = (PLpgSQL_stmt_execsql *) stmt;
+
+ prepare_expr(estate, stmt_execsql->sqlstmt, 0);
+ if (stmt_execsql->into)
+ {
+ tupdesc = expr_get_desc(estate,
+ stmt_execsql->sqlstmt,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNEXECUTE:
+ {
+ PLpgSQL_stmt_dynexecute *stmt_dynexecute = (PLpgSQL_stmt_dynexecute *) stmt;
+
+ check_expr(estate, stmt_dynexecute->query);
+
+ foreach(l, stmt_dynexecute->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ if (stmt_dynexecute->into)
+ {
+ if (stmt_dynexecute->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_row_or_rec(estate, stmt_dynexecute->row, stmt_dynexecute->rec);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_OPEN:
+ {
+ PLpgSQL_stmt_open *stmt_open = (PLpgSQL_stmt_open *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_open->curvar];
+
+ if (var->cursor_explicit_expr)
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ prepare_expr(estate, stmt_open->query, 0);
+ prepare_expr(estate, stmt_open->argquery, 0);
+ check_expr(estate, stmt_open->dynquery);
+
+ foreach(l, stmt_open->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_GETDIAG:
+ {
+ PLpgSQL_stmt_getdiag *stmt_getdiag = (PLpgSQL_stmt_getdiag *) stmt;
+ ListCell *lc;
+
+ foreach(lc, stmt_getdiag->diag_items)
+ {
+ PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
+
+ check_target(estate, diag_item->target);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_FETCH:
+ {
+ PLpgSQL_stmt_fetch *stmt_fetch = (PLpgSQL_stmt_fetch *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *)(estate->datums[stmt_fetch->curvar]);
+
+ if (var != NULL && var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+ check_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_CLOSE:
+ break;
+
+ default:
+ elog(ERROR, "unrecognized cmd_type: %d", stmt->cmd_type);
+ return; /* be compiler quite */
+ }
+ }
+
+ /*
+ * Initialize variable to NULL
+ */
+ static void
+ var_init_to_null(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) estate->datums[varno];
+ var->value = (Datum) 0;
+ var->isnull = true;
+ var->freeval = false;
+ }
*** ./src/pl/plpgsql/src/pl_handler.c.orig 2011-12-07 05:59:37.158674309 +0100
--- ./src/pl/plpgsql/src/pl_handler.c 2011-12-07 06:00:07.365331632 +0100
***************
*** 312,314 ****
--- 312,452 ----
PG_RETURN_VOID();
}
+
+ /* ----------
+ * plpgsql_checker
+ *
+ * This function attempts to check a embeded SQL inside a PL/pgSQL function at
+ * CHECK FUNCTION time. It should to have one or two parameters. Second
+ * parameter is a relation (used when function is trigger).
+ * ----------
+ */
+ PG_FUNCTION_INFO_V1(plpgsql_checker);
+
+ Datum
+ plpgsql_checker(PG_FUNCTION_ARGS)
+ {
+ Oid funcoid = PG_GETARG_OID(0);
+ Oid relid = PG_GETARG_OID(1);
+ HeapTuple tuple;
+ FunctionCallInfoData fake_fcinfo;
+ FmgrInfo flinfo;
+ TriggerData trigdata;
+ int rc;
+ PLpgSQL_function *function;
+ PLpgSQL_execstate *cur_estate;
+
+ Form_pg_proc proc;
+ char functyptype;
+ bool istrigger = false;
+
+ /* we don't need to repair a check done by validator */
+
+ tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
+ if (!HeapTupleIsValid(tuple))
+ elog(ERROR, "cache lookup failed for function %u", funcoid);
+ proc = (Form_pg_proc) GETSTRUCT(tuple);
+
+ functyptype = get_typtype(proc->prorettype);
+
+ if (functyptype == TYPTYPE_PSEUDO)
+ {
+ /* we assume OPAQUE with no arguments means a trigger */
+ if (proc->prorettype == TRIGGEROID ||
+ (proc->prorettype == OPAQUEOID && proc->pronargs == 0))
+ {
+ istrigger = true;
+ if (!OidIsValid(relid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("PL/pgSQL trigger functions cannot be checked directly"),
+ errhint("use CHECK TRIGGER statement instead")));
+ }
+ }
+
+ /*
+ * Connect to SPI manager
+ */
+ if ((rc = SPI_connect()) != SPI_OK_CONNECT)
+ elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
+
+ /*
+ * Set up a fake fcinfo with just enough info to satisfy
+ * plpgsql_compile().
+ *
+ * there should be a different real argtypes for polymorphic params
+ */
+ MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo));
+ MemSet(&flinfo, 0, sizeof(flinfo));
+ fake_fcinfo.flinfo = &flinfo;
+ flinfo.fn_oid = funcoid;
+ flinfo.fn_mcxt = CurrentMemoryContext;
+
+ if (istrigger)
+ {
+ MemSet(&trigdata, 0, sizeof(trigdata));
+ trigdata.type = T_TriggerData;
+ trigdata.tg_relation = relation_open(relid, AccessShareLock);
+ fake_fcinfo.context = (Node *) &trigdata;
+ }
+
+ /* Get a compiled function */
+ function = plpgsql_compile(&fake_fcinfo, false);
+
+ /* Must save and restore prior value of cur_estate */
+ cur_estate = function->cur_estate;
+
+ /* Mark the function as busy, so it can't be deleted from under us */
+ function->use_count++;
+
+
+ /* Create a fake runtime environment and prepare plans */
+ PG_TRY();
+ {
+ if (!istrigger)
+ plpgsql_check_function(function, &fake_fcinfo);
+ else
+ plpgsql_check_trigger(function, &trigdata);
+ }
+ PG_CATCH();
+ {
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ /*
+ * Disconnect from SPI manager
+ */
+ if ((rc = SPI_finish()) != SPI_OK_FINISH)
+ elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
+
+ ReleaseSysCache(tuple);
+
+ PG_RETURN_VOID();
+ }
*** ./src/pl/plpgsql/src/plpgsql.h.orig 2011-12-07 05:59:37.160674287 +0100
--- ./src/pl/plpgsql/src/plpgsql.h 2011-12-07 06:00:07.366331620 +0100
***************
*** 902,907 ****
--- 902,908 ----
extern void plpgsql_adddatum(PLpgSQL_datum *new);
extern int plpgsql_add_initdatums(int **varnos);
extern void plpgsql_HashTableInit(void);
+ extern void plpgsql_delete_function(PLpgSQL_function *func);
/* ----------
* Functions in pl_handler.c
***************
*** 911,916 ****
--- 912,918 ----
extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_inline_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
+ extern Datum plpgsql_checker(PG_FUNCTION_ARGS);
/* ----------
* Functions in pl_exec.c
***************
*** 928,933 ****
--- 930,939 ----
extern void exec_get_datum_type_info(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum,
Oid *typeid, int32 *typmod, Oid *collation);
+ extern void plpgsql_check_function(PLpgSQL_function *func,
+ FunctionCallInfo fcinfo);
+ extern void plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata);
/* ----------
* Functions for namespace handling in pl_funcs.c
*** ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql.orig 2011-12-07 05:59:37.162674263 +0100
--- ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql 2011-12-07 06:00:07.366331620 +0100
***************
*** 5,7 ****
--- 5,8 ----
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_call_handler();
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_inline_handler(internal);
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_validator(oid);
+ ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_checker(oid, regclass);
*** ./src/test/regress/expected/plpgsql.out.orig 2011-12-07 05:59:37.164674240 +0100
--- ./src/test/regress/expected/plpgsql.out 2011-12-07 08:00:04.000000000 +0100
***************
*** 302,307 ****
--- 302,310 ----
' language plpgsql;
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
+ NOTICE: checked function "tg_hslot_biu()"
-- ************************************************************
-- * BEFORE DELETE on HSlot
-- * - prevent from manual manipulation
***************
*** 635,640 ****
--- 638,646 ----
raise exception ''illegal backlink beginning with %'', mytype;
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
+ NOTICE: checked function "tg_backlink_set(character,character)"
-- ************************************************************
-- * Support function to clear out the backlink field if
-- * it still points to specific slot
***************
*** 2802,2807 ****
--- 2808,2842 ----
(1 row)
+ -- check function should not fail
+ check function for_vect();
+ NOTICE: checked function "for_vect()"
+ -- recheck after check function
+ select for_vect();
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 4
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1 bb cc
+ NOTICE: 2 bb cc
+ NOTICE: 3 bb cc
+ NOTICE: 4 bb cc
+ for_vect
+ ----------
+
+ (1 row)
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 3283,3288 ****
--- 3318,3326 ----
return;
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
+ NOTICE: checked function "forc01()"
select forc01();
NOTICE: 5 from c
NOTICE: 6 from c
***************
*** 3716,3721 ****
--- 3754,3762 ----
end case;
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
+ NOTICE: checked function "case_test(bigint)"
select case_test(1);
case_test
-----------
***************
*** 4571,4573 ****
--- 4612,4956 ----
CONTEXT: PL/pgSQL function "testoa" line 5 at assignment
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+ --
+ -- check function statement tests
+ --
+ create table t1(a int, b int);
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ LINE 1: update t1 set c = 30
+ ^
+ DETAIL: column "c" of relation "t1" does not exist
+ QUERY: update t1 set c = 30
+ CONTEXT: line 4 at SQL statement
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "r" has no field "c"
+ CONTEXT: line 6 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ LINE 1: SELECT a + b
+ ^
+ DETAIL: column "a" does not exist
+ QUERY: SELECT a + b
+ CONTEXT: line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: too many parameters specified for RAISE
+ CONTEXT: line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: too few parameters specified for RAISE
+ CONTEXT: line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ LINE 1: SELECT c+10
+ ^
+ DETAIL: column "c" does not exist
+ QUERY: SELECT c+10
+ CONTEXT: line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: subscripted object is not an array
+ CONTEXT: line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "_exception" has no field "hint"
+ CONTEXT: line 7 at GET DIAGNOSTICS
+ drop function f1();
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ WARNING: error in function "f1_trg()"
+ DETAIL: record "new" has no field "c"
+ CONTEXT: SQL statement "SELECT new.c"
+ line 5 at RAISE
+ insert into t1 values(6,30);
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- should to fail
+ check trigger t1_f1 on t1;
+ WARNING: error in function "f1_trg()"
+ DETAIL: record "new" has no field "c"
+ CONTEXT: line 5 at assignment
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+ ERROR: record "new" has no field "c"
+ CONTEXT: PL/pgSQL function "f1_trg" line 5 at assignment
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- ok
+ check trigger t1_f1 on t1;
+ NOTICE: checked function "f1_trg()"
+ -- ok
+ insert into t1 values(6,30);
+ drop table t1;
+ drop type _exception_type;
+ drop function f1_trg();
*** ./src/test/regress/sql/plpgsql.sql.orig 2011-12-07 05:59:37.166674218 +0100
--- ./src/test/regress/sql/plpgsql.sql 2011-12-07 06:00:07.371331565 +0100
***************
*** 366,371 ****
--- 366,373 ----
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
-- ************************************************************
-- * BEFORE DELETE on HSlot
***************
*** 747,752 ****
--- 749,757 ----
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
+
-- ************************************************************
-- * Support function to clear out the backlink field if
***************
*** 2335,2340 ****
--- 2340,2352 ----
select for_vect();
+ -- check function should not fail
+ check function for_vect();
+
+ -- recheck after check function
+ select for_vect();
+
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 2714,2719 ****
--- 2726,2734 ----
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
+
select forc01();
-- try updating the cursor's current row
***************
*** 3048,3053 ****
--- 3063,3071 ----
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
+
select case_test(1);
select case_test(2);
select case_test(3);
***************
*** 3600,3602 ****
--- 3618,3862 ----
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+
+ --
+ -- check function statement tests
+ --
+
+ create table t1(a int, b int);
+
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+
+ drop function f1();
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- should to fail
+ check trigger t1_f1 on t1;
+
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- ok
+ check trigger t1_f1 on t1;
+
+ -- ok
+ insert into t1 values(6,30);
+
+ drop table t1;
+ drop type _exception_type;
+
+ drop function f1_trg();
+
Hello
a small addition
* don't check SQL functions - are checked well now
* don't check functions from information_schema too
Regards
Pavel
2011/12/8 Pavel Stehule <pavel.stehule@gmail.com>:
Show quoted text
Hello
updated version
changes:
* CHECK FUNCTION ALL; is enabled - in this case functions from
pg_catalog schema are ignoredI looked on parser, and I didn't other changes there - IN SCHEMA, FOR
ROLE are used more time there, so our usage will be consistentRegards
Pavel
2011/12/7 Albe Laurenz <laurenz.albe@wien.gv.at>:
Pavel Stehule wrote:
The syntax error messages are still inadequate; all I can get is
'syntax error at or near "%s"'. They should be more detailed.this system is based on error messages that generates a plpgsql engine
or bison engine. I can correct only a few percent from these messages
:(internally I didn't wrote a compiler or plpgsql checker - this is just
tool that can emit some plpgsql interpret subprocess - and when these
subprocesses raises exceptions, then takes their messages.I see.
I think that at least the documentation should be improved before
I am ready to set this as "ready for committer".please, can you send a correction to documentation or error messages?
I am not able to write documentation
I'll give it a try.
Yours,
Laurenz Albe
Attachments:
check_function-2011-12-08-2.difftext/x-patch; charset=US-ASCII; name=check_function-2011-12-08-2.diffDownload
*** ./doc/src/sgml/catalogs.sgml.orig 2011-12-07 05:59:37.123674706 +0100
--- ./doc/src/sgml/catalogs.sgml 2011-12-07 06:00:07.253332903 +0100
***************
*** 3652,3657 ****
--- 3652,3668 ----
</row>
<row>
+ <entry><structfield>lanchecker</structfield></entry>
+ <entry><type>oid</type></entry>
+ <entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
+ <entry>
+ This references a language checker function that is responsible
+ for checking a embedded SQL and can provide detailed checking.
+ Zero if no checker is provided.
+ </entry>
+ </row>
+
+ <row>
<entry><structfield>lanacl</structfield></entry>
<entry><type>aclitem[]</type></entry>
<entry></entry>
*** ./doc/src/sgml/ref/allfiles.sgml.orig 2011-12-07 05:59:37.125674684 +0100
--- ./doc/src/sgml/ref/allfiles.sgml 2011-12-07 06:00:07.254332891 +0100
***************
*** 40,45 ****
--- 40,46 ----
<!ENTITY alterView SYSTEM "alter_view.sgml">
<!ENTITY analyze SYSTEM "analyze.sgml">
<!ENTITY begin SYSTEM "begin.sgml">
+ <!ENTITY checkFunction SYSTEM "check_function.sgml">
<!ENTITY checkpoint SYSTEM "checkpoint.sgml">
<!ENTITY close SYSTEM "close.sgml">
<!ENTITY cluster SYSTEM "cluster.sgml">
*** ./doc/src/sgml/ref/create_language.sgml.orig 2011-12-07 05:59:37.127674661 +0100
--- ./doc/src/sgml/ref/create_language.sgml 2011-12-07 06:00:07.256332868 +0100
***************
*** 23,29 ****
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
--- 23,29 ----
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ] [ CHECK <replaceable>checkfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
***************
*** 217,222 ****
--- 217,236 ----
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><literal>CHECK</literal> <replaceable class="parameter">checkfunction</replaceable></term>
+
+ <listitem>
+ <para><replaceable class="parameter">checkfunction</replaceable> is the
+ name of a previously registered function that will be called
+ when a new function in the language is created, to check the
+ function by statemnt <command>CHECK FUNCTION</command> or
+ <command>CHECK TRIGGER</command>.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
*** ./doc/src/sgml/reference.sgml.orig 2011-12-07 05:59:37.128674649 +0100
--- ./doc/src/sgml/reference.sgml 2011-12-07 06:00:07.257332857 +0100
***************
*** 68,73 ****
--- 68,74 ----
&alterView;
&analyze;
&begin;
+ &checkFunction;
&checkpoint;
&close;
&cluster;
*** ./doc/src/sgml/ref/check_function.sgml.orig 2011-12-07 05:59:58.965426923 +0100
--- ./doc/src/sgml/ref/check_function.sgml 2011-12-06 17:54:28.000000000 +0100
***************
*** 0 ****
--- 1,38 ----
+ <!--
+ doc/src/sgml/ref/check_function.sgml
+ -->
+
+ <refentry id="SQL-CHECKFUNCTION">
+ <refmeta>
+ <refentrytitle>CHECK FUNCTION</refentrytitle>
+ <manvolnum>7</manvolnum>
+ <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+ <refname>CHECK FUNCTION</refname>
+ <refpurpose>ensure a deep checking of existing function</refpurpose>
+ </refnamediv>
+
+ <indexterm zone="sql-checkfunction">
+ <primary>CHECK FUNCTION</primary>
+ </indexterm>
+
+ <refsynopsisdiv>
+ <synopsis>
+ CHECK FUNCTION <replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] )
+ | CHECK FUNCTION ALL [ IN LANGUAGE <replaceable class="parameter">langname</replaceable> ] [ IN SCHEMA <replaceable class="parameter">schemaname</replaceable> ] [ FOR ROLE <replaceable class="parameter">ownername</replaceable> ]
+ | CHECK TRIGGER <replaceable class="parameter">name</replaceable> ON <replaceable class="parameter">tablename</replaceable>
+ </synopsis>
+ </refsynopsisdiv>
+
+ <refsect1 id="sql-checkfunction-description">
+ <title>Description</title>
+
+ <para>
+ <command>CHECK FUNCTION</command> check a existing function.
+ <command>CHECK TRIGGER</command> check a trigger function.
+ </para>
+ </refsect1>
+
+ </refentry>
*** ./src/backend/catalog/pg_proc.c.orig 2011-12-07 05:59:37.131674614 +0100
--- ./src/backend/catalog/pg_proc.c 2011-12-07 06:00:07.260332824 +0100
***************
*** 1101,1103 ****
--- 1101,1104 ----
*newcursorpos = newcp;
return false;
}
+
*** ./src/backend/commands/functioncmds.c.orig 2011-12-07 05:59:37.132674603 +0100
--- ./src/backend/commands/functioncmds.c 2011-12-08 16:31:50.775499183 +0100
***************
*** 35,53 ****
--- 35,58 ----
#include "access/genam.h"
#include "access/heapam.h"
#include "access/sysattr.h"
+ #include "access/xact.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_language.h"
+ #include "catalog/namespace.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_proc_fn.h"
+ #include "catalog/pg_trigger.h"
#include "catalog/pg_type.h"
#include "catalog/pg_type_fn.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
+ #include "commands/trigger.h"
+ #include "executor/spi_priv.h"
#include "miscadmin.h"
#include "optimizer/var.h"
#include "parser/parse_coerce.h"
***************
*** 60,66 ****
--- 65,73 ----
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
+ #include "utils/memutils.h"
#include "utils/rel.h"
+ #include "utils/resowner.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
***************
*** 1009,1014 ****
--- 1016,1393 ----
}
}
+ /*
+ * Search and execute related checker function
+ */
+ static void
+ CheckFunctionById(Oid funcOid, Oid relid)
+ {
+ HeapTuple tup;
+ Form_pg_proc proc;
+ HeapTuple languageTuple;
+ Form_pg_language languageStruct;
+ Oid languageChecker;
+ bool found_bug;
+ char *funcname;
+
+ tup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
+ if (!HeapTupleIsValid(tup)) /* should not happen */
+ elog(ERROR, "cache lookup failed for function %u", funcOid);
+
+ proc = (Form_pg_proc) GETSTRUCT(tup);
+
+ languageTuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(proc->prolang));
+ Assert(HeapTupleIsValid(languageTuple));
+
+ languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
+ languageChecker = languageStruct->lanchecker;
+
+ funcname = format_procedure(funcOid);
+
+ /* Check a function body */
+ if (OidIsValid(languageChecker))
+ {
+ ArrayType *set_items = NULL;
+ int save_nestlevel = 0;
+ Datum datum;
+ bool isnull;
+ MemoryContext oldCxt;
+ MemoryContext checkCxt;
+ ResourceOwner oldowner;
+
+ datum = SysCacheGetAttr(PROCOID, tup, Anum_pg_proc_proconfig, &isnull);
+
+ if (!isnull)
+ {
+ /* Set per-function configuration parameters */
+ set_items = (ArrayType *) DatumGetPointer(datum);
+ if (set_items) /* Need a new GUC nesting level */
+ {
+ save_nestlevel = NewGUCNestLevel();
+ ProcessGUCArray(set_items,
+ (superuser() ? PGC_SUSET : PGC_USERSET),
+ PGC_S_SESSION,
+ GUC_ACTION_SAVE);
+ }
+ else
+ save_nestlevel = 0; /* keep compiler quiet */
+ }
+
+ checkCxt = AllocSetContextCreate(CurrentMemoryContext,
+ "Check temporary context",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
+
+ oldCxt = MemoryContextSwitchTo(checkCxt);
+
+ oldowner = CurrentResourceOwner;
+ BeginInternalSubTransaction(NULL);
+ MemoryContextSwitchTo(checkCxt);
+
+ /*
+ * forward exception to warning
+ */
+ PG_TRY();
+ {
+ OidFunctionCall2(languageChecker, ObjectIdGetDatum(funcOid),
+ ObjectIdGetDatum(relid));
+
+ RollbackAndReleaseCurrentSubTransaction();
+ MemoryContextSwitchTo(checkCxt);
+ CurrentResourceOwner = oldowner;
+
+ found_bug = false;
+ }
+ PG_CATCH();
+ {
+ ErrorData *edata;
+
+ MemoryContextSwitchTo(checkCxt);
+ edata = CopyErrorData();
+ FlushErrorState();
+
+ RollbackAndReleaseCurrentSubTransaction();
+ MemoryContextSwitchTo(checkCxt);
+ CurrentResourceOwner = oldowner;
+
+ edata->elevel = WARNING;
+
+ ereport(WARNING,
+ (errmsg("error in function \"%s\"", funcname),
+ edata->message != NULL ? errdetail_internal("%s", edata->message) : 0,
+ edata->context != NULL ? errcontext("%s",edata->context) : 0,
+ edata->internalquery != NULL ? internalerrquery(edata->internalquery) : 0,
+ internalerrposition(edata->internalpos)));
+
+ FreeErrorData(edata);
+
+ found_bug = true;
+ }
+ PG_END_TRY();
+
+ MemoryContextSwitchTo(oldCxt);
+
+ if (set_items)
+ AtEOXact_GUC(true, save_nestlevel);
+
+ if (!found_bug)
+ elog(NOTICE, "checked function \"%s\"",
+ funcname);
+ }
+ else
+ elog(NOTICE, "skip check function \"%s\", language \"%s\" hasn't checker function",
+ funcname,
+ NameStr(languageStruct->lanname));
+
+ pfree(funcname);
+
+ ReleaseSysCache(languageTuple);
+ ReleaseSysCache(tup);
+ }
+
+ /*
+ * CheckFunction
+ * call a PL checker function when this function exists.
+ */
+ void
+ CheckFunction(CheckFunctionStmt *stmt)
+ {
+ List *functionName = stmt->funcname;
+ List *argTypes = stmt->args; /* list of TypeName nodes */
+ Oid funcOid = InvalidOid;
+ HeapTuple tup;
+ Oid trgOid = InvalidOid;
+ Oid relid = InvalidOid;
+ Oid languageId;
+ int nkeys = 0;
+ List *objects = NIL;
+
+
+ if (stmt->funcname != NULL)
+ {
+ /*
+ * Find the function,
+ */
+ funcOid = LookupFuncNameTypeNames(functionName, argTypes, false);
+ }
+ else if (stmt->trgname != NULL)
+ {
+ HeapTuple ht_trig;
+ Form_pg_trigger trigrec;
+ ScanKeyData skey[1];
+ Relation tgrel;
+ SysScanDesc tgscan;
+
+ /* find a trigger function */
+ relid = RangeVarGetRelid(stmt->relation, ShareLock, false);
+ trgOid = get_trigger_oid(relid, stmt->trgname, false);
+
+ /*
+ * Fetch the pg_trigger tuple by the Oid of the trigger
+ */
+ tgrel = heap_open(TriggerRelationId, AccessShareLock);
+
+ ScanKeyInit(&skey[0],
+ ObjectIdAttributeNumber,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(trgOid));
+
+ tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
+ SnapshotNow, 1, skey);
+
+ ht_trig = systable_getnext(tgscan);
+
+ if (!HeapTupleIsValid(ht_trig))
+ elog(ERROR, "could not find tuple for trigger %u", trgOid);
+
+ trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
+
+ /* we need to know trigger function to get PL checker function */
+ funcOid = trigrec->tgfoid;
+
+ /* Clean up */
+ systable_endscan(tgscan);
+
+ heap_close(tgrel, AccessShareLock);
+ }
+ else
+ {
+ ScanKeyData key[5];
+ Relation rel;
+ HeapScanDesc scan;
+ ListCell *option;
+ bool language_opt = false;
+ bool schema_opt = false;
+ bool owner_opt = false;
+
+ /*
+ * when Check stmt is multiple statement, then
+ * prepare list of processed functions.
+ */
+ foreach(option, stmt->options)
+ {
+ DefElem *defel = (DefElem *) lfirst(option);
+
+ if (strcmp(defel->defname, "language") == 0)
+ {
+ HeapTuple languageTuple;
+ Form_pg_language languageStruct;
+ Oid languageChecker;
+ char *language = defGetString(defel);
+
+ if (language_opt)
+ elog(ERROR, "multiple usage of IN LANGUAGE option");
+ language_opt = true;
+
+ languageTuple = SearchSysCache1(LANGNAME, PointerGetDatum(language));
+ if (!HeapTupleIsValid(languageTuple))
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_OBJECT),
+ errmsg("language \"%s\" does not exist", language),
+ (PLTemplateExists(language) ?
+ errhint("Use CREATE LANGUAGE to load the language into the database.") : 0)));
+
+ languageId = HeapTupleGetOid(languageTuple);
+ if (languageId == INTERNALlanguageId || languageId == ClanguageId)
+ elog(ERROR, "cannot to check functions in C or internal languages");
+
+ languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
+ languageChecker = languageStruct->lanchecker;
+ if (!OidIsValid(languageChecker))
+ elog(ERROR, "language \"%s\" has no defined checker function",
+ language);
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_prolang,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(languageId));
+
+ ReleaseSysCache(languageTuple);
+ }
+ else if (strcmp(defel->defname, "schema") == 0)
+ {
+ Oid namespaceId = LookupExplicitNamespace(defGetString(defel));
+
+ if (schema_opt)
+ elog(ERROR, "multiple usage of IN SCHEMA option");
+ schema_opt = true;
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_pronamespace,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(namespaceId));
+
+ }
+ else if (strcmp(defel->defname, "owner") == 0)
+ {
+ Oid ownerId = get_role_oid(defGetString(defel), false);
+
+ if (owner_opt)
+ elog(ERROR, "multiple usage of FOR ROLE option");
+ owner_opt = true;
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_proowner,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(ownerId));
+ }
+ else
+ elog(ERROR, "option \"%s\" not recognized",
+ defel->defname);
+ }
+
+ /*
+ * We don't would to iterate over pg_catalog functions without
+ * explicit request and information_schema.
+ */
+ if (!schema_opt)
+ {
+ Oid information_schemaId = LookupExplicitNamespace("information_schema");
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_pronamespace,
+ BTEqualStrategyNumber, F_OIDNE,
+ ObjectIdGetDatum(PG_CATALOG_NAMESPACE));
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_pronamespace,
+ BTEqualStrategyNumber, F_OIDNE,
+ ObjectIdGetDatum(information_schemaId));
+ }
+
+ rel = heap_open(ProcedureRelationId,AccessShareLock);
+ scan = heap_beginscan(rel, SnapshotNow, nkeys, key);
+
+ while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
+ {
+ Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(tup);
+
+ funcOid = HeapTupleGetOid(tup);
+
+ /* when language is not specified, then check checker */
+ if (!language_opt)
+ {
+ Oid prolang = proc->prolang;
+
+ /* skip C, internal or SQL */
+ if (prolang == INTERNALlanguageId)
+ {
+ elog(NOTICE, "skip check function \"%s\", it uses internal language",
+ format_procedure(funcOid));
+ continue;
+ }
+ else if (prolang == ClanguageId)
+ {
+ elog(NOTICE, "skip check function \"%s\", uses C language",
+ format_procedure(funcOid));
+ continue;
+ }
+ else if (prolang == SQLlanguageId)
+ {
+ elog(NOTICE, "skip check function \"%s\", uses SQL language",
+ format_procedure(funcOid));
+ continue;
+ }
+ }
+
+ if (proc->prorettype == TRIGGEROID)
+ {
+ elog(NOTICE, "skip check function \"%s\", it is trigger function",
+ format_procedure(funcOid));
+ continue;
+ }
+
+ objects = lappend_oid(objects, funcOid);
+ }
+
+ heap_endscan(scan);
+ heap_close(rel, AccessShareLock);
+ }
+
+ if (funcOid == InvalidOid && objects == NIL)
+ {
+ elog(NOTICE, "nothing to check");
+ return;
+ }
+
+ if (objects != NIL)
+ {
+ ListCell *object;
+
+ foreach(object, objects)
+ {
+ CHECK_FOR_INTERRUPTS();
+
+ funcOid = lfirst_oid(object);
+ CheckFunctionById(funcOid, InvalidOid);
+ }
+ }
+ else
+ {
+ CheckFunctionById(funcOid, relid);
+ }
+ }
/*
* Rename function
*** ./src/backend/commands/proclang.c.orig 2011-12-07 05:59:37.134674581 +0100
--- ./src/backend/commands/proclang.c 2011-12-07 06:00:07.264332779 +0100
***************
*** 46,57 ****
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
--- 46,58 ----
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
+ char *tmplchecker; /* name of checker function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
***************
*** 67,75 ****
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid;
Oid funcrettype;
! Oid funcargtypes[1];
/*
* If we have template information for the language, ignore the supplied
--- 68,77 ----
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid,
! checkerOid;
Oid funcrettype;
! Oid funcargtypes[2];
/*
* If we have template information for the language, ignore the supplied
***************
*** 219,228 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, pltemplate->tmpltrusted);
}
else
{
--- 221,269 ----
else
valOid = InvalidOid;
+ /*
+ * Likewise for the checker, if required; but we don't care about
+ * its return type.
+ */
+ if (pltemplate->tmplchecker)
+ {
+ funcname = SystemFuncName(pltemplate->tmplchecker);
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(funcname, 2, funcargtypes, true);
+ if (!OidIsValid(checkerOid))
+ {
+ checkerOid = ProcedureCreate(pltemplate->tmplchecker,
+ PG_CATALOG_NAMESPACE,
+ false, /* replace */
+ false, /* returnsSet */
+ VOIDOID,
+ ClanguageId,
+ F_FMGR_C_VALIDATOR,
+ pltemplate->tmplchecker,
+ pltemplate->tmpllibrary,
+ false, /* isAgg */
+ false, /* isWindowFunc */
+ false, /* security_definer */
+ true, /* isStrict */
+ PROVOLATILE_VOLATILE,
+ buildoidvector(funcargtypes, 2),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ NIL,
+ PointerGetDatum(NULL),
+ 1,
+ 0);
+ }
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, pltemplate->tmpltrusted);
}
else
{
***************
*** 294,303 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, stmt->pltrusted);
}
}
--- 335,355 ----
else
valOid = InvalidOid;
+ /* validate the checker function */
+ if (stmt->plchecker)
+ {
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(stmt->plchecker, 2, funcargtypes, false);
+ /* return value is ignored, so we don't check the type */
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, stmt->pltrusted);
}
}
***************
*** 307,313 ****
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
--- 359,365 ----
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
***************
*** 337,342 ****
--- 389,395 ----
values[Anum_pg_language_lanplcallfoid - 1] = ObjectIdGetDatum(handlerOid);
values[Anum_pg_language_laninline - 1] = ObjectIdGetDatum(inlineOid);
values[Anum_pg_language_lanvalidator - 1] = ObjectIdGetDatum(valOid);
+ values[Anum_pg_language_lanchecker - 1] = ObjectIdGetDatum(checkerOid);
nulls[Anum_pg_language_lanacl - 1] = true;
/* Check for pre-existing definition */
***************
*** 423,428 ****
--- 476,490 ----
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
+ /* dependency on the checker function, if any */
+ if (OidIsValid(checkerOid))
+ {
+ referenced.classId = ProcedureRelationId;
+ referenced.objectId = checkerOid;
+ referenced.objectSubId = 0;
+ recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
+ }
+
/* Post creation hook for new procedural language */
InvokeObjectAccessHook(OAT_POST_CREATE,
LanguageRelationId, myself.objectId, 0);
***************
*** 478,483 ****
--- 540,550 ----
if (!isnull)
result->tmplvalidator = TextDatumGetCString(datum);
+ datum = heap_getattr(tup, Anum_pg_pltemplate_tmplchecker,
+ RelationGetDescr(rel), &isnull);
+ if (!isnull)
+ result->tmplchecker = TextDatumGetCString(datum);
+
datum = heap_getattr(tup, Anum_pg_pltemplate_tmpllibrary,
RelationGetDescr(rel), &isnull);
if (!isnull)
*** ./src/backend/nodes/copyfuncs.c.orig 2011-12-07 05:59:37.135674570 +0100
--- ./src/backend/nodes/copyfuncs.c 2011-12-07 06:38:24.420227380 +0100
***************
*** 2880,2885 ****
--- 2880,2900 ----
return newnode;
}
+ static CheckFunctionStmt *
+ _copyCheckFunctionStmt(CheckFunctionStmt *from)
+ {
+ CheckFunctionStmt *newnode = makeNode(CheckFunctionStmt);
+
+ COPY_NODE_FIELD(funcname);
+ COPY_NODE_FIELD(args);
+ COPY_STRING_FIELD(trgname);
+ COPY_NODE_FIELD(relation);
+ COPY_SCALAR_FIELD(is_function);
+ COPY_NODE_FIELD(options);
+
+ return newnode;
+ }
+
static DoStmt *
_copyDoStmt(DoStmt *from)
{
***************
*** 4165,4170 ****
--- 4180,4188 ----
case T_AlterFunctionStmt:
retval = _copyAlterFunctionStmt(from);
break;
+ case T_CheckFunctionStmt:
+ retval = _copyCheckFunctionStmt(from);
+ break;
case T_DoStmt:
retval = _copyDoStmt(from);
break;
*** ./src/backend/nodes/equalfuncs.c.orig 2011-12-07 05:59:37.137674548 +0100
--- ./src/backend/nodes/equalfuncs.c 2011-12-07 06:38:56.187881989 +0100
***************
*** 1292,1297 ****
--- 1292,1310 ----
}
static bool
+ _equalCheckFunctionStmt(CheckFunctionStmt *a, CheckFunctionStmt *b)
+ {
+ COMPARE_NODE_FIELD(funcname);
+ COMPARE_NODE_FIELD(args);
+ COMPARE_STRING_FIELD(trgname);
+ COMPARE_NODE_FIELD(relation);
+ COMPARE_SCALAR_FIELD(is_function);
+ COMPARE_NODE_FIELD(options);
+
+ return true;
+ }
+
+ static bool
_equalDoStmt(DoStmt *a, DoStmt *b)
{
COMPARE_NODE_FIELD(args);
***************
*** 2708,2713 ****
--- 2721,2729 ----
case T_AlterFunctionStmt:
retval = _equalAlterFunctionStmt(a, b);
break;
+ case T_CheckFunctionStmt:
+ retval = _equalCheckFunctionStmt(a, b);
+ break;
case T_DoStmt:
retval = _equalDoStmt(a, b);
break;
*** ./src/backend/parser/gram.y.orig 2011-12-07 05:59:37.138674536 +0100
--- ./src/backend/parser/gram.y 2011-12-08 14:48:01.858729545 +0100
***************
*** 227,232 ****
--- 227,233 ----
DeallocateStmt PrepareStmt ExecuteStmt
DropOwnedStmt ReassignOwnedStmt
AlterTSConfigurationStmt AlterTSDictionaryStmt
+ CheckFunctionStmt
%type <node> select_no_parens select_with_parens select_clause
simple_select values_clause
***************
*** 276,282 ****
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate
%type <range> qualified_name OptConstrFromTable
--- 277,283 ----
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate opt_checker
%type <range> qualified_name OptConstrFromTable
***************
*** 463,469 ****
%type <windef> window_definition over_clause window_specification
opt_frame_clause frame_extent frame_bound
%type <str> opt_existing_window_name
!
/*
* Non-keyword token types. These are hard-wired into the "flex" lexer.
--- 464,471 ----
%type <windef> window_definition over_clause window_specification
opt_frame_clause frame_extent frame_bound
%type <str> opt_existing_window_name
! %type <defelt> CheckFunctionOptElem
! %type <list> CheckFunctionOpts
/*
* Non-keyword token types. These are hard-wired into the "flex" lexer.
***************
*** 700,705 ****
--- 702,708 ----
| AlterUserSetStmt
| AlterUserStmt
| AnalyzeStmt
+ | CheckFunctionStmt
| CheckPointStmt
| ClosePortalStmt
| ClusterStmt
***************
*** 3174,3184 ****
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
--- 3177,3188 ----
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
+ n->plchecker = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator opt_checker
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
***************
*** 3186,3191 ****
--- 3190,3196 ----
n->plhandler = $8;
n->plinline = $9;
n->plvalidator = $10;
+ n->plchecker = $11;
n->pltrusted = $3;
$$ = (Node *)n;
}
***************
*** 3220,3225 ****
--- 3225,3235 ----
| /*EMPTY*/ { $$ = NIL; }
;
+ opt_checker:
+ CHECK handler_name { $$ = $2; }
+ | /*EMPTY*/ { $$ = NIL; }
+ ;
+
DropPLangStmt:
DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
{
***************
*** 6250,6255 ****
--- 6260,6344 ----
/*****************************************************************************
*
+ * CHECK FUNCTION funcname(args)
+ * CHECK TRIGGER triggername ON table
+ *
+ *
+ *****************************************************************************/
+
+
+ CheckFunctionStmt:
+ CHECK FUNCTION func_name func_args
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = $3;
+ n->args = extractArgTypes($4);
+ n->trgname = NULL;
+ n->relation = NULL;
+ n->is_function = true;
+ n->options = NIL;
+ $$ = (Node *) n;
+ }
+ | CHECK TRIGGER name ON qualified_name
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = $3;
+ n->relation = $5;
+ n->is_function = false;
+ n->options = NIL;
+ $$ = (Node *) n;
+ }
+ | CHECK FUNCTION ALL
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = NULL;
+ n->relation = NULL;
+ n->is_function = true;
+ n->options = NIL;
+ $$ = (Node *) n;
+ }
+ | CHECK FUNCTION ALL CheckFunctionOpts
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = NULL;
+ n->relation = NULL;
+ n->is_function = true;
+ n->options = $4;
+ $$ = (Node *) n;
+ }
+ ;
+
+ CheckFunctionOpts:
+ CheckFunctionOptElem { $$ = list_make1($1); }
+ | CheckFunctionOpts CheckFunctionOptElem { $$ = lappend($1, $2); }
+ ;
+
+ CheckFunctionOptElem:
+ IN_P LANGUAGE ColId
+ {
+ $$ = makeDefElem("language",
+ (Node *) makeString($2));
+ }
+ | IN_P SCHEMA ColId
+ {
+ $$ = makeDefElem("schema",
+ (Node *) makeString($2));
+ }
+ | FOR ROLE ColId
+ {
+ $$ = makeDefElem("owner",
+ (Node *) makeString($3));
+ }
+ ;
+
+ /*****************************************************************************
+ *
* DO <anonymous code block> [ LANGUAGE language ]
*
* We use a DefElem list for future extensibility, and to allow flexibility
*** ./src/backend/tcop/utility.c.orig 2011-12-07 05:59:37.140674512 +0100
--- ./src/backend/tcop/utility.c 2011-12-07 06:35:57.836744377 +0100
***************
*** 882,887 ****
--- 882,891 ----
AlterFunction((AlterFunctionStmt *) parsetree);
break;
+ case T_CheckFunctionStmt:
+ CheckFunction((CheckFunctionStmt *) parsetree);
+ break;
+
case T_IndexStmt: /* CREATE INDEX */
{
IndexStmt *stmt = (IndexStmt *) parsetree;
***************
*** 2125,2130 ****
--- 2129,2141 ----
}
break;
+ case T_CheckFunctionStmt:
+ if (((CheckFunctionStmt *) parsetree)->is_function)
+ tag = "CHECK FUNCTION";
+ else
+ tag = "CHECK TRIGGER";
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
***************
*** 2565,2570 ****
--- 2576,2585 ----
}
break;
+ case T_CheckFunctionStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
*** ./src/bin/pg_dump/pg_dump.c.orig 2011-12-07 05:59:37.142674489 +0100
--- ./src/bin/pg_dump/pg_dump.c 2011-12-07 06:00:07.312332234 +0100
***************
*** 5326,5338 ****
int i_lanplcallfoid;
int i_laninline;
int i_lanvalidator;
int i_lanacl;
int i_lanowner;
/* Make sure we are in proper schema */
selectSourceSchema("pg_catalog");
! if (g_fout->remoteVersion >= 90000)
{
/* pg_language has a laninline column */
appendPQExpBuffer(query, "SELECT tableoid, oid, "
--- 5326,5351 ----
int i_lanplcallfoid;
int i_laninline;
int i_lanvalidator;
+ int i_lanchecker;
int i_lanacl;
int i_lanowner;
/* Make sure we are in proper schema */
selectSourceSchema("pg_catalog");
! if (g_fout->remoteVersion >= 90200)
! {
! /* pg_language has a lanchecker column */
! appendPQExpBuffer(query, "SELECT tableoid, oid, "
! "lanname, lanpltrusted, lanplcallfoid, "
! "laninline, lanvalidator, lanchecker, lanacl, "
! "(%s lanowner) AS lanowner "
! "FROM pg_language "
! "WHERE lanispl "
! "ORDER BY oid",
! username_subquery);
! }
! else if (g_fout->remoteVersion >= 90000)
{
/* pg_language has a laninline column */
appendPQExpBuffer(query, "SELECT tableoid, oid, "
***************
*** 5409,5414 ****
--- 5422,5428 ----
/* these may fail and return -1: */
i_laninline = PQfnumber(res, "laninline");
i_lanvalidator = PQfnumber(res, "lanvalidator");
+ i_lanchecker = PQfnumber(res, "lanchecker");
i_lanacl = PQfnumber(res, "lanacl");
i_lanowner = PQfnumber(res, "lanowner");
***************
*** 5422,5427 ****
--- 5436,5445 ----
planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
+ if (i_lanchecker >= 0)
+ planginfo[i].lanchecker = atooid(PQgetvalue(res, i, i_lanchecker));
+ else
+ planginfo[i].lanchecker = InvalidOid;
if (i_laninline >= 0)
planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
else
***************
*** 8597,8602 ****
--- 8615,8621 ----
char *qlanname;
char *lanschema;
FuncInfo *funcInfo;
+ FuncInfo *checkerInfo = NULL;
FuncInfo *inlineInfo = NULL;
FuncInfo *validatorInfo = NULL;
***************
*** 8616,8621 ****
--- 8635,8647 ----
if (funcInfo != NULL && !funcInfo->dobj.dump)
funcInfo = NULL; /* treat not-dumped same as not-found */
+ if (OidIsValid(plang->lanchecker))
+ {
+ checkerInfo = findFuncByOid(plang->lanchecker);
+ if (checkerInfo != NULL && !checkerInfo->dobj.dump)
+ checkerInfo = NULL;
+ }
+
if (OidIsValid(plang->laninline))
{
inlineInfo = findFuncByOid(plang->laninline);
***************
*** 8642,8647 ****
--- 8668,8674 ----
* don't, this might not work terribly nicely.
*/
useParams = (funcInfo != NULL &&
+ (checkerInfo != NULL || !OidIsValid(plang->lanchecker)) &&
(inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
(validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
***************
*** 8697,8702 ****
--- 8724,8739 ----
appendPQExpBuffer(defqry, "%s",
fmtId(validatorInfo->dobj.name));
}
+ if (OidIsValid(plang->lanchecker))
+ {
+ appendPQExpBuffer(defqry, " CHECK ");
+ /* Cope with possibility that checker is in different schema */
+ if (checkerInfo->dobj.namespace != funcInfo->dobj.namespace)
+ appendPQExpBuffer(defqry, "%s.",
+ fmtId(checkerInfo->dobj.namespace->dobj.name));
+ appendPQExpBuffer(defqry, "%s",
+ fmtId(checkerInfo->dobj.name));
+ }
}
else
{
*** ./src/bin/pg_dump/pg_dump.h.orig 2011-12-07 05:59:37.144674467 +0100
--- ./src/bin/pg_dump/pg_dump.h 2011-12-07 06:00:07.348331825 +0100
***************
*** 387,392 ****
--- 387,393 ----
Oid lanplcallfoid;
Oid laninline;
Oid lanvalidator;
+ Oid lanchecker;
char *lanacl;
char *lanowner; /* name of owner, or empty string */
} ProcLangInfo;
*** ./src/bin/psql/tab-complete.c.orig 2011-12-07 05:59:37.146674445 +0100
--- ./src/bin/psql/tab-complete.c 2011-12-07 06:00:07.350331803 +0100
***************
*** 1,4 ****
--- 1,5 ----
/*
+ *
* psql - the PostgreSQL interactive terminal
*
* Copyright (c) 2000-2011, PostgreSQL Global Development Group
***************
*** 727,733 ****
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
--- 728,734 ----
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECK", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
***************
*** 1524,1529 ****
--- 1525,1552 ----
COMPLETE_WITH_LIST(list_TRANS);
}
+
+ /* CHECK */
+ else if (pg_strcasecmp(prev_wd, "CHECK") == 0)
+ {
+ static const char *const list_CHECK[] =
+ {"FUNCTION", "TRIGGER", NULL};
+
+ COMPLETE_WITH_LIST(list_CHECK);
+ }
+ else if (pg_strcasecmp(prev3_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
+ {
+ COMPLETE_WITH_CONST("ON");
+ }
+ else if (pg_strcasecmp(prev4_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
+ pg_strcasecmp(prev_wd, "ON") == 0)
+ {
+ completion_info_charp = prev2_wd;
+ COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger);
+ }
+
/* CLUSTER */
/*
*** ./src/include/catalog/pg_language.h.orig 2011-12-07 05:59:37.147674434 +0100
--- ./src/include/catalog/pg_language.h 2011-12-07 06:00:07.351331792 +0100
***************
*** 37,42 ****
--- 37,43 ----
Oid lanplcallfoid; /* Call handler for PL */
Oid laninline; /* Optional anonymous-block handler function */
Oid lanvalidator; /* Optional validation function */
+ Oid lanchecker; /* Optional checker function */
aclitem lanacl[1]; /* Access privileges */
} FormData_pg_language;
***************
*** 51,57 ****
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 8
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
--- 52,58 ----
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 9
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
***************
*** 59,78 ****
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanacl 8
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
--- 60,80 ----
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanchecker 8
! #define Anum_pg_language_lanacl 9
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 0 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 0 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 0 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
*** ./src/include/catalog/pg_pltemplate.h.orig 2011-12-07 05:59:37.149674412 +0100
--- ./src/include/catalog/pg_pltemplate.h 2011-12-07 06:00:07.352331780 +0100
***************
*** 36,41 ****
--- 36,42 ----
text tmplhandler; /* name of call handler function */
text tmplinline; /* name of anonymous-block handler, or NULL */
text tmplvalidator; /* name of validator function, or NULL */
+ text tmplchecker; /* name of checker function, or NULL */
text tmpllibrary; /* path of shared library */
aclitem tmplacl[1]; /* access privileges for template */
} FormData_pg_pltemplate;
***************
*** 51,65 ****
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 8
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmpllibrary 7
! #define Anum_pg_pltemplate_tmplacl 8
/* ----------------
--- 52,67 ----
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 9
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmplchecker 7
! #define Anum_pg_pltemplate_tmpllibrary 8
! #define Anum_pg_pltemplate_tmplacl 9
/* ----------------
***************
*** 67,79 ****
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
--- 69,81 ----
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "plpgsql_checker" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" _null_ "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
*** ./src/include/commands/defrem.h.orig 2011-12-07 05:59:37.150674400 +0100
--- ./src/include/commands/defrem.h 2011-12-07 06:00:07.352331780 +0100
***************
*** 62,67 ****
--- 62,68 ----
/* commands/functioncmds.c */
extern void CreateFunction(CreateFunctionStmt *stmt, const char *queryString);
extern void RemoveFunctionById(Oid funcOid);
+ extern void CheckFunction(CheckFunctionStmt *stmt);
extern void SetFunctionReturnType(Oid funcOid, Oid newRetType);
extern void SetFunctionArgType(Oid funcOid, int argIndex, Oid newArgType);
extern void RenameFunction(List *name, List *argtypes, const char *newname);
*** ./src/include/nodes/nodes.h.orig 2011-12-07 05:59:37.151674388 +0100
--- ./src/include/nodes/nodes.h 2011-12-07 06:00:07.353331768 +0100
***************
*** 291,296 ****
--- 291,297 ----
T_IndexStmt,
T_CreateFunctionStmt,
T_AlterFunctionStmt,
+ T_CheckFunctionStmt,
T_DoStmt,
T_RenameStmt,
T_RuleStmt,
*** ./src/include/nodes/parsenodes.h.orig 2011-12-07 05:59:37.153674364 +0100
--- ./src/include/nodes/parsenodes.h 2011-12-07 06:16:44.502121440 +0100
***************
*** 1734,1739 ****
--- 1734,1740 ----
List *plhandler; /* PL call handler function (qual. name) */
List *plinline; /* optional inline function (qual. name) */
List *plvalidator; /* optional validator function (qual. name) */
+ List *plchecker; /* optional checker function (qual. name) */
bool pltrusted; /* PL is trusted */
} CreatePLangStmt;
***************
*** 2077,2082 ****
--- 2078,2098 ----
} AlterFunctionStmt;
/* ----------------------
+ * Check {Function|Trigger} Statement
+ * ----------------------
+ */
+ typedef struct CheckFunctionStmt
+ {
+ NodeTag type;
+ List *funcname; /* qualified name of checked object */
+ List *args; /* types of the arguments */
+ char *trgname; /* trigger's name */
+ RangeVar *relation; /* trigger's relation */
+ bool is_function; /* true for CHECK FUNCTION statement */
+ List *options; /* other DefElem options */
+ } CheckFunctionStmt;
+
+ /* ----------------------
* DO Statement
*
* DoStmt is the raw parser output, InlineCodeBlock is the execution-time API
*** ./src/pl/plpgsql/src/pl_comp.c.orig 2011-12-07 05:59:37.155674342 +0100
--- ./src/pl/plpgsql/src/pl_comp.c 2011-12-07 06:00:07.360331689 +0100
***************
*** 115,121 ****
static void plpgsql_HashTableInsert(PLpgSQL_function *function,
PLpgSQL_func_hashkey *func_key);
static void plpgsql_HashTableDelete(PLpgSQL_function *function);
- static void delete_function(PLpgSQL_function *func);
/* ----------
* plpgsql_compile Make an execution tree for a PL/pgSQL function.
--- 115,120 ----
***************
*** 175,181 ****
* Nope, so remove it from hashtable and try to drop associated
* storage (if not done already).
*/
! delete_function(function);
/*
* If the function isn't in active use then we can overwrite the
--- 174,180 ----
* Nope, so remove it from hashtable and try to drop associated
* storage (if not done already).
*/
! plpgsql_delete_function(function);
/*
* If the function isn't in active use then we can overwrite the
***************
*** 2426,2432 ****
}
/*
! * delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
--- 2425,2431 ----
}
/*
! * plpgsql_delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
***************
*** 2439,2446 ****
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
! static void
! delete_function(PLpgSQL_function *func)
{
/* remove function from hash table (might be done already) */
plpgsql_HashTableDelete(func);
--- 2438,2445 ----
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
! void
! plpgsql_delete_function(PLpgSQL_function *func)
{
/* remove function from hash table (might be done already) */
plpgsql_HashTableDelete(func);
*** ./src/pl/plpgsql/src/pl_exec.c.orig 2011-12-07 05:59:37.157674320 +0100
--- ./src/pl/plpgsql/src/pl_exec.c 2011-12-07 07:36:23.112366630 +0100
***************
*** 210,216 ****
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
PLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
!
/* ----------
* plpgsql_exec_function Called by the call handler for
--- 210,228 ----
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
PLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
! static void check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec);
! static void check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr);
! static void assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
! PLpgSQL_row *row, PLpgSQL_rec *rec,
! TupleDesc tupdesc);
! static TupleDesc expr_get_desc(PLpgSQL_execstate *estate,
! PLpgSQL_expr *query,
! bool use_element_type,
! bool expand_record,
! bool is_expression);
! static void var_init_to_null(PLpgSQL_execstate *estate, int varno);
! static void check_stmts(PLpgSQL_execstate *estate, List *stmts);
! static void check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
/* ----------
* plpgsql_exec_function Called by the call handler for
***************
*** 6176,6178 ****
--- 6188,7240 ----
return portal;
}
+
+ /*
+ * Following code ensures a CHECK FUNCTION and CHECK TRIGGER statements for PL/pgSQL
+ *
+ */
+
+ /*
+ * append a CONTEXT to error message
+ */
+ static void
+ check_error_callback(void *arg)
+ {
+ PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;
+
+ if (estate->err_stmt != NULL)
+ {
+ /* translator: last %s is a plpgsql statement type name */
+ errcontext("line %d at %s",
+ estate->err_stmt->lineno,
+ plpgsql_stmt_typename(estate->err_stmt));
+ }
+ }
+
+ /*
+ * Check function - it prepare variables and starts a prepare plan walker
+ * called by function checker
+ */
+ void
+ plpgsql_check_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
+
+ /*
+ * Store the actual call argument values into the appropriate variables
+ */
+ for (i = 0; i < func->fn_nargs; i++)
+ {
+ int n = func->fn_argvarnos[i];
+
+ switch (estate.datums[n]->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ {
+ var_init_to_null(&estate, n);
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ {
+ PLpgSQL_row *row = (PLpgSQL_row *) estate.datums[n];
+
+ exec_move_row(&estate, NULL, row, NULL, NULL);
+ }
+ break;
+
+ default:
+ elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
+ }
+ }
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Check trigger - prepare fake environments for testing trigger
+ *
+ */
+ void
+ plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ PLpgSQL_rec *rec_new,
+ *rec_old;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, NULL);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
+
+ /*
+ * Put the OLD and NEW tuples into record variables
+ *
+ * We make the tupdescs available in both records even though only one may
+ * have a value. This allows parsing of record references to succeed in
+ * functions that are used for multiple trigger types. For example, we
+ * might have a test like "if (TG_OP = 'INSERT' and NEW.foo = 'xyz')",
+ * which should parse regardless of the current trigger type.
+ */
+ rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
+ rec_new->freetup = false;
+ rec_new->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_new, trigdata->tg_relation->rd_att);
+
+ rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
+ rec_old->freetup = false;
+ rec_old->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_old, trigdata->tg_relation->rd_att);
+
+ /*
+ * Assign the special tg_ variables
+ */
+ var_init_to_null(&estate, func->tg_op_varno);
+ var_init_to_null(&estate, func->tg_name_varno);
+ var_init_to_null(&estate, func->tg_when_varno);
+ var_init_to_null(&estate, func->tg_level_varno);
+ var_init_to_null(&estate, func->tg_relid_varno);
+ var_init_to_null(&estate, func->tg_relname_varno);
+ var_init_to_null(&estate, func->tg_table_name_varno);
+ var_init_to_null(&estate, func->tg_table_schema_varno);
+ var_init_to_null(&estate, func->tg_nargs_varno);
+ var_init_to_null(&estate, func->tg_argv_varno);
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Verify lvalue
+ * It doesn't repeat a checks that are done.
+ * Checks a subscript expressions, verify a validity of record's fields
+ */
+ static void
+ check_target(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ switch (target->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ case PLPGSQL_DTYPE_REC:
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ check_row_or_rec(estate, (PLpgSQL_row *) target, NULL);
+ break;
+
+ case PLPGSQL_DTYPE_RECFIELD:
+ {
+ PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
+ PLpgSQL_rec *rec;
+ int fno;
+
+ rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
+
+ /*
+ * Check that there is already a tuple in the record. We need
+ * that because records don't have any predefined field
+ * structure.
+ */
+ if (!HeapTupleIsValid(rec->tup))
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("record \"%s\" is not assigned to tuple structure",
+ rec->refname)));
+
+ /*
+ * Get the number of the records field to change and the
+ * number of attributes in the tuple. Note: disallow system
+ * column names because the code below won't cope.
+ */
+ fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
+ if (fno <= 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("record \"%s\" has no field \"%s\"",
+ rec->refname, recfield->fieldname)));
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ARRAYELEM:
+ {
+ /*
+ * Target is an element of an array
+ */
+ int nsubscripts;
+ Oid arrayelemtypeid;
+ Oid arraytypeid;
+
+ /*
+ * To handle constructs like x[1][2] := something, we have to
+ * be prepared to deal with a chain of arrayelem datums. Chase
+ * back to find the base array datum, and save the subscript
+ * expressions as we go. (We are scanning right to left here,
+ * but want to evaluate the subscripts left-to-right to
+ * minimize surprises.)
+ */
+ nsubscripts = 0;
+ do
+ {
+ PLpgSQL_arrayelem *arrayelem = (PLpgSQL_arrayelem *) target;
+
+ if (nsubscripts++ >= MAXDIM)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
+ nsubscripts + 1, MAXDIM)));
+
+ check_expr(estate, arrayelem->subscript);
+
+ target = estate->datums[arrayelem->arrayparentno];
+ } while (target->dtype == PLPGSQL_DTYPE_ARRAYELEM);
+
+ /* If target is domain over array, reduce to base type */
+ arraytypeid = exec_get_datum_type(estate, target);
+ arraytypeid = getBaseType(arraytypeid);
+
+ arrayelemtypeid = get_element_type(arraytypeid);
+
+ if (!OidIsValid(arrayelemtypeid))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("subscripted object is not an array")));
+ }
+ break;
+ }
+ }
+
+ /*
+ * Check composed lvalue
+ * There is nothing to check on rec variables
+ */
+ static void
+ check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec)
+ {
+ int fnum;
+
+ /* there are nothing to check on rec now */
+ if (row != NULL)
+ {
+ for (fnum = 0; fnum < row->nfields; fnum++)
+ {
+ /* skip dropped columns */
+ if (row->varnos[fnum] < 0)
+ continue;
+
+ check_target(estate, row->varnos[fnum]);
+ }
+ }
+ }
+
+ /*
+ * Generate a prepared plan - this is simplyfied copy from pl_exec.c
+ * Is not necessary to check simple plan
+ */
+ static void
+ prepare_expr(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *expr, int cursorOptions)
+ {
+ SPIPlanPtr plan;
+
+ /* leave when there are not expression */
+ if (expr == NULL)
+ return;
+
+ /* leave when plan is created */
+ if (expr->plan != NULL)
+ return;
+
+ /*
+ * The grammar can't conveniently set expr->func while building the parse
+ * tree, so make sure it's set before parser hooks need it.
+ */
+ expr->func = estate->func;
+
+ /*
+ * Generate and save the plan
+ */
+ plan = SPI_prepare_params(expr->query,
+ (ParserSetupHook) plpgsql_parser_setup,
+ (void *) expr,
+ cursorOptions);
+ if (plan == NULL)
+ {
+ /* Some SPI errors deserve specific error messages */
+ switch (SPI_result)
+ {
+ case SPI_ERROR_COPY:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot COPY to/from client in PL/pgSQL")));
+ case SPI_ERROR_TRANSACTION:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot begin/end transactions in PL/pgSQL"),
+ errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
+ default:
+ elog(ERROR, "SPI_prepare_params failed for \"%s\": %s",
+ expr->query, SPI_result_code_string(SPI_result));
+ }
+ }
+
+ expr->plan = SPI_saveplan(plan);
+ SPI_freeplan(plan);
+ }
+
+ /*
+ * Verify a expression
+ */
+ static void
+ check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
+ {
+ TupleDesc tupdesc;
+
+ if (expr != NULL)
+ {
+ prepare_expr(estate, expr, 0);
+ tupdesc = expr_get_desc(estate, expr, false, false, true);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+
+ /*
+ * We have to assign TupleDesc to all used record variables step by step.
+ * We would to use a exec routines for query preprocessing, so we must
+ * to create a typed NULL value, and this value is assigned to record
+ * variable.
+ */
+ static void
+ assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
+ PLpgSQL_row *row, PLpgSQL_rec *rec,
+ TupleDesc tupdesc)
+ {
+ bool *nulls;
+ HeapTuple tup;
+
+ if (tupdesc == NULL)
+ elog(ERROR, "tuple descriptor is empty");
+
+ /*
+ * row variable has assigned TupleDesc already, so don't be processed
+ * here
+ */
+ if (rec != NULL)
+ {
+ PLpgSQL_rec *target = (PLpgSQL_rec *)(estate->datums[rec->dno]);
+
+ if (target->freetup)
+ heap_freetuple(target->tup);
+
+ if (rec->freetupdesc)
+ FreeTupleDesc(target->tupdesc);
+
+ /* initialize rec by NULLs */
+ nulls = (bool *) palloc(tupdesc->natts * sizeof(bool));
+ memset(nulls, true, tupdesc->natts * sizeof(bool));
+
+ target->tupdesc = CreateTupleDescCopy(tupdesc);
+ target->freetupdesc = true;
+
+ tup = heap_form_tuple(tupdesc, NULL, nulls);
+ if (HeapTupleIsValid(tup))
+ {
+ target->tup = tup;
+ target->freetup = true;
+ }
+ else
+ elog(ERROR, "cannot to build valid composite value");
+ }
+ }
+
+ /*
+ * Assign a tuple descriptor to variable specified by dno
+ */
+ static void
+ assign_tupdesc_dno(PLpgSQL_execstate *estate, int varno, TupleDesc tupdesc)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ if (target->dtype == PLPGSQL_DTYPE_REC)
+ assign_tupdesc_row_or_rec(estate, NULL, (PLpgSQL_rec *) target, tupdesc);
+ }
+
+ /*
+ * Returns a tuple descriptor based on existing plan
+ */
+ static TupleDesc
+ expr_get_desc(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *query,
+ bool use_element_type,
+ bool expand_record,
+ bool is_expression)
+ {
+ TupleDesc tupdesc = NULL;
+ CachedPlanSource *plansource = NULL;
+
+ if (query->plan != NULL)
+ {
+ SPIPlanPtr plan = query->plan;
+
+ if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
+ elog(ERROR, "cached plan is not valid plan");
+
+ if (list_length(plan->plancache_list) != 1)
+ elog(ERROR, "plan is not single execution plan");
+
+ plansource = (CachedPlanSource *) linitial(plan->plancache_list);
+
+ tupdesc = CreateTupleDescCopy(plansource->resultDesc);
+ }
+ else
+ elog(ERROR, "there are no plan for query: \"%s\"",
+ query->query);
+
+ /*
+ * try to get a element type, when result is a array (used with FOREACH ARRAY stmt)
+ */
+ if (use_element_type)
+ {
+ Oid elemtype;
+ TupleDesc elemtupdesc;
+
+ /* result should be a array */
+ if (tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /* check the type of the expression - must be an array */
+ elemtype = get_element_type(tupdesc->attrs[0]->atttypid);
+ if (!OidIsValid(elemtype))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("FOREACH expression must yield an array, not type %s",
+ format_type_be(tupdesc->attrs[0]->atttypid))));
+
+ /* we can't know typmod now */
+ elemtupdesc = lookup_rowtype_tupdesc_noerror(elemtype, -1, true);
+ if (elemtupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(elemtupdesc);
+ ReleaseTupleDesc(elemtupdesc);
+ }
+ else
+ elog(ERROR, "cannot to identify real type for record type variable");
+ }
+
+ if (is_expression && tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /*
+ * One spacial case is when record is assigned to composite type, then
+ * we should to unpack composite type.
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 && expand_record)
+ {
+ TupleDesc unpack_tupdesc;
+
+ unpack_tupdesc = lookup_rowtype_tupdesc_noerror(tupdesc->attrs[0]->atttypid,
+ tupdesc->attrs[0]->atttypmod,
+ true);
+ if (unpack_tupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(unpack_tupdesc);
+ ReleaseTupleDesc(unpack_tupdesc);
+ }
+ }
+
+ /*
+ * There is special case, when returned tupdesc contains only
+ * unpined record: rec := func_with_out_parameters(). IN this case
+ * we must to dig more deep - we have to find oid of function and
+ * get their parameters,
+ *
+ * This is support for assign statement
+ * recvar := func_with_out_parameters(..)
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 &&
+ tupdesc->attrs[0]->atttypid == RECORDOID &&
+ tupdesc->attrs[0]->atttypmod == -1 &&
+ expand_record)
+ {
+ PlannedStmt *_stmt;
+ Plan *_plan;
+ TargetEntry *tle;
+ CachedPlan *cplan;
+
+ /*
+ * When tupdesc is related to unpined record, we will try
+ * to check plan if it is just function call and if it is
+ * then we can try to derive a tupledes from function's
+ * description.
+ */
+ cplan = GetCachedPlan(plansource, NULL, true);
+ _stmt = (PlannedStmt *) linitial(cplan->stmt_list);
+
+ if (IsA(_stmt, PlannedStmt) && _stmt->commandType == CMD_SELECT)
+ {
+ _plan = _stmt->planTree;
+ if (IsA(_plan, Result) && list_length(_plan->targetlist) == 1)
+ {
+ tle = (TargetEntry *) linitial(_plan->targetlist);
+ if (((Node *) tle->expr)->type == T_FuncExpr)
+ {
+ FuncExpr *fn = (FuncExpr *) tle->expr;
+ FmgrInfo flinfo;
+ FunctionCallInfoData fcinfo;
+ TupleDesc rd;
+ Oid rt;
+
+ fmgr_info(fn->funcid, &flinfo);
+ flinfo.fn_expr = (Node *) fn;
+ fcinfo.flinfo = &flinfo;
+
+ get_call_result_type(&fcinfo, &rt, &rd);
+ if (rd == NULL)
+ elog(ERROR, "function does not return composite type is not possible to identify composite type");
+
+ FreeTupleDesc(tupdesc);
+ BlessTupleDesc(rd);
+
+ tupdesc = rd;
+ }
+ }
+ }
+
+ ReleaseCachedPlan(cplan, true);
+ }
+
+ return tupdesc;
+ }
+
+ /*
+ * Ensure check for all statements in list
+ */
+ static void
+ check_stmts(PLpgSQL_execstate *estate, List *stmts)
+ {
+ ListCell *lc;
+
+ foreach(lc, stmts)
+ {
+ check_stmt(estate, (PLpgSQL_stmt *) lfirst(lc));
+ }
+ }
+
+ /*
+ * walk over all statements
+ */
+ static void
+ check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
+ {
+ TupleDesc tupdesc = NULL;
+ PLpgSQL_function *func;
+ ListCell *l;
+
+ if (stmt == NULL)
+ return;
+
+ estate->err_stmt = stmt;
+ func = estate->func;
+
+ switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
+ {
+ case PLPGSQL_STMT_BLOCK:
+ {
+ PLpgSQL_stmt_block *stmt_block = (PLpgSQL_stmt_block *) stmt;
+ int i;
+ PLpgSQL_datum *d;
+
+ for (i = 0; i < stmt_block->n_initvars; i++)
+ {
+ d = func->datums[stmt_block->initvarnos[i]];
+
+ if (d->dtype == PLPGSQL_DTYPE_VAR)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) d;
+
+ check_expr(estate, var->default_val);
+ }
+ }
+
+ check_stmts(estate, stmt_block->body);
+
+ if (stmt_block->exceptions)
+ {
+ foreach(l, stmt_block->exceptions->exc_list)
+ {
+ check_stmts(estate, ((PLpgSQL_exception *) lfirst(l))->action);
+ }
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_ASSIGN:
+ {
+ PLpgSQL_stmt_assign *stmt_assign = (PLpgSQL_stmt_assign *) stmt;
+
+ /* prepare plan if desn't exist yet */
+ prepare_expr(estate, stmt_assign->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_assign->expr,
+ false, /* no element type */
+ true, /* expand record */
+ true); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_target(estate, stmt_assign->varno);
+
+ /* assign a tupdesc to record variable */
+ assign_tupdesc_dno(estate, stmt_assign->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_IF:
+ {
+ PLpgSQL_stmt_if *stmt_if = (PLpgSQL_stmt_if *) stmt;
+ ListCell *l;
+
+ check_expr(estate, stmt_if->cond);
+
+ check_stmts(estate, stmt_if->then_body);
+
+ foreach(l, stmt_if->elsif_list)
+ {
+ PLpgSQL_if_elsif *elif = (PLpgSQL_if_elsif *) lfirst(l);
+
+ check_expr(estate, elif->cond);
+ check_stmts(estate, elif->stmts);
+ }
+
+ check_stmts(estate, stmt_if->else_body);
+ }
+ break;
+
+ case PLPGSQL_STMT_CASE:
+ {
+ PLpgSQL_stmt_case *stmt_case = (PLpgSQL_stmt_case *) stmt;
+ Oid result_oid;
+
+ if (stmt_case->t_expr != NULL)
+ {
+ PLpgSQL_var *t_var = (PLpgSQL_var *) estate->datums[stmt_case->t_varno];
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_case->t_expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_case->t_expr,
+ false, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ result_oid = tupdesc->attrs[0]->atttypid;
+
+ /*
+ * When expected datatype is different from real, change it. Note that
+ * what we're modifying here is an execution copy of the datum, so
+ * this doesn't affect the originally stored function parse tree.
+ */
+
+ if (t_var->datatype->typoid != result_oid)
+ t_var->datatype = plpgsql_build_datatype(result_oid,
+ -1,
+ estate->func->fn_input_collation);
+
+ ReleaseTupleDesc(tupdesc);
+ }
+
+ foreach(l, stmt_case->case_when_list)
+ {
+ PLpgSQL_case_when *cwt = (PLpgSQL_case_when *) lfirst(l);
+
+ check_expr(estate, cwt->expr);
+ check_stmts(estate, cwt->stmts);
+ }
+
+ check_stmts(estate, stmt_case->else_stmts);
+ }
+ break;
+
+ case PLPGSQL_STMT_LOOP:
+ check_stmts(estate, ((PLpgSQL_stmt_loop *) stmt)->body);
+ break;
+
+ case PLPGSQL_STMT_WHILE:
+ {
+ PLpgSQL_stmt_while *stmt_while = (PLpgSQL_stmt_while *) stmt;
+
+ check_expr(estate, stmt_while->cond);
+ check_stmts(estate, stmt_while->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORI:
+ {
+ PLpgSQL_stmt_fori *stmt_fori = (PLpgSQL_stmt_fori *) stmt;
+
+ check_expr(estate, stmt_fori->lower);
+ check_expr(estate, stmt_fori->upper);
+ check_expr(estate, stmt_fori->step);
+
+ check_stmts(estate, stmt_fori->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORS:
+ {
+ PLpgSQL_stmt_fors *stmt_fors = (PLpgSQL_stmt_fors *) stmt;
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_fors->query, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_fors->query,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_fors->row, stmt_fors->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fors->row, stmt_fors->rec, tupdesc);
+
+ check_stmts(estate, stmt_fors->body);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORC:
+ {
+ PLpgSQL_stmt_forc *stmt_forc = (PLpgSQL_stmt_forc *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_forc->curvar];
+
+ prepare_expr(estate, stmt_forc->argquery, 0);
+
+ if (var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_forc->row, stmt_forc->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_forc->row, stmt_forc->rec, tupdesc);
+ }
+
+ check_stmts(estate, stmt_forc->body);
+ if (tupdesc != NULL)
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNFORS:
+ {
+ PLpgSQL_stmt_dynfors * stmt_dynfors = (PLpgSQL_stmt_dynfors *) stmt;
+
+ if (stmt_dynfors->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_expr(estate, stmt_dynfors->query);
+
+ foreach(l, stmt_dynfors->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ check_stmts(estate, stmt_dynfors->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FOREACH_A:
+ {
+ PLpgSQL_stmt_foreach_a *stmt_foreach_a = (PLpgSQL_stmt_foreach_a *) stmt;
+
+ prepare_expr(estate, stmt_foreach_a->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_foreach_a->expr,
+ true, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ check_target(estate, stmt_foreach_a->varno);
+ assign_tupdesc_dno(estate, stmt_foreach_a->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+
+ check_stmts(estate, stmt_foreach_a->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_EXIT:
+ check_expr(estate, ((PLpgSQL_stmt_exit *) stmt)->cond);
+ break;
+
+ case PLPGSQL_STMT_PERFORM:
+ prepare_expr(estate, ((PLpgSQL_stmt_perform *) stmt)->expr, 0);
+ break;
+
+ case PLPGSQL_STMT_RETURN:
+ check_expr(estate, ((PLpgSQL_stmt_return *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_NEXT:
+ check_expr(estate, ((PLpgSQL_stmt_return_next *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_QUERY:
+ {
+ PLpgSQL_stmt_return_query *stmt_rq = (PLpgSQL_stmt_return_query *) stmt;
+
+ check_expr(estate, stmt_rq->dynquery);
+ prepare_expr(estate, stmt_rq->query, 0);
+
+ foreach(l, stmt_rq->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_RAISE:
+ {
+ PLpgSQL_stmt_raise *stmt_raise = (PLpgSQL_stmt_raise *) stmt;
+ ListCell *current_param;
+ char *cp;
+
+ foreach(l, stmt_raise->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ foreach(l, stmt_raise->options)
+ {
+ PLpgSQL_raise_option *opt = (PLpgSQL_raise_option *) lfirst(l);
+
+ check_expr(estate, opt->expr);
+ }
+
+ current_param = list_head(stmt_raise->params);
+
+ /* ensure any single % has a own parameter */
+ if (stmt_raise->message != NULL)
+ {
+ for (cp = stmt_raise->message; *cp; cp++)
+ {
+ if (cp[0] == '%')
+ {
+ if (cp[1] == '%')
+ {
+ cp++;
+ continue;
+ }
+
+ if (current_param == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too few parameters specified for RAISE")));
+
+ current_param = lnext(current_param);
+ }
+ }
+ }
+
+ if (current_param != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too many parameters specified for RAISE")));
+ }
+ break;
+
+ case PLPGSQL_STMT_EXECSQL:
+ {
+ PLpgSQL_stmt_execsql *stmt_execsql = (PLpgSQL_stmt_execsql *) stmt;
+
+ prepare_expr(estate, stmt_execsql->sqlstmt, 0);
+ if (stmt_execsql->into)
+ {
+ tupdesc = expr_get_desc(estate,
+ stmt_execsql->sqlstmt,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNEXECUTE:
+ {
+ PLpgSQL_stmt_dynexecute *stmt_dynexecute = (PLpgSQL_stmt_dynexecute *) stmt;
+
+ check_expr(estate, stmt_dynexecute->query);
+
+ foreach(l, stmt_dynexecute->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ if (stmt_dynexecute->into)
+ {
+ if (stmt_dynexecute->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_row_or_rec(estate, stmt_dynexecute->row, stmt_dynexecute->rec);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_OPEN:
+ {
+ PLpgSQL_stmt_open *stmt_open = (PLpgSQL_stmt_open *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_open->curvar];
+
+ if (var->cursor_explicit_expr)
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ prepare_expr(estate, stmt_open->query, 0);
+ prepare_expr(estate, stmt_open->argquery, 0);
+ check_expr(estate, stmt_open->dynquery);
+
+ foreach(l, stmt_open->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_GETDIAG:
+ {
+ PLpgSQL_stmt_getdiag *stmt_getdiag = (PLpgSQL_stmt_getdiag *) stmt;
+ ListCell *lc;
+
+ foreach(lc, stmt_getdiag->diag_items)
+ {
+ PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
+
+ check_target(estate, diag_item->target);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_FETCH:
+ {
+ PLpgSQL_stmt_fetch *stmt_fetch = (PLpgSQL_stmt_fetch *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *)(estate->datums[stmt_fetch->curvar]);
+
+ if (var != NULL && var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+ check_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_CLOSE:
+ break;
+
+ default:
+ elog(ERROR, "unrecognized cmd_type: %d", stmt->cmd_type);
+ return; /* be compiler quite */
+ }
+ }
+
+ /*
+ * Initialize variable to NULL
+ */
+ static void
+ var_init_to_null(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) estate->datums[varno];
+ var->value = (Datum) 0;
+ var->isnull = true;
+ var->freeval = false;
+ }
*** ./src/pl/plpgsql/src/pl_handler.c.orig 2011-12-07 05:59:37.158674309 +0100
--- ./src/pl/plpgsql/src/pl_handler.c 2011-12-07 06:00:07.365331632 +0100
***************
*** 312,314 ****
--- 312,452 ----
PG_RETURN_VOID();
}
+
+ /* ----------
+ * plpgsql_checker
+ *
+ * This function attempts to check a embeded SQL inside a PL/pgSQL function at
+ * CHECK FUNCTION time. It should to have one or two parameters. Second
+ * parameter is a relation (used when function is trigger).
+ * ----------
+ */
+ PG_FUNCTION_INFO_V1(plpgsql_checker);
+
+ Datum
+ plpgsql_checker(PG_FUNCTION_ARGS)
+ {
+ Oid funcoid = PG_GETARG_OID(0);
+ Oid relid = PG_GETARG_OID(1);
+ HeapTuple tuple;
+ FunctionCallInfoData fake_fcinfo;
+ FmgrInfo flinfo;
+ TriggerData trigdata;
+ int rc;
+ PLpgSQL_function *function;
+ PLpgSQL_execstate *cur_estate;
+
+ Form_pg_proc proc;
+ char functyptype;
+ bool istrigger = false;
+
+ /* we don't need to repair a check done by validator */
+
+ tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
+ if (!HeapTupleIsValid(tuple))
+ elog(ERROR, "cache lookup failed for function %u", funcoid);
+ proc = (Form_pg_proc) GETSTRUCT(tuple);
+
+ functyptype = get_typtype(proc->prorettype);
+
+ if (functyptype == TYPTYPE_PSEUDO)
+ {
+ /* we assume OPAQUE with no arguments means a trigger */
+ if (proc->prorettype == TRIGGEROID ||
+ (proc->prorettype == OPAQUEOID && proc->pronargs == 0))
+ {
+ istrigger = true;
+ if (!OidIsValid(relid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("PL/pgSQL trigger functions cannot be checked directly"),
+ errhint("use CHECK TRIGGER statement instead")));
+ }
+ }
+
+ /*
+ * Connect to SPI manager
+ */
+ if ((rc = SPI_connect()) != SPI_OK_CONNECT)
+ elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
+
+ /*
+ * Set up a fake fcinfo with just enough info to satisfy
+ * plpgsql_compile().
+ *
+ * there should be a different real argtypes for polymorphic params
+ */
+ MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo));
+ MemSet(&flinfo, 0, sizeof(flinfo));
+ fake_fcinfo.flinfo = &flinfo;
+ flinfo.fn_oid = funcoid;
+ flinfo.fn_mcxt = CurrentMemoryContext;
+
+ if (istrigger)
+ {
+ MemSet(&trigdata, 0, sizeof(trigdata));
+ trigdata.type = T_TriggerData;
+ trigdata.tg_relation = relation_open(relid, AccessShareLock);
+ fake_fcinfo.context = (Node *) &trigdata;
+ }
+
+ /* Get a compiled function */
+ function = plpgsql_compile(&fake_fcinfo, false);
+
+ /* Must save and restore prior value of cur_estate */
+ cur_estate = function->cur_estate;
+
+ /* Mark the function as busy, so it can't be deleted from under us */
+ function->use_count++;
+
+
+ /* Create a fake runtime environment and prepare plans */
+ PG_TRY();
+ {
+ if (!istrigger)
+ plpgsql_check_function(function, &fake_fcinfo);
+ else
+ plpgsql_check_trigger(function, &trigdata);
+ }
+ PG_CATCH();
+ {
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ /*
+ * Disconnect from SPI manager
+ */
+ if ((rc = SPI_finish()) != SPI_OK_FINISH)
+ elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
+
+ ReleaseSysCache(tuple);
+
+ PG_RETURN_VOID();
+ }
*** ./src/pl/plpgsql/src/plpgsql.h.orig 2011-12-07 05:59:37.160674287 +0100
--- ./src/pl/plpgsql/src/plpgsql.h 2011-12-07 06:00:07.366331620 +0100
***************
*** 902,907 ****
--- 902,908 ----
extern void plpgsql_adddatum(PLpgSQL_datum *new);
extern int plpgsql_add_initdatums(int **varnos);
extern void plpgsql_HashTableInit(void);
+ extern void plpgsql_delete_function(PLpgSQL_function *func);
/* ----------
* Functions in pl_handler.c
***************
*** 911,916 ****
--- 912,918 ----
extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_inline_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
+ extern Datum plpgsql_checker(PG_FUNCTION_ARGS);
/* ----------
* Functions in pl_exec.c
***************
*** 928,933 ****
--- 930,939 ----
extern void exec_get_datum_type_info(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum,
Oid *typeid, int32 *typmod, Oid *collation);
+ extern void plpgsql_check_function(PLpgSQL_function *func,
+ FunctionCallInfo fcinfo);
+ extern void plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata);
/* ----------
* Functions for namespace handling in pl_funcs.c
*** ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql.orig 2011-12-07 05:59:37.162674263 +0100
--- ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql 2011-12-07 06:00:07.366331620 +0100
***************
*** 5,7 ****
--- 5,8 ----
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_call_handler();
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_inline_handler(internal);
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_validator(oid);
+ ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_checker(oid, regclass);
*** ./src/test/regress/expected/plpgsql.out.orig 2011-12-07 05:59:37.164674240 +0100
--- ./src/test/regress/expected/plpgsql.out 2011-12-07 08:00:04.000000000 +0100
***************
*** 302,307 ****
--- 302,310 ----
' language plpgsql;
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
+ NOTICE: checked function "tg_hslot_biu()"
-- ************************************************************
-- * BEFORE DELETE on HSlot
-- * - prevent from manual manipulation
***************
*** 635,640 ****
--- 638,646 ----
raise exception ''illegal backlink beginning with %'', mytype;
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
+ NOTICE: checked function "tg_backlink_set(character,character)"
-- ************************************************************
-- * Support function to clear out the backlink field if
-- * it still points to specific slot
***************
*** 2802,2807 ****
--- 2808,2842 ----
(1 row)
+ -- check function should not fail
+ check function for_vect();
+ NOTICE: checked function "for_vect()"
+ -- recheck after check function
+ select for_vect();
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 4
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1 bb cc
+ NOTICE: 2 bb cc
+ NOTICE: 3 bb cc
+ NOTICE: 4 bb cc
+ for_vect
+ ----------
+
+ (1 row)
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 3283,3288 ****
--- 3318,3326 ----
return;
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
+ NOTICE: checked function "forc01()"
select forc01();
NOTICE: 5 from c
NOTICE: 6 from c
***************
*** 3716,3721 ****
--- 3754,3762 ----
end case;
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
+ NOTICE: checked function "case_test(bigint)"
select case_test(1);
case_test
-----------
***************
*** 4571,4573 ****
--- 4612,4956 ----
CONTEXT: PL/pgSQL function "testoa" line 5 at assignment
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+ --
+ -- check function statement tests
+ --
+ create table t1(a int, b int);
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ LINE 1: update t1 set c = 30
+ ^
+ DETAIL: column "c" of relation "t1" does not exist
+ QUERY: update t1 set c = 30
+ CONTEXT: line 4 at SQL statement
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "r" has no field "c"
+ CONTEXT: line 6 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ LINE 1: SELECT a + b
+ ^
+ DETAIL: column "a" does not exist
+ QUERY: SELECT a + b
+ CONTEXT: line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: too many parameters specified for RAISE
+ CONTEXT: line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: too few parameters specified for RAISE
+ CONTEXT: line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ LINE 1: SELECT c+10
+ ^
+ DETAIL: column "c" does not exist
+ QUERY: SELECT c+10
+ CONTEXT: line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: subscripted object is not an array
+ CONTEXT: line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "_exception" has no field "hint"
+ CONTEXT: line 7 at GET DIAGNOSTICS
+ drop function f1();
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ WARNING: error in function "f1_trg()"
+ DETAIL: record "new" has no field "c"
+ CONTEXT: SQL statement "SELECT new.c"
+ line 5 at RAISE
+ insert into t1 values(6,30);
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- should to fail
+ check trigger t1_f1 on t1;
+ WARNING: error in function "f1_trg()"
+ DETAIL: record "new" has no field "c"
+ CONTEXT: line 5 at assignment
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+ ERROR: record "new" has no field "c"
+ CONTEXT: PL/pgSQL function "f1_trg" line 5 at assignment
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- ok
+ check trigger t1_f1 on t1;
+ NOTICE: checked function "f1_trg()"
+ -- ok
+ insert into t1 values(6,30);
+ drop table t1;
+ drop type _exception_type;
+ drop function f1_trg();
*** ./src/test/regress/sql/plpgsql.sql.orig 2011-12-07 05:59:37.166674218 +0100
--- ./src/test/regress/sql/plpgsql.sql 2011-12-07 06:00:07.371331565 +0100
***************
*** 366,371 ****
--- 366,373 ----
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
-- ************************************************************
-- * BEFORE DELETE on HSlot
***************
*** 747,752 ****
--- 749,757 ----
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
+
-- ************************************************************
-- * Support function to clear out the backlink field if
***************
*** 2335,2340 ****
--- 2340,2352 ----
select for_vect();
+ -- check function should not fail
+ check function for_vect();
+
+ -- recheck after check function
+ select for_vect();
+
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 2714,2719 ****
--- 2726,2734 ----
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
+
select forc01();
-- try updating the cursor's current row
***************
*** 3048,3053 ****
--- 3063,3071 ----
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
+
select case_test(1);
select case_test(2);
select case_test(3);
***************
*** 3600,3602 ****
--- 3618,3862 ----
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+
+ --
+ -- check function statement tests
+ --
+
+ create table t1(a int, b int);
+
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+
+ drop function f1();
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- should to fail
+ check trigger t1_f1 on t1;
+
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- ok
+ check trigger t1_f1 on t1;
+
+ -- ok
+ insert into t1 values(6,30);
+
+ drop table t1;
+ drop type _exception_type;
+
+ drop function f1_trg();
+
Pavel Stehule wrote:
updated version
changes:
* CHECK FUNCTION ALL; is enabled - in this case functions from
pg_catalog schema are ignoredI looked on parser, and I didn't other changes there - IN SCHEMA, FOR
ROLE are used more time there, so our usage will be consistent
a small addition
* don't check SQL functions - are checked well now
* don't check functions from information_schema too
One hunk in the patch fails due to conflict with
commit d5f23af6bfbc454e86dd16e5c7a0bfc0cf6189d0
(Peter Eisentraut's const patch).
There are also compiler warnings about discarded const
qualifiers in backend/nodes/copyfuncs.c,
backend/nodes/equalfuncs.c and backend/parser/gram.y.
There is a bug when ALL IN SCHEMA or ALL IN LANGUAGE
is used:
test=> CHECK FUNCTION ALL IN LANGUAGE plpgsql;
ERROR: language "language" does not exist
test=> CHECK FUNCTION ALL IN SCHEMA laurenz;
ERROR: schema "schema" does not exist
Something gets mixed up here.
I like the idea that CHECK FUNCTION ALL without additional
clauses works and ignores pg_catalog and information_schema!
I'm working on some documentation, but it won't be final before
the functionality is agreed upon.
Yours,
Laurenz Albe
Hello
there is fixed version
Regards
Pavel
2011/12/9 Albe Laurenz <laurenz.albe@wien.gv.at>:
Show quoted text
Pavel Stehule wrote:
updated version
changes:
* CHECK FUNCTION ALL; is enabled - in this case functions from
pg_catalog schema are ignoredI looked on parser, and I didn't other changes there - IN SCHEMA, FOR
ROLE are used more time there, so our usage will be consistenta small addition
* don't check SQL functions - are checked well now
* don't check functions from information_schema tooOne hunk in the patch fails due to conflict with
commit d5f23af6bfbc454e86dd16e5c7a0bfc0cf6189d0
(Peter Eisentraut's const patch).There are also compiler warnings about discarded const
qualifiers in backend/nodes/copyfuncs.c,
backend/nodes/equalfuncs.c and backend/parser/gram.y.There is a bug when ALL IN SCHEMA or ALL IN LANGUAGE
is used:test=> CHECK FUNCTION ALL IN LANGUAGE plpgsql;
ERROR: language "language" does not exist
test=> CHECK FUNCTION ALL IN SCHEMA laurenz;
ERROR: schema "schema" does not existSomething gets mixed up here.
I like the idea that CHECK FUNCTION ALL without additional
clauses works and ignores pg_catalog and information_schema!I'm working on some documentation, but it won't be final before
the functionality is agreed upon.Yours,
Laurenz Albe
Attachments:
check_function-2011-12-09-2.difftext/x-patch; charset=US-ASCII; name=check_function-2011-12-09-2.diffDownload
*** ./doc/src/sgml/catalogs.sgml.orig 2011-12-09 15:15:09.164913469 +0100
--- ./doc/src/sgml/catalogs.sgml 2011-12-09 15:15:43.219637443 +0100
***************
*** 3652,3657 ****
--- 3652,3668 ----
</row>
<row>
+ <entry><structfield>lanchecker</structfield></entry>
+ <entry><type>oid</type></entry>
+ <entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
+ <entry>
+ This references a language checker function that is responsible
+ for checking a embedded SQL and can provide detailed checking.
+ Zero if no checker is provided.
+ </entry>
+ </row>
+
+ <row>
<entry><structfield>lanacl</structfield></entry>
<entry><type>aclitem[]</type></entry>
<entry></entry>
*** ./doc/src/sgml/ref/allfiles.sgml.orig 2011-12-09 15:15:09.165913461 +0100
--- ./doc/src/sgml/ref/allfiles.sgml 2011-12-09 15:15:43.219637443 +0100
***************
*** 40,45 ****
--- 40,46 ----
<!ENTITY alterView SYSTEM "alter_view.sgml">
<!ENTITY analyze SYSTEM "analyze.sgml">
<!ENTITY begin SYSTEM "begin.sgml">
+ <!ENTITY checkFunction SYSTEM "check_function.sgml">
<!ENTITY checkpoint SYSTEM "checkpoint.sgml">
<!ENTITY close SYSTEM "close.sgml">
<!ENTITY cluster SYSTEM "cluster.sgml">
*** ./doc/src/sgml/ref/create_language.sgml.orig 2011-12-09 15:15:09.167913445 +0100
--- ./doc/src/sgml/ref/create_language.sgml 2011-12-09 15:15:43.264637074 +0100
***************
*** 23,29 ****
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
--- 23,29 ----
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ] [ CHECK <replaceable>checkfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
***************
*** 217,222 ****
--- 217,236 ----
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><literal>CHECK</literal> <replaceable class="parameter">checkfunction</replaceable></term>
+
+ <listitem>
+ <para><replaceable class="parameter">checkfunction</replaceable> is the
+ name of a previously registered function that will be called
+ when a new function in the language is created, to check the
+ function by statemnt <command>CHECK FUNCTION</command> or
+ <command>CHECK TRIGGER</command>.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
*** ./doc/src/sgml/reference.sgml.orig 2011-12-09 15:15:09.168913437 +0100
--- ./doc/src/sgml/reference.sgml 2011-12-09 15:15:43.264637074 +0100
***************
*** 68,73 ****
--- 68,74 ----
&alterView;
&analyze;
&begin;
+ &checkFunction;
&checkpoint;
&close;
&cluster;
*** ./doc/src/sgml/ref/check_function.sgml.orig 2011-12-09 15:15:36.767690075 +0100
--- ./doc/src/sgml/ref/check_function.sgml 2011-12-09 15:15:43.265637066 +0100
***************
*** 0 ****
--- 1,38 ----
+ <!--
+ doc/src/sgml/ref/check_function.sgml
+ -->
+
+ <refentry id="SQL-CHECKFUNCTION">
+ <refmeta>
+ <refentrytitle>CHECK FUNCTION</refentrytitle>
+ <manvolnum>7</manvolnum>
+ <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+ <refname>CHECK FUNCTION</refname>
+ <refpurpose>ensure a deep checking of existing function</refpurpose>
+ </refnamediv>
+
+ <indexterm zone="sql-checkfunction">
+ <primary>CHECK FUNCTION</primary>
+ </indexterm>
+
+ <refsynopsisdiv>
+ <synopsis>
+ CHECK FUNCTION <replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] )
+ | CHECK FUNCTION ALL [ IN LANGUAGE <replaceable class="parameter">langname</replaceable> ] [ IN SCHEMA <replaceable class="parameter">schemaname</replaceable> ] [ FOR ROLE <replaceable class="parameter">ownername</replaceable> ]
+ | CHECK TRIGGER <replaceable class="parameter">name</replaceable> ON <replaceable class="parameter">tablename</replaceable>
+ </synopsis>
+ </refsynopsisdiv>
+
+ <refsect1 id="sql-checkfunction-description">
+ <title>Description</title>
+
+ <para>
+ <command>CHECK FUNCTION</command> check a existing function.
+ <command>CHECK TRIGGER</command> check a trigger function.
+ </para>
+ </refsect1>
+
+ </refentry>
*** ./src/backend/catalog/pg_proc.c.orig 2011-12-09 15:15:09.171913413 +0100
--- ./src/backend/catalog/pg_proc.c 2011-12-09 15:15:43.266637058 +0100
***************
*** 1101,1103 ****
--- 1101,1104 ----
*newcursorpos = newcp;
return false;
}
+
*** ./src/backend/commands/functioncmds.c.orig 2011-12-09 15:15:09.173913397 +0100
--- ./src/backend/commands/functioncmds.c 2011-12-09 15:15:43.267637050 +0100
***************
*** 35,53 ****
--- 35,58 ----
#include "access/genam.h"
#include "access/heapam.h"
#include "access/sysattr.h"
+ #include "access/xact.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_language.h"
+ #include "catalog/namespace.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_proc_fn.h"
+ #include "catalog/pg_trigger.h"
#include "catalog/pg_type.h"
#include "catalog/pg_type_fn.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
+ #include "commands/trigger.h"
+ #include "executor/spi_priv.h"
#include "miscadmin.h"
#include "optimizer/var.h"
#include "parser/parse_coerce.h"
***************
*** 60,66 ****
--- 65,73 ----
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
+ #include "utils/memutils.h"
#include "utils/rel.h"
+ #include "utils/resowner.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
***************
*** 1009,1014 ****
--- 1016,1393 ----
}
}
+ /*
+ * Search and execute related checker function
+ */
+ static void
+ CheckFunctionById(Oid funcOid, Oid relid)
+ {
+ HeapTuple tup;
+ Form_pg_proc proc;
+ HeapTuple languageTuple;
+ Form_pg_language languageStruct;
+ Oid languageChecker;
+ bool found_bug;
+ char *funcname;
+
+ tup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
+ if (!HeapTupleIsValid(tup)) /* should not happen */
+ elog(ERROR, "cache lookup failed for function %u", funcOid);
+
+ proc = (Form_pg_proc) GETSTRUCT(tup);
+
+ languageTuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(proc->prolang));
+ Assert(HeapTupleIsValid(languageTuple));
+
+ languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
+ languageChecker = languageStruct->lanchecker;
+
+ funcname = format_procedure(funcOid);
+
+ /* Check a function body */
+ if (OidIsValid(languageChecker))
+ {
+ ArrayType *set_items = NULL;
+ int save_nestlevel = 0;
+ Datum datum;
+ bool isnull;
+ MemoryContext oldCxt;
+ MemoryContext checkCxt;
+ ResourceOwner oldowner;
+
+ datum = SysCacheGetAttr(PROCOID, tup, Anum_pg_proc_proconfig, &isnull);
+
+ if (!isnull)
+ {
+ /* Set per-function configuration parameters */
+ set_items = (ArrayType *) DatumGetPointer(datum);
+ if (set_items) /* Need a new GUC nesting level */
+ {
+ save_nestlevel = NewGUCNestLevel();
+ ProcessGUCArray(set_items,
+ (superuser() ? PGC_SUSET : PGC_USERSET),
+ PGC_S_SESSION,
+ GUC_ACTION_SAVE);
+ }
+ else
+ save_nestlevel = 0; /* keep compiler quiet */
+ }
+
+ checkCxt = AllocSetContextCreate(CurrentMemoryContext,
+ "Check temporary context",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
+
+ oldCxt = MemoryContextSwitchTo(checkCxt);
+
+ oldowner = CurrentResourceOwner;
+ BeginInternalSubTransaction(NULL);
+ MemoryContextSwitchTo(checkCxt);
+
+ /*
+ * forward exception to warning
+ */
+ PG_TRY();
+ {
+ OidFunctionCall2(languageChecker, ObjectIdGetDatum(funcOid),
+ ObjectIdGetDatum(relid));
+
+ RollbackAndReleaseCurrentSubTransaction();
+ MemoryContextSwitchTo(checkCxt);
+ CurrentResourceOwner = oldowner;
+
+ found_bug = false;
+ }
+ PG_CATCH();
+ {
+ ErrorData *edata;
+
+ MemoryContextSwitchTo(checkCxt);
+ edata = CopyErrorData();
+ FlushErrorState();
+
+ RollbackAndReleaseCurrentSubTransaction();
+ MemoryContextSwitchTo(checkCxt);
+ CurrentResourceOwner = oldowner;
+
+ edata->elevel = WARNING;
+
+ ereport(WARNING,
+ (errmsg("error in function \"%s\"", funcname),
+ edata->message != NULL ? errdetail_internal("%s", edata->message) : 0,
+ edata->context != NULL ? errcontext("%s",edata->context) : 0,
+ edata->internalquery != NULL ? internalerrquery(edata->internalquery) : 0,
+ internalerrposition(edata->internalpos)));
+
+ FreeErrorData(edata);
+
+ found_bug = true;
+ }
+ PG_END_TRY();
+
+ MemoryContextSwitchTo(oldCxt);
+
+ if (set_items)
+ AtEOXact_GUC(true, save_nestlevel);
+
+ if (!found_bug)
+ elog(NOTICE, "checked function \"%s\"",
+ funcname);
+ }
+ else
+ elog(NOTICE, "skip check function \"%s\", language \"%s\" hasn't checker function",
+ funcname,
+ NameStr(languageStruct->lanname));
+
+ pfree(funcname);
+
+ ReleaseSysCache(languageTuple);
+ ReleaseSysCache(tup);
+ }
+
+ /*
+ * CheckFunction
+ * call a PL checker function when this function exists.
+ */
+ void
+ CheckFunction(CheckFunctionStmt *stmt)
+ {
+ List *functionName = stmt->funcname;
+ List *argTypes = stmt->args; /* list of TypeName nodes */
+ Oid funcOid = InvalidOid;
+ HeapTuple tup;
+ Oid trgOid = InvalidOid;
+ Oid relid = InvalidOid;
+ Oid languageId;
+ int nkeys = 0;
+ List *objects = NIL;
+
+
+ if (stmt->funcname != NULL)
+ {
+ /*
+ * Find the function,
+ */
+ funcOid = LookupFuncNameTypeNames(functionName, argTypes, false);
+ }
+ else if (stmt->trgname != NULL)
+ {
+ HeapTuple ht_trig;
+ Form_pg_trigger trigrec;
+ ScanKeyData skey[1];
+ Relation tgrel;
+ SysScanDesc tgscan;
+
+ /* find a trigger function */
+ relid = RangeVarGetRelid(stmt->relation, ShareLock, false);
+ trgOid = get_trigger_oid(relid, stmt->trgname, false);
+
+ /*
+ * Fetch the pg_trigger tuple by the Oid of the trigger
+ */
+ tgrel = heap_open(TriggerRelationId, AccessShareLock);
+
+ ScanKeyInit(&skey[0],
+ ObjectIdAttributeNumber,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(trgOid));
+
+ tgscan = systable_beginscan(tgrel, TriggerOidIndexId, true,
+ SnapshotNow, 1, skey);
+
+ ht_trig = systable_getnext(tgscan);
+
+ if (!HeapTupleIsValid(ht_trig))
+ elog(ERROR, "could not find tuple for trigger %u", trgOid);
+
+ trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
+
+ /* we need to know trigger function to get PL checker function */
+ funcOid = trigrec->tgfoid;
+
+ /* Clean up */
+ systable_endscan(tgscan);
+
+ heap_close(tgrel, AccessShareLock);
+ }
+ else
+ {
+ ScanKeyData key[5];
+ Relation rel;
+ HeapScanDesc scan;
+ ListCell *option;
+ bool language_opt = false;
+ bool schema_opt = false;
+ bool owner_opt = false;
+
+ /*
+ * when Check stmt is multiple statement, then
+ * prepare list of processed functions.
+ */
+ foreach(option, stmt->options)
+ {
+ DefElem *defel = (DefElem *) lfirst(option);
+
+ if (strcmp(defel->defname, "language") == 0)
+ {
+ HeapTuple languageTuple;
+ Form_pg_language languageStruct;
+ Oid languageChecker;
+ char *language = defGetString(defel);
+
+ if (language_opt)
+ elog(ERROR, "multiple usage of IN LANGUAGE option");
+ language_opt = true;
+
+ languageTuple = SearchSysCache1(LANGNAME, PointerGetDatum(language));
+ if (!HeapTupleIsValid(languageTuple))
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_OBJECT),
+ errmsg("language \"%s\" does not exist", language),
+ (PLTemplateExists(language) ?
+ errhint("Use CREATE LANGUAGE to load the language into the database.") : 0)));
+
+ languageId = HeapTupleGetOid(languageTuple);
+ if (languageId == INTERNALlanguageId || languageId == ClanguageId)
+ elog(ERROR, "cannot to check functions in C or internal languages");
+
+ languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
+ languageChecker = languageStruct->lanchecker;
+ if (!OidIsValid(languageChecker))
+ elog(ERROR, "language \"%s\" has no defined checker function",
+ language);
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_prolang,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(languageId));
+
+ ReleaseSysCache(languageTuple);
+ }
+ else if (strcmp(defel->defname, "schema") == 0)
+ {
+ Oid namespaceId = LookupExplicitNamespace(defGetString(defel));
+
+ if (schema_opt)
+ elog(ERROR, "multiple usage of IN SCHEMA option");
+ schema_opt = true;
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_pronamespace,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(namespaceId));
+
+ }
+ else if (strcmp(defel->defname, "owner") == 0)
+ {
+ Oid ownerId = get_role_oid(defGetString(defel), false);
+
+ if (owner_opt)
+ elog(ERROR, "multiple usage of FOR ROLE option");
+ owner_opt = true;
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_proowner,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(ownerId));
+ }
+ else
+ elog(ERROR, "option \"%s\" not recognized",
+ defel->defname);
+ }
+
+ /*
+ * We don't would to iterate over pg_catalog functions without
+ * explicit request and information_schema.
+ */
+ if (!schema_opt)
+ {
+ Oid information_schemaId = LookupExplicitNamespace("information_schema");
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_pronamespace,
+ BTEqualStrategyNumber, F_OIDNE,
+ ObjectIdGetDatum(PG_CATALOG_NAMESPACE));
+
+ ScanKeyInit(&key[nkeys++],
+ Anum_pg_proc_pronamespace,
+ BTEqualStrategyNumber, F_OIDNE,
+ ObjectIdGetDatum(information_schemaId));
+ }
+
+ rel = heap_open(ProcedureRelationId,AccessShareLock);
+ scan = heap_beginscan(rel, SnapshotNow, nkeys, key);
+
+ while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
+ {
+ Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(tup);
+
+ funcOid = HeapTupleGetOid(tup);
+
+ /* when language is not specified, then check checker */
+ if (!language_opt)
+ {
+ Oid prolang = proc->prolang;
+
+ /* skip C, internal or SQL */
+ if (prolang == INTERNALlanguageId)
+ {
+ elog(NOTICE, "skip check function \"%s\", it uses internal language",
+ format_procedure(funcOid));
+ continue;
+ }
+ else if (prolang == ClanguageId)
+ {
+ elog(NOTICE, "skip check function \"%s\", uses C language",
+ format_procedure(funcOid));
+ continue;
+ }
+ else if (prolang == SQLlanguageId)
+ {
+ elog(NOTICE, "skip check function \"%s\", uses SQL language",
+ format_procedure(funcOid));
+ continue;
+ }
+ }
+
+ if (proc->prorettype == TRIGGEROID)
+ {
+ elog(NOTICE, "skip check function \"%s\", it is trigger function",
+ format_procedure(funcOid));
+ continue;
+ }
+
+ objects = lappend_oid(objects, funcOid);
+ }
+
+ heap_endscan(scan);
+ heap_close(rel, AccessShareLock);
+ }
+
+ if (funcOid == InvalidOid && objects == NIL)
+ {
+ elog(NOTICE, "nothing to check");
+ return;
+ }
+
+ if (objects != NIL)
+ {
+ ListCell *object;
+
+ foreach(object, objects)
+ {
+ CHECK_FOR_INTERRUPTS();
+
+ funcOid = lfirst_oid(object);
+ CheckFunctionById(funcOid, InvalidOid);
+ }
+ }
+ else
+ {
+ CheckFunctionById(funcOid, relid);
+ }
+ }
/*
* Rename function
*** ./src/backend/commands/proclang.c.orig 2011-12-09 15:15:09.174913389 +0100
--- ./src/backend/commands/proclang.c 2011-12-09 15:15:43.269637034 +0100
***************
*** 46,57 ****
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
--- 46,58 ----
char *tmplhandler; /* name of handler function */
char *tmplinline; /* name of anonymous-block handler, or NULL */
char *tmplvalidator; /* name of validator function, or NULL */
+ char *tmplchecker; /* name of checker function, or NULL */
char *tmpllibrary; /* path of shared library */
} PLTemplate;
static void create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted);
static PLTemplate *find_language_template(const char *languageName);
static void AlterLanguageOwner_internal(HeapTuple tup, Relation rel,
Oid newOwnerId);
***************
*** 67,75 ****
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid;
Oid funcrettype;
! Oid funcargtypes[1];
/*
* If we have template information for the language, ignore the supplied
--- 68,77 ----
PLTemplate *pltemplate;
Oid handlerOid,
inlineOid,
! valOid,
! checkerOid;
Oid funcrettype;
! Oid funcargtypes[2];
/*
* If we have template information for the language, ignore the supplied
***************
*** 219,228 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, pltemplate->tmpltrusted);
}
else
{
--- 221,269 ----
else
valOid = InvalidOid;
+ /*
+ * Likewise for the checker, if required; but we don't care about
+ * its return type.
+ */
+ if (pltemplate->tmplchecker)
+ {
+ funcname = SystemFuncName(pltemplate->tmplchecker);
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(funcname, 2, funcargtypes, true);
+ if (!OidIsValid(checkerOid))
+ {
+ checkerOid = ProcedureCreate(pltemplate->tmplchecker,
+ PG_CATALOG_NAMESPACE,
+ false, /* replace */
+ false, /* returnsSet */
+ VOIDOID,
+ ClanguageId,
+ F_FMGR_C_VALIDATOR,
+ pltemplate->tmplchecker,
+ pltemplate->tmpllibrary,
+ false, /* isAgg */
+ false, /* isWindowFunc */
+ false, /* security_definer */
+ true, /* isStrict */
+ PROVOLATILE_VOLATILE,
+ buildoidvector(funcargtypes, 2),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ PointerGetDatum(NULL),
+ NIL,
+ PointerGetDatum(NULL),
+ 1,
+ 0);
+ }
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, pltemplate->tmpltrusted);
}
else
{
***************
*** 294,303 ****
else
valOid = InvalidOid;
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, stmt->pltrusted);
}
}
--- 335,355 ----
else
valOid = InvalidOid;
+ /* validate the checker function */
+ if (stmt->plchecker)
+ {
+ funcargtypes[0] = OIDOID;
+ funcargtypes[1] = REGCLASSOID;
+ checkerOid = LookupFuncName(stmt->plchecker, 2, funcargtypes, false);
+ /* return value is ignored, so we don't check the type */
+ }
+ else
+ checkerOid = InvalidOid;
+
/* ok, create it */
create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
handlerOid, inlineOid,
! valOid, checkerOid, stmt->pltrusted);
}
}
***************
*** 307,313 ****
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
--- 359,365 ----
static void
create_proc_lang(const char *languageName, bool replace,
Oid languageOwner, Oid handlerOid, Oid inlineOid,
! Oid valOid, Oid checkerOid, bool trusted)
{
Relation rel;
TupleDesc tupDesc;
***************
*** 337,342 ****
--- 389,395 ----
values[Anum_pg_language_lanplcallfoid - 1] = ObjectIdGetDatum(handlerOid);
values[Anum_pg_language_laninline - 1] = ObjectIdGetDatum(inlineOid);
values[Anum_pg_language_lanvalidator - 1] = ObjectIdGetDatum(valOid);
+ values[Anum_pg_language_lanchecker - 1] = ObjectIdGetDatum(checkerOid);
nulls[Anum_pg_language_lanacl - 1] = true;
/* Check for pre-existing definition */
***************
*** 423,428 ****
--- 476,490 ----
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
}
+ /* dependency on the checker function, if any */
+ if (OidIsValid(checkerOid))
+ {
+ referenced.classId = ProcedureRelationId;
+ referenced.objectId = checkerOid;
+ referenced.objectSubId = 0;
+ recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
+ }
+
/* Post creation hook for new procedural language */
InvokeObjectAccessHook(OAT_POST_CREATE,
LanguageRelationId, myself.objectId, 0);
***************
*** 478,483 ****
--- 540,550 ----
if (!isnull)
result->tmplvalidator = TextDatumGetCString(datum);
+ datum = heap_getattr(tup, Anum_pg_pltemplate_tmplchecker,
+ RelationGetDescr(rel), &isnull);
+ if (!isnull)
+ result->tmplchecker = TextDatumGetCString(datum);
+
datum = heap_getattr(tup, Anum_pg_pltemplate_tmpllibrary,
RelationGetDescr(rel), &isnull);
if (!isnull)
*** ./src/backend/nodes/copyfuncs.c.orig 2011-12-09 14:29:07.861472089 +0100
--- ./src/backend/nodes/copyfuncs.c 2011-12-09 15:15:43.271637018 +0100
***************
*** 2880,2885 ****
--- 2880,2900 ----
return newnode;
}
+ static CheckFunctionStmt *
+ _copyCheckFunctionStmt(CheckFunctionStmt *from)
+ {
+ CheckFunctionStmt *newnode = makeNode(CheckFunctionStmt);
+
+ COPY_NODE_FIELD(funcname);
+ COPY_NODE_FIELD(args);
+ COPY_STRING_FIELD(trgname);
+ COPY_NODE_FIELD(relation);
+ COPY_SCALAR_FIELD(is_function);
+ COPY_NODE_FIELD(options);
+
+ return newnode;
+ }
+
static DoStmt *
_copyDoStmt(const DoStmt *from)
{
***************
*** 4165,4170 ****
--- 4180,4188 ----
case T_AlterFunctionStmt:
retval = _copyAlterFunctionStmt(from);
break;
+ case T_CheckFunctionStmt:
+ retval = _copyCheckFunctionStmt(from);
+ break;
case T_DoStmt:
retval = _copyDoStmt(from);
break;
*** ./src/backend/nodes/equalfuncs.c.orig 2011-12-09 14:29:07.861472089 +0100
--- ./src/backend/nodes/equalfuncs.c 2011-12-09 15:18:32.018205894 +0100
***************
*** 1292,1297 ****
--- 1292,1310 ----
}
static bool
+ _equalCheckFunctionStmt(CheckFunctionStmt *a, CheckFunctionStmt *b)
+ {
+ COMPARE_NODE_FIELD(funcname);
+ COMPARE_NODE_FIELD(args);
+ COMPARE_STRING_FIELD(trgname);
+ COMPARE_NODE_FIELD(relation);
+ COMPARE_SCALAR_FIELD(is_function);
+ COMPARE_NODE_FIELD(options);
+
+ return true;
+ }
+
+ static bool
_equalDoStmt(const DoStmt *a, const DoStmt *b)
{
COMPARE_NODE_FIELD(args);
***************
*** 2708,2713 ****
--- 2721,2729 ----
case T_AlterFunctionStmt:
retval = _equalAlterFunctionStmt(a, b);
break;
+ case T_CheckFunctionStmt:
+ retval = _equalCheckFunctionStmt(a, b);
+ break;
case T_DoStmt:
retval = _equalDoStmt(a, b);
break;
*** ./src/backend/parser/gram.y.orig 2011-12-09 15:15:09.179913349 +0100
--- ./src/backend/parser/gram.y 2011-12-09 15:15:43.280636943 +0100
***************
*** 227,232 ****
--- 227,233 ----
DeallocateStmt PrepareStmt ExecuteStmt
DropOwnedStmt ReassignOwnedStmt
AlterTSConfigurationStmt AlterTSDictionaryStmt
+ CheckFunctionStmt
%type <node> select_no_parens select_with_parens select_clause
simple_select values_clause
***************
*** 276,282 ****
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate
%type <range> qualified_name OptConstrFromTable
--- 277,283 ----
%type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
opt_class opt_inline_handler opt_validator validator_clause
! opt_collate opt_checker
%type <range> qualified_name OptConstrFromTable
***************
*** 463,469 ****
%type <windef> window_definition over_clause window_specification
opt_frame_clause frame_extent frame_bound
%type <str> opt_existing_window_name
!
/*
* Non-keyword token types. These are hard-wired into the "flex" lexer.
--- 464,471 ----
%type <windef> window_definition over_clause window_specification
opt_frame_clause frame_extent frame_bound
%type <str> opt_existing_window_name
! %type <defelt> CheckFunctionOptElem
! %type <list> CheckFunctionOpts
/*
* Non-keyword token types. These are hard-wired into the "flex" lexer.
***************
*** 700,705 ****
--- 702,708 ----
| AlterUserSetStmt
| AlterUserStmt
| AnalyzeStmt
+ | CheckFunctionStmt
| CheckPointStmt
| ClosePortalStmt
| ClusterStmt
***************
*** 3174,3184 ****
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
--- 3177,3188 ----
n->plhandler = NIL;
n->plinline = NIL;
n->plvalidator = NIL;
+ n->plchecker = NIL;
n->pltrusted = false;
$$ = (Node *)n;
}
| CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
! HANDLER handler_name opt_inline_handler opt_validator opt_checker
{
CreatePLangStmt *n = makeNode(CreatePLangStmt);
n->replace = $2;
***************
*** 3186,3191 ****
--- 3190,3196 ----
n->plhandler = $8;
n->plinline = $9;
n->plvalidator = $10;
+ n->plchecker = $11;
n->pltrusted = $3;
$$ = (Node *)n;
}
***************
*** 3220,3225 ****
--- 3225,3235 ----
| /*EMPTY*/ { $$ = NIL; }
;
+ opt_checker:
+ CHECK handler_name { $$ = $2; }
+ | /*EMPTY*/ { $$ = NIL; }
+ ;
+
DropPLangStmt:
DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
{
***************
*** 6250,6255 ****
--- 6260,6344 ----
/*****************************************************************************
*
+ * CHECK FUNCTION funcname(args)
+ * CHECK TRIGGER triggername ON table
+ *
+ *
+ *****************************************************************************/
+
+
+ CheckFunctionStmt:
+ CHECK FUNCTION func_name func_args
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = $3;
+ n->args = extractArgTypes($4);
+ n->trgname = NULL;
+ n->relation = NULL;
+ n->is_function = true;
+ n->options = NIL;
+ $$ = (Node *) n;
+ }
+ | CHECK TRIGGER name ON qualified_name
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = $3;
+ n->relation = $5;
+ n->is_function = false;
+ n->options = NIL;
+ $$ = (Node *) n;
+ }
+ | CHECK FUNCTION ALL
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = NULL;
+ n->relation = NULL;
+ n->is_function = true;
+ n->options = NIL;
+ $$ = (Node *) n;
+ }
+ | CHECK FUNCTION ALL CheckFunctionOpts
+ {
+ CheckFunctionStmt *n = makeNode(CheckFunctionStmt);
+ n->funcname = NULL;
+ n->args = NIL;
+ n->trgname = NULL;
+ n->relation = NULL;
+ n->is_function = true;
+ n->options = $4;
+ $$ = (Node *) n;
+ }
+ ;
+
+ CheckFunctionOpts:
+ CheckFunctionOptElem { $$ = list_make1($1); }
+ | CheckFunctionOpts CheckFunctionOptElem { $$ = lappend($1, $2); }
+ ;
+
+ CheckFunctionOptElem:
+ IN_P LANGUAGE ColId
+ {
+ $$ = makeDefElem("language",
+ (Node *) makeString($3));
+ }
+ | IN_P SCHEMA ColId
+ {
+ $$ = makeDefElem("schema",
+ (Node *) makeString($3));
+ }
+ | FOR ROLE ColId
+ {
+ $$ = makeDefElem("owner",
+ (Node *) makeString($3));
+ }
+ ;
+
+ /*****************************************************************************
+ *
* DO <anonymous code block> [ LANGUAGE language ]
*
* We use a DefElem list for future extensibility, and to allow flexibility
*** ./src/backend/tcop/utility.c.orig 2011-12-09 15:15:09.180913341 +0100
--- ./src/backend/tcop/utility.c 2011-12-09 15:15:43.314636666 +0100
***************
*** 882,887 ****
--- 882,891 ----
AlterFunction((AlterFunctionStmt *) parsetree);
break;
+ case T_CheckFunctionStmt:
+ CheckFunction((CheckFunctionStmt *) parsetree);
+ break;
+
case T_IndexStmt: /* CREATE INDEX */
{
IndexStmt *stmt = (IndexStmt *) parsetree;
***************
*** 2125,2130 ****
--- 2129,2141 ----
}
break;
+ case T_CheckFunctionStmt:
+ if (((CheckFunctionStmt *) parsetree)->is_function)
+ tag = "CHECK FUNCTION";
+ else
+ tag = "CHECK TRIGGER";
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
***************
*** 2565,2570 ****
--- 2576,2585 ----
}
break;
+ case T_CheckFunctionStmt:
+ lev = LOGSTMT_ALL;
+ break;
+
default:
elog(WARNING, "unrecognized node type: %d",
(int) nodeTag(parsetree));
*** ./src/bin/pg_dump/pg_dump.c.orig 2011-12-09 15:15:09.182913325 +0100
--- ./src/bin/pg_dump/pg_dump.c 2011-12-09 15:15:43.320636618 +0100
***************
*** 5326,5338 ****
int i_lanplcallfoid;
int i_laninline;
int i_lanvalidator;
int i_lanacl;
int i_lanowner;
/* Make sure we are in proper schema */
selectSourceSchema("pg_catalog");
! if (g_fout->remoteVersion >= 90000)
{
/* pg_language has a laninline column */
appendPQExpBuffer(query, "SELECT tableoid, oid, "
--- 5326,5351 ----
int i_lanplcallfoid;
int i_laninline;
int i_lanvalidator;
+ int i_lanchecker;
int i_lanacl;
int i_lanowner;
/* Make sure we are in proper schema */
selectSourceSchema("pg_catalog");
! if (g_fout->remoteVersion >= 90200)
! {
! /* pg_language has a lanchecker column */
! appendPQExpBuffer(query, "SELECT tableoid, oid, "
! "lanname, lanpltrusted, lanplcallfoid, "
! "laninline, lanvalidator, lanchecker, lanacl, "
! "(%s lanowner) AS lanowner "
! "FROM pg_language "
! "WHERE lanispl "
! "ORDER BY oid",
! username_subquery);
! }
! else if (g_fout->remoteVersion >= 90000)
{
/* pg_language has a laninline column */
appendPQExpBuffer(query, "SELECT tableoid, oid, "
***************
*** 5409,5414 ****
--- 5422,5428 ----
/* these may fail and return -1: */
i_laninline = PQfnumber(res, "laninline");
i_lanvalidator = PQfnumber(res, "lanvalidator");
+ i_lanchecker = PQfnumber(res, "lanchecker");
i_lanacl = PQfnumber(res, "lanacl");
i_lanowner = PQfnumber(res, "lanowner");
***************
*** 5422,5427 ****
--- 5436,5445 ----
planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
+ if (i_lanchecker >= 0)
+ planginfo[i].lanchecker = atooid(PQgetvalue(res, i, i_lanchecker));
+ else
+ planginfo[i].lanchecker = InvalidOid;
if (i_laninline >= 0)
planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
else
***************
*** 8597,8602 ****
--- 8615,8621 ----
char *qlanname;
char *lanschema;
FuncInfo *funcInfo;
+ FuncInfo *checkerInfo = NULL;
FuncInfo *inlineInfo = NULL;
FuncInfo *validatorInfo = NULL;
***************
*** 8616,8621 ****
--- 8635,8647 ----
if (funcInfo != NULL && !funcInfo->dobj.dump)
funcInfo = NULL; /* treat not-dumped same as not-found */
+ if (OidIsValid(plang->lanchecker))
+ {
+ checkerInfo = findFuncByOid(plang->lanchecker);
+ if (checkerInfo != NULL && !checkerInfo->dobj.dump)
+ checkerInfo = NULL;
+ }
+
if (OidIsValid(plang->laninline))
{
inlineInfo = findFuncByOid(plang->laninline);
***************
*** 8642,8647 ****
--- 8668,8674 ----
* don't, this might not work terribly nicely.
*/
useParams = (funcInfo != NULL &&
+ (checkerInfo != NULL || !OidIsValid(plang->lanchecker)) &&
(inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
(validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
***************
*** 8697,8702 ****
--- 8724,8739 ----
appendPQExpBuffer(defqry, "%s",
fmtId(validatorInfo->dobj.name));
}
+ if (OidIsValid(plang->lanchecker))
+ {
+ appendPQExpBuffer(defqry, " CHECK ");
+ /* Cope with possibility that checker is in different schema */
+ if (checkerInfo->dobj.namespace != funcInfo->dobj.namespace)
+ appendPQExpBuffer(defqry, "%s.",
+ fmtId(checkerInfo->dobj.namespace->dobj.name));
+ appendPQExpBuffer(defqry, "%s",
+ fmtId(checkerInfo->dobj.name));
+ }
}
else
{
*** ./src/bin/pg_dump/pg_dump.h.orig 2011-12-09 15:15:09.184913309 +0100
--- ./src/bin/pg_dump/pg_dump.h 2011-12-09 15:15:43.350636372 +0100
***************
*** 387,392 ****
--- 387,393 ----
Oid lanplcallfoid;
Oid laninline;
Oid lanvalidator;
+ Oid lanchecker;
char *lanacl;
char *lanowner; /* name of owner, or empty string */
} ProcLangInfo;
*** ./src/bin/psql/tab-complete.c.orig 2011-12-09 15:15:09.185913301 +0100
--- ./src/bin/psql/tab-complete.c 2011-12-09 15:15:43.352636356 +0100
***************
*** 1,4 ****
--- 1,5 ----
/*
+ *
* psql - the PostgreSQL interactive terminal
*
* Copyright (c) 2000-2011, PostgreSQL Global Development Group
***************
*** 727,733 ****
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
--- 728,734 ----
#define prev6_wd (previous_words[5])
static const char *const sql_commands[] = {
! "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECK", "CHECKPOINT", "CLOSE", "CLUSTER",
"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
***************
*** 1524,1529 ****
--- 1525,1552 ----
COMPLETE_WITH_LIST(list_TRANS);
}
+
+ /* CHECK */
+ else if (pg_strcasecmp(prev_wd, "CHECK") == 0)
+ {
+ static const char *const list_CHECK[] =
+ {"FUNCTION", "TRIGGER", NULL};
+
+ COMPLETE_WITH_LIST(list_CHECK);
+ }
+ else if (pg_strcasecmp(prev3_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
+ {
+ COMPLETE_WITH_CONST("ON");
+ }
+ else if (pg_strcasecmp(prev4_wd, "CHECK") == 0 &&
+ pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
+ pg_strcasecmp(prev_wd, "ON") == 0)
+ {
+ completion_info_charp = prev2_wd;
+ COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger);
+ }
+
/* CLUSTER */
/*
*** ./src/include/catalog/pg_language.h.orig 2011-12-09 15:15:09.186913293 +0100
--- ./src/include/catalog/pg_language.h 2011-12-09 15:15:43.353636348 +0100
***************
*** 37,42 ****
--- 37,43 ----
Oid lanplcallfoid; /* Call handler for PL */
Oid laninline; /* Optional anonymous-block handler function */
Oid lanvalidator; /* Optional validation function */
+ Oid lanchecker; /* Optional checker function */
aclitem lanacl[1]; /* Access privileges */
} FormData_pg_language;
***************
*** 51,57 ****
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 8
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
--- 52,58 ----
* compiler constants for pg_language
* ----------------
*/
! #define Natts_pg_language 9
#define Anum_pg_language_lanname 1
#define Anum_pg_language_lanowner 2
#define Anum_pg_language_lanispl 3
***************
*** 59,78 ****
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanacl 8
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
--- 60,80 ----
#define Anum_pg_language_lanplcallfoid 5
#define Anum_pg_language_laninline 6
#define Anum_pg_language_lanvalidator 7
! #define Anum_pg_language_lanchecker 8
! #define Anum_pg_language_lanacl 9
/* ----------------
* initial contents of pg_language
* ----------------
*/
! DATA(insert OID = 12 ( "internal" PGUID f f 0 0 2246 0 _null_ ));
DESCR("built-in functions");
#define INTERNALlanguageId 12
! DATA(insert OID = 13 ( "c" PGUID f f 0 0 2247 0 _null_ ));
DESCR("dynamically-loaded C functions");
#define ClanguageId 13
! DATA(insert OID = 14 ( "sql" PGUID f t 0 0 2248 0 _null_ ));
DESCR("SQL-language functions");
#define SQLlanguageId 14
*** ./src/include/catalog/pg_pltemplate.h.orig 2011-12-09 15:15:09.188913277 +0100
--- ./src/include/catalog/pg_pltemplate.h 2011-12-09 15:15:43.354636340 +0100
***************
*** 36,41 ****
--- 36,42 ----
text tmplhandler; /* name of call handler function */
text tmplinline; /* name of anonymous-block handler, or NULL */
text tmplvalidator; /* name of validator function, or NULL */
+ text tmplchecker; /* name of checker function, or NULL */
text tmpllibrary; /* path of shared library */
aclitem tmplacl[1]; /* access privileges for template */
} FormData_pg_pltemplate;
***************
*** 51,65 ****
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 8
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmpllibrary 7
! #define Anum_pg_pltemplate_tmplacl 8
/* ----------------
--- 52,67 ----
* compiler constants for pg_pltemplate
* ----------------
*/
! #define Natts_pg_pltemplate 9
#define Anum_pg_pltemplate_tmplname 1
#define Anum_pg_pltemplate_tmpltrusted 2
#define Anum_pg_pltemplate_tmpldbacreate 3
#define Anum_pg_pltemplate_tmplhandler 4
#define Anum_pg_pltemplate_tmplinline 5
#define Anum_pg_pltemplate_tmplvalidator 6
! #define Anum_pg_pltemplate_tmplchecker 7
! #define Anum_pg_pltemplate_tmpllibrary 8
! #define Anum_pg_pltemplate_tmplacl 9
/* ----------------
***************
*** 67,79 ****
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
--- 69,81 ----
* ----------------
*/
! DATA(insert ( "plpgsql" t t "plpgsql_call_handler" "plpgsql_inline_handler" "plpgsql_validator" "plpgsql_checker" "$libdir/plpgsql" _null_ ));
! DATA(insert ( "pltcl" t t "pltcl_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "pltclu" f f "pltclu_call_handler" _null_ _null_ _null_ "$libdir/pltcl" _null_ ));
! DATA(insert ( "plperl" t t "plperl_call_handler" "plperl_inline_handler" "plperl_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plperlu" f f "plperlu_call_handler" "plperlu_inline_handler" "plperlu_validator" _null_ "$libdir/plperl" _null_ ));
! DATA(insert ( "plpythonu" f f "plpython_call_handler" "plpython_inline_handler" "plpython_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython2u" f f "plpython2_call_handler" "plpython2_inline_handler" "plpython2_validator" _null_ "$libdir/plpython2" _null_ ));
! DATA(insert ( "plpython3u" f f "plpython3_call_handler" "plpython3_inline_handler" "plpython3_validator" _null_ "$libdir/plpython3" _null_ ));
#endif /* PG_PLTEMPLATE_H */
*** ./src/include/commands/defrem.h.orig 2011-12-09 15:15:09.189913269 +0100
--- ./src/include/commands/defrem.h 2011-12-09 15:15:43.355636332 +0100
***************
*** 62,67 ****
--- 62,68 ----
/* commands/functioncmds.c */
extern void CreateFunction(CreateFunctionStmt *stmt, const char *queryString);
extern void RemoveFunctionById(Oid funcOid);
+ extern void CheckFunction(CheckFunctionStmt *stmt);
extern void SetFunctionReturnType(Oid funcOid, Oid newRetType);
extern void SetFunctionArgType(Oid funcOid, int argIndex, Oid newArgType);
extern void RenameFunction(List *name, List *argtypes, const char *newname);
*** ./src/include/nodes/nodes.h.orig 2011-12-09 15:15:09.190913261 +0100
--- ./src/include/nodes/nodes.h 2011-12-09 15:15:43.356636323 +0100
***************
*** 291,296 ****
--- 291,297 ----
T_IndexStmt,
T_CreateFunctionStmt,
T_AlterFunctionStmt,
+ T_CheckFunctionStmt,
T_DoStmt,
T_RenameStmt,
T_RuleStmt,
*** ./src/include/nodes/parsenodes.h.orig 2011-12-09 15:15:09.192913245 +0100
--- ./src/include/nodes/parsenodes.h 2011-12-09 15:15:43.357636314 +0100
***************
*** 1734,1739 ****
--- 1734,1740 ----
List *plhandler; /* PL call handler function (qual. name) */
List *plinline; /* optional inline function (qual. name) */
List *plvalidator; /* optional validator function (qual. name) */
+ List *plchecker; /* optional checker function (qual. name) */
bool pltrusted; /* PL is trusted */
} CreatePLangStmt;
***************
*** 2077,2082 ****
--- 2078,2098 ----
} AlterFunctionStmt;
/* ----------------------
+ * Check {Function|Trigger} Statement
+ * ----------------------
+ */
+ typedef struct CheckFunctionStmt
+ {
+ NodeTag type;
+ List *funcname; /* qualified name of checked object */
+ List *args; /* types of the arguments */
+ char *trgname; /* trigger's name */
+ RangeVar *relation; /* trigger's relation */
+ bool is_function; /* true for CHECK FUNCTION statement */
+ List *options; /* other DefElem options */
+ } CheckFunctionStmt;
+
+ /* ----------------------
* DO Statement
*
* DoStmt is the raw parser output, InlineCodeBlock is the execution-time API
*** ./src/pl/plpgsql/src/pl_comp.c.orig 2011-12-09 15:15:09.193913237 +0100
--- ./src/pl/plpgsql/src/pl_comp.c 2011-12-09 15:15:43.359636297 +0100
***************
*** 115,121 ****
static void plpgsql_HashTableInsert(PLpgSQL_function *function,
PLpgSQL_func_hashkey *func_key);
static void plpgsql_HashTableDelete(PLpgSQL_function *function);
- static void delete_function(PLpgSQL_function *func);
/* ----------
* plpgsql_compile Make an execution tree for a PL/pgSQL function.
--- 115,120 ----
***************
*** 175,181 ****
* Nope, so remove it from hashtable and try to drop associated
* storage (if not done already).
*/
! delete_function(function);
/*
* If the function isn't in active use then we can overwrite the
--- 174,180 ----
* Nope, so remove it from hashtable and try to drop associated
* storage (if not done already).
*/
! plpgsql_delete_function(function);
/*
* If the function isn't in active use then we can overwrite the
***************
*** 2426,2432 ****
}
/*
! * delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
--- 2425,2431 ----
}
/*
! * plpgsql_delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
***************
*** 2439,2446 ****
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
! static void
! delete_function(PLpgSQL_function *func)
{
/* remove function from hash table (might be done already) */
plpgsql_HashTableDelete(func);
--- 2438,2445 ----
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
! void
! plpgsql_delete_function(PLpgSQL_function *func)
{
/* remove function from hash table (might be done already) */
plpgsql_HashTableDelete(func);
*** ./src/pl/plpgsql/src/pl_exec.c.orig 2011-12-09 15:15:09.197913205 +0100
--- ./src/pl/plpgsql/src/pl_exec.c 2011-12-09 15:15:43.363636265 +0100
***************
*** 210,216 ****
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
PLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
!
/* ----------
* plpgsql_exec_function Called by the call handler for
--- 210,228 ----
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate,
PLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
! static void check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec);
! static void check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr);
! static void assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
! PLpgSQL_row *row, PLpgSQL_rec *rec,
! TupleDesc tupdesc);
! static TupleDesc expr_get_desc(PLpgSQL_execstate *estate,
! PLpgSQL_expr *query,
! bool use_element_type,
! bool expand_record,
! bool is_expression);
! static void var_init_to_null(PLpgSQL_execstate *estate, int varno);
! static void check_stmts(PLpgSQL_execstate *estate, List *stmts);
! static void check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
/* ----------
* plpgsql_exec_function Called by the call handler for
***************
*** 6176,6178 ****
--- 6188,7240 ----
return portal;
}
+
+ /*
+ * Following code ensures a CHECK FUNCTION and CHECK TRIGGER statements for PL/pgSQL
+ *
+ */
+
+ /*
+ * append a CONTEXT to error message
+ */
+ static void
+ check_error_callback(void *arg)
+ {
+ PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;
+
+ if (estate->err_stmt != NULL)
+ {
+ /* translator: last %s is a plpgsql statement type name */
+ errcontext("line %d at %s",
+ estate->err_stmt->lineno,
+ plpgsql_stmt_typename(estate->err_stmt));
+ }
+ }
+
+ /*
+ * Check function - it prepare variables and starts a prepare plan walker
+ * called by function checker
+ */
+ void
+ plpgsql_check_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
+
+ /*
+ * Store the actual call argument values into the appropriate variables
+ */
+ for (i = 0; i < func->fn_nargs; i++)
+ {
+ int n = func->fn_argvarnos[i];
+
+ switch (estate.datums[n]->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ {
+ var_init_to_null(&estate, n);
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ {
+ PLpgSQL_row *row = (PLpgSQL_row *) estate.datums[n];
+
+ exec_move_row(&estate, NULL, row, NULL, NULL);
+ }
+ break;
+
+ default:
+ elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
+ }
+ }
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Check trigger - prepare fake environments for testing trigger
+ *
+ */
+ void
+ plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata)
+ {
+ PLpgSQL_execstate estate;
+ ErrorContextCallback plerrcontext;
+ PLpgSQL_rec *rec_new,
+ *rec_old;
+ int i;
+
+ /* Setup error callback for ereport */
+ plerrcontext.callback = check_error_callback;
+ plerrcontext.arg = &estate;
+ plerrcontext.previous = error_context_stack;
+ error_context_stack = &plerrcontext;
+
+ /*
+ * Setup the execution state - we would to reuse some exec routines
+ * so we need a estate
+ */
+ plpgsql_estate_setup(&estate, func, NULL);
+
+ /*
+ * Make local execution copies of all the datums
+ */
+ for (i = 0; i < estate.ndatums; i++)
+ estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
+
+ /*
+ * Put the OLD and NEW tuples into record variables
+ *
+ * We make the tupdescs available in both records even though only one may
+ * have a value. This allows parsing of record references to succeed in
+ * functions that are used for multiple trigger types. For example, we
+ * might have a test like "if (TG_OP = 'INSERT' and NEW.foo = 'xyz')",
+ * which should parse regardless of the current trigger type.
+ */
+ rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
+ rec_new->freetup = false;
+ rec_new->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_new, trigdata->tg_relation->rd_att);
+
+ rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
+ rec_old->freetup = false;
+ rec_old->freetupdesc = false;
+ assign_tupdesc_row_or_rec(&estate, NULL, rec_old, trigdata->tg_relation->rd_att);
+
+ /*
+ * Assign the special tg_ variables
+ */
+ var_init_to_null(&estate, func->tg_op_varno);
+ var_init_to_null(&estate, func->tg_name_varno);
+ var_init_to_null(&estate, func->tg_when_varno);
+ var_init_to_null(&estate, func->tg_level_varno);
+ var_init_to_null(&estate, func->tg_relid_varno);
+ var_init_to_null(&estate, func->tg_relname_varno);
+ var_init_to_null(&estate, func->tg_table_name_varno);
+ var_init_to_null(&estate, func->tg_table_schema_varno);
+ var_init_to_null(&estate, func->tg_nargs_varno);
+ var_init_to_null(&estate, func->tg_argv_varno);
+
+ /*
+ * Now check the toplevel block of statements
+ */
+ check_stmt(&estate, (PLpgSQL_stmt *) func->action);
+
+ /* Cleanup temporary memory */
+ plpgsql_destroy_econtext(&estate);
+
+ /* Pop the error context stack */
+ error_context_stack = plerrcontext.previous;
+ }
+
+ /*
+ * Verify lvalue
+ * It doesn't repeat a checks that are done.
+ * Checks a subscript expressions, verify a validity of record's fields
+ */
+ static void
+ check_target(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ switch (target->dtype)
+ {
+ case PLPGSQL_DTYPE_VAR:
+ case PLPGSQL_DTYPE_REC:
+ break;
+
+ case PLPGSQL_DTYPE_ROW:
+ check_row_or_rec(estate, (PLpgSQL_row *) target, NULL);
+ break;
+
+ case PLPGSQL_DTYPE_RECFIELD:
+ {
+ PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
+ PLpgSQL_rec *rec;
+ int fno;
+
+ rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
+
+ /*
+ * Check that there is already a tuple in the record. We need
+ * that because records don't have any predefined field
+ * structure.
+ */
+ if (!HeapTupleIsValid(rec->tup))
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("record \"%s\" is not assigned to tuple structure",
+ rec->refname)));
+
+ /*
+ * Get the number of the records field to change and the
+ * number of attributes in the tuple. Note: disallow system
+ * column names because the code below won't cope.
+ */
+ fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
+ if (fno <= 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("record \"%s\" has no field \"%s\"",
+ rec->refname, recfield->fieldname)));
+ }
+ break;
+
+ case PLPGSQL_DTYPE_ARRAYELEM:
+ {
+ /*
+ * Target is an element of an array
+ */
+ int nsubscripts;
+ Oid arrayelemtypeid;
+ Oid arraytypeid;
+
+ /*
+ * To handle constructs like x[1][2] := something, we have to
+ * be prepared to deal with a chain of arrayelem datums. Chase
+ * back to find the base array datum, and save the subscript
+ * expressions as we go. (We are scanning right to left here,
+ * but want to evaluate the subscripts left-to-right to
+ * minimize surprises.)
+ */
+ nsubscripts = 0;
+ do
+ {
+ PLpgSQL_arrayelem *arrayelem = (PLpgSQL_arrayelem *) target;
+
+ if (nsubscripts++ >= MAXDIM)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
+ nsubscripts + 1, MAXDIM)));
+
+ check_expr(estate, arrayelem->subscript);
+
+ target = estate->datums[arrayelem->arrayparentno];
+ } while (target->dtype == PLPGSQL_DTYPE_ARRAYELEM);
+
+ /* If target is domain over array, reduce to base type */
+ arraytypeid = exec_get_datum_type(estate, target);
+ arraytypeid = getBaseType(arraytypeid);
+
+ arrayelemtypeid = get_element_type(arraytypeid);
+
+ if (!OidIsValid(arrayelemtypeid))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("subscripted object is not an array")));
+ }
+ break;
+ }
+ }
+
+ /*
+ * Check composed lvalue
+ * There is nothing to check on rec variables
+ */
+ static void
+ check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec)
+ {
+ int fnum;
+
+ /* there are nothing to check on rec now */
+ if (row != NULL)
+ {
+ for (fnum = 0; fnum < row->nfields; fnum++)
+ {
+ /* skip dropped columns */
+ if (row->varnos[fnum] < 0)
+ continue;
+
+ check_target(estate, row->varnos[fnum]);
+ }
+ }
+ }
+
+ /*
+ * Generate a prepared plan - this is simplyfied copy from pl_exec.c
+ * Is not necessary to check simple plan
+ */
+ static void
+ prepare_expr(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *expr, int cursorOptions)
+ {
+ SPIPlanPtr plan;
+
+ /* leave when there are not expression */
+ if (expr == NULL)
+ return;
+
+ /* leave when plan is created */
+ if (expr->plan != NULL)
+ return;
+
+ /*
+ * The grammar can't conveniently set expr->func while building the parse
+ * tree, so make sure it's set before parser hooks need it.
+ */
+ expr->func = estate->func;
+
+ /*
+ * Generate and save the plan
+ */
+ plan = SPI_prepare_params(expr->query,
+ (ParserSetupHook) plpgsql_parser_setup,
+ (void *) expr,
+ cursorOptions);
+ if (plan == NULL)
+ {
+ /* Some SPI errors deserve specific error messages */
+ switch (SPI_result)
+ {
+ case SPI_ERROR_COPY:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot COPY to/from client in PL/pgSQL")));
+ case SPI_ERROR_TRANSACTION:
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot begin/end transactions in PL/pgSQL"),
+ errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
+ default:
+ elog(ERROR, "SPI_prepare_params failed for \"%s\": %s",
+ expr->query, SPI_result_code_string(SPI_result));
+ }
+ }
+
+ expr->plan = SPI_saveplan(plan);
+ SPI_freeplan(plan);
+ }
+
+ /*
+ * Verify a expression
+ */
+ static void
+ check_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
+ {
+ TupleDesc tupdesc;
+
+ if (expr != NULL)
+ {
+ prepare_expr(estate, expr, 0);
+ tupdesc = expr_get_desc(estate, expr, false, false, true);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+
+ /*
+ * We have to assign TupleDesc to all used record variables step by step.
+ * We would to use a exec routines for query preprocessing, so we must
+ * to create a typed NULL value, and this value is assigned to record
+ * variable.
+ */
+ static void
+ assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate,
+ PLpgSQL_row *row, PLpgSQL_rec *rec,
+ TupleDesc tupdesc)
+ {
+ bool *nulls;
+ HeapTuple tup;
+
+ if (tupdesc == NULL)
+ elog(ERROR, "tuple descriptor is empty");
+
+ /*
+ * row variable has assigned TupleDesc already, so don't be processed
+ * here
+ */
+ if (rec != NULL)
+ {
+ PLpgSQL_rec *target = (PLpgSQL_rec *)(estate->datums[rec->dno]);
+
+ if (target->freetup)
+ heap_freetuple(target->tup);
+
+ if (rec->freetupdesc)
+ FreeTupleDesc(target->tupdesc);
+
+ /* initialize rec by NULLs */
+ nulls = (bool *) palloc(tupdesc->natts * sizeof(bool));
+ memset(nulls, true, tupdesc->natts * sizeof(bool));
+
+ target->tupdesc = CreateTupleDescCopy(tupdesc);
+ target->freetupdesc = true;
+
+ tup = heap_form_tuple(tupdesc, NULL, nulls);
+ if (HeapTupleIsValid(tup))
+ {
+ target->tup = tup;
+ target->freetup = true;
+ }
+ else
+ elog(ERROR, "cannot to build valid composite value");
+ }
+ }
+
+ /*
+ * Assign a tuple descriptor to variable specified by dno
+ */
+ static void
+ assign_tupdesc_dno(PLpgSQL_execstate *estate, int varno, TupleDesc tupdesc)
+ {
+ PLpgSQL_datum *target = estate->datums[varno];
+
+ if (target->dtype == PLPGSQL_DTYPE_REC)
+ assign_tupdesc_row_or_rec(estate, NULL, (PLpgSQL_rec *) target, tupdesc);
+ }
+
+ /*
+ * Returns a tuple descriptor based on existing plan
+ */
+ static TupleDesc
+ expr_get_desc(PLpgSQL_execstate *estate,
+ PLpgSQL_expr *query,
+ bool use_element_type,
+ bool expand_record,
+ bool is_expression)
+ {
+ TupleDesc tupdesc = NULL;
+ CachedPlanSource *plansource = NULL;
+
+ if (query->plan != NULL)
+ {
+ SPIPlanPtr plan = query->plan;
+
+ if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
+ elog(ERROR, "cached plan is not valid plan");
+
+ if (list_length(plan->plancache_list) != 1)
+ elog(ERROR, "plan is not single execution plan");
+
+ plansource = (CachedPlanSource *) linitial(plan->plancache_list);
+
+ tupdesc = CreateTupleDescCopy(plansource->resultDesc);
+ }
+ else
+ elog(ERROR, "there are no plan for query: \"%s\"",
+ query->query);
+
+ /*
+ * try to get a element type, when result is a array (used with FOREACH ARRAY stmt)
+ */
+ if (use_element_type)
+ {
+ Oid elemtype;
+ TupleDesc elemtupdesc;
+
+ /* result should be a array */
+ if (tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /* check the type of the expression - must be an array */
+ elemtype = get_element_type(tupdesc->attrs[0]->atttypid);
+ if (!OidIsValid(elemtype))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("FOREACH expression must yield an array, not type %s",
+ format_type_be(tupdesc->attrs[0]->atttypid))));
+
+ /* we can't know typmod now */
+ elemtupdesc = lookup_rowtype_tupdesc_noerror(elemtype, -1, true);
+ if (elemtupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(elemtupdesc);
+ ReleaseTupleDesc(elemtupdesc);
+ }
+ else
+ elog(ERROR, "cannot to identify real type for record type variable");
+ }
+
+ if (is_expression && tupdesc->natts != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg_plural("query \"%s\" returned %d column",
+ "query \"%s\" returned %d columns",
+ tupdesc->natts,
+ query->query,
+ tupdesc->natts)));
+
+ /*
+ * One spacial case is when record is assigned to composite type, then
+ * we should to unpack composite type.
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 && expand_record)
+ {
+ TupleDesc unpack_tupdesc;
+
+ unpack_tupdesc = lookup_rowtype_tupdesc_noerror(tupdesc->attrs[0]->atttypid,
+ tupdesc->attrs[0]->atttypmod,
+ true);
+ if (unpack_tupdesc != NULL)
+ {
+ FreeTupleDesc(tupdesc);
+ tupdesc = CreateTupleDescCopy(unpack_tupdesc);
+ ReleaseTupleDesc(unpack_tupdesc);
+ }
+ }
+
+ /*
+ * There is special case, when returned tupdesc contains only
+ * unpined record: rec := func_with_out_parameters(). IN this case
+ * we must to dig more deep - we have to find oid of function and
+ * get their parameters,
+ *
+ * This is support for assign statement
+ * recvar := func_with_out_parameters(..)
+ */
+ if (tupdesc->tdtypeid == RECORDOID &&
+ tupdesc->tdtypmod == -1 &&
+ tupdesc->natts == 1 &&
+ tupdesc->attrs[0]->atttypid == RECORDOID &&
+ tupdesc->attrs[0]->atttypmod == -1 &&
+ expand_record)
+ {
+ PlannedStmt *_stmt;
+ Plan *_plan;
+ TargetEntry *tle;
+ CachedPlan *cplan;
+
+ /*
+ * When tupdesc is related to unpined record, we will try
+ * to check plan if it is just function call and if it is
+ * then we can try to derive a tupledes from function's
+ * description.
+ */
+ cplan = GetCachedPlan(plansource, NULL, true);
+ _stmt = (PlannedStmt *) linitial(cplan->stmt_list);
+
+ if (IsA(_stmt, PlannedStmt) && _stmt->commandType == CMD_SELECT)
+ {
+ _plan = _stmt->planTree;
+ if (IsA(_plan, Result) && list_length(_plan->targetlist) == 1)
+ {
+ tle = (TargetEntry *) linitial(_plan->targetlist);
+ if (((Node *) tle->expr)->type == T_FuncExpr)
+ {
+ FuncExpr *fn = (FuncExpr *) tle->expr;
+ FmgrInfo flinfo;
+ FunctionCallInfoData fcinfo;
+ TupleDesc rd;
+ Oid rt;
+
+ fmgr_info(fn->funcid, &flinfo);
+ flinfo.fn_expr = (Node *) fn;
+ fcinfo.flinfo = &flinfo;
+
+ get_call_result_type(&fcinfo, &rt, &rd);
+ if (rd == NULL)
+ elog(ERROR, "function does not return composite type is not possible to identify composite type");
+
+ FreeTupleDesc(tupdesc);
+ BlessTupleDesc(rd);
+
+ tupdesc = rd;
+ }
+ }
+ }
+
+ ReleaseCachedPlan(cplan, true);
+ }
+
+ return tupdesc;
+ }
+
+ /*
+ * Ensure check for all statements in list
+ */
+ static void
+ check_stmts(PLpgSQL_execstate *estate, List *stmts)
+ {
+ ListCell *lc;
+
+ foreach(lc, stmts)
+ {
+ check_stmt(estate, (PLpgSQL_stmt *) lfirst(lc));
+ }
+ }
+
+ /*
+ * walk over all statements
+ */
+ static void
+ check_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
+ {
+ TupleDesc tupdesc = NULL;
+ PLpgSQL_function *func;
+ ListCell *l;
+
+ if (stmt == NULL)
+ return;
+
+ estate->err_stmt = stmt;
+ func = estate->func;
+
+ switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
+ {
+ case PLPGSQL_STMT_BLOCK:
+ {
+ PLpgSQL_stmt_block *stmt_block = (PLpgSQL_stmt_block *) stmt;
+ int i;
+ PLpgSQL_datum *d;
+
+ for (i = 0; i < stmt_block->n_initvars; i++)
+ {
+ d = func->datums[stmt_block->initvarnos[i]];
+
+ if (d->dtype == PLPGSQL_DTYPE_VAR)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) d;
+
+ check_expr(estate, var->default_val);
+ }
+ }
+
+ check_stmts(estate, stmt_block->body);
+
+ if (stmt_block->exceptions)
+ {
+ foreach(l, stmt_block->exceptions->exc_list)
+ {
+ check_stmts(estate, ((PLpgSQL_exception *) lfirst(l))->action);
+ }
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_ASSIGN:
+ {
+ PLpgSQL_stmt_assign *stmt_assign = (PLpgSQL_stmt_assign *) stmt;
+
+ /* prepare plan if desn't exist yet */
+ prepare_expr(estate, stmt_assign->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_assign->expr,
+ false, /* no element type */
+ true, /* expand record */
+ true); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_target(estate, stmt_assign->varno);
+
+ /* assign a tupdesc to record variable */
+ assign_tupdesc_dno(estate, stmt_assign->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_IF:
+ {
+ PLpgSQL_stmt_if *stmt_if = (PLpgSQL_stmt_if *) stmt;
+ ListCell *l;
+
+ check_expr(estate, stmt_if->cond);
+
+ check_stmts(estate, stmt_if->then_body);
+
+ foreach(l, stmt_if->elsif_list)
+ {
+ PLpgSQL_if_elsif *elif = (PLpgSQL_if_elsif *) lfirst(l);
+
+ check_expr(estate, elif->cond);
+ check_stmts(estate, elif->stmts);
+ }
+
+ check_stmts(estate, stmt_if->else_body);
+ }
+ break;
+
+ case PLPGSQL_STMT_CASE:
+ {
+ PLpgSQL_stmt_case *stmt_case = (PLpgSQL_stmt_case *) stmt;
+ Oid result_oid;
+
+ if (stmt_case->t_expr != NULL)
+ {
+ PLpgSQL_var *t_var = (PLpgSQL_var *) estate->datums[stmt_case->t_varno];
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_case->t_expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_case->t_expr,
+ false, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ result_oid = tupdesc->attrs[0]->atttypid;
+
+ /*
+ * When expected datatype is different from real, change it. Note that
+ * what we're modifying here is an execution copy of the datum, so
+ * this doesn't affect the originally stored function parse tree.
+ */
+
+ if (t_var->datatype->typoid != result_oid)
+ t_var->datatype = plpgsql_build_datatype(result_oid,
+ -1,
+ estate->func->fn_input_collation);
+
+ ReleaseTupleDesc(tupdesc);
+ }
+
+ foreach(l, stmt_case->case_when_list)
+ {
+ PLpgSQL_case_when *cwt = (PLpgSQL_case_when *) lfirst(l);
+
+ check_expr(estate, cwt->expr);
+ check_stmts(estate, cwt->stmts);
+ }
+
+ check_stmts(estate, stmt_case->else_stmts);
+ }
+ break;
+
+ case PLPGSQL_STMT_LOOP:
+ check_stmts(estate, ((PLpgSQL_stmt_loop *) stmt)->body);
+ break;
+
+ case PLPGSQL_STMT_WHILE:
+ {
+ PLpgSQL_stmt_while *stmt_while = (PLpgSQL_stmt_while *) stmt;
+
+ check_expr(estate, stmt_while->cond);
+ check_stmts(estate, stmt_while->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORI:
+ {
+ PLpgSQL_stmt_fori *stmt_fori = (PLpgSQL_stmt_fori *) stmt;
+
+ check_expr(estate, stmt_fori->lower);
+ check_expr(estate, stmt_fori->upper);
+ check_expr(estate, stmt_fori->step);
+
+ check_stmts(estate, stmt_fori->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORS:
+ {
+ PLpgSQL_stmt_fors *stmt_fors = (PLpgSQL_stmt_fors *) stmt;
+
+ /* we need to set hidden variable type */
+ prepare_expr(estate, stmt_fors->query, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_fors->query,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_fors->row, stmt_fors->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fors->row, stmt_fors->rec, tupdesc);
+
+ check_stmts(estate, stmt_fors->body);
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_FORC:
+ {
+ PLpgSQL_stmt_forc *stmt_forc = (PLpgSQL_stmt_forc *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_forc->curvar];
+
+ prepare_expr(estate, stmt_forc->argquery, 0);
+
+ if (var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ check_row_or_rec(estate, stmt_forc->row, stmt_forc->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_forc->row, stmt_forc->rec, tupdesc);
+ }
+
+ check_stmts(estate, stmt_forc->body);
+ if (tupdesc != NULL)
+ ReleaseTupleDesc(tupdesc);
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNFORS:
+ {
+ PLpgSQL_stmt_dynfors * stmt_dynfors = (PLpgSQL_stmt_dynfors *) stmt;
+
+ if (stmt_dynfors->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_expr(estate, stmt_dynfors->query);
+
+ foreach(l, stmt_dynfors->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ check_stmts(estate, stmt_dynfors->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_FOREACH_A:
+ {
+ PLpgSQL_stmt_foreach_a *stmt_foreach_a = (PLpgSQL_stmt_foreach_a *) stmt;
+
+ prepare_expr(estate, stmt_foreach_a->expr, 0);
+
+ tupdesc = expr_get_desc(estate,
+ stmt_foreach_a->expr,
+ true, /* no element type */
+ false, /* expand record */
+ true); /* is expression */
+
+ check_target(estate, stmt_foreach_a->varno);
+ assign_tupdesc_dno(estate, stmt_foreach_a->varno, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+
+ check_stmts(estate, stmt_foreach_a->body);
+ }
+ break;
+
+ case PLPGSQL_STMT_EXIT:
+ check_expr(estate, ((PLpgSQL_stmt_exit *) stmt)->cond);
+ break;
+
+ case PLPGSQL_STMT_PERFORM:
+ prepare_expr(estate, ((PLpgSQL_stmt_perform *) stmt)->expr, 0);
+ break;
+
+ case PLPGSQL_STMT_RETURN:
+ check_expr(estate, ((PLpgSQL_stmt_return *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_NEXT:
+ check_expr(estate, ((PLpgSQL_stmt_return_next *) stmt)->expr);
+ break;
+
+ case PLPGSQL_STMT_RETURN_QUERY:
+ {
+ PLpgSQL_stmt_return_query *stmt_rq = (PLpgSQL_stmt_return_query *) stmt;
+
+ check_expr(estate, stmt_rq->dynquery);
+ prepare_expr(estate, stmt_rq->query, 0);
+
+ foreach(l, stmt_rq->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_RAISE:
+ {
+ PLpgSQL_stmt_raise *stmt_raise = (PLpgSQL_stmt_raise *) stmt;
+ ListCell *current_param;
+ char *cp;
+
+ foreach(l, stmt_raise->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ foreach(l, stmt_raise->options)
+ {
+ PLpgSQL_raise_option *opt = (PLpgSQL_raise_option *) lfirst(l);
+
+ check_expr(estate, opt->expr);
+ }
+
+ current_param = list_head(stmt_raise->params);
+
+ /* ensure any single % has a own parameter */
+ if (stmt_raise->message != NULL)
+ {
+ for (cp = stmt_raise->message; *cp; cp++)
+ {
+ if (cp[0] == '%')
+ {
+ if (cp[1] == '%')
+ {
+ cp++;
+ continue;
+ }
+
+ if (current_param == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too few parameters specified for RAISE")));
+
+ current_param = lnext(current_param);
+ }
+ }
+ }
+
+ if (current_param != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("too many parameters specified for RAISE")));
+ }
+ break;
+
+ case PLPGSQL_STMT_EXECSQL:
+ {
+ PLpgSQL_stmt_execsql *stmt_execsql = (PLpgSQL_stmt_execsql *) stmt;
+
+ prepare_expr(estate, stmt_execsql->sqlstmt, 0);
+ if (stmt_execsql->into)
+ {
+ tupdesc = expr_get_desc(estate,
+ stmt_execsql->sqlstmt,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+
+ /* check target, ensure target can get a result */
+ check_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_execsql->row, stmt_execsql->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_DYNEXECUTE:
+ {
+ PLpgSQL_stmt_dynexecute *stmt_dynexecute = (PLpgSQL_stmt_dynexecute *) stmt;
+
+ check_expr(estate, stmt_dynexecute->query);
+
+ foreach(l, stmt_dynexecute->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+
+ if (stmt_dynexecute->into)
+ {
+ if (stmt_dynexecute->rec != NULL)
+ elog(ERROR, "cannot determinate a result of dynamic SQL");
+
+ check_row_or_rec(estate, stmt_dynexecute->row, stmt_dynexecute->rec);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_OPEN:
+ {
+ PLpgSQL_stmt_open *stmt_open = (PLpgSQL_stmt_open *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_open->curvar];
+
+ if (var->cursor_explicit_expr)
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+
+ prepare_expr(estate, stmt_open->query, 0);
+ prepare_expr(estate, stmt_open->argquery, 0);
+ check_expr(estate, stmt_open->dynquery);
+
+ foreach(l, stmt_open->params)
+ {
+ check_expr(estate, (PLpgSQL_expr *) lfirst(l));
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_GETDIAG:
+ {
+ PLpgSQL_stmt_getdiag *stmt_getdiag = (PLpgSQL_stmt_getdiag *) stmt;
+ ListCell *lc;
+
+ foreach(lc, stmt_getdiag->diag_items)
+ {
+ PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
+
+ check_target(estate, diag_item->target);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_FETCH:
+ {
+ PLpgSQL_stmt_fetch *stmt_fetch = (PLpgSQL_stmt_fetch *) stmt;
+ PLpgSQL_var *var = (PLpgSQL_var *)(estate->datums[stmt_fetch->curvar]);
+
+ if (var != NULL && var->cursor_explicit_expr != NULL)
+ {
+ prepare_expr(estate, var->cursor_explicit_expr,
+ var->cursor_options);
+ tupdesc = expr_get_desc(estate,
+ var->cursor_explicit_expr,
+ false, /* no element type */
+ false, /* expand record */
+ false); /* is expression */
+ check_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec);
+ assign_tupdesc_row_or_rec(estate, stmt_fetch->row, stmt_fetch->rec, tupdesc);
+ ReleaseTupleDesc(tupdesc);
+ }
+ }
+ break;
+
+ case PLPGSQL_STMT_CLOSE:
+ break;
+
+ default:
+ elog(ERROR, "unrecognized cmd_type: %d", stmt->cmd_type);
+ return; /* be compiler quite */
+ }
+ }
+
+ /*
+ * Initialize variable to NULL
+ */
+ static void
+ var_init_to_null(PLpgSQL_execstate *estate, int varno)
+ {
+ PLpgSQL_var *var = (PLpgSQL_var *) estate->datums[varno];
+ var->value = (Datum) 0;
+ var->isnull = true;
+ var->freeval = false;
+ }
*** ./src/pl/plpgsql/src/pl_handler.c.orig 2011-12-09 15:15:09.198913197 +0100
--- ./src/pl/plpgsql/src/pl_handler.c 2011-12-09 15:15:43.364636257 +0100
***************
*** 312,314 ****
--- 312,452 ----
PG_RETURN_VOID();
}
+
+ /* ----------
+ * plpgsql_checker
+ *
+ * This function attempts to check a embeded SQL inside a PL/pgSQL function at
+ * CHECK FUNCTION time. It should to have one or two parameters. Second
+ * parameter is a relation (used when function is trigger).
+ * ----------
+ */
+ PG_FUNCTION_INFO_V1(plpgsql_checker);
+
+ Datum
+ plpgsql_checker(PG_FUNCTION_ARGS)
+ {
+ Oid funcoid = PG_GETARG_OID(0);
+ Oid relid = PG_GETARG_OID(1);
+ HeapTuple tuple;
+ FunctionCallInfoData fake_fcinfo;
+ FmgrInfo flinfo;
+ TriggerData trigdata;
+ int rc;
+ PLpgSQL_function *function;
+ PLpgSQL_execstate *cur_estate;
+
+ Form_pg_proc proc;
+ char functyptype;
+ bool istrigger = false;
+
+ /* we don't need to repair a check done by validator */
+
+ tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
+ if (!HeapTupleIsValid(tuple))
+ elog(ERROR, "cache lookup failed for function %u", funcoid);
+ proc = (Form_pg_proc) GETSTRUCT(tuple);
+
+ functyptype = get_typtype(proc->prorettype);
+
+ if (functyptype == TYPTYPE_PSEUDO)
+ {
+ /* we assume OPAQUE with no arguments means a trigger */
+ if (proc->prorettype == TRIGGEROID ||
+ (proc->prorettype == OPAQUEOID && proc->pronargs == 0))
+ {
+ istrigger = true;
+ if (!OidIsValid(relid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("PL/pgSQL trigger functions cannot be checked directly"),
+ errhint("use CHECK TRIGGER statement instead")));
+ }
+ }
+
+ /*
+ * Connect to SPI manager
+ */
+ if ((rc = SPI_connect()) != SPI_OK_CONNECT)
+ elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
+
+ /*
+ * Set up a fake fcinfo with just enough info to satisfy
+ * plpgsql_compile().
+ *
+ * there should be a different real argtypes for polymorphic params
+ */
+ MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo));
+ MemSet(&flinfo, 0, sizeof(flinfo));
+ fake_fcinfo.flinfo = &flinfo;
+ flinfo.fn_oid = funcoid;
+ flinfo.fn_mcxt = CurrentMemoryContext;
+
+ if (istrigger)
+ {
+ MemSet(&trigdata, 0, sizeof(trigdata));
+ trigdata.type = T_TriggerData;
+ trigdata.tg_relation = relation_open(relid, AccessShareLock);
+ fake_fcinfo.context = (Node *) &trigdata;
+ }
+
+ /* Get a compiled function */
+ function = plpgsql_compile(&fake_fcinfo, false);
+
+ /* Must save and restore prior value of cur_estate */
+ cur_estate = function->cur_estate;
+
+ /* Mark the function as busy, so it can't be deleted from under us */
+ function->use_count++;
+
+
+ /* Create a fake runtime environment and prepare plans */
+ PG_TRY();
+ {
+ if (!istrigger)
+ plpgsql_check_function(function, &fake_fcinfo);
+ else
+ plpgsql_check_trigger(function, &trigdata);
+ }
+ PG_CATCH();
+ {
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+
+ if (istrigger)
+ relation_close(trigdata.tg_relation, AccessShareLock);
+
+ function->cur_estate = cur_estate;
+ function->use_count--;
+
+ /*
+ * We cannot to preserve instance of this function, because
+ * expressions are not consistent - a tests on simple expression
+ * was be processed newer.
+ */
+ plpgsql_delete_function(function);
+
+ /*
+ * Disconnect from SPI manager
+ */
+ if ((rc = SPI_finish()) != SPI_OK_FINISH)
+ elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
+
+ ReleaseSysCache(tuple);
+
+ PG_RETURN_VOID();
+ }
*** ./src/pl/plpgsql/src/plpgsql.h.orig 2011-12-09 15:15:09.199913189 +0100
--- ./src/pl/plpgsql/src/plpgsql.h 2011-12-09 15:15:43.365636249 +0100
***************
*** 902,907 ****
--- 902,908 ----
extern void plpgsql_adddatum(PLpgSQL_datum *new);
extern int plpgsql_add_initdatums(int **varnos);
extern void plpgsql_HashTableInit(void);
+ extern void plpgsql_delete_function(PLpgSQL_function *func);
/* ----------
* Functions in pl_handler.c
***************
*** 911,916 ****
--- 912,918 ----
extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_inline_handler(PG_FUNCTION_ARGS);
extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
+ extern Datum plpgsql_checker(PG_FUNCTION_ARGS);
/* ----------
* Functions in pl_exec.c
***************
*** 928,933 ****
--- 930,939 ----
extern void exec_get_datum_type_info(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum,
Oid *typeid, int32 *typmod, Oid *collation);
+ extern void plpgsql_check_function(PLpgSQL_function *func,
+ FunctionCallInfo fcinfo);
+ extern void plpgsql_check_trigger(PLpgSQL_function *func,
+ TriggerData *trigdata);
/* ----------
* Functions for namespace handling in pl_funcs.c
*** ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql.orig 2011-12-09 15:15:09.201913173 +0100
--- ./src/pl/plpgsql/src/plpgsql--unpackaged--1.0.sql 2011-12-09 15:15:43.366636241 +0100
***************
*** 5,7 ****
--- 5,8 ----
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_call_handler();
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_inline_handler(internal);
ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_validator(oid);
+ ALTER EXTENSION plpgsql ADD FUNCTION plpgsql_checker(oid, regclass);
*** ./src/test/regress/expected/plpgsql.out.orig 2011-12-09 15:15:09.202913165 +0100
--- ./src/test/regress/expected/plpgsql.out 2011-12-09 15:15:43.368636225 +0100
***************
*** 302,307 ****
--- 302,310 ----
' language plpgsql;
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
+ NOTICE: checked function "tg_hslot_biu()"
-- ************************************************************
-- * BEFORE DELETE on HSlot
-- * - prevent from manual manipulation
***************
*** 635,640 ****
--- 638,646 ----
raise exception ''illegal backlink beginning with %'', mytype;
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
+ NOTICE: checked function "tg_backlink_set(character,character)"
-- ************************************************************
-- * Support function to clear out the backlink field if
-- * it still points to specific slot
***************
*** 2802,2807 ****
--- 2808,2842 ----
(1 row)
+ -- check function should not fail
+ check function for_vect();
+ NOTICE: checked function "for_vect()"
+ -- recheck after check function
+ select for_vect();
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1
+ NOTICE: 2
+ NOTICE: 3
+ NOTICE: 4
+ NOTICE: 1 BB CC
+ NOTICE: 2 BB CC
+ NOTICE: 3 BB CC
+ NOTICE: 4 BB CC
+ NOTICE: 1 bb cc
+ NOTICE: 2 bb cc
+ NOTICE: 3 bb cc
+ NOTICE: 4 bb cc
+ for_vect
+ ----------
+
+ (1 row)
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 3283,3288 ****
--- 3318,3326 ----
return;
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
+ NOTICE: checked function "forc01()"
select forc01();
NOTICE: 5 from c
NOTICE: 6 from c
***************
*** 3716,3721 ****
--- 3754,3762 ----
end case;
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
+ NOTICE: checked function "case_test(bigint)"
select case_test(1);
case_test
-----------
***************
*** 4571,4573 ****
--- 4612,4956 ----
CONTEXT: PL/pgSQL function "testoa" line 5 at assignment
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+ --
+ -- check function statement tests
+ --
+ create table t1(a int, b int);
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ LINE 1: update t1 set c = 30
+ ^
+ DETAIL: column "c" of relation "t1" does not exist
+ QUERY: update t1 set c = 30
+ CONTEXT: line 4 at SQL statement
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "r" has no field "c"
+ CONTEXT: SQL statement "SELECT r.c"
+ line 6 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "r" has no field "c"
+ CONTEXT: line 6 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ drop function g1();
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ LINE 1: SELECT a + b
+ ^
+ DETAIL: column "a" does not exist
+ QUERY: SELECT a + b
+ CONTEXT: line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: too many parameters specified for RAISE
+ CONTEXT: line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: too few parameters specified for RAISE
+ CONTEXT: line 4 at RAISE
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ LINE 1: SELECT c+10
+ ^
+ DETAIL: column "c" does not exist
+ QUERY: SELECT c+10
+ CONTEXT: line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: subscripted object is not an array
+ CONTEXT: line 5 at assignment
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ drop function f1();
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+ select f1();
+ f1
+ ----
+
+ (1 row)
+
+ check function f1();
+ WARNING: error in function "f1()"
+ DETAIL: record "_exception" has no field "hint"
+ CONTEXT: line 7 at GET DIAGNOSTICS
+ drop function f1();
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ WARNING: error in function "f1_trg()"
+ DETAIL: record "new" has no field "c"
+ CONTEXT: SQL statement "SELECT new.c"
+ line 5 at RAISE
+ insert into t1 values(6,30);
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- should to fail
+ check trigger t1_f1 on t1;
+ WARNING: error in function "f1_trg()"
+ DETAIL: record "new" has no field "c"
+ CONTEXT: line 5 at assignment
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+ ERROR: record "new" has no field "c"
+ CONTEXT: PL/pgSQL function "f1_trg" line 5 at assignment
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+ -- ok
+ check trigger t1_f1 on t1;
+ NOTICE: checked function "f1_trg()"
+ -- ok
+ insert into t1 values(6,30);
+ drop table t1;
+ drop type _exception_type;
+ drop function f1_trg();
*** ./src/test/regress/sql/plpgsql.sql.orig 2011-12-09 15:15:09.204913149 +0100
--- ./src/test/regress/sql/plpgsql.sql 2011-12-09 15:15:43.371636201 +0100
***************
*** 366,371 ****
--- 366,373 ----
create trigger tg_hslot_biu before insert or update
on HSlot for each row execute procedure tg_hslot_biu();
+ -- check trigger should not fail
+ check trigger tg_hslot_biu on HSlot;
-- ************************************************************
-- * BEFORE DELETE on HSlot
***************
*** 747,752 ****
--- 749,757 ----
end;
' language plpgsql;
+ -- check function should not fail
+ check function tg_backlink_set(bpchar, bpchar);
+
-- ************************************************************
-- * Support function to clear out the backlink field if
***************
*** 2335,2340 ****
--- 2340,2352 ----
select for_vect();
+ -- check function should not fail
+ check function for_vect();
+
+ -- recheck after check function
+ select for_vect();
+
+
-- regression test: verify that multiple uses of same plpgsql datum within
-- a SQL command all get mapped to the same $n parameter. The return value
-- of the SELECT is not important, we only care that it doesn't fail with
***************
*** 2714,2719 ****
--- 2726,2734 ----
end;
$$ language plpgsql;
+ -- check function should not fail
+ check function forc01();
+
select forc01();
-- try updating the cursor's current row
***************
*** 3048,3053 ****
--- 3063,3071 ----
end;
$$ language plpgsql immutable;
+ -- check function should not fail
+ check function case_test(bigint);
+
select case_test(1);
select case_test(2);
select case_test(3);
***************
*** 3600,3602 ****
--- 3618,3862 ----
drop function arrayassign1();
drop function testoa(x1 int, x2 int, x3 int);
+
+ --
+ -- check function statement tests
+ --
+
+ create table t1(a int, b int);
+
+ create function f1()
+ returns void as $$
+ begin
+ if false then
+ update t1 set c = 30;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create function g1(out a int, out b int)
+ as $$
+ select 10,20;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ r := g1();
+ if false then
+ raise notice '%', r.c;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function g1(out a int, out b int)
+ returns setof record as $$
+ select * from t1;
+ $$ language sql;
+
+ create function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ raise notice '%', r.c;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r record;
+ begin
+ for r in select * from g1()
+ loop
+ r.c := 20;
+ end loop;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+ drop function g1();
+
+ create function f1()
+ returns int as $$
+ declare r int;
+ begin
+ if false then
+ r := a + b;
+ end if;
+ return r;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '%', 1, 2;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ begin
+ if false then
+ raise notice '% %';
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int[];
+ begin
+ if false then
+ r[c+10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create or replace function f1()
+ returns void as $$
+ declare r int;
+ begin
+ if false then
+ r[10] := 20;
+ end if;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+ select f1();
+
+ drop function f1();
+
+ create type _exception_type as (
+ state text,
+ message text,
+ detail text);
+
+ create or replace function f1()
+ returns void as $$
+ declare
+ _exception record;
+ begin
+ _exception := NULL::_exception_type;
+ exception when others then
+ get stacked diagnostics
+ _exception.state = RETURNED_SQLSTATE,
+ _exception.message = MESSAGE_TEXT,
+ _exception.detail = PG_EXCEPTION_DETAIL,
+ _exception.hint = PG_EXCEPTION_HINT;
+ end;
+ $$ language plpgsql;
+
+ select f1();
+ check function f1();
+
+ drop function f1();
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ if new.a > 10 then
+ raise notice '%', new.b;
+ raise notice '%', new.c;
+ end if;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ create trigger t1_f1 before insert on t1
+ for each row
+ execute procedure f1_trg();
+
+ insert into t1 values(6,30);
+ check trigger t1_f1 on t1;
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ new.c := 30;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- should to fail
+ check trigger t1_f1 on t1;
+
+ -- should to fail but not crash
+ insert into t1 values(6,30);
+
+ create or replace function f1_trg()
+ returns trigger as $$
+ begin
+ new.a := new.a + 10;
+ new.b := new.b + 10;
+ return new;
+ end;
+ $$ language plpgsql;
+
+ -- ok
+ check trigger t1_f1 on t1;
+
+ -- ok
+ insert into t1 values(6,30);
+
+ drop table t1;
+ drop type _exception_type;
+
+ drop function f1_trg();
+
Pavel Stehule wrote:
there is fixed version
Here is my attempt at a doc patch.
Could you add it to your patch so that all is in a single patch?
Yours,
Laurenz Albe
Attachments:
check_function_docs.patchapplication/octet-stream; name=check_function_docs.patchDownload
diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml
new file mode 100644
index b8cc16f..a436aeb
*** a/doc/src/sgml/catalogs.sgml
--- b/doc/src/sgml/catalogs.sgml
***************
*** 3652,3657 ****
--- 3652,3669 ----
</row>
<row>
+ <entry><structfield>lanchecker</structfield></entry>
+ <entry><type>oid</type></entry>
+ <entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
+ <entry>
+ This references a correctness check function that is used by
+ <command>CHECK FUNCTION</> and <command>CHECK TRIGGER</> for in-depth
+ checks of function bodies.
+ Zero if no such function is provided.
+ </entry>
+ </row>
+
+ <row>
<entry><structfield>lanacl</structfield></entry>
<entry><type>aclitem[]</type></entry>
<entry></entry>
***************
*** 4262,4267 ****
--- 4274,4285 ----
</row>
<row>
+ <entry><structfield>tmplchecker</structfield></entry>
+ <entry><type>text</type></entry>
+ <entry>Name of correctness check function, or null if none</entry>
+ </row>
+
+ <row>
<entry><structfield>tmpllibrary</structfield></entry>
<entry><type>text</type></entry>
<entry>Path of shared library that implements language</entry>
diff --git a/doc/src/sgml/plhandler.sgml b/doc/src/sgml/plhandler.sgml
new file mode 100644
index 54b88ef..21d043b
*** a/doc/src/sgml/plhandler.sgml
--- b/doc/src/sgml/plhandler.sgml
*************** CREATE LANGUAGE plsample
*** 161,170 ****
Although providing a call handler is sufficient to create a minimal
procedural language, there are two other functions that can optionally
be provided to make the language more convenient to use. These
! are a <firstterm>validator</firstterm> and an
! <firstterm>inline handler</firstterm>. A validator can be provided
! to allow language-specific checking to be done during
! <xref linkend="sql-createfunction">.
An inline handler can be provided to allow the language to support
anonymous code blocks executed via the <xref linkend="sql-do"> command.
</para>
--- 161,173 ----
Although providing a call handler is sufficient to create a minimal
procedural language, there are two other functions that can optionally
be provided to make the language more convenient to use. These
! are a <firstterm>validator</firstterm>, a <firstterm>checker</firstterm>
! and an <firstterm>inline handler</firstterm>.
! A validator can be provided to allow language-specific checking to be
! done during <xref linkend="sql-createfunction">.
! A checker performs in-depth checks of function bodies via the
! commands <xref linkend="sql-checkfunction"> and
! <xref linkend="sql-checktrigger">.
An inline handler can be provided to allow the language to support
anonymous code blocks executed via the <xref linkend="sql-do"> command.
</para>
*************** CREATE LANGUAGE plsample
*** 203,208 ****
--- 206,232 ----
</para>
<para>
+ If a checker is provided by a procedural language, it must be declared
+ as a function taking two arguments, one of type <type>oid</> and one of
+ type <type>regclass</>. The checker's result is ignored, so it is customarily
+ declared to return <type>void</>.
+ The checker will be called whenever a function is checked with
+ <command>CHECK FUNCTION</> or <command>CHECK TRIGGER</>.
+ The passed-in OID is the OID of the function's <classname>pg_proc</>
+ row. The passed-in <type>regclass</> is the OID of the
+ <classname>pg_class</> row of the table on which the trigger is defined,
+ or <symbol>InvalidOid</symbol> in the case of <command>CHECK FUNCTION</>.
+ The checker must fetch these rows in the usual way, and do
+ whatever checking is appropriate.
+ Typical checks include verifying that all referenced database objects
+ actually exist or style checks like verifying that all declared
+ variables are actually used. If the checker finds the function to be okay,
+ it should just return. If it finds a problem, it should report a
+ <literal>WARNING</> via the normal <function>ereport()</> error reporting
+ mechanism.
+ </para>
+
+ <para>
If an inline handler is provided by a procedural language, it
must be declared as a function taking a single parameter of type
<type>internal</>. The inline handler's result is ignored, so it is
diff --git a/doc/src/sgml/ref/allfiles.sgml b/doc/src/sgml/ref/allfiles.sgml
new file mode 100644
index 382d297..ef75780
*** a/doc/src/sgml/ref/allfiles.sgml
--- b/doc/src/sgml/ref/allfiles.sgml
*************** Complete list of usable sgml source file
*** 40,45 ****
--- 40,47 ----
<!ENTITY alterView SYSTEM "alter_view.sgml">
<!ENTITY analyze SYSTEM "analyze.sgml">
<!ENTITY begin SYSTEM "begin.sgml">
+ <!ENTITY checkFunction SYSTEM "check_function.sgml">
+ <!ENTITY checkTrigger SYSTEM "check_trigger.sgml">
<!ENTITY checkpoint SYSTEM "checkpoint.sgml">
<!ENTITY close SYSTEM "close.sgml">
<!ENTITY cluster SYSTEM "cluster.sgml">
diff --git a/doc/src/sgml/ref/check_function.sgml b/doc/src/sgml/ref/check_function.sgml
new file mode 100644
index ...6549846
*** a/doc/src/sgml/ref/check_function.sgml
--- b/doc/src/sgml/ref/check_function.sgml
***************
*** 0 ****
--- 1,138 ----
+ <!--
+ doc/src/sgml/ref/check_function.sgml
+ -->
+
+ <refentry id="SQL-CHECKFUNCTION">
+ <refmeta>
+ <refentrytitle>CHECK FUNCTION</refentrytitle>
+ <manvolnum>7</manvolnum>
+ <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+ <refname>CHECK FUNCTION</refname>
+ <refpurpose>perform an in-depth check of an existing function</refpurpose>
+ </refnamediv>
+
+ <indexterm zone="sql-checkfunction">
+ <primary>CHECK FUNCTION</primary>
+ </indexterm>
+
+ <refsynopsisdiv>
+ <synopsis>
+ CHECK FUNCTION <replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [, ...] ] )
+ | CHECK FUNCTION ALL [ IN LANGUAGE <replaceable class="parameter">lang_name</replaceable> ] [ IN SCHEMA <replaceable class="parameter">schema_name</replaceable> ] [ FOR ROLE <replaceable class="parameter">owner_name</replaceable> ]
+ </synopsis>
+ </refsynopsisdiv>
+
+ <refsect1 id="sql-checkfunction-description">
+ <title>Description</title>
+
+ <para>
+ <command>CHECK FUNCTION</command> performs an in-depth check of existing
+ functions if the procedural language supports it (currently only
+ <link linkend="plpgsql">PL/pgSQL</link> is defined with a check function).
+ The checks performed depend on the language, but typically include checks
+ for the existence of database objects referenced by the function or style
+ checks like defined but unused variables.
+ </para>
+
+ <para>
+ If a problem is found with a function, a <literal>WARNING</> will be raised.
+ </para>
+
+ <para>The form <command>CHECK FUNCTION ALL</command> will check all functions
+ matching the specified criteria. If no criteria are specified, functions in
+ the schemas <literal>pg_catalog</literal> and <literal>information_schema</literal>
+ will be excluded from the check.
+ </para>
+ </refsect1>
+
+ <refsect1>
+ <title>Parameters</title>
+
+ <variablelist>
+ <varlistentry>
+ <term><replaceable class="parameter">name</replaceable></term>
+
+ <listitem>
+ <para>
+ The name (optionally schema-qualified) of an existing function.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">argmode</replaceable></term>
+
+ <listitem>
+ <para>
+ The mode of an argument: <literal>IN</>, <literal>OUT</>,
+ <literal>INOUT</>, or <literal>VARIADIC</>.
+ If omitted, the default is <literal>IN</>.
+ Note that <command>CHECK FUNCTION</command> does not actually pay
+ any attention to <literal>OUT</> arguments, since only the input
+ arguments are needed to determine the function's identity.
+ So it is sufficient to list the <literal>IN</>, <literal>INOUT</>,
+ and <literal>VARIADIC</> arguments.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">argname</replaceable></term>
+
+ <listitem>
+ <para>
+ The name of an argument.
+ Note that <command>CHECK FUNCTION</command> does not actually pay
+ any attention to argument names, since only the argument data
+ types are needed to determine the function's identity.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">lang_name</replaceable></term>
+
+ <listitem>
+ <para>
+ The name of a procedural language.
+ Specifies that only functions written in this language should
+ be checked.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">schema_name</replaceable></term>
+
+ <listitem>
+ <para>
+ Specifies that only functions in this schema should be checked.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">owner_name</replaceable></term>
+
+ <listitem>
+ <para>
+ Specifies that only functions owned by this role should be checked.
+ </para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
+ </refsect1>
+
+ <refsect1 id="sql-checkfunction-compat">
+ <title>Compatibility</title>
+
+ <para>
+ The <command>CHECK FUNCTION</command> statement is a
+ <productname>PostgreSQL</productname> extension.
+ </para>
+ </refsect1>
+
+ </refentry>
diff --git a/doc/src/sgml/ref/check_trigger.sgml b/doc/src/sgml/ref/check_trigger.sgml
new file mode 100644
index ...d97b396
*** a/doc/src/sgml/ref/check_trigger.sgml
--- b/doc/src/sgml/ref/check_trigger.sgml
***************
*** 0 ****
--- 1,79 ----
+ <!--
+ doc/src/sgml/ref/check_trigger.sgml
+ -->
+
+ <refentry id="SQL-CHECKTRIGGER">
+ <refmeta>
+ <refentrytitle>CHECK TRIGGER</refentrytitle>
+ <manvolnum>7</manvolnum>
+ <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+ <refname>CHECK TRIGGER</refname>
+ <refpurpose>perform an in-depth check of an existing trigger function</refpurpose>
+ </refnamediv>
+
+ <indexterm zone="sql-checktrigger">
+ <primary>CHECK TRIGGER</primary>
+ </indexterm>
+
+ <refsynopsisdiv>
+ <synopsis>
+ CHECK TRIGGER <replaceable class="parameter">name</replaceable> ON <replaceable class="parameter">tablename</replaceable>
+ </synopsis>
+ </refsynopsisdiv>
+
+ <refsect1 id="sql-checktrigger-description">
+ <title>Description</title>
+
+ <para>
+ <command>CHECK FUNCTION</command> performs an in-depth check of a trigger
+ function if the procedural language supports it (currently only
+ <link linkend="plpgsql">PL/pgSQL</link> is defined with a check function).
+ The checks performed depend on the language, but typically include checks
+ for the existence of database objects referenced by the function or style
+ checks like defined but unused variables.
+ </para>
+
+ <para>
+ If a problem is found with the function, a <literal>WARNING</> will be raised.
+ </para>
+ </refsect1>
+
+ <refsect1>
+ <title>Parameters</title>
+
+ <variablelist>
+ <varlistentry>
+ <term><replaceable class="parameter">name</replaceable></term>
+ <listitem>
+ <para>
+ The name of a trigger. The trigger function for this trigger will
+ be checked.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">table</replaceable></term>
+ <listitem>
+ <para>
+ The name (optionally schema-qualified) of the table or view the trigger
+ is for.
+ </para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
+ </refsect1>
+
+ <refsect1 id="sql-checktrigger-compat">
+ <title>Compatibility</title>
+
+ <para>
+ The <command>CHECK TRIGGER</command> statement is a
+ <productname>PostgreSQL</productname> extension.
+ </para>
+ </refsect1>
+
+ </refentry>
diff --git a/doc/src/sgml/ref/create_language.sgml b/doc/src/sgml/ref/create_language.sgml
new file mode 100644
index 0995b13..34fc36e
*** a/doc/src/sgml/ref/create_language.sgml
--- b/doc/src/sgml/ref/create_language.sgml
*************** PostgreSQL documentation
*** 23,29 ****
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
--- 23,29 ----
<synopsis>
CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <replaceable class="parameter">name</replaceable>
! HANDLER <replaceable class="parameter">call_handler</replaceable> [ INLINE <replaceable class="parameter">inline_handler</replaceable> ] [ VALIDATOR <replaceable>valfunction</replaceable> ] [ CHECK <replaceable>checkfunction</replaceable> ]
</synopsis>
</refsynopsisdiv>
*************** CREATE [ OR REPLACE ] [ TRUSTED ] [ PROC
*** 217,222 ****
--- 217,249 ----
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><literal>CHECK</literal> <replaceable class="parameter">checkfunction</replaceable></term>
+
+ <listitem>
+ <para><replaceable class="parameter">checkfunction</replaceable> is the
+ name of a previously registered function that will be called
+ by <command>CHECK FUNCTION</command> and <command>CHECK TRIGGER</command>
+ to check the bodies of functions defined in the language.
+ If no check function is specified, this kind of check is not available.
+ The check function must take two arguments, one of type <type>oid</type>
+ (the OID of the function to be checked), and one of type <type>regclass</type>
+ (the table with the trigger for <command>CHECK TRIGGER</command>).
+ The check function will typically return <type>void</>.
+ </para>
+
+ <para>
+ A check function would typically perform an in-depth check of the function
+ body, for example that all referenced database objects exist, but it could
+ also check for stylistic problems like defined but unreferenced variables.
+ To signal a problem found during the check, the function should use the
+ <function>ereport()</function> to report a <literal>WARNING</>.
+ The return value of the function is ignored.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
diff --git a/doc/src/sgml/reference.sgml b/doc/src/sgml/reference.sgml
new file mode 100644
index 7326519..460794e
*** a/doc/src/sgml/reference.sgml
--- b/doc/src/sgml/reference.sgml
***************
*** 68,73 ****
--- 68,75 ----
&alterView;
&analyze;
&begin;
+ &checkFunction;
+ &checkTrigger;
&checkpoint;
&close;
&cluster;
Hello
2011/12/9 Albe Laurenz <laurenz.albe@wien.gv.at>:
Pavel Stehule wrote:
there is fixed version
Here is my attempt at a doc patch.
Could you add it to your patch so that all is in a single patch?
there is merged patch
Thank you
Regards
Pavel
Show quoted text
Yours,
Laurenz Albe
Attachments:
Pavel Stehule wrote:
there is merged patch
Works fine, except that there are still missing const qualifiers
in copyfuncs.c and equalfuncs.c that lead to compiler warnings.
One thing I forgot to mention:
I thought there was a consensus to add a WITH() or OPTIONS() clause
to pass options to the checker function:
http://archives.postgresql.org/message-id/12568.1322669638@sss.pgh.pa.us
I think this should be there so that the API does not have to be
changed in the future.
Yours,
Laurenz Albe
hello
2011/12/12 Albe Laurenz <laurenz.albe@wien.gv.at>:
Pavel Stehule wrote:
there is merged patch
Works fine, except that there are still missing const qualifiers
in copyfuncs.c and equalfuncs.c that lead to compiler warnings.One thing I forgot to mention:
I thought there was a consensus to add a WITH() or OPTIONS() clause
to pass options to the checker function:
http://archives.postgresql.org/message-id/12568.1322669638@sss.pgh.pa.usI think this should be there so that the API does not have to be
changed in the future.
there is just one question - how propagate options to check functions
I am thinking about third parameter - probably text array
??
Regards
Pavel
Show quoted text
Yours,
Laurenz Albe
Pavel Stehule wrote:
One thing I forgot to mention:
I thought there was a consensus to add a WITH() or OPTIONS() clause
to pass options to the checker function:
http://archives.postgresql.org/message-id/12568.1322669638@sss.pgh.pa.usI think this should be there so that the API does not have to be
changed in the future.
there is just one question - how propagate options to check functions
I am thinking about third parameter - probably text array
Either that, or couldn't you pass an option List as data type "internal"?
I don't know what is most natural or convenient.
Yours,
Laurenz Albe
2011/12/13 Albe Laurenz <laurenz.albe@wien.gv.at>:
Pavel Stehule wrote:
One thing I forgot to mention:
I thought there was a consensus to add a WITH() or OPTIONS() clause
to pass options to the checker function:
http://archives.postgresql.org/message-id/12568.1322669638@sss.pgh.pa.usI think this should be there so that the API does not have to be
changed in the future.there is just one question - how propagate options to check functions
I am thinking about third parameter - probably text array
Either that, or couldn't you pass an option List as data type "internal"?
this is question - internal is most simply solution, but then we
cannot to call check function directly
Regards
Pavel
Show quoted text
I don't know what is most natural or convenient.
Yours,
Laurenz Albe
Pavel Stehule <pavel.stehule@gmail.com> writes:
2011/12/13 Albe Laurenz <laurenz.albe@wien.gv.at>:
Either that, or couldn't you pass an option List as data type "internal"?
this is question - internal is most simply solution, but then we
cannot to call check function directly
Yeah, one of the proposals for allowing people to specify complicated
conditions about what to check was to tell them to do
select checker(oid) from pg_proc where any-random-condition;
If the checker isn't user-callable then we lose that escape hatch, and
the only selection conditions that will ever be possible are the ones
we take the trouble to shoehorn into the CHECK FUNCTION statement.
Doesn't seem like a good thing to me.
regards, tom lane
2011/12/13 Tom Lane <tgl@sss.pgh.pa.us>:
Pavel Stehule <pavel.stehule@gmail.com> writes:
2011/12/13 Albe Laurenz <laurenz.albe@wien.gv.at>:
Either that, or couldn't you pass an option List as data type "internal"?
this is question - internal is most simply solution, but then we
cannot to call check function directlyYeah, one of the proposals for allowing people to specify complicated
conditions about what to check was to tell them to do
select checker(oid) from pg_proc where any-random-condition;
If the checker isn't user-callable then we lose that escape hatch, and
the only selection conditions that will ever be possible are the ones
we take the trouble to shoehorn into the CHECK FUNCTION statement.
Doesn't seem like a good thing to me.
yes, it is reason why I thinking just about string array.
I have not idea about other PL, but options for plpgsql can be one
word and checker function can simply parse two or more words options.
Now I would to implement flags "quite" - ignore NOTIFY messages and
"fatal_errors" to stop on first error.
Regards
Pavel
Show quoted text
regards, tom lane
Hello
2011/12/12 Albe Laurenz <laurenz.albe@wien.gv.at>:
Pavel Stehule wrote:
there is merged patch
Works fine, except that there are still missing const qualifiers
in copyfuncs.c and equalfuncs.c that lead to compiler warnings.One thing I forgot to mention:
I thought there was a consensus to add a WITH() or OPTIONS() clause
to pass options to the checker function:
http://archives.postgresql.org/message-id/12568.1322669638@sss.pgh.pa.usI think this should be there so that the API does not have to be
changed in the future.
changes:
* fixed warnings
* support for options - actually only two options are supported -
quite and fatal_errors
these options are +/- useful - main reason for their existence is
testing of support of options - processing on CHECK ... stmt side and
processing on checker function side.
options are send as 2d text array - some like
'{{quite,on},{fatal_errors,on}} - so direct call of checker function
is possible
* regress test for multi check
Regards
Pavel
Show quoted text
Yours,
Laurenz Albe
Attachments:
Pavel Stehule wrote:
changes:
* fixed warnings
* support for options - actually only two options are supported -
quite and fatal_errorsthese options are +/- useful - main reason for their existence is
testing of support of options - processing on CHECK ... stmt side and
processing on checker function side.options are send as 2d text array - some like
'{{quite,on},{fatal_errors,on}} - so direct call of checker function
is possible* regress test for multi check
First of all: It should be "quiet" and not "quite".
The patch applies and builds fine.
It fails one of ist own regression tests, here is the diff:
*** /postgres/cvs/postgresql/src/test/regress/expected/plpgsql.out 2011-12-14 11:50:44.000000000 +0100
--- /postgres/cvs/postgresql/src/test/regress/results/plpgsql.out 2011-12-14 16:19:45.000000000 +0100
***************
*** 4975,4991 ****
end;
$$ language plpgsql;
check function all in schema plpgsql_check;
- NOTICE: checked function "plpgsql_check.fce1()"
WARNING: error in function "plpgsql_check.fce2()"
DETAIL: too few parameters specified for RAISE
CONTEXT: line 3 at RAISE
NOTICE: checked function "plpgsql_check.fce3()"
check function all in schema plpgsql_check with (quite);
WARNING: error in function "plpgsql_check.fce2()"
DETAIL: too few parameters specified for RAISE
CONTEXT: line 3 at RAISE
check function all in schema plpgsql_check with (fatal_errors);
- NOTICE: checked function "plpgsql_check.fce1()"
ERROR: too few parameters specified for RAISE
CONTEXT: PL/pgSQL function "fce2" line 3 at RAISE
check function all in schema plpgsql_check with (quite, fatal_errors on);
--- 4975,4990 ----
end;
$$ language plpgsql;
check function all in schema plpgsql_check;
WARNING: error in function "plpgsql_check.fce2()"
DETAIL: too few parameters specified for RAISE
CONTEXT: line 3 at RAISE
NOTICE: checked function "plpgsql_check.fce3()"
+ NOTICE: checked function "plpgsql_check.fce1()"
check function all in schema plpgsql_check with (quite);
WARNING: error in function "plpgsql_check.fce2()"
DETAIL: too few parameters specified for RAISE
CONTEXT: line 3 at RAISE
check function all in schema plpgsql_check with (fatal_errors);
ERROR: too few parameters specified for RAISE
CONTEXT: PL/pgSQL function "fce2" line 3 at RAISE
check function all in schema plpgsql_check with (quite, fatal_errors on);
======================================================================
The "quiet" option is not very intuitive:
test=> CHECK FUNCTION ALL WITH (quite 'off');
NOTICE: skip check function "atrig()", it is trigger function
NOTICE: skip check function "perl_max(integer,integer)", language "plperl" hasn't checker function
NOTICE: checked function "ok()"
NOTICE: checked function "newstyle(integer)"
CHECK FUNCTION
test=> CHECK FUNCTION ALL WITH (quite 'on');
NOTICE: skip check function "atrig()", it is trigger function
CHECK FUNCTION
I understand that "quiet" cannot silence this message, nor
"skip ..., uses C language" and "skip ..., it uses internal language",
but that means that it is not very useful as it is.
If all we need is a sample option, I think that "fatal_errors" is
enough, and I think that is an option that can be useful.
Yours,
Laurenz Albe
Hello
so removed "quite" option
and removed multiple check regression tests also - there is missing
explicit order of function checking, so regress tests can fail :(
Regards
Pavel
2011/12/14 Albe Laurenz <laurenz.albe@wien.gv.at>:
Show quoted text
Pavel Stehule wrote:
changes:
* fixed warnings
* support for options - actually only two options are supported -
quite and fatal_errorsthese options are +/- useful - main reason for their existence is
testing of support of options - processing on CHECK ... stmt side and
processing on checker function side.options are send as 2d text array - some like
'{{quite,on},{fatal_errors,on}} - so direct call of checker function
is possible* regress test for multi check
First of all: It should be "quiet" and not "quite".
The patch applies and builds fine.
It fails one of ist own regression tests, here is the diff:
*** /postgres/cvs/postgresql/src/test/regress/expected/plpgsql.out 2011-12-14 11:50:44.000000000 +0100 --- /postgres/cvs/postgresql/src/test/regress/results/plpgsql.out 2011-12-14 16:19:45.000000000 +0100 *************** *** 4975,4991 **** end; $$ language plpgsql; check function all in schema plpgsql_check; - NOTICE: checked function "plpgsql_check.fce1()" WARNING: error in function "plpgsql_check.fce2()" DETAIL: too few parameters specified for RAISE CONTEXT: line 3 at RAISE NOTICE: checked function "plpgsql_check.fce3()" check function all in schema plpgsql_check with (quite); WARNING: error in function "plpgsql_check.fce2()" DETAIL: too few parameters specified for RAISE CONTEXT: line 3 at RAISE check function all in schema plpgsql_check with (fatal_errors); - NOTICE: checked function "plpgsql_check.fce1()" ERROR: too few parameters specified for RAISE CONTEXT: PL/pgSQL function "fce2" line 3 at RAISE check function all in schema plpgsql_check with (quite, fatal_errors on); --- 4975,4990 ---- end; $$ language plpgsql; check function all in schema plpgsql_check; WARNING: error in function "plpgsql_check.fce2()" DETAIL: too few parameters specified for RAISE CONTEXT: line 3 at RAISE NOTICE: checked function "plpgsql_check.fce3()" + NOTICE: checked function "plpgsql_check.fce1()" check function all in schema plpgsql_check with (quite); WARNING: error in function "plpgsql_check.fce2()" DETAIL: too few parameters specified for RAISE CONTEXT: line 3 at RAISE check function all in schema plpgsql_check with (fatal_errors); ERROR: too few parameters specified for RAISE CONTEXT: PL/pgSQL function "fce2" line 3 at RAISE check function all in schema plpgsql_check with (quite, fatal_errors on);======================================================================
The "quiet" option is not very intuitive:
test=> CHECK FUNCTION ALL WITH (quite 'off');
NOTICE: skip check function "atrig()", it is trigger function
NOTICE: skip check function "perl_max(integer,integer)", language "plperl" hasn't checker function
NOTICE: checked function "ok()"
NOTICE: checked function "newstyle(integer)"
CHECK FUNCTIONtest=> CHECK FUNCTION ALL WITH (quite 'on');
NOTICE: skip check function "atrig()", it is trigger function
CHECK FUNCTIONI understand that "quiet" cannot silence this message, nor
"skip ..., uses C language" and "skip ..., it uses internal language",
but that means that it is not very useful as it is.If all we need is a sample option, I think that "fatal_errors" is
enough, and I think that is an option that can be useful.Yours,
Laurenz Albe
Attachments:
check_function-2011-12-14-3.diff.gzapplication/x-gzip; name=check_function-2011-12-14-3.diff.gzDownload
�X�N check_function-2011-12-14-3.diff �\{s�F��[�]mB�$��DI�y�P��:E�It����Ck`0�d&��~�=@�R��[�-�������u���q�E�����|�N��\4����|��l���v�y�Z������I����f��|V��wR�9��u����^�u��)"�|f���^�s|�fT����^����������8z���)������I����$^��������]p�=�����C�x�s�Y�~�{�����]�'<v��g�����}%��
������ �%���S,��P��!��7hHC�dT��&_���x�C��07�cD����f��M�(d��I�_���MJ����K'����F��a��^
'�����>��|��f����kgQ�����U�H����E�t�����h�m�����3FL��E��������yA(
x�L������A���;��CsX�����:�o������i����V7U�n����N��*u�\��� ��<��W���~��Tc���:�"��3,�'��b'���u*�O��'Y�|���A�� jC��| ����9�W4o�V��B��r�yt��>�lfI*�k���N��'�V��n����;�f��Uk��R���A������J~8G���j�q���l��>L�%sc�$�-��_:�����{k�FF:5���d�C�"|4�!�T]'d�
`��"v����!��{��e������<�|��j����?�����;���H�N.��N�{N�.H�r9��c��C��\����!FS�8V��%�Q�`���w}���!(kh=�D%��`�S'!~ �R�Zx���^A��F����d$��=]��8Q����e��
y���f�������(`����L�~�������>)�T��m��R�~-�����H�#�|�b�>;���z�hY��4��,�)����/� �~_k���tJ��4�aQL;66N��y���u�,x�����')Dh7�k����tw��������3�.���{�O�r-�S�
�-*b��3:Rs1��7_���@@��,��g��F��N��Z�|# �qe��c�E�cg�*NW�m,��^�s�1�X�f�y���1������EK'��q�q�c���P�t/�.�=�`�@<h���������V����7x��i��i��H�{u?d����G9|�� �I���B� �F���Rl�7��)��@T��8w�A���S���H��3��^M������.
�����5�^�����#H���X�V��3�H&�0)4��X��?8�i�)�0hE�}���p�Pe��|Pm���
�`��?������ �` �h.w�-��r�5z&�">�2�&��v��/$������(z!����[�A� �d�����(|��(��q���c�BQ�="iA�$A�F]���1G���3:t�qps5�z�
��$lSp���}.;W����1HC� b����#���ZmuXq���6�[����P���bs�0�W1���6����e~5�DPk��f��m�����CX���_�t!t�4�G��N�w�*@����nN1w�:��t������cO{���FW������ �|������o'���>UM������g��7�r�
����Nw|��u�*��y��v�+���.T5�jP�w��_Q_e8��U���iZU�/�/�!Uv�6��P��T�{N��)R�#�n�uZg�����eD�5��
�������6@�S�� ���Jax3LF�gv}�nFo.�{�on�����7�Kx�\�~;x=b�`���������7�}��n���j�k���[��gM���W�����#��������.�W�� -W ������`r�e�$�m��p��������'�K��VU��V�^U�$1[�C&��DE(���^������{���Z��z }arW���B����&_��;����|�����V.��C�#���)�&IK���A�O���J�"8��>D��&!D�y#�� d��
��R���w�'���$&�(���Q�����14�7.$%1�3��I��dvW��(��<JySK(#������,�&�H�hL(��=!J�O)�JIt�p�
1�2|�&��rEte���i&>��Euqu��I���������R����(�HGP' ���S^p�NF�}8Y���D���5����:A(�")M'F�T �[����
��j"�uh�f�7[�Ds �i��������jYQa�~�-]e�2���[Q��[x��`��|KI���h�����2{���t�����M�� ?J��GC'g�v��<n�4OwCQ��'���Z���#|<����&
z�d�):y!��u&$��XC�J#S�1@�@��E)���o�eyt�:���q�����=��if��:H�u�89j�n��C��T��Z���N1��?O���� ��Z��1�{����u2��_QhG���*56]@����\�V� #z�z����������>0���R'�o���]���:���\��&��)S���E*7�V�x:��iW���!!N���+�*��? �a���*L�KVV��t�
���rC��y">�#��,K���qV(��c���/#/����:��� �����~�5�w4����w��#tdz��L�*
���b\^Rt��(1��p�D��?x�� �Y:ed^adv}�(+�C��2"�Gl���1����X~
I_���T�h�n#�M�Q�#O��=�Da���0k�Pj�����`B��+{�K�x�V Z��RY�1<F���B��
���F��.`}�'�U�u@ ���)w�k��0����������e|H���P��P=�|� �g� DS�O dw5I@����� ���k-z���8}!���Y K�s9���R�g��HP�$�^&*D�&��_hxY8
�\X���������e�H�(�dy�$�Bcn&2P\`�w&�Ttc�����H���<�LM��2�'t�1p��\vO[J�Z@�iD�q�Lc����������3���6bk�f���E9�"��Nw+������������?�������V�ZeL��
Y�"������8|������N>��j4�S�Z�|�v�i��6TUtV�K���a&,�-�$����:����v��}?n�����>�\9�}U'�0'A��$InjvF�d���~�Z')!���p�����
-��.w��{8h�Igu�/d�5����!e�`�mM�+�z������u�f����$z0b`[3L5:MC���k��}�J��\�"�d� ���
<h�b��L���\�LV-�s�y�+
2~��vEC�sx_��"��-GaP|�!
����vq7�) c����$������Pov�0���aB�z����l7O��&�<�`��s!�0f�K�S���[w�R���k�z'��^�����m����N��>�������S�'��%����=��S�ON����/6�gs�����$ ���g�?��gI�)j���{<eHGk����rI4��T�OO%9Z��erI����T�=��%���R#� ��7��y{����������~�<�(����/��W'=R���B��@~n��u������m�~��P���pK����V�u��tb��t'�m:]���� J�G���[�f����~]d�X�;�x!Zb/>��\~O]��9�<����(��z]�\�p���-�� `u��I� ��Z'�;���i���99��zu��vd O� �K��}�u��s�L��~Q��;��J�N��X�|����&��z��3o�)kA��nY�t{r��6�P�6�_)o�:��Q�6��4uJ_������6��������h�]J9
��=R������ h!�[�`��C������]/_���C��a��}~�o�oq�~c���c���J�mj����x,�_xE�Y;>�����hS���`�.�q�{[J'k���5�l�O����^�%}(����[�e����Cn�&�ceU�1m6Ok`=�oJ���Z��kc���$C�qx�����r'v�=W�f6
|C����!���.".�o �� D��Y
p�N��\��x_�/��"�[�Sz[I"��� ��C����w^��0�����f�U�}y���>