]> granicus.if.org Git - postgresql/blobdiff - src/pl/plpgsql/src/gram.y
Make all our flex and bison files use %option prefix or %name-prefix
[postgresql] / src / pl / plpgsql / src / gram.y
index 22bd1ef8fd7ef0a7dd70c8bf104d2651bfe92fb1..c92438d9b46c7fe4ee8f8cce0706198d66b1f4eb 100644 (file)
@@ -4,7 +4,7 @@
  *                                               procedural language
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/pl/plpgsql/src/gram.y,v 1.48 2003/10/30 17:18:55 tgl Exp $
+ *       $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.86 2006/03/07 01:03:12 tgl Exp $
  *
  *       This software is copyrighted by Jan Wieck - Hamburg.
  *
 
 #include "plpgsql.h"
 
+#include "parser/parser.h"
 
-static PLpgSQL_expr    *read_sql_construct(int until,
+static PLpgSQL_expr            *read_sql_construct(int until,
+                                                                                       int until2,
                                                                                        const char *expected,
+                                                                                       const char *sqlstart,
                                                                                        bool isexpression,
-                                                                                       const char *sqlstart);
+                                                                                       bool valid_sql,
+                                                                                       int *endtoken);
 static PLpgSQL_expr    *read_sql_stmt(const char *sqlstart);
 static PLpgSQL_type    *read_datatype(int tok);
 static PLpgSQL_stmt    *make_select_stmt(void);
 static PLpgSQL_stmt    *make_fetch_stmt(void);
-static void check_assignable(PLpgSQL_datum *datum);
+static void                     check_assignable(PLpgSQL_datum *datum);
+static PLpgSQL_row             *read_into_scalar_list(const char *initial_name,
+                                                                                          PLpgSQL_datum *initial_datum);
+static PLpgSQL_row             *make_scalar_list1(const char *initial_name,
+                                                                                  PLpgSQL_datum *initial_datum,
+                                                                                  int lineno);
+static void                     check_sql_expr(const char *stmt);
+static void                     plpgsql_sql_error_callback(void *arg);
+static void                     check_labels(const char *start_label,
+                                                                         const char *end_label);
 
 %}
 
+%name-prefix="plpgsql_yy"
+
 %union {
                int32                                   ival;
+               bool                                    boolean;
                char                                    *str;
                struct
                {
@@ -61,46 +77,48 @@ static      void check_assignable(PLpgSQL_datum *datum);
                }                                               varname;
                struct
                {
-                       int  nalloc;
-                       int  nused;
-                       int      *nums;
-               }                                               intlist;
-               struct
-               {
-                       int  nalloc;
-                       int  nused;
-                       PLpgSQL_diag_item *dtitems;
-               }                                               dtlist;
-               struct
-               {
-                       int  reverse;
-                       PLpgSQL_expr *expr;
-               }                                               forilow;
+                       char *name;
+                       int  lineno;
+                       PLpgSQL_datum   *scalar;
+                       PLpgSQL_rec     *rec;
+                       PLpgSQL_row     *row;
+               }                                               forvariable;
                struct
                {
                        char *label;
                        int  n_initvars;
                        int  *initvarnos;
                }                                               declhdr;
+               struct
+               {
+                       char *end_label;
+                       List *stmts;
+               }                                               loop_body;
+               List                                    *list;
                PLpgSQL_type                    *dtype;
-               PLpgSQL_datum                   *variable; /* a VAR, RECFIELD, or TRIGARG */
+               PLpgSQL_datum                   *scalar;        /* a VAR, RECFIELD, or TRIGARG */
+               PLpgSQL_variable                *variable;      /* a VAR, REC, or ROW */
                PLpgSQL_var                             *var;
                PLpgSQL_row                             *row;
                PLpgSQL_rec                             *rec;
                PLpgSQL_expr                    *expr;
                PLpgSQL_stmt                    *stmt;
-               PLpgSQL_stmts                   *stmts;
                PLpgSQL_stmt_block              *program;
+               PLpgSQL_condition               *condition;
+               PLpgSQL_exception               *exception;
+               PLpgSQL_exception_block *exception_block;
                PLpgSQL_nsitem                  *nsitem;
+               PLpgSQL_diag_item               *diagitem;
 }
 
 %type <declhdr> decl_sect
 %type <varname> decl_varname
 %type <str>            decl_renname
-%type <ival>   decl_const decl_notnull
+%type <boolean>        decl_const decl_notnull exit_type
 %type <expr>   decl_defval decl_cursor_query
 %type <dtype>  decl_datatype
-%type <row>            decl_rowtype decl_cursor_args decl_cursor_arglist
+%type <row>            decl_cursor_args
+%type <list>   decl_cursor_arglist
 %type <nsitem> decl_aliasitem
 %type <str>            decl_stmts decl_stmt
 
@@ -109,29 +127,35 @@ static    void check_assignable(PLpgSQL_datum *datum);
 %type <expr>   opt_exitcond
 
 %type <ival>   assign_var cursor_variable
-%type <var>            fori_var cursor_varptr decl_cursor_arg
-%type <varname> fori_varname
-%type <forilow> fori_lower
-%type <rec>            fors_target
+%type <var>            cursor_varptr
+%type <variable>       decl_cursor_arg
+%type <forvariable>    for_variable
+%type <stmt>   for_control
 
-%type <str>            opt_lblname opt_label
-%type <str>            opt_exitlabel
+%type <str>            opt_lblname opt_block_label opt_label
 %type <str>            execsql_start
 
-%type <stmts>  proc_sect proc_stmts stmt_else loop_body
+%type <list>   proc_sect proc_stmts stmt_else
+%type <loop_body>      loop_body
 %type <stmt>   proc_stmt pl_block
 %type <stmt>   stmt_assign stmt_if stmt_loop stmt_while stmt_exit
 %type <stmt>   stmt_return stmt_return_next stmt_raise stmt_execsql
-%type <stmt>   stmt_fori stmt_fors stmt_select stmt_perform
-%type <stmt>   stmt_dynexecute stmt_dynfors stmt_getdiag
-%type <stmt>   stmt_open stmt_fetch stmt_close
+%type <stmt>   stmt_for stmt_select stmt_perform
+%type <stmt>   stmt_dynexecute stmt_getdiag
+%type <stmt>   stmt_open stmt_fetch stmt_close stmt_null
+
+%type <list>   proc_exceptions
+%type <exception_block> exception_sect
+%type <exception>      proc_exception
+%type <condition>      proc_conditions
 
-%type <intlist>        raise_params
-%type <ival>   raise_level raise_param
+
+%type <ival>   raise_level
 %type <str>            raise_msg
 
-%type <dtlist> getdiag_list
-%type <ival>   getdiag_item getdiag_target
+%type <list>   getdiag_list
+%type <diagitem> getdiag_list_item
+%type <ival>   getdiag_kind getdiag_target
 
 %type <ival>   lno
 
@@ -143,6 +167,7 @@ static      void check_assignable(PLpgSQL_datum *datum);
 %token K_BEGIN
 %token K_CLOSE
 %token K_CONSTANT
+%token K_CONTINUE
 %token K_CURSOR
 %token K_DEBUG
 %token K_DECLARE
@@ -171,10 +196,10 @@ static    void check_assignable(PLpgSQL_datum *datum);
 %token K_NOTICE
 %token K_NULL
 %token K_OPEN
+%token K_OR
 %token K_PERFORM
 %token K_ROW_COUNT
 %token K_RAISE
-%token K_RECORD
 %token K_RENAME
 %token K_RESULT_OID
 %token K_RETURN
@@ -195,7 +220,7 @@ static      void check_assignable(PLpgSQL_datum *datum);
 %token T_TRIGGER
 %token T_STRING
 %token T_NUMBER
-%token T_VARIABLE                              /* a VAR, RECFIELD, or TRIGARG */
+%token T_SCALAR                                /* a VAR, RECFIELD, or TRIGARG */
 %token T_ROW
 %token T_RECORD
 %token T_DTYPE
@@ -228,7 +253,7 @@ comp_options        : comp_options comp_option
 
 comp_option            : O_OPTION O_DUMP
                                        {
-                                               plpgsql_DumpExecTree = 1;
+                                               plpgsql_DumpExecTree = true;
                                        }
                                ;
 
@@ -236,12 +261,11 @@ opt_semi          :
                                | ';'
                                ;
 
-pl_block               : decl_sect K_BEGIN lno proc_sect K_END
+pl_block               : decl_sect K_BEGIN lno proc_sect exception_sect K_END opt_label
                                        {
                                                PLpgSQL_stmt_block *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_block));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_block));
+                                               new = palloc0(sizeof(PLpgSQL_stmt_block));
 
                                                new->cmd_type   = PLPGSQL_STMT_BLOCK;
                                                new->lineno             = $3;
@@ -249,7 +273,9 @@ pl_block            : decl_sect K_BEGIN lno proc_sect K_END
                                                new->n_initvars = $1.n_initvars;
                                                new->initvarnos = $1.initvarnos;
                                                new->body               = $4;
+                                               new->exceptions = $5;
 
+                                               check_labels($1.label, $7);
                                                plpgsql_ns_pop();
 
                                                $$ = (PLpgSQL_stmt *)new;
@@ -257,7 +283,7 @@ pl_block            : decl_sect K_BEGIN lno proc_sect K_END
                                ;
 
 
-decl_sect              : opt_label
+decl_sect              : opt_block_label
                                        {
                                                plpgsql_ns_setlocal(false);
                                                $$.label          = $1;
@@ -265,7 +291,7 @@ decl_sect           : opt_label
                                                $$.initvarnos = NULL;
                                                plpgsql_add_initdatums(NULL);
                                        }
-                               | opt_label decl_start
+                               | opt_block_label decl_start
                                        {
                                                plpgsql_ns_setlocal(false);
                                                $$.label          = $1;
@@ -273,7 +299,7 @@ decl_sect           : opt_label
                                                $$.initvarnos = NULL;
                                                plpgsql_add_initdatums(NULL);
                                        }
-                               | opt_label decl_start decl_stmts
+                               | opt_block_label decl_start decl_stmts
                                        {
                                                plpgsql_ns_setlocal(false);
                                                if ($3 != NULL)
@@ -306,86 +332,42 @@ decl_stmt         : '<' '<' opt_lblname '>' '>'
 
 decl_statement : decl_varname decl_const decl_datatype decl_notnull decl_defval
                                        {
-                                               if (!OidIsValid($3->typrelid))
-                                               {
-                                                       /* Ordinary scalar datatype */
-                                                       PLpgSQL_var             *var;
+                                               PLpgSQL_variable        *var;
 
-                                                       var = malloc(sizeof(PLpgSQL_var));
-                                                       memset(var, 0, sizeof(PLpgSQL_var));
-
-                                                       var->dtype              = PLPGSQL_DTYPE_VAR;
-                                                       var->refname    = $1.name;
-                                                       var->lineno             = $1.lineno;
-
-                                                       var->datatype   = $3;
-                                                       var->isconst    = $2;
-                                                       var->notnull    = $4;
-                                                       var->default_val = $5;
-
-                                                       plpgsql_adddatum((PLpgSQL_datum *)var);
-                                                       plpgsql_ns_additem(PLPGSQL_NSTYPE_VAR,
-                                                                                          var->varno,
-                                                                                          $1.name);
-                                               }
-                                               else
+                                               var = plpgsql_build_variable($1.name, $1.lineno,
+                                                                                                        $3, true);
+                                               if ($2)
                                                {
-                                                       /* Composite type --- treat as rowtype */
-                                                       PLpgSQL_row        *row;
-
-                                                       row = plpgsql_build_rowtype($3->typrelid);
-                                                       row->dtype              = PLPGSQL_DTYPE_ROW;
-                                                       row->refname    = $1.name;
-                                                       row->lineno             = $1.lineno;
-
-                                                       if ($2)
+                                                       if (var->dtype == PLPGSQL_DTYPE_VAR)
+                                                               ((PLpgSQL_var *) var)->isconst = $2;
+                                                       else
                                                                ereport(ERROR,
                                                                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                                                                errmsg("rowtype variable cannot be CONSTANT")));
-                                                       if ($4)
+                                                                                errmsg("row or record variable cannot be CONSTANT")));
+                                               }
+                                               if ($4)
+                                               {
+                                                       if (var->dtype == PLPGSQL_DTYPE_VAR)
+                                                               ((PLpgSQL_var *) var)->notnull = $4;
+                                                       else
                                                                ereport(ERROR,
                                                                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                                                                errmsg("rowtype variable cannot be NOT NULL")));
