Thread

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Add const qualifiers to node inspection functions

  1. patch: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-11-26T08:51:17Z

    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
    
  2. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-11-29T12:17:01Z

    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
    
  3. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-11-29T14:21:55Z

    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
    
    >
    > Yours,
    > Laurenz Albe
    >
    
    
  4. Re: review: CHECK FUNCTION statement

    Tom Lane <tgl@sss.pgh.pa.us> — 2011-11-29T17:15:26Z

    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
    
    
  5. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-11-29T17:37:24Z

    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
    
    >
    >                        regards, tom lane
    >
    
    
  6. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2011-11-29T18:38:28Z

    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
    
    
  7. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-11-29T19:37:15Z

    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>:
    > 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
    >
    
  8. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-11-29T19:38:06Z

    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
    
    >
    > 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
    >>
    >
    
    
  9. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-11-30T15:23:16Z

    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
    
  10. Re: review: CHECK FUNCTION statement

    Tom Lane <tgl@sss.pgh.pa.us> — 2011-11-30T15:53:42Z

    "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
    
    
  11. Re: review: CHECK FUNCTION statement

    Robert Haas <robertmhaas@gmail.com> — 2011-11-30T16:03:59Z

    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
    
    
  12. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-11-30T16:06:02Z

    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 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
    >
    
    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
    
    
  13. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2011-11-30T16:09:13Z

    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
    
    
  14. Re: review: CHECK FUNCTION statement

    Tom Lane <tgl@sss.pgh.pa.us> — 2011-11-30T16:13:58Z

    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
    
    
  15. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-11-30T16:14:25Z

    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
    
    
    
    >
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    >
    
    
  16. Re: review: CHECK FUNCTION statement

    Tom Lane <tgl@sss.pgh.pa.us> — 2011-11-30T16:19:05Z

    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
    
    
  17. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-11-30T16:39:13Z

    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
    
    >
    >                        regards, tom lane
    >
    
    
  18. Re: review: CHECK FUNCTION statement

    Tom Lane <tgl@sss.pgh.pa.us> — 2011-11-30T16:58:46Z

    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
    
    
  19. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-11-30T18:21:54Z

    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
    
    
  20. Re: review: CHECK FUNCTION statement

    Tom Lane <tgl@sss.pgh.pa.us> — 2011-11-30T18:30:03Z

    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
    
    
  21. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-11-30T18:34:07Z

    Hello
    
    updated patch with documentation
    
    Regards
    
    Pavel
    
  22. Re: review: CHECK FUNCTION statement

    Peter Eisentraut <peter_e@gmx.net> — 2011-12-02T10:14:17Z

    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?
    
    
    
    
  23. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-12-02T10:52:19Z

    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
    
    
  24. Re: review: CHECK FUNCTION statement

    Tom Lane <tgl@sss.pgh.pa.us> — 2011-12-02T15:05:15Z

    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
    
    
  25. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-02T18:45:16Z

    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
    
    
    
    >                        regards, tom lane
    
    
  26. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-02T20:33:00Z

    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
    
    
  27. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-02T20:47:02Z

    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
    
    
  28. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-12-03T11:02:13Z

    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
    
    
  29. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-07T07:23:32Z

    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>:
    > 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
    
  30. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-12-07T14:46:14Z

    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
    
  31. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-07T15:17:59Z

    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
    >
    > Yours,
    > Laurenz Albe
    
    
  32. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-12-07T15:30:50Z

    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
    
  33. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-08T14:07:07Z

    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>:
    > 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
    
  34. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-08T15:34:10Z

    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>:
    > 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>:
    >> 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
    
  35. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-12-09T11:27:36Z

    Pavel Stehule wrote:
    > 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
    
    > 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
    
  36. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-09T14:35:12Z

    Hello
    
    there is fixed version
    
    Regards
    
    Pavel
    
    2011/12/9 Albe Laurenz <laurenz.albe@wien.gv.at>:
    > Pavel Stehule wrote:
    >> 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
    >
    >> 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
    
  37. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-12-09T16:02:19Z

    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
    
  38. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-09T17:08:30Z

    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
    
    > Yours,
    > Laurenz Albe
    
  39. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-12-12T15:49:18Z

    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
    
  40. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-12T15:55:35Z

    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.us
    >
    > I 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
    
    > Yours,
    > Laurenz Albe
    
    
  41. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-12-13T10:01:27Z

    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.us
    >>
    >> I 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
    
  42. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-13T12:59:51Z

    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.us
    >>>
    >>> I 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
    
    
    
    
    > I don't know what is most natural or convenient.
    >
    > Yours,
    > Laurenz Albe
    
    
  43. Re: review: CHECK FUNCTION statement

    Tom Lane <tgl@sss.pgh.pa.us> — 2011-12-13T15:37:30Z

    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
    
    
  44. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-13T16:55:40Z

    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 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.
    
    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
    
    
    >
    >                        regards, tom lane
    
    
  45. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-14T06:33:03Z

    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.us
    >
    > I 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
    
    > Yours,
    > Laurenz Albe
    
  46. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-12-14T15:30:02Z

    Pavel Stehule wrote:
    > 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
    
    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
    
  47. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-14T20:52:08Z

    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>:
    > Pavel Stehule wrote:
    >> 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
    >
    > 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
    
  48. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-12-15T09:38:55Z

    Pavel Stehule wrote:
    > so removed "quite" option
    > and removed multiple check regression tests also - there is missing
    > explicit order of function checking, so regress tests can fail :(
    
    There seems to be a problem with the SET clause of CREATE FUNCTION:
    
    ftest=# CREATE OR REPLACE FUNCTION a(integer) RETURNS integer
            LANGUAGE plpgsql AS 'BEGIN RETURN 2*$1; END';
    CREATE FUNCTION
    ftest=# CHECK FUNCTION a(integer);
    NOTICE:  checked function "a(integer)"
    CHECK FUNCTION
    ftest=# CREATE OR REPLACE FUNCTION a(integer) RETURNS integer
            LANGUAGE plpgsql SET search_path=public AS 'BEGIN RETURN 2*$1; END';
    CREATE FUNCTION
    ftest=# CHECK FUNCTION a(integer);
    The connection to the server was lost. Attempting reset: Failed.
    !>
    
    Yours,
    Laurenz Albe
    
  49. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-15T11:36:24Z

    Hello
    
    2011/12/15 Albe Laurenz <laurenz.albe@wien.gv.at>:
    > Pavel Stehule wrote:
    >> so removed "quite" option
    >> and removed multiple check regression tests also - there is missing
    >> explicit order of function checking, so regress tests can fail :(
    >
    > There seems to be a problem with the SET clause of CREATE FUNCTION:
    >
    > ftest=# CREATE OR REPLACE FUNCTION a(integer) RETURNS integer
    >        LANGUAGE plpgsql AS 'BEGIN RETURN 2*$1; END';
    > CREATE FUNCTION
    > ftest=# CHECK FUNCTION a(integer);
    > NOTICE:  checked function "a(integer)"
    > CHECK FUNCTION
    > ftest=# CREATE OR REPLACE FUNCTION a(integer) RETURNS integer
    >        LANGUAGE plpgsql SET search_path=public AS 'BEGIN RETURN 2*$1; END';
    > CREATE FUNCTION
    > ftest=# CHECK FUNCTION a(integer);
    > The connection to the server was lost. Attempting reset: Failed.
    > !>
    >
    
    There was bug - missing detoast call
    
    fixed
    
    Regards
    
    Pavel
    
    
    > Yours,
    > Laurenz Albe
    
  50. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-15T13:04:37Z

    Hello
    
    one small update - better emulation of environment for security
    definer functions
    
    Regards
    
    Pavel
    
  51. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2011-12-16T13:49:43Z

    Pavel Stehule wrote:
    > one small update - better emulation of environment for security
    > definer functions
    
    Patch applies and compiles fine, core functionality works fine.
    
    I found a little bug:
    
    In backend/commands/functioncmds.c,
    function CheckFunction(CheckFunctionStmt *stmt),
    while you perform the table scan for CHECK FUNCTION ALL,
    you use the variable funcOid to hold the OID of the current
    function in the loop.
    
    If no appropriate function is found in the loop, the
    check immediately after the table scan will not succeed
    because funcOid holds the OID of the last function scanned
    in the loop.
    As a consequence, CheckFunctionById is called for this
    function.
    
    Here is a demonstration:
    test=> CHECK FUNCTION ALL IN SCHEMA pg_catalog;
    [...]
    NOTICE:  skip check function "plpgsql_checker(oid,regclass,text[])", uses C language
    NOTICE:  skip check function "plperl_call_handler()", uses C language
    NOTICE:  skip check function "plperl_inline_handler(internal)", uses C language
    NOTICE:  skip check function "plperl_validator(oid)", uses C language
    NOTICE:  skip check function "plperl_validator(oid)", language "c" hasn't checker function
    CHECK FUNCTION
    
    when it should be:
    test=> CHECK FUNCTION ALL IN SCHEMA pg_catalog;
    [...]
    NOTICE:  skip check function "plpgsql_checker(oid,regclass,text[])", uses C language
    NOTICE:  skip check function "plperl_call_handler()", uses C language
    NOTICE:  skip check function "plperl_inline_handler(internal)", uses C language
    NOTICE:  skip check function "plperl_validator(oid)", uses C language
    NOTICE:  nothing to check
    CHECK FUNCTION
    
    
    Another thing struck me as odd:
    
    You have the option "fatal_errors" for the checker function, but you
    special case it in CheckFunction(CheckFunctionStmt *stmt) and turn
    errors to warnings if it is not set.
    
    Wouldn't it be better to have the checker function ereport a WARNING
    or an ERROR depending on the setting? Options should be handled by the
    checker function.
    
    Yours,
    Laurenz Albe
    
  52. Re: review: CHECK FUNCTION statement

    Greg Smith <greg@2ndquadrant.com> — 2011-12-16T18:49:48Z

    I just poked at this a bit myself to see how the patch looked.  There's 
    just over 4000 lines in the diff.  Even though 1/4 of that is tests, 
    which is itself encouraging, that's still a good sized feature.  The 
    rate at which code here has still been changing regularly here has me 
    nervous about considering this a commit candidate right now though.  It 
    seems like it still needs a bit more time to have problems squeezed out 
    still.
    
    Two ideas I was thinking about here:
    
    -If you take a step back and look at where the problem parts of the code 
    have been recently, are there any new tests or assertions you might add 
    to try and detect problems like that in the future?  I haven't been 
    following this closely enough to have any suggestions where, and there 
    is a lot of error checking aimed at logging already; maybe there's 
    nothing new to chase there.
    
    -Can we find some larger functions you haven't tested this against yet 
    to throw at it?  It seems able to consume all the cases you've 
    constructed for it; it would be nice to find some brand new ones it's 
    never seen before to check.
    
    This has made a lot of progress and seems it will be a good commit 
    candidate for the next CF.  I think it justs a bit more time than we 
    have left in this CommitFest for it right now, particularly given the 
    size of the patch.  I'm turning this one into "returned with feedback", 
    but as a mediocre pl/pgsql author I'm hoping to see more updates still.
    
    -- 
    Greg Smith   2ndQuadrant US    greg@2ndQuadrant.com   Baltimore, MD
    PostgreSQL Training, Services, and 24x7 Support  www.2ndQuadrant.us
    
    
    
  53. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-17T20:37:36Z

    2011/12/16 Albe Laurenz <laurenz.albe@wien.gv.at>:
    > Pavel Stehule wrote:
    >> one small update - better emulation of environment for security
    >> definer functions
    >
    > Patch applies and compiles fine, core functionality works fine.
    >
    > I found a little bug:
    >
    > In backend/commands/functioncmds.c,
    > function CheckFunction(CheckFunctionStmt *stmt),
    > while you perform the table scan for CHECK FUNCTION ALL,
    > you use the variable funcOid to hold the OID of the current
    > function in the loop.
    >
    > If no appropriate function is found in the loop, the
    > check immediately after the table scan will not succeed
    > because funcOid holds the OID of the last function scanned
    > in the loop.
    > As a consequence, CheckFunctionById is called for this
    > function.
    >
    > Here is a demonstration:
    > test=> CHECK FUNCTION ALL IN SCHEMA pg_catalog;
    > [...]
    > NOTICE:  skip check function "plpgsql_checker(oid,regclass,text[])", uses C language
    > NOTICE:  skip check function "plperl_call_handler()", uses C language
    > NOTICE:  skip check function "plperl_inline_handler(internal)", uses C language
    > NOTICE:  skip check function "plperl_validator(oid)", uses C language
    > NOTICE:  skip check function "plperl_validator(oid)", language "c" hasn't checker function
    > CHECK FUNCTION
    >
    > when it should be:
    > test=> CHECK FUNCTION ALL IN SCHEMA pg_catalog;
    > [...]
    > NOTICE:  skip check function "plpgsql_checker(oid,regclass,text[])", uses C language
    > NOTICE:  skip check function "plperl_call_handler()", uses C language
    > NOTICE:  skip check function "plperl_inline_handler(internal)", uses C language
    > NOTICE:  skip check function "plperl_validator(oid)", uses C language
    > NOTICE:  nothing to check
    > CHECK FUNCTION
    
    I'll fix it
    >
    >
    > Another thing struck me as odd:
    >
    > You have the option "fatal_errors" for the checker function, but you
    > special case it in CheckFunction(CheckFunctionStmt *stmt) and turn
    > errors to warnings if it is not set.
    >
    > Wouldn't it be better to have the checker function ereport a WARNING
    > or an ERROR depending on the setting? Options should be handled by the
    > checker function.
    
    The behave that I use, is more rubust and there is only a few lines of
    code more.
    
    a) It ensure expectable behave for third party checker function -
    exception in checker function doesn't break a multi statement check
    function
    b) It can ensure same format of error message - because it is
    transformed on top level
    
    Regards
    
    Pavel
    
    >
    > Yours,
    > Laurenz Albe
    
    
  54. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-17T21:00:56Z

    2011/12/16 Greg Smith <greg@2ndquadrant.com>:
    > I just poked at this a bit myself to see how the patch looked.  There's just
    > over 4000 lines in the diff.  Even though 1/4 of that is tests, which is
    > itself encouraging, that's still a good sized feature.  The rate at which
    > code here has still been changing regularly here has me nervous about
    > considering this a commit candidate right now though.  It seems like it
    > still needs a bit more time to have problems squeezed out still.
    >
    > Two ideas I was thinking about here:
    >
    > -If you take a step back and look at where the problem parts of the code
    > have been recently, are there any new tests or assertions you might add to
    > try and detect problems like that in the future?  I haven't been following
    > this closely enough to have any suggestions where, and there is a lot of
    > error checking aimed at logging already; maybe there's nothing new to chase
    > there.
    
    last bug was based on wrong untoasting of function parameters - and it
    was hang on assertion - so it was ok
    
    I can recheck code where I can add asserts
    
    There is known issue - I cannot to check multi check statement in
    regress tests, because results (notices, errors) can be in random
    order. We don't sort functions by oid in check_functions_lookup.
    
    
    >
    > -Can we find some larger functions you haven't tested this against yet to
    > throw at it?  It seems able to consume all the cases you've constructed for
    > it; it would be nice to find some brand new ones it's never seen before to
    > check.
    >
    
    I use it for checking of my most large plpgsql project - it is about
    300KB plpgsql procedures - but this code is not free - and this module
    helps to find lot of bugs.
    
    I have plan to check a more functions from regress tests - but these
    tests are no bugs - I checked almost all  four months ago - dynamic
    sql based and temp table based cannot check.
    
    > This has made a lot of progress and seems it will be a good commit candidate
    > for the next CF.  I think it justs a bit more time than we have left in this
    > CommitFest for it right now, particularly given the size of the patch.  I'm
    > turning this one into "returned with feedback", but as a mediocre pl/pgsql
    > author I'm hoping to see more updates still.
    
    I'll send update early
    
    Regards
    
    Pavel
    
    >
    > --
    > Greg Smith   2ndQuadrant US    greg@2ndQuadrant.com   Baltimore, MD
    > PostgreSQL Training, Services, and 24x7 Support  www.2ndQuadrant.us
    >
    >
    > --
    > Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
    > To make changes to your subscription:
    > http://www.postgresql.org/mailpref/pgsql-hackers
    
    
  55. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-17T21:25:02Z

    Hello
    
    >
    > You have the option "fatal_errors" for the checker function, but you
    > special case it in CheckFunction(CheckFunctionStmt *stmt) and turn
    > errors to warnings if it is not set.
    >
    > Wouldn't it be better to have the checker function ereport a WARNING
    > or an ERROR depending on the setting? Options should be handled by the
    > checker function.
    >
    
    A would to process fatal_errors out of checker function - just it is
    more robust. This flag has not too sense in plpgsql - but can have a
    more sense in other languages.
    
    But I'll think again about flags
    
    note about warnings and errors. Warnings are useless on checker
    function level, because they are just shown, but they cannot be
    trapped.
    
    maybe result based on tuplestore can be better - I have to look on it.
    
    Regards
    
    Pavel
    
    
    > Yours,
    > Laurenz Albe
    
    
  56. Re: review: CHECK FUNCTION statement

    Greg Smith <greg@2ndquadrant.com> — 2011-12-19T14:56:25Z

    On 12/17/2011 04:00 PM, Pavel Stehule wrote:
    > I use it for checking of my most large plpgsql project - it is about
    > 300KB plpgsql procedures - but this code is not free - and this module
    > helps to find lot of bugs.
    
    Great.  If you continue to check against that regularly, that makes me 
    feel better.  I was guessing you had a large body of such source code 
    around, and knowing it executed correctly against all of it improves my 
    confidence here.
    
    -- 
    Greg Smith   2ndQuadrant US    greg@2ndQuadrant.com   Baltimore, MD
    PostgreSQL Training, Services, and 24x7 Support  www.2ndQuadrant.us
    
    
    
  57. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2011-12-19T15:47:25Z

    2011/12/19 Greg Smith <greg@2ndquadrant.com>:
    > On 12/17/2011 04:00 PM, Pavel Stehule wrote:
    >>
    >> I use it for checking of my most large plpgsql project - it is about
    >> 300KB plpgsql procedures - but this code is not free - and this module
    >> helps to find lot of bugs.
    >
    >
    > Great.  If you continue to check against that regularly, that makes me feel
    > better.  I was guessing you had a large body of such source code around, and
    > knowing it executed correctly against all of it improves my confidence here.
    >
    
    I am not alone
    
    a subset is used in plpgsql_lint and I know about some commercial
    subjects that use it too.
    
    https://github.com/okbob/plpgsql_lint
    
    but code in check function is little newer. It can interesting test
    some code that is wroted by person with background from other db,
    because they use a different patterns
    
    I don't use a explicit cursors for example - on other hand, I use
    exception intensively in my last project. We can ask people from
    LedgerSMB about testing if somebody has contact
    
    
    Regards
    
    Pavel
    
    
    
    >
    > --
    > Greg Smith   2ndQuadrant US    greg@2ndQuadrant.com   Baltimore, MD
    > PostgreSQL Training, Services, and 24x7 Support  www.2ndQuadrant.us
    >
    
    
  58. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-01-01T12:01:08Z

    Hello all
    
    here is new version of CHECK FUNCTION patch
    
    I changed implementation of interface:
    
    * checked functions returns table instead raising exceptions - it
    necessary for describing more issues inside one function - and it
    allow to use better structured data then ExceptionData
    
     postgres=# select lineno, statement, sqlstate, message, detail, hint,
    level, "position", query from plpgsql_checker('f1()', 0, '{}', false);
     lineno |   statement   | sqlstate |                  message
             | detail |  hint  | level | position |        query
    --------+---------------+----------+--------------------------------------------+--------+--------+-------+----------+----------------------
          4 | SQL statement | 42703    | column "c" of relation "t1" does
    not exist | [null] | [null] | error |       15 | update t1 set c = 30
          7 | RAISE         | 42P01    | missing FROM-clause entry for
    table "r"    | [null] | [null] | error |        8 | SELECT r.c
          7 | RAISE         | 42601    | too few parameters specified for
    RAISE     | [null] | [null] | error |   [null] | [null]
    (3 rows)
    
    * result of CHECK FUNCTION is simple table (like EXPLAIN - based on
    Tom proposition)
    
    postgres=# check function f1();
                                 CHECK FUNCTION
    ------------------------------------------------------------------------
     In function: 'f1()'
     error:42703:4:SQL statement:column "c" of relation "t1" does not exist
     query:update t1 set c = 30
                         ^
     error:42P01:7:RAISE:missing FROM-clause entry for table "r"
     query:SELECT r.c
                  ^
     error:42601:7:RAISE:too few parameters specified for RAISE
    (8 rows)
    
    This change allow a more playing with output
    
    postgres=# check function all in schema public;
                                 CHECK FUNCTION
    ────────────────────────────────────────────────────────────────────────
     In function: 'bubu(integer)'
     error:42703:2:assignment:column "v" does not exist
     query:SELECT a + v
                      ^
     error:42601:3:RETURN:query "SELECT 1,1" returned 2 columns
     query:SELECT 1,1
    
     In function: 'f1()'
     error:42703:4:SQL statement:column "c" of relation "t1" does not exist
     query:update t1 set c = 30
                         ^
     error:42P01:7:RAISE:missing FROM-clause entry for table "r"
     query:SELECT r.c
                  ^
     error:42601:7:RAISE:too few parameters specified for RAISE
    
     Function is valid: 'ff(integer)'
     Function is valid: 'fff(integer)'
    (18 rows)
    
    Regards
    
    Pavel Stehule
    
  59. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2012-01-03T11:08:37Z

    Pavel Stehule wrote:
    
    > here is new version of CHECK FUNCTION patch
    
    I won't be able to review that one because I'll be in
    California from Jan 6 to Jan 29.
    
    Yours,
    Laurenz Albe
    
  60. Re: review: CHECK FUNCTION statement

    Albe Laurenz <laurenz.albe@wien.gv.at> — 2012-01-04T14:27:08Z

    Pavel Stehule wrote:
    > here is new version of CHECK FUNCTION patch
    > 
    > I changed implementation of interface:
    > 
    > * checked functions returns table instead raising exceptions - it
    > necessary for describing more issues inside one function - and it
    > allow to use better structured data then ExceptionData
    
    [...]
    
    > * result of CHECK FUNCTION is simple table (like EXPLAIN - based on
    > Tom proposition)
    
    I don't have the time for a complete review, but I tried the patch
    and found:
    
    It is in context diff and applies to current master (there is fuzz 1
    in one hunk). It contains documentation and regression tests.
    Compiles without warnings and passes regression tests.
    
    The two or three CHECK FUNCTIONs I ran worked ok.
    
    The documentation (that I wrote) will need to get updated: currently
    it states in two places that the checker function should throw a
    warning if it encounters a problem.
    
    Yours,
    Laurenz Albe
    
  61. Re: review: CHECK FUNCTION statement

    Petr Jelinek <pjmodos@pjmodos.net> — 2012-01-29T13:20:00Z

    On 01/01/2012 01:01 PM, Pavel Stehule wrote:
    > Hello all
    >
    > here is new version of CHECK FUNCTION patch
    
    Hi,
    
    I took a shot at reviewing this. The attached version is made against 
    yesterdays HEAD (which means it applies cleanly) with some updates to 
    documentation.
    
    > I changed implementation of interface:
    >
    > * checked functions returns table instead raising exceptions - it
    > necessary for describing more issues inside one function - and it
    > allow to use better structured data then ExceptionDat
    
    The new interface makes sense to me the way it is, should be usable by 
    other languages too and for external tools it should enable sufficient 
    filtering options for what to care about and what not to care about.
    
    Anyway on to full review:
    
    Submission:
    Patch has enough documentation (mostly written by Albe Laurenz with some 
    adjustments from me). It has quite nice set of regression tests which it 
    passes on both my machines.
    
    Usability:
    We certainly want (IMHO it's something we should have had long time ago) 
    this feature and patch implements it in a way that seems to be useful.
    It has pg_dump support.
    
    Feature test:
    Works as advertised, passes all regression tests, I tested many real 
    world functions written by various people and was unable to crash it or 
    make it misbehave. I can imagine it not working properly with some 
    EXECUTE statements but I don't believe that is avoidable due to the 
    nature of PL/pgSQL.
    
    Coding review:
    I have no complaints about the code itself, neither did my compiler. 
    There is no interaction with system so it should not cause any 
    portability issues.
    
     From my point of view this seems to be ready for committer and barring 
    any objections I will mark it as such.
    
    Regards
    Petr Jelinek (PJMODOS)
    
  62. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-02-28T16:48:35Z

    
    I have a few comments about this patch:
    
    I didn't like the fact that the checker calling infrastructure uses
    SPI instead of just a FunctionCallN to call the checker function.  I
    think this should be easily avoidable.
    
    Second, I see that functioncmds.c gets a lot into trigger internals just
    to be able to figure out the function starting from a trigger name.  I
    think it'd be saner to have a new function in trigger.c that returns the
    required function OID.
    
    I think CheckFunction would be clearer if the code to check multiple
    objects is split out into a separate subroutine.
    
    After CheckFunction there is a leftover function comment without any
    following function.  There are other spurious hunks that add or remove
    single lines too (once in an otherwise untouched file).
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  63. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-02-28T19:30:58Z

    Hello
    
    Dne 28. února 2012 17:48 Alvaro Herrera <alvherre@commandprompt.com> napsal(a):
    >
    >
    > I have a few comments about this patch:
    >
    > I didn't like the fact that the checker calling infrastructure uses
    > SPI instead of just a FunctionCallN to call the checker function.  I
    > think this should be easily avoidable.
    >
    
    It is not possible - or it has not simple solution (I don't how to do
    it). PLpgSQL_checker is SRF function. SPI is used for processing
    returned resultset. I looked to pg source code, and I didn't find any
    other pattern than using SPI for SRF function call. It is probably
    possible, but it means some code duplication too. I invite any ideas.
    
    > Second, I see that functioncmds.c gets a lot into trigger internals just
    > to be able to figure out the function starting from a trigger name.  I
    > think it'd be saner to have a new function in trigger.c that returns the
    > required function OID.
    
    done
    
    >
    > I think CheckFunction would be clearer if the code to check multiple
    > objects is split out into a separate subroutine.
    
    done
    
    >
    > After CheckFunction there is a leftover function comment without any
    > following function.  There are other spurious hunks that add or remove
    > single lines too (once in an otherwise untouched file).
    
    fixed
    
    >
    
    I refreshed patch for current git repository.
    
    Regards
    
    Pavel
    
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
  64. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-02-28T19:42:22Z

    Excerpts from Pavel Stehule's message of mar feb 28 16:30:58 -0300 2012:
    
    > I refreshed patch for current git repository.
    
    Thanks, I'll have a look.
    
    Oh, another thing -- you shouldn't patch the 1.0 version of the plpgsql
    extension.  Rather I think you should produce a 1.1 version.
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  65. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-02-28T20:41:13Z

    2012/2/28 Alvaro Herrera <alvherre@commandprompt.com>:
    >
    > Excerpts from Pavel Stehule's message of mar feb 28 16:30:58 -0300 2012:
    >
    >> I refreshed patch for current git repository.
    >
    > Thanks, I'll have a look.
    >
    > Oh, another thing -- you shouldn't patch the 1.0 version of the plpgsql
    > extension.  Rather I think you should produce a 1.1 version.
    >
    
    there is patch with updated tag. I am not sure if there are some
    changes in pg_upgrade are necessary??
    
    Regards and thank you
    
    Pavel
    
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
  66. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-02-28T20:44:02Z

    
    In gram.y we have a new check_option_list nonterminal.  This is mostly
    identical to explain_option_list, except that the option args do not
    take a NumericOnly (only opt_boolean_or_string and empty).  I wonder if
    it's really worthwhile having a bunch of separate productions for this;
    how about we just use the existing explain_option_list instead and get
    rid of those extra productions?
    
    elog() is used in many user-facing messages (errors and notices).  Full
    ereport() calls should be used there, so that messages are marked for
    translations and so on.
    
    Does the patched pg_dump work with older servers?
    
    I don't like CheckFunction being declared in defrem.h.  It seems
    completely out of place there.  I don't see any better place though, so
    I'm thinking maybe we should have a new header file for it (say
    commands/functions.h; but we already have executor/functions.h so
    perhaps it's better to find another name).  This addition means that
    there's a distressingly large number of .c files that are now getting
    dest.h, which was previously pretty confined.
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  67. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-02-28T21:27:51Z

    2012/2/28 Alvaro Herrera <alvherre@commandprompt.com>:
    >
    >
    > In gram.y we have a new check_option_list nonterminal.  This is mostly
    > identical to explain_option_list, except that the option args do not
    > take a NumericOnly (only opt_boolean_or_string and empty).  I wonder if
    > it's really worthwhile having a bunch of separate productions for this;
    > how about we just use the existing explain_option_list instead and get
    > rid of those extra productions?
    
    I have to look on it
    
    >
    > elog() is used in many user-facing messages (errors and notices).  Full
    > ereport() calls should be used there, so that messages are marked for
    > translations and so on.
    
    yes, I'll fix it
    
    >
    > Does the patched pg_dump work with older servers?
    >
    
    yes, It should to do
    
    > I don't like CheckFunction being declared in defrem.h.  It seems
    > completely out of place there.  I don't see any better place though, so
    > I'm thinking maybe we should have a new header file for it (say
    > commands/functions.h; but we already have executor/functions.h so
    > perhaps it's better to find another name).  This addition means that
    > there's a distressingly large number of .c files that are now getting
    > dest.h, which was previously pretty confined.
    >
    
    you have much better knowledge about headers then me, so, please,
    propose solution.
    
    Regards
    
    Pavel
    
    
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  68. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-02-29T13:37:48Z

    Hello
    
    2012/2/28 Alvaro Herrera <alvherre@commandprompt.com>:
    >
    >
    > In gram.y we have a new check_option_list nonterminal.  This is mostly
    > identical to explain_option_list, except that the option args do not
    > take a NumericOnly (only opt_boolean_or_string and empty).  I wonder if
    > it's really worthwhile having a bunch of separate productions for this;
    > how about we just use the existing explain_option_list instead and get
    > rid of those extra productions?
    >
    > elog() is used in many user-facing messages (errors and notices).  Full
    > ereport() calls should be used there, so that messages are marked for
    > translations and so on.
    
    I replaced elog by ereport for all not internal errors
    
    >
    > Does the patched pg_dump work with older servers?
    >
    
    it should to do
    
    > I don't like CheckFunction being declared in defrem.h.  It seems
    > completely out of place there.  I don't see any better place though, so
    > I'm thinking maybe we should have a new header file for it (say
    > commands/functions.h; but we already have executor/functions.h so
    > perhaps it's better to find another name).  This addition means that
    > there's a distressingly large number of .c files that are now getting
    > dest.h, which was previously pretty confined.
    
    please, fix it like you wish
    
    Regards
    
    Pavel
    
    >
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
  69. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-02-29T18:53:03Z

    I think the way we're passing down the options to the checker is a bit
    of a mess.  The way it is formulated, it seems to me that we'd need to
    add support code in the core CheckFunction for each option we might want
    to accept in the PL-specific checkers -- including what type of value
    the option receives.  As an example, right now we have all but one
    option taking a string argument (judging from usage of defGetString());
    however, option fatal_errors takes a boolean value, and it converts to
    string "on" or "off" which is supposed to be passed down to the checker.
    
    This doesn't seem very future-proof.
    
    (Also, the patch seems to be passing the fatal_errors value twice: first
    in the options array, where it is ignored by the plpgsql checker, and a
    second time as a separate boolean option.  This needs a cleanup).
    
    I don't see any good way to pass down generic options in a generic way.
    Maybe we can just state that all option values are going to be passed as
    strings -- is that good enough?  The other option would be to pass them
    using something like pg_node_tree, but then it wouldn't be possible to
    call the checker directly instead of through CHECK FUNCTION, which I
    think was a requirement.  And it'd be a stronger argument against usage
    of SPI to call the checker function from CHECK FUNCTION, but that's an
    unsolved problem.
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  70. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-02-29T20:25:30Z

    Hello
    
    2012/2/29 Alvaro Herrera <alvherre@commandprompt.com>:
    >
    > I think the way we're passing down the options to the checker is a bit
    > of a mess.  The way it is formulated, it seems to me that we'd need to
    > add support code in the core CheckFunction for each option we might want
    > to accept in the PL-specific checkers -- including what type of value
    > the option receives.  As an example, right now we have all but one
    > option taking a string argument (judging from usage of defGetString());
    > however, option fatal_errors takes a boolean value, and it converts to
    > string "on" or "off" which is supposed to be passed down to the checker.
    > This doesn't seem very future-proof.
    
    I don't agree - we has not any datatype that can hold "key/value" list
    - and can be used via SQL interface. So two dimensional text array is
    simple and generic data type. Theoretically we can use JSON type now,
    but I think so array is more generic and better checked then JSON now
    (and it require less code - and JSON is still string too). We don't
    plan to modify parser to better support of JSON, so text array is more
    user friendly. I think so pl checker function can be simply called
    with used concept. But I am not against to your proposals. Actually
    "concept" of generic options was required in initial discuss and then
    there is implemented, but it not used. But cannot be removed, because
    probably don't would to change API in next version.
    
    >
    > (Also, the patch seems to be passing the fatal_errors value twice: first
    > in the options array, where it is ignored by the plpgsql checker, and a
    > second time as a separate boolean option.  This needs a cleanup).
    >
    
    This is by design. One request for checker function (and check
    function statement) was generic support for some optional data. This
    can has sense for plperl or plpython, and it are not used now.
    Fatal_errors is only proof concept and can be removed. I plan to use
    these options in 9.3 for checking of "inline blocks".
    
    > I don't see any good way to pass down generic options in a generic way.
    > Maybe we can just state that all option values are going to be passed as
    > strings -- is that good enough?  The other option would be to pass them
    > using something like pg_node_tree, but then it wouldn't be possible to
    > call the checker directly instead of through CHECK FUNCTION, which I
    > think was a requirement.  And it'd be a stronger argument against usage
    > of SPI to call the checker function from CHECK FUNCTION, but that's an
    > unsolved problem.
    
    Using just string needs more complex parsing, and I don't like some
    like pg_node_tree too, because it cannot be simple created by hands
    for direct call of checker function. Please, accept fact, so we would
    to call directly PL checker function - and there all params of this
    function should be simple created - and using two dimensional array is
    really simple: ARRAY [['source',$$....$$]].
    
    I don't understand why you  don't like pass generic options by generic way. Why?
    
    Regards
    
    Pavel Stehule
    
    >
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  71. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-03-01T19:46:43Z

    
    Why does CollectCheckedFunctions skip trigger functions?  My only guess
    is that at one point the checker was not supposed to know how to check
    them, and a later version learned about it and this bit wasn't updated;
    but maybe there's another reason?
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  72. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-03-02T02:44:59Z

    I've cleaned up the backend code a bit -- see attached.  More yet to go
    through; I'm mainly sending it out for you (and everyone, really) to
    give your opinion on my changes so far.
    
    (I split out the plpgsql checker for the time being into a separate
    branch; I'll get on it after I get this part committed.)
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
  73. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-02T08:29:26Z

    Hello
    
    2012/3/1 Alvaro Herrera <alvherre@commandprompt.com>:
    >
    >
    > Why does CollectCheckedFunctions skip trigger functions?  My only guess
    > is that at one point the checker was not supposed to know how to check
    > them, and a later version learned about it and this bit wasn't updated;
    > but maybe there's another reason?
    
    you cannot to check trigger function without assigned relation -
    TupleDescription should be assigned to NEW and OLD variables.
    
    Regards
    
    Pavel
    
    
    
    >
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  74. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-02T08:30:01Z

    2012/3/2 Alvaro Herrera <alvherre@commandprompt.com>:
    > I've cleaned up the backend code a bit -- see attached.  More yet to go
    > through; I'm mainly sending it out for you (and everyone, really) to
    > give your opinion on my changes so far.
    >
    > (I split out the plpgsql checker for the time being into a separate
    > branch; I'll get on it after I get this part committed.)
    
    it looks well
    
    Regards
    
    Pavel Stěhule
    
    >
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  75. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-03-02T14:46:27Z

    Excerpts from Pavel Stehule's message of vie mar 02 05:29:26 -0300 2012:
    
    > you cannot to check trigger function without assigned relation -
    > TupleDescription should be assigned to NEW and OLD variables.
    
    Oh, I see, that makes sense.
    
    After mulling over this a bit, I'm dubious about having two separate
    commands, one which checks triggers and another that checks non-trigger
    functions.  Wouldn't it make more sense to have some options into CHECK
    FUNCTION so that it receives the trigger and corresponding relation name
    to check?  For example "check function foo() trigger on tab" or
    something like that?
    
    I also wonder if it would make sense to have grammar for "check all
    triggers on table xyz" or some such, and even "check all triggers on all
    functions".
    
    Another thing is that "CHECK FUNCTION ALL FOR ROLE foo" seems a bit
    strange to me.  What about "CHECK FUNCTION ALL OWNED BY foo" instead?
    ("CHECK FUNCTION ALL" seems strange as a whole, but I'm not sure that we
    can improve that ... still, if anyone has ideas I'm sure we can discuss)
    
    As a reminder: we also have
    CHECK FUNCTION ALL IN SCHEMA f
    and
    CHECK FUNCTION ALL IN LANGUAGE f
    (and combinations thereof)
    
    Thoughts?
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  76. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-02T15:07:41Z

    Hello
    
    2012/3/2 Alvaro Herrera <alvherre@commandprompt.com>:
    >
    > Excerpts from Pavel Stehule's message of vie mar 02 05:29:26 -0300 2012:
    >
    >> you cannot to check trigger function without assigned relation -
    >> TupleDescription should be assigned to NEW and OLD variables.
    >
    > Oh, I see, that makes sense.
    >
    > After mulling over this a bit, I'm dubious about having two separate
    > commands, one which checks triggers and another that checks non-trigger
    > functions.  Wouldn't it make more sense to have some options into CHECK
    > FUNCTION so that it receives the trigger and corresponding relation name
    > to check?  For example "check function foo() trigger on tab" or
    > something like that?
    >
    
    I don't like it - "check trigger" is simple - and consistent
    
    > I also wonder if it would make sense to have grammar for "check all
    > triggers on table xyz" or some such, and even "check all triggers on all
    > functions".
    
    this is good idea - and I like it
    
    CHECK TRIGGER ALL ON TABLE X
    
    there are possible some combination like CHECK TRIGGER ALL IN SCHEMA ...;
    
    and similar. But I am not sure, if this is necessary - for some more
    complex usage developer can use a direct PL check function.
    
    >
    > Another thing is that "CHECK FUNCTION ALL FOR ROLE foo" seems a bit
    > strange to me.  What about "CHECK FUNCTION ALL OWNED BY foo" instead?
    > ("CHECK FUNCTION ALL" seems strange as a whole, but I'm not sure that we
    > can improve that ... still, if anyone has ideas I'm sure we can discuss)
    
    this should not be nice from language view, but it doesn't need new keywords
    
    pattern FOR ROLE, IN SCHEMA are used
    
    ALTER DEFAULT PRIVILEGES FOR ROLE
    
    but OWNED BY is used too (SeqOptElem:)
    
    
    I agree so OWNED BY sounds better (can be changed)
    
    >
    > As a reminder: we also have
    > CHECK FUNCTION ALL IN SCHEMA f
    
    it is ok
    
    > and
    > CHECK FUNCTION ALL IN LANGUAGE f
    > (and combinations thereof)
    >
    
    it is ok too
    
    Regards
    
    Pavel
    
    > Thoughts?
    >
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  77. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-03-03T01:24:46Z

    Excerpts from Pavel Stehule's message of mar feb 28 16:30:58 -0300 2012:
    > Hello
    > 
    > Dne 28. února 2012 17:48 Alvaro Herrera <alvherre@commandprompt.com> napsal(a):
    > >
    > >
    > > I have a few comments about this patch:
    > >
    > > I didn't like the fact that the checker calling infrastructure uses
    > > SPI instead of just a FunctionCallN to call the checker function.  I
    > > think this should be easily avoidable.
    > 
    > It is not possible - or it has not simple solution (I don't how to do
    > it). PLpgSQL_checker is SRF function. SPI is used for processing
    > returned resultset. I looked to pg source code, and I didn't find any
    > other pattern than using SPI for SRF function call. It is probably
    > possible, but it means some code duplication too. I invite any ideas.
    
    It wasn't all that difficult -- see below.  While at this, I have a
    question: how attached you are to the current return format for CHECK
    FUNCTION?
    
    check function f1();
                           CHECK FUNCTION                        
    -------------------------------------------------------------
     In function: 'f1()'
     error:42804:5:assignment:subscripted object is not an array
    (2 rows)
    
    It seems to me that it'd be trivial to make it look like this instead:
    
    check function f1();
    function | lineno | statement  | sqlstate |              message               | detail | hint | level | position | query 
    ---------+--------+------------+----------+------------------------------------+--------+------+-------+----------+-------
    f1()     |      5 | assignment | 42804    | subscripted object is not an array |        |      | error |          | 
    (1 row)
    
    This looks much nicer to me.
    
    One thing we lose is the caret marking the position of the error -- but
    I'm wondering if that really works well.  I didn't test it but from the
    code it looks to me like it'd misbehave if you had a multiline statement.
    
    Opinions?
    
    
    /*
     * Search and execute the checker function.
     *
     *   returns true, when checked function is valid
     */
    static bool
    CheckFunctionById(Oid funcOid, Oid relid, ArrayType *options,
    				  bool fatal_errors, TupOutputState *tstate)
    {
    	HeapTuple		tup;
    	Form_pg_proc	proc;
    	HeapTuple		languageTuple;
    	Form_pg_language lanForm;
    	Oid				languageChecker;
    	char		   *funcname;
    	int				result;
    
    	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));
    
    	lanForm = (Form_pg_language) GETSTRUCT(languageTuple);
    	languageChecker = lanForm->lanchecker;
    
    	funcname = format_procedure(funcOid);
    
    	/* We're all set to call the checker */
    	if (OidIsValid(languageChecker))
    	{
    		TupleDesc		tupdesc;
    		Datum			checkret;
    		FmgrInfo		flinfo;
    		ReturnSetInfo	rsinfo;
    		FunctionCallInfoData fcinfo;
    
    		/* create the tuple descriptor that the checker is supposed to return */
    		tupdesc = CreateTemplateTupleDesc(10, false);
    		TupleDescInitEntry(tupdesc, (AttrNumber) 1, "functionid", REGPROCOID, -1, 0);
    		TupleDescInitEntry(tupdesc, (AttrNumber) 2, "lineno", INT4OID, -1, 0);
    		TupleDescInitEntry(tupdesc, (AttrNumber) 3, "statement", TEXTOID, -1, 0);
    		TupleDescInitEntry(tupdesc, (AttrNumber) 4, "sqlstate", TEXTOID, -1, 0);
    		TupleDescInitEntry(tupdesc, (AttrNumber) 5, "message", TEXTOID, -1, 0);
    		TupleDescInitEntry(tupdesc, (AttrNumber) 6, "detail", TEXTOID, -1, 0);
    		TupleDescInitEntry(tupdesc, (AttrNumber) 7, "hint", TEXTOID, -1, 0);
    		TupleDescInitEntry(tupdesc, (AttrNumber) 8, "level", TEXTOID, -1, 0);
    		TupleDescInitEntry(tupdesc, (AttrNumber) 9, "position", INT4OID, -1, 0);
    		TupleDescInitEntry(tupdesc, (AttrNumber) 10, "query", TEXTOID, -1, 0);
    		
    		fmgr_info(languageChecker, &flinfo);
    
    		rsinfo.type = T_ReturnSetInfo;
    		rsinfo.econtext = CreateStandaloneExprContext();
    		rsinfo.expectedDesc = tupdesc;
    		rsinfo.allowedModes = (int) SFRM_Materialize;
    		/* returnMode is set by the checker, hopefully ... */
    		/* isDone is not relevant, since not using ValuePerCall */
    		rsinfo.setResult = NULL;
    		rsinfo.setDesc = NULL;
    
    		InitFunctionCallInfoData(fcinfo, &flinfo, 4, InvalidOid, NULL, (Node *) &rsinfo);
    		fcinfo.arg[0] = ObjectIdGetDatum(funcOid);
    		fcinfo.arg[1] = ObjectIdGetDatum(relid);
    		fcinfo.arg[2] = PointerGetDatum(options);
    		fcinfo.arg[3] = BoolGetDatum(fatal_errors);
    		fcinfo.argnull[0] = false;
    		fcinfo.argnull[1] = false;
    		fcinfo.argnull[2] = false;
    		fcinfo.argnull[3] = false;
    
    		checkret = FunctionCallInvoke(&fcinfo);
    
    		if (rsinfo.returnMode != SFRM_Materialize)
    			elog(ERROR, "checker function didn't return a proper return set");
    		/* XXX we have to do some checking on rsinfo.isDone and checkret here */
    
    		if (rsinfo.setResult != NULL)
    		{
    			bool	isnull;
    			StringInfoData	str;
    			TupleTableSlot	*slot = MakeSingleTupleTableSlot(tupdesc);
    
    			initStringInfo(&str);
    
    			while (tuplestore_gettupleslot(rsinfo.setResult, true, false, slot))
    			{
    				text *message = (text *) DatumGetPointer(slot_getattr(slot, 5, &isnull));
    
    				resetStringInfo(&str);
    
    				appendStringInfo(&str, "got a message: %s", text_to_cstring(message));
    				do_text_output_oneline(tstate, str.data);
    			}
    
    			pfree(str.data);
    			ExecDropSingleTupleTableSlot(slot);
    		}
    	}
    
    	pfree(funcname);
    
    	ReleaseSysCache(languageTuple);
    	ReleaseSysCache(tup);
    
    	return result;
    }
    
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  78. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-03T05:25:52Z

    Hello
    
    >
    > It wasn't all that difficult -- see below.  While at this, I have a
    > question: how attached you are to the current return format for CHECK
    > FUNCTION?
    
    TupleDescr is created by language creator. This ensure exactly
    expected format, because there are no possible registry check function
    with other output tuple descriptor.
    
    >
    > check function f1();
    >                       CHECK FUNCTION
    > -------------------------------------------------------------
    >  In function: 'f1()'
    >  error:42804:5:assignment:subscripted object is not an array
    > (2 rows)
    >
    > It seems to me that it'd be trivial to make it look like this instead:
    >
    > check function f1();
    > function | lineno | statement  | sqlstate |              message               | detail | hint | level | position | query
    > ---------+--------+------------+----------+------------------------------------+--------+------+-------+----------+-------
    > f1()     |      5 | assignment | 42804    | subscripted object is not an array |        |      | error |          |
    > (1 row)
    >
    > This looks much nicer to me.
    
    I am strongly disagree.
    
    1. This format is not consistent with other commands,
    2. This format is difficult for copy/paste
    3. THE ARE NOT CARET - this is really important
    5. This form is bad for terminals - there are long rows, and with \x
    outout, there are lot of "garbage" for multicommand
    4. When you would to this form, you can to directly call SRF PL check
    functions.
    
    >
    > One thing we lose is the caret marking the position of the error -- but
    > I'm wondering if that really works well.  I didn't test it but from the
    > code it looks to me like it'd misbehave if you had a multiline statement.
    >
    > Opinions?
    
    -1
    
    >
    >
    > /*
    >  * Search and execute the checker function.
    >  *
    >  *   returns true, when checked function is valid
    >  */
    > static bool
    > CheckFunctionById(Oid funcOid, Oid relid, ArrayType *options,
    >                                  bool fatal_errors, TupOutputState *tstate)
    > {
    >        HeapTuple               tup;
    >        Form_pg_proc    proc;
    >        HeapTuple               languageTuple;
    >        Form_pg_language lanForm;
    >        Oid                             languageChecker;
    >        char               *funcname;
    >        int                             result;
    >
    >        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));
    >
    >        lanForm = (Form_pg_language) GETSTRUCT(languageTuple);
    >        languageChecker = lanForm->lanchecker;
    >
    >        funcname = format_procedure(funcOid);
    >
    >        /* We're all set to call the checker */
    >        if (OidIsValid(languageChecker))
    >        {
    >                TupleDesc               tupdesc;
    >                Datum                   checkret;
    >                FmgrInfo                flinfo;
    >                ReturnSetInfo   rsinfo;
    >                FunctionCallInfoData fcinfo;
    >
    >                /* create the tuple descriptor that the checker is supposed to return */
    >                tupdesc = CreateTemplateTupleDesc(10, false);
    >                TupleDescInitEntry(tupdesc, (AttrNumber) 1, "functionid", REGPROCOID, -1, 0);
    >                TupleDescInitEntry(tupdesc, (AttrNumber) 2, "lineno", INT4OID, -1, 0);
    >                TupleDescInitEntry(tupdesc, (AttrNumber) 3, "statement", TEXTOID, -1, 0);
    >                TupleDescInitEntry(tupdesc, (AttrNumber) 4, "sqlstate", TEXTOID, -1, 0);
    >                TupleDescInitEntry(tupdesc, (AttrNumber) 5, "message", TEXTOID, -1, 0);
    >                TupleDescInitEntry(tupdesc, (AttrNumber) 6, "detail", TEXTOID, -1, 0);
    >                TupleDescInitEntry(tupdesc, (AttrNumber) 7, "hint", TEXTOID, -1, 0);
    >                TupleDescInitEntry(tupdesc, (AttrNumber) 8, "level", TEXTOID, -1, 0);
    >                TupleDescInitEntry(tupdesc, (AttrNumber) 9, "position", INT4OID, -1, 0);
    >                TupleDescInitEntry(tupdesc, (AttrNumber) 10, "query", TEXTOID, -1, 0);
    >
    >                fmgr_info(languageChecker, &flinfo);
    >
    >                rsinfo.type = T_ReturnSetInfo;
    >                rsinfo.econtext = CreateStandaloneExprContext();
    >                rsinfo.expectedDesc = tupdesc;
    >                rsinfo.allowedModes = (int) SFRM_Materialize;
    >                /* returnMode is set by the checker, hopefully ... */
    >                /* isDone is not relevant, since not using ValuePerCall */
    >                rsinfo.setResult = NULL;
    >                rsinfo.setDesc = NULL;
    >
    >                InitFunctionCallInfoData(fcinfo, &flinfo, 4, InvalidOid, NULL, (Node *) &rsinfo);
    >                fcinfo.arg[0] = ObjectIdGetDatum(funcOid);
    >                fcinfo.arg[1] = ObjectIdGetDatum(relid);
    >                fcinfo.arg[2] = PointerGetDatum(options);
    >                fcinfo.arg[3] = BoolGetDatum(fatal_errors);
    >                fcinfo.argnull[0] = false;
    >                fcinfo.argnull[1] = false;
    >                fcinfo.argnull[2] = false;
    >                fcinfo.argnull[3] = false;
    >
    >                checkret = FunctionCallInvoke(&fcinfo);
    >
    >                if (rsinfo.returnMode != SFRM_Materialize)
    >                        elog(ERROR, "checker function didn't return a proper return set");
    >                /* XXX we have to do some checking on rsinfo.isDone and checkret here */
    >
    >                if (rsinfo.setResult != NULL)
    >                {
    >                        bool    isnull;
    >                        StringInfoData  str;
    >                        TupleTableSlot  *slot = MakeSingleTupleTableSlot(tupdesc);
    >
    >                        initStringInfo(&str);
    >
    >                        while (tuplestore_gettupleslot(rsinfo.setResult, true, false, slot))
    >                        {
    >                                text *message = (text *) DatumGetPointer(slot_getattr(slot, 5, &isnull));
    >
    >                                resetStringInfo(&str);
    >
    >                                appendStringInfo(&str, "got a message: %s", text_to_cstring(message));
    >                                do_text_output_oneline(tstate, str.data);
    >                        }
    >
    >                        pfree(str.data);
    >                        ExecDropSingleTupleTableSlot(slot);
    >                }
    >        }
    >
    >        pfree(funcname);
    >
    >        ReleaseSysCache(languageTuple);
    >        ReleaseSysCache(tup);
    >
    >        return result;
    > }
    >
    
    Without correct registration you cannot to call PL check function
    directly simply. I don't thing so this is good price for removing a
    few SPI lines. I don't understand why you don't like SPI. It is used
    more times in code for similar purpose.
    
    so from me -1
    
    Regards
    
    Pavel Stehule
    
    >
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
  79. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-03T05:45:06Z

    2012/3/3 Pavel Stehule <pavel.stehule@gmail.com>:
    > Hello
    >
    >>
    >> It wasn't all that difficult -- see below.  While at this, I have a
    >> question: how attached you are to the current return format for CHECK
    >> FUNCTION?
    >
    > TupleDescr is created by language creator. This ensure exactly
    > expected format, because there are no possible registry check function
    > with other output tuple descriptor.
    >
    >>
    >> check function f1();
    >>                       CHECK FUNCTION
    >> -------------------------------------------------------------
    >>  In function: 'f1()'
    >>  error:42804:5:assignment:subscripted object is not an array
    >> (2 rows)
    >>
    >> It seems to me that it'd be trivial to make it look like this instead:
    >>
    >> check function f1();
    >> function | lineno | statement  | sqlstate |              message               | detail | hint | level | position | query
    >> ---------+--------+------------+----------+------------------------------------+--------+------+-------+----------+-------
    >> f1()     |      5 | assignment | 42804    | subscripted object is not an array |        |      | error |          |
    >> (1 row)
    >>
    >> This looks much nicer to me.
    
    This was similar to first design - it is near to result of direct PL
    check function call. But Tom and Albe had different opinion - check a
    thread three months ago, and I had to agree with them - they proposed
    better behave for using in psql console - and result is more similar
    to usual output when exception is raised.
    
    >
    > I am strongly disagree.
    >
    > 1. This format is not consistent with other commands,
    > 2. This format is difficult for copy/paste
    > 3. THE ARE NOT CARET - this is really important
    > 5. This form is bad for terminals - there are long rows, and with \x
    > outout, there are lot of "garbage" for multicommand
    > 4. When you would to this form, you can to directly call SRF PL check
    > functions.
    >
    >>
    >> One thing we lose is the caret marking the position of the error -- but
    >> I'm wondering if that really works well.  I didn't test it but from the
    >> code it looks to me like it'd misbehave if you had a multiline statement.
    >>
    >> Opinions?
    >
    >
    >
    >>
    >>
    >> /*
    >>  * Search and execute the checker function.
    >>  *
    >>  *   returns true, when checked function is valid
    >>  */
    >> static bool
    >> CheckFunctionById(Oid funcOid, Oid relid, ArrayType *options,
    >>                                  bool fatal_errors, TupOutputState *tstate)
    >> {
    >>        HeapTuple               tup;
    >>        Form_pg_proc    proc;
    >>        HeapTuple               languageTuple;
    >>        Form_pg_language lanForm;
    >>        Oid                             languageChecker;
    >>        char               *funcname;
    >>        int                             result;
    >>
    >>        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));
    >>
    >>        lanForm = (Form_pg_language) GETSTRUCT(languageTuple);
    >>        languageChecker = lanForm->lanchecker;
    >>
    >>        funcname = format_procedure(funcOid);
    >>
    >>        /* We're all set to call the checker */
    >>        if (OidIsValid(languageChecker))
    >>        {
    >>                TupleDesc               tupdesc;
    >>                Datum                   checkret;
    >>                FmgrInfo                flinfo;
    >>                ReturnSetInfo   rsinfo;
    >>                FunctionCallInfoData fcinfo;
    >>
    >>                /* create the tuple descriptor that the checker is supposed to return */
    >>                tupdesc = CreateTemplateTupleDesc(10, false);
    >>                TupleDescInitEntry(tupdesc, (AttrNumber) 1, "functionid", REGPROCOID, -1, 0);
    >>                TupleDescInitEntry(tupdesc, (AttrNumber) 2, "lineno", INT4OID, -1, 0);
    >>                TupleDescInitEntry(tupdesc, (AttrNumber) 3, "statement", TEXTOID, -1, 0);
    >>                TupleDescInitEntry(tupdesc, (AttrNumber) 4, "sqlstate", TEXTOID, -1, 0);
    >>                TupleDescInitEntry(tupdesc, (AttrNumber) 5, "message", TEXTOID, -1, 0);
    >>                TupleDescInitEntry(tupdesc, (AttrNumber) 6, "detail", TEXTOID, -1, 0);
    >>                TupleDescInitEntry(tupdesc, (AttrNumber) 7, "hint", TEXTOID, -1, 0);
    >>                TupleDescInitEntry(tupdesc, (AttrNumber) 8, "level", TEXTOID, -1, 0);
    >>                TupleDescInitEntry(tupdesc, (AttrNumber) 9, "position", INT4OID, -1, 0);
    >>                TupleDescInitEntry(tupdesc, (AttrNumber) 10, "query", TEXTOID, -1, 0);
    >>
    >>                fmgr_info(languageChecker, &flinfo);
    >>
    >>                rsinfo.type = T_ReturnSetInfo;
    >>                rsinfo.econtext = CreateStandaloneExprContext();
    >>                rsinfo.expectedDesc = tupdesc;
    >>                rsinfo.allowedModes = (int) SFRM_Materialize;
    >>                /* returnMode is set by the checker, hopefully ... */
    >>                /* isDone is not relevant, since not using ValuePerCall */
    >>                rsinfo.setResult = NULL;
    >>                rsinfo.setDesc = NULL;
    >>
    >>                InitFunctionCallInfoData(fcinfo, &flinfo, 4, InvalidOid, NULL, (Node *) &rsinfo);
    >>                fcinfo.arg[0] = ObjectIdGetDatum(funcOid);
    >>                fcinfo.arg[1] = ObjectIdGetDatum(relid);
    >>                fcinfo.arg[2] = PointerGetDatum(options);
    >>                fcinfo.arg[3] = BoolGetDatum(fatal_errors);
    >>                fcinfo.argnull[0] = false;
    >>                fcinfo.argnull[1] = false;
    >>                fcinfo.argnull[2] = false;
    >>                fcinfo.argnull[3] = false;
    >>
    >>                checkret = FunctionCallInvoke(&fcinfo);
    >>
    >>                if (rsinfo.returnMode != SFRM_Materialize)
    >>                        elog(ERROR, "checker function didn't return a proper return set");
    >>                /* XXX we have to do some checking on rsinfo.isDone and checkret here */
    >>
    >>                if (rsinfo.setResult != NULL)
    >>                {
    >>                        bool    isnull;
    >>                        StringInfoData  str;
    >>                        TupleTableSlot  *slot = MakeSingleTupleTableSlot(tupdesc);
    >>
    >>                        initStringInfo(&str);
    >>
    >>                        while (tuplestore_gettupleslot(rsinfo.setResult, true, false, slot))
    >>                        {
    >>                                text *message = (text *) DatumGetPointer(slot_getattr(slot, 5, &isnull));
    >>
    >>                                resetStringInfo(&str);
    >>
    >>                                appendStringInfo(&str, "got a message: %s", text_to_cstring(message));
    >>                                do_text_output_oneline(tstate, str.data);
    >>                        }
    >>
    >>                        pfree(str.data);
    >>                        ExecDropSingleTupleTableSlot(slot);
    >>                }
    >>        }
    >>
    >>        pfree(funcname);
    >>
    >>        ReleaseSysCache(languageTuple);
    >>        ReleaseSysCache(tup);
    >>
    >>        return result;
    >> }
    >>
    >
    > Without correct registration you cannot to call PL check function
    > directly simply. I don't thing so this is good price for removing a
    > few SPI lines. I don't understand why you don't like SPI. It is used
    > more times in code for similar purpose.
    >
    > so from me -1
    
    this disallow direct PL check function call - so any more complex
    situation cannot be solved by SQL, but must be solved by PL/pgSQL with
    dynamic SQL
    
    so I don't like it. We can talk about format for check_options - but I
    don't would to lost a possibility to simple call check function.
    
    Regards
    
    Pavel
    
    >
    > Regards
    >
    > Pavel Stehule
    >
    >>
    >> --
    >> Álvaro Herrera <alvherre@commandprompt.com>
    >> The PostgreSQL Company - Command Prompt, Inc.
    >> PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  80. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-03-03T06:01:45Z

    Excerpts from Pavel Stehule's message of sáb mar 03 02:45:06 -0300 2012:
    
    > > Without correct registration you cannot to call PL check function
    > > directly simply. I don't thing so this is good price for removing a
    > > few SPI lines. I don't understand why you don't like SPI.
    
    I don't dislike SPI in general.  I just dislike using it internally in
    the backend.  Other than RI, it's not used anywhere.
    
    > > It is used more times in code for similar purpose.
    > 
    > this disallow direct PL check function call - so any more complex
    > situation cannot be solved by SQL, but must be solved by PL/pgSQL with
    > dynamic SQL
    
    Nonsense.  Where did you get this idea?  I did not touch the plpgsql
    code at all, it'd still work exactly as in your original patch.
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  81. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-03T06:03:25Z

    2012/3/3 Alvaro Herrera <alvherre@commandprompt.com>:
    >
    > Excerpts from Pavel Stehule's message of sáb mar 03 02:45:06 -0300 2012:
    >
    >> > Without correct registration you cannot to call PL check function
    >> > directly simply. I don't thing so this is good price for removing a
    >> > few SPI lines. I don't understand why you don't like SPI.
    >
    > I don't dislike SPI in general.  I just dislike using it internally in
    > the backend.  Other than RI, it's not used anywhere.
    
    >
    >> > It is used more times in code for similar purpose.
    >>
    >> this disallow direct PL check function call - so any more complex
    >> situation cannot be solved by SQL, but must be solved by PL/pgSQL with
    >> dynamic SQL
    >
    > Nonsense.  Where did you get this idea?  I did not touch the plpgsql
    > code at all, it'd still work exactly as in your original patch.
    
    
    ok
    
    I am sorry
    
    Pavel
    
    >
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  82. Re: review: CHECK FUNCTION statement

    Petr Jelinek <pjmodos@pjmodos.net> — 2012-03-03T13:26:04Z

    On 03/03/2012 02:24 AM, Alvaro Herrera wrote:
    > question: how attached you are to the current return format for CHECK
    > FUNCTION?
    >
    > check function f1();
    >                         CHECK FUNCTION
    > -------------------------------------------------------------
    >   In function: 'f1()'
    >   error:42804:5:assignment:subscripted object is not an array
    > (2 rows)
    >
    > It seems to me that it'd be trivial to make it look like this instead:
    >
    > check function f1();
    > function | lineno | statement  | sqlstate |              message               | detail | hint | level | position | query
    > ---------+--------+------------+----------+------------------------------------+--------+------+-------+----------+-------
    > f1()     |      5 | assignment | 42804    | subscripted object is not an array |        |      | error |          |
    > (1 row)
    >
    > This looks much nicer to me.
    >
    > One thing we lose is the caret marking the position of the error -- but
    > I'm wondering if that really works well.  I didn't test it but from the
    > code it looks to me like it'd misbehave if you had a multiline statement.
    >
    > Opinions?
    
    Well, if you want nicely formated table you can always call the checker 
    function directly, I think the statement returning something that is 
    more human and less machine is more consistent approach with the rest of 
    the utility commands. In other words I don't really see the point of it.
    
    Petr
    
    
  83. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-03-03T19:42:47Z

    Excerpts from Petr Jelínek's message of sáb mar 03 10:26:04 -0300 2012:
    > On 03/03/2012 02:24 AM, Alvaro Herrera wrote:
    > > question: how attached you are to the current return format for CHECK
    > > FUNCTION?
    > >
    > > check function f1();
    > >                         CHECK FUNCTION
    > > -------------------------------------------------------------
    > >   In function: 'f1()'
    > >   error:42804:5:assignment:subscripted object is not an array
    > > (2 rows)
    
    > Well, if you want nicely formated table you can always call the checker 
    > function directly, I think the statement returning something that is 
    > more human and less machine is more consistent approach with the rest of 
    > the utility commands. In other words I don't really see the point of it.
    
    I am not against having some more human readable output than plain
    tabular.  In particular the idea that we need to have all fields is of
    course open to discussion.  But is the output as proposed above really
    all that human friendly?  I disagree that it cannot be improved.
    
    BTW one thing that's missing in this feature so far is some
    translatability of the returned output.
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  84. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-03-03T19:54:19Z

    Excerpts from Pavel Stehule's message of sáb mar 03 02:25:52 -0300 2012:
    > Hello
    > 
    > >
    > > It wasn't all that difficult -- see below.  While at this, I have a
    > > question: how attached you are to the current return format for CHECK
    > > FUNCTION?
    > 
    > TupleDescr is created by language creator. This ensure exactly
    > expected format, because there are no possible registry check function
    > with other output tuple descriptor.
    
    I'm not sure what you're saying.  What TupDesc are you talking about?
    The tupdesc returned by the checker is certainly hardcoded by the core
    support; the language creator cannot deviate from it.
    
    
    > > check function f1();
    > > function | lineno | statement  | sqlstate |              message               | detail | hint | level | position | query
    > > ---------+--------+------------+----------+------------------------------------+--------+------+-------+----------+-------
    > > f1()     |      5 | assignment | 42804    | subscripted object is not an array |        |      | error |          |
    > > (1 row)
    > >
    > > This looks much nicer to me.
    > 
    > I am strongly disagree.
    > 
    > 1. This format is not consistent with other commands,
    > 2. This format is difficult for copy/paste
    > 3. THE ARE NOT CARET - this is really important
    > 5. This form is bad for terminals - there are long rows, and with \x
    > outout, there are lot of "garbage" for multicommand
    > 4. When you would to this form, you can to directly call SRF PL check
    > functions.
    
    I am not sure that consistency is the most important thing here; I think
    what we care about is that it's usable.  So yeah, it might be hard to
    cut and paste, and also too wide.  Maybe we can run some of the fields
    together, and omit others.
    
    I am not sure about the caret thingy -- mainly because I don't think it
    works all that well.  I don't know how psql does it, but I notice that
    it shows a single line in a multiline query -- so it's not just a matter
    of adding some number of spaces.
    
    Given the negative feedback, I'm going to leave this output format
    unchanged; we can tweak it later.
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  85. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-03-03T20:56:23Z

    Excerpts from Alvaro Herrera's message of sáb mar 03 16:54:19 -0300 2012:
    > Excerpts from Pavel Stehule's message of sáb mar 03 02:25:52 -0300 2012:
    
    > > 3. THE ARE NOT CARET - this is really important
    
    > I am not sure about the caret thingy -- mainly because I don't think it
    > works all that well.  I don't know how psql does it, but I notice that
    > it shows a single line in a multiline query -- so it's not just a matter
    > of adding some number of spaces.
    
    I checked how this works in psql.  It is upwards of 200 lines of code --
    see reportErrorPosition in libpq/fe-protocol3.c.  I'm not sure this can
    be made to work sensibly here.
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  86. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-03-03T23:13:04Z

    Excerpts from Alvaro Herrera's message of sáb mar 03 17:56:23 -0300 2012:
    > Excerpts from Alvaro Herrera's message of sáb mar 03 16:54:19 -0300 2012:
    > > Excerpts from Pavel Stehule's message of sáb mar 03 02:25:52 -0300 2012:
    > 
    > > > 3. THE ARE NOT CARET - this is really important
    > 
    > > I am not sure about the caret thingy -- mainly because I don't think it
    > > works all that well.
    
    It doesn't work correctly with your patch; see sample below.  Note the
    caret is pointing to an entirely nonsensical position.  I'm not sure
    about duplicating the libpq line-counting logic in the backend.
    
    Also note i18n seems to be working well, except for the "In function"
    header, "query", and the error level.  That seems easily fixable.
    
    I remain unconvinced that this is the best possible output.
    
    alvherre=# create function f() returns int language plpgsql as $$
    begin select
    var
    from
    foo; end; $$;
    CREATE FUNCTION
    alvherre=# check function f();
                         CHECK FUNCTION                      
    ---------------------------------------------------------
     In function: 'f()'
     error:42P01:2:sentencia SQL:no existe la relación «foo»
     query:select                                           +
     var                                                    +
     from                                                   +
     foo
                           ^
    (4 filas)
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  87. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-04T19:33:08Z

    Hello
    
    2012/3/4 Alvaro Herrera <alvherre@commandprompt.com>:
    >
    > Excerpts from Alvaro Herrera's message of sáb mar 03 17:56:23 -0300 2012:
    >> Excerpts from Alvaro Herrera's message of sáb mar 03 16:54:19 -0300 2012:
    >> > Excerpts from Pavel Stehule's message of sáb mar 03 02:25:52 -0300 2012:
    >>
    >> > > 3. THE ARE NOT CARET - this is really important
    >>
    >> > I am not sure about the caret thingy -- mainly because I don't think it
    >> > works all that well.
    >
    > It doesn't work correctly with your patch; see sample below.  Note the
    > caret is pointing to an entirely nonsensical position.  I'm not sure
    > about duplicating the libpq line-counting logic in the backend.
    >
    > Also note i18n seems to be working well, except for the "In function"
    > header, "query", and the error level.  That seems easily fixable.
    >
    > I remain unconvinced that this is the best possible output.
    >
    > alvherre=# create function f() returns int language plpgsql as $$
    > begin select
    > var
    > from
    > foo; end; $$;
    > CREATE FUNCTION
    > alvherre=# check function f();
    >                     CHECK FUNCTION
    > ---------------------------------------------------------
    >  In function: 'f()'
    >  error:42P01:2:sentencia SQL:no existe la relación «foo»
    >  query:select                                           +
    >  var                                                    +
    >  from                                                   +
    >  foo
    >                       ^
    > (4 filas)
    >
    
    this should be fixed. I checked expressions, that works (I expect)
    correctly. Caret helps - (really). Sometimes man is blind :).
    
    I'll look on this topic tomorrow and I hope this will be solvable simply.
    
    Regards
    
    Pavel
    
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  88. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-03-05T02:54:36Z

    Excerpts from Pavel Stehule's message of dom mar 04 16:33:08 -0300 2012:
    > 
    > Hello
    > 
    > 2012/3/4 Alvaro Herrera <alvherre@commandprompt.com>:
    
    > >                     CHECK FUNCTION
    > > ---------------------------------------------------------
    > >  In function: 'f()'
    > >  error:42P01:2:sentencia SQL:no existe la relación «foo»
    > >  query:select                                           +
    > >  var                                                    +
    > >  from                                                   +
    > >  foo
    > >                       ^
    > > (4 filas)
    > >
    > 
    > this should be fixed. I checked expressions, that works (I expect)
    > correctly. Caret helps - (really). Sometimes man is blind :).
    
    Agreed.
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  89. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-05T13:49:01Z

    Hello
    
    2012/3/5 Alvaro Herrera <alvherre@commandprompt.com>:
    >
    > Excerpts from Pavel Stehule's message of dom mar 04 16:33:08 -0300 2012:
    >>
    >> Hello
    >>
    >> 2012/3/4 Alvaro Herrera <alvherre@commandprompt.com>:
    >
    >> >                     CHECK FUNCTION
    >> > ---------------------------------------------------------
    >> >  In function: 'f()'
    >> >  error:42P01:2:sentencia SQL:no existe la relación «foo»
    >> >  query:select                                           +
    >> >  var                                                    +
    >> >  from                                                   +
    >> >  foo
    >> >                       ^
    >> > (4 filas)
    >> >
    >>
    >> this should be fixed. I checked expressions, that works (I expect)
    >> correctly. Caret helps - (really). Sometimes man is blind :).
    >
    > Agreed.
    >
    
    I don't have your last version, so I am sending just part of
    CheckFunctionById function - this fragment ensures a output
    
    or please, send me your last patch and I'll do merge
    
    now result is better
    
    postgres=> create function f() returns int language plpgsql as $$
    postgres$> begin select
    postgres$> var
    postgres$> from
    postgres$> foo; end; $$;
    CREATE FUNCTION
    postgres=> check function f();
                          CHECK FUNCTION
    -----------------------------------------------------------
     In function: f()
     error:42P01:2:SQL statement:relation "foo" does not exist
     query:select
           var
           from
           foo
           ^
    (7 rows)
    
    and some utf8 fce
    
    postgres=> check function fx(int);
                      CHECK FUNCTION
    --------------------------------------------------
     In function: fx(integer)
     error:42703:3:RETURN:column "ýšý" does not exist
     query:SELECT (select žlutý
                 from jj
           /* ýšý */
           where /*ýšýšý8*/ ýšý = 10)
                            ^
    (7 rows)
    
    postgres=> check function fx(int);
                     CHECK FUNCTION
    -------------------------------------------------
     In function: fx(integer)
     error:42703:3:RETURN:column "xx" does not exist
     query:SELECT (select t.a
                 from t
           /* ýšý */
           where /*ýšýšý8*/ xx = 10)
                            ^
    (7 rows)
    
    caret is ok
    
    regards
    
    Pavel
    
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
  90. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-05T16:02:50Z

    small fix of CheckFunctionById function
    
    Regards
    
    p.s. Alvaro, please, send your patch and I'll merge it
    
  91. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-03-05T18:11:41Z

    Excerpts from Pavel Stehule's message of lun mar 05 13:02:50 -0300 2012:
    > small fix of CheckFunctionById function
    > 
    > Regards
    > 
    > p.s. Alvaro, please, send your patch and I'll merge it
    
    Here it is, with your changes already merged.  I also added back the
    new reference doc files which were dropped after the 2012-01-01 version.
    Note I haven't touched or read the plpgsql checker code at all (only
    some automatic indentation changes IIRC).  I haven't verified the
    regression tests either.
    
    FWIW I'm not going to participate in the other thread; neither I am
    going to work any more on this patch until the other thread sees some
    reasonable conclusion.
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
  92. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-06T06:43:06Z

    Hello
    
    * I refreshed regress tests and appended tests for multi lines query
    * There are enhanced checking of SELECT INTO statement
    * I fixed showing details and hints
    
    Regards
    
    Pavel Stehule
    
    
    2012/3/5 Alvaro Herrera <alvherre@commandprompt.com>:
    > Excerpts from Pavel Stehule's message of lun mar 05 13:02:50 -0300 2012:
    >> small fix of CheckFunctionById function
    >>
    >> Regards
    >>
    >> p.s. Alvaro, please, send your patch and I'll merge it
    >
    > Here it is, with your changes already merged.  I also added back the
    > new reference doc files which were dropped after the 2012-01-01 version.
    > Note I haven't touched or read the plpgsql checker code at all (only
    > some automatic indentation changes IIRC).  I haven't verified the
    > regression tests either.
    >
    > FWIW I'm not going to participate in the other thread; neither I am
    > going to work any more on this patch until the other thread sees some
    > reasonable conclusion.
    >
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
  93. Re: review: CHECK FUNCTION statement

    Alvaro Herrera <alvherre@commandprompt.com> — 2012-03-06T13:21:28Z

    Excerpts from Pavel Stehule's message of mar mar 06 03:43:06 -0300 2012:
    > Hello
    > 
    > * I refreshed regress tests and appended tests for multi lines query
    > * There are enhanced checking of SELECT INTO statement
    > * I fixed showing details and hints
    
    Oh, I forgot to remove the do_tup_output_slot() function which I added
    in some previous version but is no longer necessary.  Also, there are
    two includes that I put separately in functioncmds.c that are only
    necessary for the CHECK FUNCTION stuff; those should be merged in with
    the other includes there.  (I was toying with the idea of moving all
    that code to a new file).
    
    -- 
    Álvaro Herrera <alvherre@commandprompt.com>
    The PostgreSQL Company - Command Prompt, Inc.
    PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
    
  94. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-06T14:12:57Z

    2012/3/6 Alvaro Herrera <alvherre@commandprompt.com>:
    >
    > Excerpts from Pavel Stehule's message of mar mar 06 10:44:09 -0300 2012:
    >>
    >> 2012/3/6 Alvaro Herrera <alvherre@commandprompt.com>:
    >> >
    >> > Excerpts from Pavel Stehule's message of mar mar 06 03:43:06 -0300 2012:
    >> >> Hello
    >> >>
    >> >> * I refreshed regress tests and appended tests for multi lines query
    >> >> * There are enhanced checking of SELECT INTO statement
    >> >> * I fixed showing details and hints
    >> >
    >> > Oh, I forgot to remove the do_tup_output_slot() function which I added
    >> > in some previous version but is no longer necessary.  Also, there are
    >> > two includes that I put separately in functioncmds.c that are only
    >> > necessary for the CHECK FUNCTION stuff; those should be merged in with
    >> > the other includes there.  (I was toying with the idea of moving all
    >> > that code to a new file).
    >> >
    >>
    >> I am not sure, what you did do. Can you remove this useless code, please?
    >
    > It's just a three line function that's not called anywhere.
    
    ok
    
    fixed patch
    
    Pavel
    
    >
    > --
    > Álvaro Herrera <alvherre@commandprompt.com>
    > The PostgreSQL Company - Command Prompt, Inc.
    > PostgreSQL Replication, Consulting, Custom Development, 24x7 support
    
  95. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-06T21:52:21Z

    Hello
    
    there is new version
    
    * fixed small formatting issues related to drop SPI call
    * long functions was divided
    * CREATE TRIGGER ALL ON table support
    
    Regards
    
    Pavel
    
  96. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-07T05:35:06Z

    Hello
    
    I found one small issue where Query was not forwarded when trigger
    record was broken. I had to append "context" forwarding.
    
    Regards
    
    Pavel
    
    2012/3/6 Pavel Stehule <pavel.stehule@gmail.com>:
    > Hello
    >
    > there is new version
    >
    > * fixed small formatting issues related to drop SPI call
    > * long functions was divided
    > * CREATE TRIGGER ALL ON table support
    >
    > Regards
    >
    > Pavel
    
  97. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-03-07T15:55:12Z

    Hello
    
    multicheck for triggers are supported now
    
    CHECK TRIGGER ALL;
    CHECK TRIGGER ALL IN SCHEMA xxx FOR ROLE yyy;
    
    Regards
    
    Pavel Stehule
    
  98. Re: review: CHECK FUNCTION statement

    Bruce Momjian <bruce@momjian.us> — 2012-08-17T01:08:00Z

    What happened to this feature patch?  A TODO?
    
    ---------------------------------------------------------------------------
    
    On Tue, Nov 29, 2011 at 08:37:15PM +0100, Pavel Stehule wrote:
    > 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>:
    > > 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
    > >
    
    > *** ./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();
    > + 
    
    > 
    > -- 
    > Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
    > To make changes to your subscription:
    > http://www.postgresql.org/mailpref/pgsql-hackers
    
    
    -- 
      Bruce Momjian  <bruce@momjian.us>        http://momjian.us
      EnterpriseDB                             http://enterprisedb.com
    
      + It's impossible for everything to be true. +
    
    
    
  99. Re: review: CHECK FUNCTION statement

    Pavel Stehule <pavel.stehule@gmail.com> — 2012-08-17T03:51:35Z

    Hello
    
    it is in open commitfest
    
    http://archives.postgresql.org/message-id/CAFj8pRAYVTQYCL8_NF_hDQjc0m+JBvbwR6E_ZJ0SJfkKQ9m2kA@mail.gmail.com
    
    Regards
    
    Pavel
    
    2012/8/17 Bruce Momjian <bruce@momjian.us>:
    >
    > What happened to this feature patch?  A TODO?
    >
    > ---------------------------------------------------------------------------
    >
    > On Tue, Nov 29, 2011 at 08:37:15PM +0100, Pavel Stehule wrote:
    >> 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>:
    >> > 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
    >> >
    >
    >> *** ./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();
    >> +
    >
    >>
    >> --
    >> Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
    >> To make changes to your subscription:
    >> http://www.postgresql.org/mailpref/pgsql-hackers
    >
    >
    > --
    >   Bruce Momjian  <bruce@momjian.us>        http://momjian.us
    >   EnterpriseDB                             http://enterprisedb.com
    >
    >   + It's impossible for everything to be true. +