-                                                       if ($5 != NULL)
+                                                                                errmsg("row or record variable cannot be NOT NULL")));
+                                               }
+                                               if ($5 != NULL)
+                                               {
+                                                       if (var->dtype == PLPGSQL_DTYPE_VAR)
+                                                               ((PLpgSQL_var *) var)->default_val = $5;
+                                                       else
                                                                ereport(ERROR,
                                                                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                                                                errmsg("default value for rowtype variable is not supported")));
-
-                                                       plpgsql_adddatum((PLpgSQL_datum *)row);
-                                                       plpgsql_ns_additem(PLPGSQL_NSTYPE_ROW,
-                                                                                          row->rowno,
-                                                                                          $1.name);
-
+                                                                                errmsg("default value for row or record variable is not supported")));
                                                }
                                        }
-                               | decl_varname K_RECORD ';'
-                                       {
-                                               PLpgSQL_rec             *var;
-
-                                               var = malloc(sizeof(PLpgSQL_rec));
-
-                                               var->dtype              = PLPGSQL_DTYPE_REC;
-                                               var->refname    = $1.name;
-                                               var->lineno             = $1.lineno;
-
-                                               plpgsql_adddatum((PLpgSQL_datum *)var);
-                                               plpgsql_ns_additem(PLPGSQL_NSTYPE_REC, var->recno,
-                                                                                               $1.name);
-                                       }
-                               | decl_varname decl_rowtype ';'
-                                       {
-                                               $2->dtype               = PLPGSQL_DTYPE_ROW;
-                                               $2->refname             = $1.name;
-                                               $2->lineno              = $1.lineno;
-
-                                               plpgsql_adddatum((PLpgSQL_datum *)$2);
-                                               plpgsql_ns_additem(PLPGSQL_NSTYPE_ROW, $2->rowno,
-                                                                                               $1.name);
-                                       }
                                | decl_varname K_ALIAS K_FOR decl_aliasitem ';'
                                        {
                                                plpgsql_ns_additem($4->itemtype,
-                                                                               $4->itemno, $1.name);
+                                                                                  $4->itemno, $1.name);
                                        }
                                | K_RENAME decl_renname K_TO decl_renname ';'
                                        {
@@ -404,41 +386,36 @@ decl_statement    : decl_varname decl_const decl_datatype decl_notnull decl_defval
                                                /* pop local namespace for cursor args */
                                                plpgsql_ns_pop();
 
-                                               new = malloc(sizeof(PLpgSQL_var));
-                                               memset(new, 0, sizeof(PLpgSQL_var));
+                                               new = (PLpgSQL_var *)
+                                                       plpgsql_build_variable($1.name, $1.lineno,
+                                                                                                  plpgsql_build_datatype(REFCURSOROID,
+                                                                                                                                                 -1),
+                                                                                                  true);
 
-                                               curname_def = malloc(sizeof(PLpgSQL_expr));
-                                               memset(curname_def, 0, sizeof(PLpgSQL_expr));
-
-                                               new->dtype              = PLPGSQL_DTYPE_VAR;
-                                               new->refname    = $1.name;
-                                               new->lineno             = $1.lineno;
+                                               curname_def = palloc0(sizeof(PLpgSQL_expr));
 
                                                curname_def->dtype = PLPGSQL_DTYPE_EXPR;
-                                               strcpy(buf, "SELECT '");
+                                               strcpy(buf, "SELECT ");
                                                cp1 = new->refname;
                                                cp2 = buf + strlen(buf);
-                                               while (*cp1 != '\0')
+                                               if (strchr(cp1, '\\') != NULL)
+                                                       *cp2++ = ESCAPE_STRING_SYNTAX;
+                                               *cp2++ = '\'';
+                                               while (*cp1)
                                                {
-                                                       if (*cp1 == '\\' || *cp1 == '\'')
-                                                               *cp2++ = '\\';
+                                                       if (SQL_STR_DOUBLE(*cp1))
+                                                               *cp2++ = *cp1;
                                                        *cp2++ = *cp1++;
                                                }
                                                strcpy(cp2, "'::refcursor");
-                                               curname_def->query = strdup(buf);
+                                               curname_def->query = pstrdup(buf);
                                                new->default_val = curname_def;
 
-                                               new->datatype = plpgsql_parse_datatype("refcursor");
-
                                                new->cursor_explicit_expr = $6;
                                                if ($4 == NULL)
                                                        new->cursor_explicit_argrow = -1;
                                                else
                                                        new->cursor_explicit_argrow = $4->rowno;
-
-                                               plpgsql_adddatum((PLpgSQL_datum *)new);
-                                               plpgsql_ns_additem(PLPGSQL_NSTYPE_VAR, new->varno,
-                                                                                  $1.name);
                                        }
                                ;
 
@@ -449,7 +426,7 @@ decl_cursor_query :
                                                plpgsql_ns_setlocal(false);
                                                query = read_sql_stmt("");
                                                plpgsql_ns_setlocal(true);
-                                               
+
                                                $$ = query;
                                        }
                                ;
@@ -459,86 +436,48 @@ decl_cursor_args :
                                                $$ = NULL;
                                        }
                                | '(' decl_cursor_arglist ')'
-                                       {
-                                               /* Copy the temp arrays to malloc'd storage */
-                                               int nfields = $2->nfields;
-                                               char **ftmp;
-                                               int *vtmp;
-
-                                               ftmp = malloc(nfields * sizeof(char *));
-                                               vtmp = malloc(nfields * sizeof(int));
-                                               memcpy(ftmp, $2->fieldnames, nfields * sizeof(char *));
-                                               memcpy(vtmp, $2->varnos, nfields * sizeof(int));
-
-                                               pfree((char *)($2->fieldnames));
-                                               pfree((char *)($2->varnos));
-
-                                               $2->fieldnames = ftmp;
-                                               $2->varnos = vtmp;
-
-                                               plpgsql_adddatum((PLpgSQL_datum *)$2);
-
-                                               $$ = $2;
-                                       }
-                               ;
-
-decl_cursor_arglist : decl_cursor_arg
                                        {
                                                PLpgSQL_row *new;
+                                               int i;
+                                               ListCell *l;
 
-                                               new = malloc(sizeof(PLpgSQL_row));
-                                               memset(new, 0, sizeof(PLpgSQL_row));
-
+                                               new = palloc0(sizeof(PLpgSQL_row));
                                                new->dtype = PLPGSQL_DTYPE_ROW;
-                                               new->refname = strdup("*internal*");
                                                new->lineno = plpgsql_scanner_lineno();
                                                new->rowtupdesc = NULL;
-                                               /*
-                                                * We make temporary fieldnames/varnos arrays that
-                                                * are much bigger than necessary.  We will resize
-                                                * them to just the needed size in the
-                                                * decl_cursor_args production.
-                                                */
-                                               new->fieldnames = palloc(1024 * sizeof(char *));
-                                               new->varnos = palloc(1024 * sizeof(int));
-                                               new->nfields = 1;
+                                               new->nfields = list_length($2);
+                                               new->fieldnames = palloc(new->nfields * sizeof(char *));
+                                               new->varnos = palloc(new->nfields * sizeof(int));
 
-                                               new->fieldnames[0] = $1->refname;
-                                               new->varnos[0] = $1->varno;
+                                               i = 0;
+                                               foreach (l, $2)
+                                               {
+                                                       PLpgSQL_variable *arg = (PLpgSQL_variable *) lfirst(l);
+                                                       new->fieldnames[i] = arg->refname;
+                                                       new->varnos[i] = arg->dno;
+                                                       i++;
+                                               }
+                                               list_free($2);
 
+                                               plpgsql_adddatum((PLpgSQL_datum *) new);
                                                $$ = new;
                                        }
+                               ;
+
+decl_cursor_arglist : decl_cursor_arg
+                                       {
+                                               $$ = list_make1($1);
+                                       }
                                | decl_cursor_arglist ',' decl_cursor_arg
                                        {
-                                               int i = $1->nfields++;
-
-                                               $1->fieldnames[i] = $3->refname;
-                                               $1->varnos[i] = $3->varno;
-
-                                               $$ = $1;
+                                               $$ = lappend($1, $3);
                                        }
                                ;
 
 decl_cursor_arg : decl_varname decl_datatype
                                        {
-                                               PLpgSQL_var *new;
-
-                                               new = malloc(sizeof(PLpgSQL_var));
-                                               memset(new, 0, sizeof(PLpgSQL_var));
-
-                                               new->dtype              = PLPGSQL_DTYPE_VAR;
-                                               new->refname    = $1.name;
-                                               new->lineno             = $1.lineno;
-
-                                               new->datatype   = $2;
-                                               new->isconst    = false;
-                                               new->notnull    = false;
-
-                                               plpgsql_adddatum((PLpgSQL_datum *)new);
-                                               plpgsql_ns_additem(PLPGSQL_NSTYPE_VAR, new->varno,
-                                                                                  $1.name);
-                                               
-                                               $$ = new;
+                                               $$ = plpgsql_build_variable($1.name, $1.lineno,
+                                                                                                       $2, true);
                                        }
                                ;
 
@@ -573,21 +512,13 @@ decl_aliasitem    : T_WORD
                                        }
                                ;
 
-decl_rowtype   : T_ROW
-                                       {
-                                               $$ = yylval.row;
-                                       }
-                               ;
-
 decl_varname   : T_WORD
                                        {
                                                char    *name;
 
                                                plpgsql_convert_ident(yytext, &name, 1);
-                                               /* name should be malloc'd for use as varname */
-                                               $$.name = strdup(name);
+                                               $$.name = name;
                                                $$.lineno  = plpgsql_scanner_lineno();
-                                               pfree(name);
                                        }
                                ;
 
@@ -602,9 +533,9 @@ decl_renname        : T_WORD
                                ;
 
 decl_const             :
-                                       { $$ = 0; }
+                                       { $$ = false; }
                                | K_CONSTANT
-                                       { $$ = 1; }
+                                       { $$ = true; }
                                ;
 
 decl_datatype  :
@@ -619,61 +550,18 @@ decl_datatype     :
                                ;
 
 decl_notnull   :
-                                       { $$ = 0; }
+                                       { $$ = false; }
                                | K_NOT K_NULL
-                                       { $$ = 1; }
+                                       { $$ = true; }
                                ;
 
 decl_defval            : ';'
                                        { $$ = NULL; }
                                | decl_defkey
                                        {
-                                               int                             tok;
-                                               int                             lno;
-                                               PLpgSQL_dstring ds;
-                                               PLpgSQL_expr    *expr;
-
-                                               lno = plpgsql_scanner_lineno();
-                                               expr = malloc(sizeof(PLpgSQL_expr));
-                                               plpgsql_dstring_init(&ds);
-                                               plpgsql_dstring_append(&ds, "SELECT ");
-
-                                               expr->dtype   = PLPGSQL_DTYPE_EXPR;
-                                               expr->plan        = NULL;
-                                               expr->nparams = 0;
-
-                                               tok = yylex();
-                                               switch (tok)
-                                               {
-                                                       case 0:
-                                                               yyerror("unexpected end of function");
-                                                       case K_NULL:
-                                                               if (yylex() != ';')
-                                                                       yyerror("expected \";\" after \"NULL\"");
-
-                                                               free(expr);
-                                                               plpgsql_dstring_free(&ds);
-
-                                                               $$ = NULL;
-                                                               break;
-
-                                                       default:
-                                                               plpgsql_dstring_append(&ds, yytext);
-                                                               while ((tok = yylex()) != ';')
-                                                               {
-                                                                       if (tok == 0)
-                                                                               yyerror("unterminated default value");
-
-                                                                       if (plpgsql_SpaceScanned)
-                                                                               plpgsql_dstring_append(&ds, " ");
-                                                                       plpgsql_dstring_append(&ds, yytext);
-                                                               }
-                                                               expr->query = strdup(plpgsql_dstring_get(&ds));
-                                                               plpgsql_dstring_free(&ds);
-
-                                                               $$ = expr;
-                                                               break;
-                                               }
+                                               plpgsql_ns_setlocal(false);
+                                               $$ = plpgsql_read_expression(';', ";");
+                                               plpgsql_ns_setlocal(true);
                                        }
                                ;
 
@@ -683,11 +571,7 @@ decl_defkey                : K_ASSIGN
 
 proc_sect              :
                                        {
-                                                       PLpgSQL_stmts   *new;
-
-                                                       new = malloc(sizeof(PLpgSQL_stmts));
-                                                       memset(new, 0, sizeof(PLpgSQL_stmts));
-                                                       $$ = new;
+                                               $$ = NIL;
                                        }
                                | proc_stmts
                                        { $$ = $1; }
@@ -695,29 +579,17 @@ proc_sect         :
 
 proc_stmts             : proc_stmts proc_stmt
                                                {
-                                                               if ($1->stmts_used == $1->stmts_alloc)
-                                                               {
-                                                                       $1->stmts_alloc *= 2;
-                                                                       $1->stmts = realloc($1->stmts, sizeof(PLpgSQL_stmt *) * $1->stmts_alloc);
-                                                               }
-                                                               $1->stmts[$1->stmts_used++] = (struct PLpgSQL_stmt *)$2;
-
+                                                       if ($2 == NULL)
                                                                $$ = $1;
+                                                       else
+                                                               $$ = lappend($1, $2);
                                                }
                                | proc_stmt
                                                {
-                                                               PLpgSQL_stmts   *new;
-
-                                                               new = malloc(sizeof(PLpgSQL_stmts));
-                                                               memset(new, 0, sizeof(PLpgSQL_stmts));
-
-                                                               new->stmts_alloc = 64;
-                                                               new->stmts_used  = 1;
-                                                               new->stmts = malloc(sizeof(PLpgSQL_stmt *) * new->stmts_alloc);
-                                                               new->stmts[0] = (struct PLpgSQL_stmt *)$1;
-
-                                                               $$ = new;
-
+                                                       if ($1 == NULL)
+                                                               $$ = NULL;
+                                                       else
+                                                               $$ = list_make1($1);
                                                }
                                ;
 
@@ -731,9 +603,7 @@ proc_stmt           : pl_block ';'
                                                { $$ = $1; }
                                | stmt_while
                                                { $$ = $1; }
-                               | stmt_fori
-                                               { $$ = $1; }
-                               | stmt_fors
+                               | stmt_for
                                                { $$ = $1; }
                                | stmt_select
                                                { $$ = $1; }
@@ -749,8 +619,6 @@ proc_stmt           : pl_block ';'
                                                { $$ = $1; }
                                | stmt_dynexecute
                                                { $$ = $1; }
-                               | stmt_dynfors
-                                               { $$ = $1; }
                                | stmt_perform
                                                { $$ = $1; }
                                | stmt_getdiag
@@ -761,15 +629,15 @@ proc_stmt         : pl_block ';'
                                                { $$ = $1; }
                                | stmt_close
                                                { $$ = $1; }
+                               | stmt_null
+                                               { $$ = $1; }
                                ;
 
 stmt_perform   : K_PERFORM lno expr_until_semi
                                        {
                                                PLpgSQL_stmt_perform *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_perform));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_perform));
-
+                                               new = palloc0(sizeof(PLpgSQL_stmt_perform));
                                                new->cmd_type = PLPGSQL_STMT_PERFORM;
                                                new->lineno   = $2;
                                                new->expr  = $3;
@@ -782,9 +650,7 @@ stmt_assign         : assign_var lno K_ASSIGN expr_until_semi
                                        {
                                                PLpgSQL_stmt_assign *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_assign));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_assign));
-
+                                               new = palloc0(sizeof(PLpgSQL_stmt_assign));
                                                new->cmd_type = PLPGSQL_STMT_ASSIGN;
                                                new->lineno   = $2;
                                                new->varno = $1;
@@ -798,45 +664,38 @@ stmt_getdiag      : K_GET K_DIAGNOSTICS lno getdiag_list ';'
                                        {
                                                PLpgSQL_stmt_getdiag     *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_getdiag));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_getdiag));
-
+                                               new = palloc0(sizeof(PLpgSQL_stmt_getdiag));
                                                new->cmd_type = PLPGSQL_STMT_GETDIAG;
                                                new->lineno   = $3;
-                                               new->ndtitems = $4.nused;
-                                               new->dtitems  = malloc(sizeof(PLpgSQL_diag_item) * $4.nused);
-                                               memcpy(new->dtitems, $4.dtitems, sizeof(PLpgSQL_diag_item) * $4.nused);
+                                               new->diag_items  = $4;
 
                                                $$ = (PLpgSQL_stmt *)new;
                                        }
                                ;
 
-getdiag_list : getdiag_list ',' getdiag_target K_ASSIGN getdiag_item
+getdiag_list : getdiag_list ',' getdiag_list_item
                                        {
-                                               if ($1.nused == $1.nalloc)
-                                               {
-                                                       $1.nalloc *= 2;
-                                                       $1.dtitems = repalloc($1.dtitems, sizeof(PLpgSQL_diag_item) * $1.nalloc);
-                                               }
-                                               $1.dtitems[$1.nused].target = $3;
-                                               $1.dtitems[$1.nused].item   = $5;
-                                               $1.nused++;
-
-                                               $$.nalloc = $1.nalloc;
-                                               $$.nused  = $1.nused;
-                                               $$.dtitems = $1.dtitems;
+                                               $$ = lappend($1, $3);
                                        }
-                               | getdiag_target K_ASSIGN getdiag_item
+                               | getdiag_list_item
                                        {
-                                               $$.nalloc = 1;
-                                               $$.nused  = 1;
-                                               $$.dtitems = palloc(sizeof(PLpgSQL_diag_item) * $$.nalloc);
-                                               $$.dtitems[0].target = $1;
-                                               $$.dtitems[0].item   = $3;
+                                               $$ = list_make1($1);
                                        }
                                ;
 
-getdiag_item : K_ROW_COUNT
+getdiag_list_item : getdiag_target K_ASSIGN getdiag_kind
+                                       {
+                                               PLpgSQL_diag_item *new;
+
+                                               new = palloc(sizeof(PLpgSQL_diag_item));
+                                               new->target = $1;
+                                               new->kind = $3;
+
+                                               $$ = new;
+                                       }
+                               ;
+
+getdiag_kind : K_ROW_COUNT
                                        {
                                                $$ = PLPGSQL_GETDIAG_ROW_COUNT;
                                        }
@@ -846,26 +705,34 @@ getdiag_item : K_ROW_COUNT
                                        }
                                ;
 
-getdiag_target : T_VARIABLE
+getdiag_target : T_SCALAR
                                        {
-                                               check_assignable(yylval.variable);
-                                               $$ = yylval.variable->dno;
+                                               check_assignable(yylval.scalar);
+                                               $$ = yylval.scalar->dno;
                                        }
                                ;
 
 
-assign_var             : T_VARIABLE
+assign_var             : T_SCALAR
+                                       {
+                                               check_assignable(yylval.scalar);
+                                               $$ = yylval.scalar->dno;
+                                       }
+                               | T_ROW
+                                       {
+                                               check_assignable((PLpgSQL_datum *) yylval.row);
+                                               $$ = yylval.row->rowno;
+                                       }
+                               | T_RECORD
                                        {
-                                               check_assignable(yylval.variable);
-                                               $$ = yylval.variable->dno;
+                                               check_assignable((PLpgSQL_datum *) yylval.rec);
+                                               $$ = yylval.rec->recno;
                                        }
                                | assign_var '[' expr_until_rightbracket
                                        {
                                                PLpgSQL_arrayelem       *new;
 
-                                               new = malloc(sizeof(PLpgSQL_arrayelem));
-                                               memset(new, 0, sizeof(PLpgSQL_arrayelem));
-
+                                               new = palloc0(sizeof(PLpgSQL_arrayelem));
                                                new->dtype              = PLPGSQL_DTYPE_ARRAYELEM;
                                                new->subscript  = $3;
                                                new->arrayparentno = $1;
@@ -880,9 +747,7 @@ stmt_if                     : K_IF lno expr_until_then proc_sect stmt_else K_END K_IF ';'
                                        {
                                                PLpgSQL_stmt_if *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_if));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_if));
-
+                                               new = palloc0(sizeof(PLpgSQL_stmt_if));
                                                new->cmd_type   = PLPGSQL_STMT_IF;
                                                new->lineno             = $2;
                                                new->cond               = $3;
@@ -895,254 +760,357 @@ stmt_if                 : K_IF lno expr_until_then proc_sect stmt_else K_END K_IF ';'
 
 stmt_else              :
                                        {
-                                                       PLpgSQL_stmts   *new;
-
-                                                       new = malloc(sizeof(PLpgSQL_stmts));
-                                                       memset(new, 0, sizeof(PLpgSQL_stmts));
-                                                       $$ = new;
+                                               $$ = NIL;
                                        }
                                | K_ELSIF lno expr_until_then proc_sect stmt_else
                                        {
-                                         /*
-                                          * Translate the structure:      into:
-                                          *
-                                          * IF c1 THEN                                    IF c1 THEN            
-                                          *     ...                                               ...                           
-                                          * ELSIF c2 THEN                                 ELSE 
-                                          *                                                               IF c2 THEN   
-                                          *     ...                                                       ...                           
-                                          * ELSE                                                          ELSE                          
-                                          *     ...                                                       ...                           
-                                          * END IF                                                        END IF                        
-                                          *                                                       END IF
-                                          * 
-                                          */
-
-                                               PLpgSQL_stmts   *new;
+                                               /*
+                                                * Translate the structure:        into:
+                                                *
+                                                * IF c1 THEN                              IF c1 THEN
+                                                *       ...                                               ...
+                                                * ELSIF c2 THEN                           ELSE
+                                                *                                                                 IF c2 THEN
+                                                *       ...                                                       ...
+                                                * ELSE                                                    ELSE
+                                                *       ...                                                       ...
+                                                * END IF                                                  END IF
+                                                *                                                         END IF
+                                                */
                                                PLpgSQL_stmt_if *new_if;
 
                                                /* first create a new if-statement */
-                                               new_if = malloc(sizeof(PLpgSQL_stmt_if));
-                                               memset(new_if, 0, sizeof(PLpgSQL_stmt_if));
-
+                                               new_if = palloc0(sizeof(PLpgSQL_stmt_if));
                                                new_if->cmd_type        = PLPGSQL_STMT_IF;
                                                new_if->lineno          = $2;
                                                new_if->cond            = $3;
                                                new_if->true_body       = $4;
                                                new_if->false_body      = $5;
-                                               
-                                               /* this is a 'container' for the if-statement */
-                                               new = malloc(sizeof(PLpgSQL_stmts));
-                                               memset(new, 0, sizeof(PLpgSQL_stmts));
-                                               
-                                               new->stmts_alloc = 64;
-                                               new->stmts_used  = 1;
-                                               new->stmts = malloc(sizeof(PLpgSQL_stmt *) * new->stmts_alloc);
-                                               new->stmts[0] = (struct PLpgSQL_stmt *)new_if;
 
-                                               $$ = new;
-                                               
+                                               /* wrap the if-statement in a "container" list */
+                                               $$ = list_make1(new_if);
                                        }
 
                                | K_ELSE proc_sect
                                        {
-                                               $$ = $2;                                
+                                               $$ = $2;
                                        }
                                ;
 
-stmt_loop              : opt_label K_LOOP lno loop_body
+stmt_loop              : opt_block_label K_LOOP lno loop_body
                                        {
                                                PLpgSQL_stmt_loop *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_loop));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_loop));
-
+                                               new = palloc0(sizeof(PLpgSQL_stmt_loop));
                                                new->cmd_type = PLPGSQL_STMT_LOOP;
                                                new->lineno   = $3;
                                                new->label        = $1;
-                                               new->body         = $4;
+                                               new->body         = $4.stmts;
 
+                                               check_labels($1, $4.end_label);
                                                plpgsql_ns_pop();
 
                                                $$ = (PLpgSQL_stmt *)new;
                                        }
                                ;
 
-stmt_while             : opt_label K_WHILE lno expr_until_loop loop_body
+stmt_while             : opt_block_label K_WHILE lno expr_until_loop loop_body
                                        {
                                                PLpgSQL_stmt_while *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_while));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_while));
-
+                                               new = palloc0(sizeof(PLpgSQL_stmt_while));
                                                new->cmd_type = PLPGSQL_STMT_WHILE;
                                                new->lineno   = $3;
                                                new->label        = $1;
                                                new->cond         = $4;
-                                               new->body         = $5;
+                                               new->body         = $5.stmts;
 
+                                               check_labels($1, $5.end_label);
                                                plpgsql_ns_pop();
 
                                                $$ = (PLpgSQL_stmt *)new;
                                        }
                                ;
 
-stmt_fori              : opt_label K_FOR lno fori_var K_IN fori_lower expr_until_loop loop_body
+stmt_for               : opt_block_label K_FOR for_control loop_body
                                        {
-                                               PLpgSQL_stmt_fori               *new;
+                                               /* This runs after we've scanned the loop body */
+                                               if ($3->cmd_type == PLPGSQL_STMT_FORI)
+                                               {
+                                                       PLpgSQL_stmt_fori               *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_fori));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_fori));
+                                                       new = (PLpgSQL_stmt_fori *) $3;
+                                                       new->label        = $1;
+                                                       new->body         = $4.stmts;
+                                                       $$ = (PLpgSQL_stmt *) new;
+                                               }
+                                               else if ($3->cmd_type == PLPGSQL_STMT_FORS)
+                                               {
+                                                       PLpgSQL_stmt_fors               *new;
 
-                                               new->cmd_type = PLPGSQL_STMT_FORI;
-                                               new->lineno   = $3;
-                                               new->label        = $1;
-                                               new->var          = $4;
-                                               new->reverse  = $6.reverse;
-                                               new->lower        = $6.expr;
-                                               new->upper        = $7;
-                                               new->body         = $8;
+                                                       new = (PLpgSQL_stmt_fors *) $3;
+                                                       new->label        = $1;
+                                                       new->body         = $4.stmts;
+                                                       $$ = (PLpgSQL_stmt *) new;
+                                               }
+                                               else
+                                               {
+                                                       PLpgSQL_stmt_dynfors    *new;
 
-                                               plpgsql_ns_pop();
+                                                       Assert($3->cmd_type == PLPGSQL_STMT_DYNFORS);
+                                                       new = (PLpgSQL_stmt_dynfors *) $3;
+                                                       new->label        = $1;
+                                                       new->body         = $4.stmts;
+                                                       $$ = (PLpgSQL_stmt *) new;
+                                               }
 
-                                               $$ = (PLpgSQL_stmt *)new;
+                                               check_labels($1, $4.end_label);
+                                               /* close namespace started in opt_label */
+                                               plpgsql_ns_pop();
                                        }
                                ;
 
-fori_var               : fori_varname
+for_control            :
+                               lno for_variable K_IN
                                        {
-                                               PLpgSQL_var             *new;
+                                               int                     tok = yylex();
 
-                                               new = malloc(sizeof(PLpgSQL_var));
-                                               memset(new, 0, sizeof(PLpgSQL_var));
+                                               /* Simple case: EXECUTE is a dynamic FOR loop */
+                                               if (tok == K_EXECUTE)
+                                               {
+                                                       PLpgSQL_stmt_dynfors    *new;
+                                                       PLpgSQL_expr                    *expr;
 
-                                               new->dtype              = PLPGSQL_DTYPE_VAR;
-                                               new->refname    = $1.name;
-                                               new->lineno             = $1.lineno;
+                                                       expr = plpgsql_read_expression(K_LOOP, "LOOP");
 
-                                               new->datatype   = plpgsql_parse_datatype("integer");
-                                               new->isconst    = false;
-                                               new->notnull    = false;
-                                               new->default_val = NULL;
+                                                       new = palloc0(sizeof(PLpgSQL_stmt_dynfors));
+                                                       new->cmd_type = PLPGSQL_STMT_DYNFORS;
+                                                       new->lineno   = $1;
+                                                       if ($2.rec)
+                                                       {
+                                                               new->rec = $2.rec;
+                                                               check_assignable((PLpgSQL_datum *) new->rec);
+                                                       }
+                                                       else if ($2.row)
+                                                       {
+                                                               new->row = $2.row;
+                                                               check_assignable((PLpgSQL_datum *) new->row);
+                                                       }
+                                                       else if ($2.scalar)
+                                                       {
+                                                               /* convert single scalar to list */
+                                                               new->row = make_scalar_list1($2.name, $2.scalar, $2.lineno);
+                                                               /* no need for check_assignable */
+                                                       }
+                                                       else
+                                                       {
+                                                               plpgsql_error_lineno = $2.lineno;
+                                                               yyerror("loop variable of loop over rows must be a record or row variable or list of scalar variables");
+                                                       }
+                                                       new->query = expr;
 
-                                               plpgsql_adddatum((PLpgSQL_datum *)new);
-                                               plpgsql_ns_additem(PLPGSQL_NSTYPE_VAR, new->varno,
-                                                                                               $1.name);
+                                                       $$ = (PLpgSQL_stmt *) new;
+                                               }
+                                               else
+                                               {
+                                                       PLpgSQL_expr    *expr1;
+                                                       bool                     reverse = false;
+
+                                                       /*
+                                                        * We have to distinguish between two
+                                                        * alternatives: FOR var IN a .. b and FOR
+                                                        * var IN query. Unfortunately this is
+                                                        * tricky, since the query in the second
+                                                        * form needn't start with a SELECT
+                                                        * keyword.  We use the ugly hack of
+                                                        * looking for two periods after the first
+                                                        * token. We also check for the REVERSE
+                                                        * keyword, which means it must be an
+                                                        * integer loop.
+                                                        */
+                                                       if (tok == K_REVERSE)
+                                                               reverse = true;
+                                                       else
+                                                               plpgsql_push_back_token(tok);
 
-                                               plpgsql_add_initdatums(NULL);
+                                                       /*
+                                                        * Read tokens until we see either a ".."
+                                                        * or a LOOP. The text we read may not
+                                                        * necessarily be a well-formed SQL
+                                                        * statement, so we need to invoke
+                                                        * read_sql_construct directly.
+                                                        */
+                                                       expr1 = read_sql_construct(K_DOTDOT,
+                                                                                                          K_LOOP,
+                                                                                                          "LOOP",
+                                                                                                          "SELECT ",
+                                                                                                          true,
+                                                                                                          false,
+                                                                                                          &tok);
+
+                                                       if (tok == K_DOTDOT)
+                                                       {
+                                                               /* Saw "..", so it must be an integer loop */
+                                                               PLpgSQL_expr            *expr2;
+                                                               PLpgSQL_var                     *fvar;
+                                                               PLpgSQL_stmt_fori       *new;
+                                                               char                            *varname;
 
-                                               $$ = new;
-                                       }
-                               ;
+                                                               /* First expression is well-formed */
+                                                               check_sql_expr(expr1->query);
 
-fori_varname   : T_VARIABLE
-                                       {
-                                               char    *name;
+                                                               expr2 = plpgsql_read_expression(K_LOOP, "LOOP");
 
-                                               plpgsql_convert_ident(yytext, &name, 1);
-                                               /* name should be malloc'd for use as varname */
-                                               $$.name = strdup(name);
-                                               $$.lineno  = plpgsql_scanner_lineno();
-                                               pfree(name);
-                                       }
-                               | T_WORD
-                                       {
-                                               char    *name;
+                                                               /* should have had a single variable name */
+                                                               plpgsql_error_lineno = $2.lineno;
+                                                               if ($2.scalar && $2.row)
+                                                                       ereport(ERROR,
+                                                                                       (errcode(ERRCODE_SYNTAX_ERROR),
+                                                                                        errmsg("integer FOR loop must have just one target variable")));
+
+                                                               /* create loop's private variable */
+                                                               plpgsql_convert_ident($2.name, &varname, 1);
+                                                               fvar = (PLpgSQL_var *)
+                                                                       plpgsql_build_variable(varname,
+                                                                                                                  $2.lineno,
+                                                                                                                  plpgsql_build_datatype(INT4OID,
+                                                                                                                                                                 -1),
+                                                                                                                  true);
+
+                                                               /* put the for-variable into the local block */
+                                                               plpgsql_add_initdatums(NULL);
+
+                                                               new = palloc0(sizeof(PLpgSQL_stmt_fori));
+                                                               new->cmd_type = PLPGSQL_STMT_FORI;
+                                                               new->lineno   = $1;
+                                                               new->var          = fvar;
+                                                               new->reverse  = reverse;
+                                                               new->lower        = expr1;
+                                                               new->upper        = expr2;
+
+                                                               $$ = (PLpgSQL_stmt *) new;
+                                                       }
+                                                       else
+                                                       {
+                                                               /*
+                                                                * No "..", so it must be a query loop. We've prefixed an
+                                                                * extra SELECT to the query text, so we need to remove that
+                                                                * before performing syntax checking.
+                                                                */
+                                                               char                            *tmp_query;
+                                                               PLpgSQL_stmt_fors       *new;
 
-                                               plpgsql_convert_ident(yytext, &name, 1);
-                                               /* name should be malloc'd for use as varname */
-                                               $$.name = strdup(name);
-                                               $$.lineno  = plpgsql_scanner_lineno();
-                                               pfree(name);
+                                                               if (reverse)
+                                                                       yyerror("cannot specify REVERSE in query FOR loop");
+
+                                                               Assert(strncmp(expr1->query, "SELECT ", 7) == 0);
+                                                               tmp_query = pstrdup(expr1->query + 7);
+                                                               pfree(expr1->query);
+                                                               expr1->query = tmp_query;
+
+                                                               check_sql_expr(expr1->query);
+
+                                                               new = palloc0(sizeof(PLpgSQL_stmt_fors));
+                                                               new->cmd_type = PLPGSQL_STMT_FORS;
+                                                               new->lineno   = $1;
+                                                               if ($2.rec)
+                                                               {
+                                                                       new->rec = $2.rec;
+                                                                       check_assignable((PLpgSQL_datum *) new->rec);
+                                                               }
+                                                               else if ($2.row)
+                                                               {
+                                                                       new->row = $2.row;
+                                                                       check_assignable((PLpgSQL_datum *) new->row);
+                                                               }
+                                                               else if ($2.scalar)
+                                                               {
+                                                                       /* convert single scalar to list */
+                                                                       new->row = make_scalar_list1($2.name, $2.scalar, $2.lineno);
+                                                                       /* no need for check_assignable */
+                                                               }
+                                                               else
+                                                               {
+                                                                       plpgsql_error_lineno = $2.lineno;
+                                                                       yyerror("loop variable of loop over rows must be a record or row variable or list of scalar variables");
+                                                               }
+
+                                                               new->query = expr1;
+                                                               $$ = (PLpgSQL_stmt *) new;
+                                                       }
+                                               }
                                        }
                                ;
 
-fori_lower             :
+/*
+ * Processing the for_variable is tricky because we don't yet know if the
+ * FOR is an integer FOR loop or a loop over query results.  In the former
+ * case, the variable is just a name that we must instantiate as a loop
+ * local variable, regardless of any other definition it might have.
+ * Therefore, we always save the actual identifier into $$.name where it
+ * can be used for that case.  We also save the outer-variable definition,
+ * if any, because that's what we need for the loop-over-query case.  Note
+ * that we must NOT apply check_assignable() or any other semantic check
+ * until we know what's what.
+ *
+ * However, if we see a comma-separated list of names, we know that it
+ * can't be an integer FOR loop and so it's OK to check the variables
+ * immediately.  In particular, for T_WORD followed by comma, we should
+ * complain that the name is not known rather than say it's a syntax error.
+ * Note that the non-error result of this case sets *both* $$.scalar and
+ * $$.row; see the for_control production.
+ */
+for_variable   : T_SCALAR
                                        {
                                                int                     tok;
 
+                                               $$.name = pstrdup(yytext);
+                                               $$.lineno  = plpgsql_scanner_lineno();
+                                               $$.scalar = yylval.scalar;
+                                               $$.rec = NULL;
+                                               $$.row = NULL;
+                                               /* check for comma-separated list */
                                                tok = yylex();
-                                               if (tok == K_REVERSE)
-                                               {
-                                                       $$.reverse = 1;
-                                               }
-                                               else
-                                               {
-                                                       $$.reverse = 0;
-                                                       plpgsql_push_back_token(tok);
-                                               }
-
-                                               $$.expr = plpgsql_read_expression(K_DOTDOT, "..");
+                                               plpgsql_push_back_token(tok);
+                                               if (tok == ',')
+                                                       $$.row = read_into_scalar_list($$.name, $$.scalar);
                                        }
-                               ;
-
-stmt_fors              : opt_label K_FOR lno fors_target K_IN K_SELECT expr_until_loop loop_body
+                               | T_WORD
                                        {
-                                               PLpgSQL_stmt_fors               *new;
-
-                                               new = malloc(sizeof(PLpgSQL_stmt_fors));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_fors));
+                                               int                     tok;
 
-                                               new->cmd_type = PLPGSQL_STMT_FORS;
-                                               new->lineno   = $3;
-                                               new->label        = $1;
-                                               switch ($4->dtype)
+                                               $$.name = pstrdup(yytext);
+                                               $$.lineno  = plpgsql_scanner_lineno();
+                                               $$.scalar = NULL;
+                                               $$.rec = NULL;
+                                               $$.row = NULL;
+                                               /* check for comma-separated list */
+                                               tok = yylex();
+                                               plpgsql_push_back_token(tok);
+                                               if (tok == ',')
                                                {
-                                                       case PLPGSQL_DTYPE_REC:
-                                                               new->rec = $4;
-                                                               break;
-                                                       case PLPGSQL_DTYPE_ROW:
-                                                               new->row = (PLpgSQL_row *)$4;
-                                                               break;
-                                                       default:
-                                                               elog(ERROR, "unrecognized dtype: %d",
-                                                                        $4->dtype);
+                                                       plpgsql_error_lineno = $$.lineno;
+                                                       ereport(ERROR,
+                                                                       (errcode(ERRCODE_SYNTAX_ERROR),
+                                                                        errmsg("\"%s\" is not a scalar variable",
+                                                                                       $$.name)));
                                                }
-                                               new->query = $7;
-                                               new->body  = $8;
-
-                                               plpgsql_ns_pop();
-
-                                               $$ = (PLpgSQL_stmt *)new;
                                        }
-                               ;
-
-stmt_dynfors : opt_label K_FOR lno fors_target K_IN K_EXECUTE expr_until_loop loop_body
+                               | T_RECORD
                                        {
-                                               PLpgSQL_stmt_dynfors    *new;
-
-                                               new = malloc(sizeof(PLpgSQL_stmt_dynfors));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_dynfors));
-
-                                               new->cmd_type = PLPGSQL_STMT_DYNFORS;
-                                               new->lineno   = $3;
-                                               new->label        = $1;
-                                               switch ($4->dtype)
-                                               {
-                                                       case PLPGSQL_DTYPE_REC:
-                                                               new->rec = $4;
-                                                               break;
-                                                       case PLPGSQL_DTYPE_ROW:
-                                                               new->row = (PLpgSQL_row *)$4;
-                                                               break;
-                                                       default:
-                                                               elog(ERROR, "unrecognized dtype: %d",
-                                                                        $4->dtype);
-                                               }
-                                               new->query = $7;
-                                               new->body  = $8;
-
-                                               plpgsql_ns_pop();
-
-                                               $$ = (PLpgSQL_stmt *)new;
+                                               $$.name = pstrdup(yytext);
+                                               $$.lineno  = plpgsql_scanner_lineno();
+                                               $$.scalar = NULL;
+                                               $$.rec = yylval.rec;
+                                               $$.row = NULL;
                                        }
-                               ;
-
-fors_target            : T_RECORD
-                                       { $$ = yylval.rec; }
                                | T_ROW
                                        {
-                                               $$ = (PLpgSQL_rec *)(yylval.row);
+                                               $$.name = pstrdup(yytext);
+                                               $$.lineno  = plpgsql_scanner_lineno();
+                                               $$.scalar = NULL;
+                                               $$.row = yylval.row;
+                                               $$.rec = NULL;
                                        }
                                ;
 
@@ -1153,15 +1121,14 @@ stmt_select             : K_SELECT lno
                                        }
                                ;
 
-stmt_exit              : K_EXIT lno opt_exitlabel opt_exitcond
+stmt_exit              : exit_type lno opt_label opt_exitcond
                                        {
                                                PLpgSQL_stmt_exit *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_exit));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_exit));
-
+                                               new = palloc0(sizeof(PLpgSQL_stmt_exit));
                                                new->cmd_type = PLPGSQL_STMT_EXIT;
-                                               new->lineno   = $2;
+                                               new->is_exit  = $1;
+                                               new->lineno       = $2;
                                                new->label        = $3;
                                                new->cond         = $4;
 
@@ -1169,44 +1136,74 @@ stmt_exit               : K_EXIT lno opt_exitlabel opt_exitcond
                                        }
                                ;
 
+exit_type              : K_EXIT
+                                       {
+                                               $$ = true;
+                                       }
+                               | K_CONTINUE
+                                       {
+                                               $$ = false;
+                                       }
+                               ;
+
 stmt_return            : K_RETURN lno
                                        {
                                                PLpgSQL_stmt_return *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_return));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_return));
-                                               new->expr = NULL;
-                                               new->retrecno   = -1;
-                                               new->retrowno   = -1;
+                                               new = palloc0(sizeof(PLpgSQL_stmt_return));
+                                               new->cmd_type = PLPGSQL_STMT_RETURN;
+                                               new->lineno   = $2;
+                                               new->expr         = NULL;
+                                               new->retvarno = -1;
 
-                                               if (plpgsql_curr_compile->fn_retistuple &&
-                                                       !plpgsql_curr_compile->fn_retset)
+                                               if (plpgsql_curr_compile->fn_retset)
                                                {
-                                                       switch (yylex())
-                                                       {
-                                                               case K_NULL:
-                                                                       break;
-
-                                                               case T_ROW:
-                                                                       new->retrowno = yylval.row->rowno;
+                                                       if (yylex() != ';')
+                                                               yyerror("RETURN cannot have a parameter in function returning set; use RETURN NEXT");
+                                               }
+                                               else if (plpgsql_curr_compile->out_param_varno >= 0)
+                                               {
+                                                       if (yylex() != ';')
+                                                               yyerror("RETURN cannot have a parameter in function with OUT parameters");
+                                                       new->retvarno = plpgsql_curr_compile->out_param_varno;
+                                               }
+                                               else if (plpgsql_curr_compile->fn_rettype == VOIDOID)
+                                               {
+                                                       if (yylex() != ';')
+                                                               yyerror("RETURN cannot have a parameter in function returning void");
+                                               }
+                                               else if (plpgsql_curr_compile->fn_retistuple)
+                                               {
+                                                       switch (yylex())
+                                                       {
+                                                               case K_NULL:
+                                                                       /* we allow this to support RETURN NULL in triggers */
+                                                                       break;
+
+                                                               case T_ROW:
+                                                                       new->retvarno = yylval.row->rowno;
                                                                        break;
 
                                                                case T_RECORD:
-                                                                       new->retrecno = yylval.rec->recno;
+                                                                       new->retvarno = yylval.rec->recno;
                                                                        break;
 
                                                                default:
-                                                                       yyerror("return type mismatch in function returning tuple");
+                                                                       yyerror("RETURN must specify a record or row variable in function returning tuple");
                                                                        break;
                                                        }
                                                        if (yylex() != ';')
-                                                               yyerror("expected \";\"");
+                                                               yyerror("RETURN must specify a record or row variable in function returning tuple");
                                                }
                                                else
+                                               {
+                                                       /*
+                                                        * Note that a well-formed expression is
+                                                        * _required_ here; anything else is a
+                                                        * compile-time error.
+                                                        */
                                                        new->expr = plpgsql_read_expression(';', ";");
-
-                                               new->cmd_type = PLPGSQL_STMT_RETURN;
-                                               new->lineno   = $2;
+                                               }
 
                                                $$ = (PLpgSQL_stmt *)new;
                                        }
@@ -1216,25 +1213,39 @@ stmt_return_next: K_RETURN_NEXT lno
                                        {
                                                PLpgSQL_stmt_return_next *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_return_next));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_return_next));
+                                               if (!plpgsql_curr_compile->fn_retset)
+                                                       yyerror("cannot use RETURN NEXT in a non-SETOF function");
 
+                                               new = palloc0(sizeof(PLpgSQL_stmt_return_next));
                                                new->cmd_type   = PLPGSQL_STMT_RETURN_NEXT;
                                                new->lineno             = $2;
+                                               new->expr = NULL;
+                                               new->retvarno   = -1;
 
-                                               if (plpgsql_curr_compile->fn_retistuple)
+                                               if (plpgsql_curr_compile->out_param_varno >= 0)
+                                               {
+                                                       if (yylex() != ';')
+                                                               yyerror("RETURN NEXT cannot have a parameter in function with OUT parameters");
+                                                       new->retvarno = plpgsql_curr_compile->out_param_varno;
+                                               }
+                                               else if (plpgsql_curr_compile->fn_retistuple)
                                                {
-                                                       int tok = yylex();
+                                                       switch (yylex())
+                                                       {
+                                                               case T_ROW:
+                                                                       new->retvarno = yylval.row->rowno;
+                                                                       break;
 
-                                                       if (tok == T_RECORD)
-                                                               new->rec = yylval.rec;
-                                                       else if (tok == T_ROW)
-                                                               new->row = yylval.row;
-                                                       else
-                                                               yyerror("incorrect argument to RETURN NEXT");
+                                                               case T_RECORD:
+                                                                       new->retvarno = yylval.rec->recno;
+                                                                       break;
 
+                                                               default:
+                                                                       yyerror("RETURN NEXT must specify a record or row variable in function returning tuple");
+                                                                       break;
+                                                       }
                                                        if (yylex() != ';')
-                                                               yyerror("expected \";\"");
+                                                               yyerror("RETURN NEXT must specify a record or row variable in function returning tuple");
                                                }
                                                else
                                                        new->expr = plpgsql_read_expression(';', ";");
@@ -1243,34 +1254,44 @@ stmt_return_next: K_RETURN_NEXT lno
                                        }
                                ;
 
-stmt_raise             : K_RAISE lno raise_level raise_msg raise_params ';'
+stmt_raise             : K_RAISE lno raise_level raise_msg
                                        {
                                                PLpgSQL_stmt_raise              *new;
+                                               int     tok;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_raise));
+                                               new = palloc(sizeof(PLpgSQL_stmt_raise));
 
                                                new->cmd_type   = PLPGSQL_STMT_RAISE;
                                                new->lineno             = $2;
                                                new->elog_level = $3;
                                                new->message    = $4;
-                                               new->nparams    = $5.nused;
-                                               new->params             = malloc(sizeof(int) * $5.nused);
-                                               memcpy(new->params, $5.nums, sizeof(int) * $5.nused);
+                                               new->params             = NIL;
 
-                                               $$ = (PLpgSQL_stmt *)new;
-                                       }
-                               | K_RAISE lno raise_level raise_msg ';'
-                                       {
-                                               PLpgSQL_stmt_raise              *new;
+                                               tok = yylex();
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_raise));
+                                               /*
+                                                * We expect either a semi-colon, which
+                                                * indicates no parameters, or a comma that
+                                                * begins the list of parameter expressions
+                                                */
+                                               if (tok != ',' && tok != ';')
+                                                       yyerror("syntax error");
 
-                                               new->cmd_type   = PLPGSQL_STMT_RAISE;
-                                               new->lineno             = $2;
-                                               new->elog_level = $3;
-                                               new->message    = $4;
-                                               new->nparams    = 0;
-                                               new->params             = NULL;
+                                               if (tok == ',')
+                                               {
+                                                       PLpgSQL_expr *expr;
+                                                       int term;
+
+                                                       for (;;)
+                                                       {
+                                                               expr = read_sql_construct(',', ';', ", or ;",
+                                                                                                                 "SELECT ",
+                                                                                                                 true, true, &term);
+                                                               new->params = lappend(new->params, expr);
+                                                               if (term == ';')
+                                                                       break;
+                                                       }
+                                               }
 
                                                $$ = (PLpgSQL_stmt *)new;
                                        }
@@ -1278,7 +1299,7 @@ stmt_raise                : K_RAISE lno raise_level raise_msg raise_params ';'
 
 raise_msg              : T_STRING
                                        {
-                                               $$ = strdup(yytext);
+                                               $$ = plpgsql_get_string_value();
                                        }
                                ;
 
@@ -1304,47 +1325,22 @@ raise_level             : K_EXCEPTION
                                        }
                                | K_DEBUG
                                        {
-                                               $$ = DEBUG2;
-                                       }
-                               ;
-
-raise_params   : raise_params raise_param
-                                       {
-                                               if ($1.nused == $1.nalloc)
-                                               {
-                                                       $1.nalloc *= 2;
-                                                       $1.nums = repalloc($1.nums, sizeof(int) * $1.nalloc);
-                                               }
-                                               $1.nums[$1.nused++] = $2;
-
-                                               $$.nalloc = $1.nalloc;
-                                               $$.nused  = $1.nused;
-                                               $$.nums   = $1.nums;
-                                       }
-                               | raise_param
-                                       {
-                                               $$.nalloc = 1;
-                                               $$.nused  = 1;
-                                               $$.nums   = palloc(sizeof(int) * $$.nalloc);
-                                               $$.nums[0] = $1;
+                                               $$ = DEBUG1;
                                        }
                                ;
 
-raise_param            : ',' T_VARIABLE
+loop_body              : proc_sect K_END K_LOOP opt_label ';'
                                        {
-                                               $$ = yylval.variable->dno;
+                                               $$.stmts = $1;
+                                               $$.end_label = $4;
                                        }
                                ;
 
-loop_body              : proc_sect K_END K_LOOP ';'
-                                       { $$ = $1; }
-                               ;
-
 stmt_execsql   : execsql_start lno
                                        {
                                                PLpgSQL_stmt_execsql    *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_execsql));
+                                               new = palloc(sizeof(PLpgSQL_stmt_execsql));
                                                new->cmd_type = PLPGSQL_STMT_EXECSQL;
                                                new->lineno   = $2;
                                                new->sqlstmt  = read_sql_stmt($1);
@@ -1353,27 +1349,67 @@ stmt_execsql    : execsql_start lno
                                        }
                                ;
 
-stmt_dynexecute : K_EXECUTE lno expr_until_semi
+stmt_dynexecute : K_EXECUTE lno
+                                       {
+                                               PLpgSQL_stmt_dynexecute *new;
+                                               PLpgSQL_expr *expr;
+                                               int endtoken;
+
+                                               expr = read_sql_construct(K_INTO, ';', "INTO|;", "SELECT ",
+                                                                                                 true, true, &endtoken);
+
+                                               new = palloc(sizeof(PLpgSQL_stmt_dynexecute));
+                                               new->cmd_type = PLPGSQL_STMT_DYNEXECUTE;
+                                               new->lineno   = $2;
+                                               new->query    = expr;
+                                               new->rec = NULL;
+                                               new->row = NULL;
+
+                                               /*
+                                                * If we saw "INTO", look for a following row
+                                                * var, record var, or list of scalars.
+                                                */
+                                               if (endtoken == K_INTO)
                                                {
-                                                       PLpgSQL_stmt_dynexecute *new;
+                                                       switch (yylex())
+                                                       {
+                                                               case T_ROW:
+                                                                       new->row = yylval.row;
+                                                                       check_assignable((PLpgSQL_datum *) new->row);
+                                                                       break;
 
-                                                       new = malloc(sizeof(PLpgSQL_stmt_dynexecute));
-                                                       new->cmd_type = PLPGSQL_STMT_DYNEXECUTE;
-                                                       new->lineno   = $2;
-                                                       new->query        = $3;
+                                                               case T_RECORD:
+                                                                       new->rec = yylval.rec;
+                                                                       check_assignable((PLpgSQL_datum *) new->rec);
+                                                                       break;
+
+                                                               case T_SCALAR:
+                                                                       new->row = read_into_scalar_list(yytext, yylval.scalar);
+                                                                       break;
 
-                                                       $$ = (PLpgSQL_stmt *)new;
+                                                               default:
+                                                                       plpgsql_error_lineno = $2;
+                                                                       ereport(ERROR,
+                                                                                       (errcode(ERRCODE_SYNTAX_ERROR),
+                                                                                        errmsg("syntax error at \"%s\"", yytext),
+                                                                                        errdetail("Expected record variable, row variable, "
+                                                                                                          "or list of scalar variables.")));
+                                                       }
+                                                       if (yylex() != ';')
+                                                               yyerror("syntax error");
                                                }
+
+                                               $$ = (PLpgSQL_stmt *)new;
+                                       }
                                ;
 
+
 stmt_open              : K_OPEN lno cursor_varptr
                                        {
                                                PLpgSQL_stmt_open *new;
                                                int                               tok;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_open));
-                                               memset(new, 0, sizeof(PLpgSQL_stmt_open));
-
+                                               new = palloc0(sizeof(PLpgSQL_stmt_open));
                                                new->cmd_type = PLPGSQL_STMT_OPEN;
                                                new->lineno = $2;
                                                new->curvar = $3->varno;
@@ -1381,7 +1417,6 @@ stmt_open         : K_OPEN lno cursor_varptr
                                                if ($3->cursor_explicit_expr == NULL)
                                                {
                                                    tok = yylex();
-
                                                        if (tok != K_FOR)
                                                        {
                                                                plpgsql_error_lineno = $2;
@@ -1393,26 +1428,15 @@ stmt_open               : K_OPEN lno cursor_varptr
                                                        }
 
                                                        tok = yylex();
-                                                       switch (tok)
+                                                       if (tok == K_EXECUTE)
                                                        {
-                                                               case K_SELECT:
-                                                               case '(':
-                                                                       plpgsql_push_back_token(tok);
-                                                                       new->query = read_sql_stmt("");
-                                                                       break;
-
-                                                               case K_EXECUTE:
-                                                                       new->dynquery = read_sql_stmt("SELECT ");
-                                                                       break;
-
-                                                               default:
-                                                                       plpgsql_error_lineno = $2;
-                                                                       ereport(ERROR,
-                                                                                       (errcode(ERRCODE_SYNTAX_ERROR),
-                                                                                        errmsg("syntax error at \"%s\"",
-                                                                                                       yytext)));
+                                                               new->dynquery = read_sql_stmt("SELECT ");
+                                                       }
+                                                       else
+                                                       {
+                                                               plpgsql_push_back_token(tok);
+                                                               new->query = read_sql_stmt("");
                                                        }
-
                                                }
                                                else
                                                {
@@ -1421,7 +1445,6 @@ stmt_open         : K_OPEN lno cursor_varptr
                                                                char   *cp;
 
                                                                tok = yylex();
-
                                                                if (tok != '(')
                                                                {
                                                                        plpgsql_error_lineno = plpgsql_scanner_lineno();
@@ -1474,7 +1497,6 @@ stmt_open         : K_OPEN lno cursor_varptr
                                                        else
                                                        {
                                                                tok = yylex();
-
                                                                if (tok == '(')
                                                                {
                                                                        plpgsql_error_lineno = plpgsql_scanner_lineno();
@@ -1483,7 +1505,7 @@ stmt_open         : K_OPEN lno cursor_varptr
                                                                                         errmsg("cursor \"%s\" has no arguments",
                                                                                                        $3->refname)));
                                                                }
-                                                               
+
                                                                if (tok != ';')
                                                                {
                                                                        plpgsql_error_lineno = plpgsql_scanner_lineno();
@@ -1515,7 +1537,7 @@ stmt_close                : K_CLOSE lno cursor_variable ';'
                                        {
                                                PLpgSQL_stmt_close *new;
 
-                                               new = malloc(sizeof(PLpgSQL_stmt_close));
+                                               new = palloc(sizeof(PLpgSQL_stmt_close));
                                                new->cmd_type = PLPGSQL_STMT_CLOSE;
                                                new->lineno = $2;
                                                new->curvar = $3;
@@ -1524,44 +1546,125 @@ stmt_close             : K_CLOSE lno cursor_variable ';'
                                        }
                                ;
 
-cursor_varptr  : T_VARIABLE
+stmt_null              : K_NULL ';'
                                        {
-                                               if (yylval.variable->dtype != PLPGSQL_DTYPE_VAR)
+                                               /* We do not bother building a node for NULL */
+                                               $$ = NULL;
+                                       }
+                               ;
+
+cursor_varptr  : T_SCALAR
+                                       {
+                                               if (yylval.scalar->dtype != PLPGSQL_DTYPE_VAR)
                                                        yyerror("cursor variable must be a simple variable");
 
-                                               if (((PLpgSQL_var *) yylval.variable)->datatype->typoid != REFCURSOROID)
+                                               if (((PLpgSQL_var *) yylval.scalar)->datatype->typoid != REFCURSOROID)
                                                {
                                                        plpgsql_error_lineno = plpgsql_scanner_lineno();
                                                        ereport(ERROR,
                                                                        (errcode(ERRCODE_DATATYPE_MISMATCH),
                                                                         errmsg("\"%s\" must be of type cursor or refcursor",
-                                                                                       ((PLpgSQL_var *) yylval.variable)->refname)));
+                                                                                       ((PLpgSQL_var *) yylval.scalar)->refname)));
                                                }
-                                               $$ = (PLpgSQL_var *) yylval.variable;
+                                               $$ = (PLpgSQL_var *) yylval.scalar;
                                        }
                                ;
 
-cursor_variable        : T_VARIABLE
+cursor_variable        : T_SCALAR
                                        {
-                                               if (yylval.variable->dtype != PLPGSQL_DTYPE_VAR)
+                                               if (yylval.scalar->dtype != PLPGSQL_DTYPE_VAR)
                                                        yyerror("cursor variable must be a simple variable");
 
-                                               if (((PLpgSQL_var *) yylval.variable)->datatype->typoid != REFCURSOROID)
+                                               if (((PLpgSQL_var *) yylval.scalar)->datatype->typoid != REFCURSOROID)
                                                {
                                                        plpgsql_error_lineno = plpgsql_scanner_lineno();
                                                        ereport(ERROR,
                                                                        (errcode(ERRCODE_DATATYPE_MISMATCH),
                                                                         errmsg("\"%s\" must be of type refcursor",
-                                                                                       ((PLpgSQL_var *) yylval.variable)->refname)));
+                                                                                       ((PLpgSQL_var *) yylval.scalar)->refname)));
                                                }
-                                               $$ = yylval.variable->dno;
+                                               $$ = yylval.scalar->dno;
                                        }
                                ;
 
 execsql_start  : T_WORD
-                                       { $$ = strdup(yytext); }
+                                       { $$ = pstrdup(yytext); }
                                | T_ERROR
-                                       { $$ = strdup(yytext); }
+                                       { $$ = pstrdup(yytext); }
+                               ;
+
+exception_sect :
+                                       { $$ = NULL; }
+                               | K_EXCEPTION lno
+                                       {
+                                               /*
+                                                * We use a mid-rule action to add these
+                                                * special variables to the namespace before
+                                                * parsing the WHEN clauses themselves.
+                                                */
+                                               PLpgSQL_exception_block *new = palloc(sizeof(PLpgSQL_exception_block));
+                                               PLpgSQL_variable *var;
+
+                                               var = plpgsql_build_variable("sqlstate", $2,
+                                                                                                        plpgsql_build_datatype(TEXTOID, -1),
+                                                                                                        true);
+                                               ((PLpgSQL_var *) var)->isconst = true;
+                                               new->sqlstate_varno = var->dno;
+
+                                               var = plpgsql_build_variable("sqlerrm", $2,
+                                                                                                        plpgsql_build_datatype(TEXTOID, -1),
+                                                                                                        true);
+                                               ((PLpgSQL_var *) var)->isconst = true;
+                                               new->sqlerrm_varno = var->dno;
+
+                                               $<exception_block>$ = new;
+                                       }
+                                       proc_exceptions
+                                       {
+                                               PLpgSQL_exception_block *new = $<exception_block>3;
+                                               new->exc_list = $4;
+
+                                               $$ = new;
+                                       }
+                               ;
+
+proc_exceptions        : proc_exceptions proc_exception
+                                               {
+                                                       $$ = lappend($1, $2);
+                                               }
+                               | proc_exception
+                                               {
+                                                       $$ = list_make1($1);
+                                               }
+                               ;
+
+proc_exception : K_WHEN lno proc_conditions K_THEN proc_sect
+                                       {
+                                               PLpgSQL_exception *new;
+
+                                               new = palloc0(sizeof(PLpgSQL_exception));
+                                               new->lineno     = $2;
+                                               new->conditions = $3;
+                                               new->action         = $5;
+
+                                               $$ = new;
+                                       }
+                               ;
+
+proc_conditions        : proc_conditions K_OR opt_lblname
+                                               {
+                                                       PLpgSQL_condition       *old;
+
+                                                       for (old = $1; old->next != NULL; old = old->next)
+                                                               /* skip */ ;
+                                                       old->next = plpgsql_parse_err_condition($3);
+
+                                                       $$ = $1;
+                                               }
+                               | opt_lblname
+                                               {
+                                                       $$ = plpgsql_parse_err_condition($1);
+                                               }
                                ;
 
 expr_until_semi :
@@ -1580,7 +1683,7 @@ expr_until_loop :
                                        { $$ = plpgsql_read_expression(K_LOOP, "LOOP"); }
                                ;
 
-opt_label              :
+opt_block_label        :
                                        {
                                                plpgsql_ns_push(NULL);
                                                $$ = NULL;
@@ -1592,10 +1695,21 @@ opt_label               :
                                        }
                                ;
 
-opt_exitlabel  :
-                                       { $$ = NULL; }
+opt_label      :
+                                       {
+                                               $$ = NULL;
+                                       }
                                | T_LABEL
-                                       { $$ = strdup(yytext); }
+                                       {
+                                               char *label_name;
+                                               plpgsql_convert_ident(yytext, &label_name, 1);
+                                               $$ = label_name;
+                                       }
+                               | T_WORD
+                                       {
+                                               /* just to give a better error than "syntax error" */
+                                               yyerror("no such label");
+                                       }
                                ;
 
 opt_exitcond   : ';'
@@ -1609,8 +1723,7 @@ opt_lblname               : T_WORD
                                                char    *name;
 
                                                plpgsql_convert_ident(yytext, &name, 1);
-                                               $$ = strdup(name);
-                                               pfree(name);
+                                               $$ = name;
                                        }
                                ;
 
@@ -1626,20 +1739,35 @@ lno                             :
 PLpgSQL_expr *
 plpgsql_read_expression(int until, const char *expected)
 {
-       return read_sql_construct(until, expected, true, "SELECT ");
+       return read_sql_construct(until, 0, expected, "SELECT ", true, true, NULL);
 }
 
 static PLpgSQL_expr *
 read_sql_stmt(const char *sqlstart)
 {
-       return read_sql_construct(';', ";", false, sqlstart);
+       return read_sql_construct(';', 0, ";", sqlstart, false, true, NULL);
 }
 
+/*
+ * Read a SQL construct and build a PLpgSQL_expr for it.
+ *
+ * until:              token code for expected terminator
+ * until2:             token code for alternate terminator (pass 0 if none)
+ * expected:   text to use in complaining that terminator was not found
+ * sqlstart:   text to prefix to the accumulated SQL text
+ * isexpression: whether to say we're reading an "expression" or a "statement"
+ * valid_sql:   whether to check the syntax of the expr (prefixed with sqlstart)
+ * endtoken:   if not NULL, ending token is stored at *endtoken
+ *                             (this is only interesting if until2 isn't zero)
+ */
 static PLpgSQL_expr *
 read_sql_construct(int until,
+                                  int until2,
                                   const char *expected,
+                                  const char *sqlstart,
                                   bool isexpression,
-                                  const char *sqlstart)
+                                  bool valid_sql,
+                                  int *endtoken)
 {
        int                                     tok;
        int                                     lno;
@@ -1652,13 +1780,15 @@ read_sql_construct(int until,
 
        lno = plpgsql_scanner_lineno();
        plpgsql_dstring_init(&ds);
-       plpgsql_dstring_append(&ds, (char *) sqlstart);
+       plpgsql_dstring_append(&ds, sqlstart);
 
        for (;;)
        {
                tok = yylex();
                if (tok == until && parenlevel == 0)
                        break;
+               if (tok == until2 && parenlevel == 0)
+                       break;
                if (tok == '(' || tok == '[')
                        parenlevel++;
                else if (tok == ')' || tok == ']')
@@ -1691,14 +1821,36 @@ read_sql_construct(int until,
                                                (errcode(ERRCODE_SYNTAX_ERROR),
                                                 errmsg("missing \"%s\" at end of SQL statement",
                                                                expected)));
-                       break;
                }
+
                if (plpgsql_SpaceScanned)
                        plpgsql_dstring_append(&ds, " ");
+
+               /* Check for array overflow */
+               if (nparams >= 1024)
+               {
+                       plpgsql_error_lineno = lno;
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                        errmsg("too many variables specified in SQL statement")));
+               }
+
                switch (tok)
                {
-                       case T_VARIABLE:
-                               params[nparams] = yylval.variable->dno;
+                       case T_SCALAR:
+                               params[nparams] = yylval.scalar->dno;
+                               snprintf(buf, sizeof(buf), " $%d ", ++nparams);
+                               plpgsql_dstring_append(&ds, buf);
+                               break;
+
+                       case T_ROW:
+                               params[nparams] = yylval.row->rowno;
+                               snprintf(buf, sizeof(buf), " $%d ", ++nparams);
+                               plpgsql_dstring_append(&ds, buf);
+                               break;
+
+                       case T_RECORD:
+                               params[nparams] = yylval.rec->recno;
                                snprintf(buf, sizeof(buf), " $%d ", ++nparams);
                                plpgsql_dstring_append(&ds, buf);
                                break;
@@ -1709,15 +1861,21 @@ read_sql_construct(int until,
                }
        }
 
-       expr = malloc(sizeof(PLpgSQL_expr) + sizeof(int) * nparams - sizeof(int));
+       if (endtoken)
+               *endtoken = tok;
+
+       expr = palloc(sizeof(PLpgSQL_expr) + sizeof(int) * nparams - sizeof(int));
        expr->dtype                     = PLPGSQL_DTYPE_EXPR;
-       expr->query                     = strdup(plpgsql_dstring_get(&ds));
+       expr->query                     = pstrdup(plpgsql_dstring_get(&ds));
        expr->plan                      = NULL;
        expr->nparams           = nparams;
        while(nparams-- > 0)
                expr->params[nparams] = params[nparams];
        plpgsql_dstring_free(&ds);
 
+       if (valid_sql)
+               check_sql_expr(expr->query);
+
        return expr;
 }
 
@@ -1786,7 +1944,6 @@ read_datatype(int tok)
        return result;
 }
 
-
 static PLpgSQL_stmt *
 make_select_stmt(void)
 {
@@ -1797,18 +1954,16 @@ make_select_stmt(void)
        PLpgSQL_expr            *expr;
        PLpgSQL_row                     *row = NULL;
        PLpgSQL_rec                     *rec = NULL;
-       int                                     tok = 0;
-       int                                     have_nexttok = 0;
-       int                                     have_into = 0;
+       int                                     tok;
+       bool                            have_into = false;
 
        plpgsql_dstring_init(&ds);
        plpgsql_dstring_append(&ds, "SELECT ");
 
-       while(1)
+       while (1)
        {
-               if (!have_nexttok)
-                       tok = yylex();
-               have_nexttok = 0;
+               tok = yylex();
+
                if (tok == ';')
                        break;
                if (tok == 0)
@@ -1832,69 +1987,25 @@ make_select_stmt(void)
                        {
                                case T_ROW:
                                        row = yylval.row;
-                                       have_into = 1;
+                                       check_assignable((PLpgSQL_datum *) row);
+                                       have_into = true;
                                        break;
 
                                case T_RECORD:
                                        rec = yylval.rec;
-                                       have_into = 1;
+                                       check_assignable((PLpgSQL_datum *) rec);
+                                       have_into = true;
                                        break;
 
-                               case T_VARIABLE:
-                               {
-                                       int                             nfields = 1;
-                                       char                    *fieldnames[1024];
-                                       int                             varnos[1024];
-
-                                       check_assignable(yylval.variable);
-                                       fieldnames[0] = strdup(yytext);
-                                       varnos[0]         = yylval.variable->dno;
-
-                                       while ((tok = yylex()) == ',')
-                                       {
-                                               tok = yylex();
-                                               switch(tok)
-                                               {
-                                                       case T_VARIABLE:
-                                                               check_assignable(yylval.variable);
-                                                               fieldnames[nfields] = strdup(yytext);
-                                                               varnos[nfields++]       = yylval.variable->dno;
-                                                               break;
-
-                                                       default:
-                                                               plpgsql_error_lineno = plpgsql_scanner_lineno();
-                                                               ereport(ERROR,
-                                                                               (errcode(ERRCODE_SYNTAX_ERROR),
-                                                                                errmsg("\"%s\" is not a variable",
-                                                                                               yytext)));
-                                               }
-                                       }
-                                       have_nexttok = 1;
-
-                                       row = malloc(sizeof(PLpgSQL_row));
-                                       row->dtype = PLPGSQL_DTYPE_ROW;
-                                       row->refname = strdup("*internal*");
-                                       row->lineno = plpgsql_scanner_lineno();
-                                       row->rowtupdesc = NULL;
-                                       row->nfields = nfields;
-                                       row->fieldnames = malloc(sizeof(char *) * nfields);
-                                       row->varnos = malloc(sizeof(int) * nfields);
-                                       while (--nfields >= 0)
-                                       {
-                                               row->fieldnames[nfields] = fieldnames[nfields];
-                                               row->varnos[nfields] = varnos[nfields];
-                                       }
-
-                                       plpgsql_adddatum((PLpgSQL_datum *)row);
-
-                                       have_into = 1;
-                               }
-                               break;
+                               case T_SCALAR:
+                                       row = read_into_scalar_list(yytext, yylval.scalar);
+                                       have_into = true;
+                                       break;
 
                                default:
                                        /* Treat the INTO as non-special */
                                        plpgsql_dstring_append(&ds, " INTO ");
-                                       have_nexttok = 1;
+                                       plpgsql_push_back_token(tok);
                                        break;
                        }
                        continue;
@@ -1902,10 +2013,32 @@ make_select_stmt(void)
 
                if (plpgsql_SpaceScanned)
                        plpgsql_dstring_append(&ds, " ");
+
+               /* Check for array overflow */
+               if (nparams >= 1024)
+               {
+                       plpgsql_error_lineno = plpgsql_scanner_lineno();
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                        errmsg("too many parameters specified in SQL statement")));
+               }
+
                switch (tok)
                {
-                       case T_VARIABLE:
-                               params[nparams] = yylval.variable->dno;
+                       case T_SCALAR:
+                               params[nparams] = yylval.scalar->dno;
+                               snprintf(buf, sizeof(buf), " $%d ", ++nparams);
+                               plpgsql_dstring_append(&ds, buf);
+                               break;
+
+                       case T_ROW:
+                               params[nparams] = yylval.row->rowno;
+                               snprintf(buf, sizeof(buf), " $%d ", ++nparams);
+                               plpgsql_dstring_append(&ds, buf);
+                               break;
+
+                       case T_RECORD:
+                               params[nparams] = yylval.rec->recno;
                                snprintf(buf, sizeof(buf), " $%d ", ++nparams);
                                plpgsql_dstring_append(&ds, buf);
                                break;
@@ -1916,21 +2049,22 @@ make_select_stmt(void)
                }
        }
 
-       expr = malloc(sizeof(PLpgSQL_expr) + sizeof(int) * nparams - sizeof(int));
+       expr = palloc(sizeof(PLpgSQL_expr) + sizeof(int) * nparams - sizeof(int));
        expr->dtype                     = PLPGSQL_DTYPE_EXPR;
-       expr->query                     = strdup(plpgsql_dstring_get(&ds));
+       expr->query                     = pstrdup(plpgsql_dstring_get(&ds));
        expr->plan                      = NULL;
        expr->nparams           = nparams;
        while(nparams-- > 0)
                expr->params[nparams] = params[nparams];
        plpgsql_dstring_free(&ds);
 
+       check_sql_expr(expr->query);
+
        if (have_into)
        {
                PLpgSQL_stmt_select *select;
 
-               select = malloc(sizeof(PLpgSQL_stmt_select));
-               memset(select, 0, sizeof(PLpgSQL_stmt_select));
+               select = palloc0(sizeof(PLpgSQL_stmt_select));
                select->cmd_type = PLPGSQL_STMT_SELECT;
                select->rec              = rec;
                select->row              = row;
@@ -1942,7 +2076,7 @@ make_select_stmt(void)
        {
                PLpgSQL_stmt_execsql *execsql;
 
-               execsql = malloc(sizeof(PLpgSQL_stmt_execsql));
+               execsql = palloc(sizeof(PLpgSQL_stmt_execsql));
                execsql->cmd_type = PLPGSQL_STMT_EXECSQL;
                execsql->sqlstmt  = expr;
 
@@ -1958,7 +2092,6 @@ make_fetch_stmt(void)
        PLpgSQL_row                *row = NULL;
        PLpgSQL_rec                *rec = NULL;
        PLpgSQL_stmt_fetch *fetch;
-       int                                     have_nexttok = 0;
 
        /* We have already parsed everything through the INTO keyword */
 
@@ -1967,73 +2100,32 @@ make_fetch_stmt(void)
        {
                case T_ROW:
                        row = yylval.row;
+                       check_assignable((PLpgSQL_datum *) row);
                        break;
 
                case T_RECORD:
                        rec = yylval.rec;
+                       check_assignable((PLpgSQL_datum *) rec);
                        break;
 
-               case T_VARIABLE:
-                       {
-                               int                             nfields = 1;
-                               char                    *fieldnames[1024];
-                               int                             varnos[1024];
-
-                               check_assignable(yylval.variable);
-                               fieldnames[0] = strdup(yytext);
-                               varnos[0]         = yylval.variable->dno;
-
-                               while ((tok = yylex()) == ',')
-                               {
-                                       tok = yylex();
-                                       switch(tok)
-                                       {
-                                               case T_VARIABLE:
-                                                       check_assignable(yylval.variable);
-                                                       fieldnames[nfields] = strdup(yytext);
-                                                       varnos[nfields++]       = yylval.variable->dno;
-                                                       break;
-
-                                               default:
-                                                       plpgsql_error_lineno = plpgsql_scanner_lineno();
-                                                       ereport(ERROR,
-                                                                       (errcode(ERRCODE_SYNTAX_ERROR),
-                                                                        errmsg("\"%s\" is not a variable",
-                                                                                       yytext)));
-                                       }
-                               }
-                               have_nexttok = 1;
-
-                               row = malloc(sizeof(PLpgSQL_row));
-                               row->dtype = PLPGSQL_DTYPE_ROW;
-                               row->refname = strdup("*internal*");
-                               row->lineno = plpgsql_scanner_lineno();
-                               row->rowtupdesc = NULL;
-                               row->nfields = nfields;
-                               row->fieldnames = malloc(sizeof(char *) * nfields);
-                               row->varnos = malloc(sizeof(int) * nfields);
-                               while (--nfields >= 0)
-                               {
-                                       row->fieldnames[nfields] = fieldnames[nfields];
-                                       row->varnos[nfields] = varnos[nfields];
-                               }
-
-                               plpgsql_adddatum((PLpgSQL_datum *)row);
-                       }
+               case T_SCALAR:
+                       row = read_into_scalar_list(yytext, yylval.scalar);
                        break;
 
                default:
-                       yyerror("syntax error");
+                       plpgsql_error_lineno = plpgsql_scanner_lineno();
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_SYNTAX_ERROR),
+                                        errmsg("syntax error at \"%s\"", yytext),
+                                        errdetail("Expected record variable, row variable, "
+                                                          "or list of scalar variables.")));
        }
 
-       if (!have_nexttok)
-               tok = yylex();
-
+       tok = yylex();
        if (tok != ';')
                yyerror("syntax error");
 
-       fetch = malloc(sizeof(PLpgSQL_stmt_select));
-       memset(fetch, 0, sizeof(PLpgSQL_stmt_fetch));
+       fetch = palloc0(sizeof(PLpgSQL_stmt_select));
        fetch->cmd_type = PLPGSQL_STMT_FETCH;
        fetch->rec               = rec;
        fetch->row               = row;
@@ -2057,6 +2149,12 @@ check_assignable(PLpgSQL_datum *datum)
                                                                ((PLpgSQL_var *) datum)->refname)));
                        }
                        break;
+               case PLPGSQL_DTYPE_ROW:
+                       /* always assignable? */
+                       break;
+               case PLPGSQL_DTYPE_REC:
+                       /* always assignable?  What about NEW/OLD? */
+                       break;
                case PLPGSQL_DTYPE_RECFIELD:
                        /* always assignable? */
                        break;
@@ -2072,4 +2170,202 @@ check_assignable(PLpgSQL_datum *datum)
        }
 }
 
+/*
+ * Given the first datum and name in the INTO list, continue to read
+ * comma-separated scalar variables until we run out. Then construct
+ * and return a fake "row" variable that represents the list of
+ * scalars.
+ */
+static PLpgSQL_row *
+read_into_scalar_list(const char *initial_name,
+                                         PLpgSQL_datum *initial_datum)
+{
+       int                              nfields;
+       char                    *fieldnames[1024];
+       int                              varnos[1024];
+       PLpgSQL_row             *row;
+       int                              tok;
+
+       check_assignable(initial_datum);
+       fieldnames[0] = pstrdup(initial_name);
+       varnos[0]         = initial_datum->dno;
+       nfields           = 1;
+
+       while ((tok = yylex()) == ',')
+       {
+               /* Check for array overflow */
+               if (nfields >= 1024)
+               {
+                       plpgsql_error_lineno = plpgsql_scanner_lineno();
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                        errmsg("too many INTO variables specified")));
+               }
+
+               tok = yylex();
+               switch(tok)
+               {
+                       case T_SCALAR:
+                               check_assignable(yylval.scalar);
+                               fieldnames[nfields] = pstrdup(yytext);
+                               varnos[nfields++]       = yylval.scalar->dno;
+                               break;
+
+                       default:
+                               plpgsql_error_lineno = plpgsql_scanner_lineno();
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("\"%s\" is not a scalar variable",
+                                                               yytext)));
+               }
+       }
+
+       /*
+        * We read an extra, non-comma token from yylex(), so push it
+        * back onto the input stream
+        */
+       plpgsql_push_back_token(tok);
+
+       row = palloc(sizeof(PLpgSQL_row));
+       row->dtype = PLPGSQL_DTYPE_ROW;
+       row->refname = pstrdup("*internal*");
+       row->lineno = plpgsql_scanner_lineno();
+       row->rowtupdesc = NULL;
+       row->nfields = nfields;
+       row->fieldnames = palloc(sizeof(char *) * nfields);
+       row->varnos = palloc(sizeof(int) * nfields);
+       while (--nfields >= 0)
+       {
+               row->fieldnames[nfields] = fieldnames[nfields];
+               row->varnos[nfields] = varnos[nfields];
+       }
+
+       plpgsql_adddatum((PLpgSQL_datum *)row);
+
+       return row;
+}
+
+/*
+ * Convert a single scalar into a "row" list.  This is exactly
+ * like read_into_scalar_list except we never consume any input.
+ * In fact, since this can be invoked long after the source
+ * input was actually read, the lineno has to be passed in.
+ */
+static PLpgSQL_row *
+make_scalar_list1(const char *initial_name,
+                                 PLpgSQL_datum *initial_datum,
+                                 int lineno)
+{
+       PLpgSQL_row             *row;
+
+       check_assignable(initial_datum);
+
+       row = palloc(sizeof(PLpgSQL_row));
+       row->dtype = PLPGSQL_DTYPE_ROW;
+       row->refname = pstrdup("*internal*");
+       row->lineno = lineno;
+       row->rowtupdesc = NULL;
+       row->nfields = 1;
+       row->fieldnames = palloc(sizeof(char *));
+       row->varnos = palloc(sizeof(int));
+       row->fieldnames[0] = pstrdup(initial_name);
+       row->varnos[0] = initial_datum->dno;
+
+       plpgsql_adddatum((PLpgSQL_datum *)row);
+
+       return row;
+}
+
+/*
+ * When the PL/PgSQL parser expects to see a SQL statement, it is very
+ * liberal in what it accepts; for example, we often assume an
+ * unrecognized keyword is the beginning of a SQL statement. This
+ * avoids the need to duplicate parts of the SQL grammar in the
+ * PL/PgSQL grammar, but it means we can accept wildly malformed
+ * input. To try and catch some of the more obviously invalid input,
+ * we run the strings we expect to be SQL statements through the main
+ * SQL parser.
+ *
+ * We only invoke the raw parser (not the analyzer); this doesn't do
+ * any database access and does not check any semantic rules, it just
+ * checks for basic syntactic correctness. We do this here, rather
+ * than after parsing has finished, because a malformed SQL statement
+ * may cause the PL/PgSQL parser to become confused about statement
+ * borders. So it is best to bail out as early as we can.
+ */
+static void
+check_sql_expr(const char *stmt)
+{
+       ErrorContextCallback  syntax_errcontext;
+       ErrorContextCallback *previous_errcontext;
+       MemoryContext oldCxt;
+
+       if (!plpgsql_check_syntax)
+               return;
+
+       /*
+        * Setup error traceback support for ereport(). The previous
+        * ereport callback is installed by pl_comp.c, but we don't want
+        * that to be invoked (since it will try to transpose the syntax
+        * error to be relative to the CREATE FUNCTION), so temporarily
+        * remove it from the list of callbacks.
+        */
+       Assert(error_context_stack->callback == plpgsql_compile_error_callback);
+
+       previous_errcontext = error_context_stack;
+       syntax_errcontext.callback = plpgsql_sql_error_callback;
+       syntax_errcontext.arg = (char *) stmt;
+       syntax_errcontext.previous = error_context_stack->previous;
+       error_context_stack = &syntax_errcontext;
+
+       oldCxt = MemoryContextSwitchTo(compile_tmp_cxt);
+       (void) raw_parser(stmt);
+       MemoryContextSwitchTo(oldCxt);
+
+       /* Restore former ereport callback */
+       error_context_stack = previous_errcontext;
+}
+
+static void
+plpgsql_sql_error_callback(void *arg)
+{
+       char *sql_stmt = (char *) arg;
+
+       Assert(plpgsql_error_funcname);
+
+       errcontext("SQL statement in PL/PgSQL function \"%s\" near line %d",
+                          plpgsql_error_funcname, plpgsql_error_lineno);
+       internalerrquery(sql_stmt);
+       internalerrposition(geterrposition());
+       errposition(0);
+}
+
+static void
+check_labels(const char *start_label, const char *end_label)
+{
+       if (end_label)
+       {
+               if (!start_label)
+               {
+                       plpgsql_error_lineno = plpgsql_scanner_lineno();
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_SYNTAX_ERROR),
+                                        errmsg("end label \"%s\" specified for unlabelled block",
+                                                       end_label)));
+               }
+
+               if (strcmp(start_label, end_label) != 0)
+               {
+                       plpgsql_error_lineno = plpgsql_scanner_lineno();
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_SYNTAX_ERROR),
+                                        errmsg("end label \"%s\" differs from block's label \"%s\"",
+                                                       end_label, start_label)));
+               }
+       }
+}
+
+/* Needed to avoid conflict between different prefix settings: */
+#undef yylex
+
 #include "pl_scan.c"