Header And Logo

PostgreSQL
| The world's most advanced open source database.

plpgsql.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * plpgsql.h        - Definitions for the PL/pgSQL
00004  *            procedural language
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/pl/plpgsql/src/plpgsql.h
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 
00016 #ifndef PLPGSQL_H
00017 #define PLPGSQL_H
00018 
00019 #include "postgres.h"
00020 
00021 #include "access/xact.h"
00022 #include "commands/event_trigger.h"
00023 #include "commands/trigger.h"
00024 #include "executor/spi.h"
00025 
00026 /**********************************************************************
00027  * Definitions
00028  **********************************************************************/
00029 
00030 /* define our text domain for translations */
00031 #undef TEXTDOMAIN
00032 #define TEXTDOMAIN PG_TEXTDOMAIN("plpgsql")
00033 
00034 #undef _
00035 #define _(x) dgettext(TEXTDOMAIN, x)
00036 
00037 /* ----------
00038  * Compiler's namespace item types
00039  * ----------
00040  */
00041 enum
00042 {
00043     PLPGSQL_NSTYPE_LABEL,
00044     PLPGSQL_NSTYPE_VAR,
00045     PLPGSQL_NSTYPE_ROW,
00046     PLPGSQL_NSTYPE_REC
00047 };
00048 
00049 /* ----------
00050  * Datum array node types
00051  * ----------
00052  */
00053 enum
00054 {
00055     PLPGSQL_DTYPE_VAR,
00056     PLPGSQL_DTYPE_ROW,
00057     PLPGSQL_DTYPE_REC,
00058     PLPGSQL_DTYPE_RECFIELD,
00059     PLPGSQL_DTYPE_ARRAYELEM,
00060     PLPGSQL_DTYPE_EXPR
00061 };
00062 
00063 /* ----------
00064  * Variants distinguished in PLpgSQL_type structs
00065  * ----------
00066  */
00067 enum
00068 {
00069     PLPGSQL_TTYPE_SCALAR,       /* scalar types and domains */
00070     PLPGSQL_TTYPE_ROW,          /* composite types */
00071     PLPGSQL_TTYPE_REC,          /* RECORD pseudotype */
00072     PLPGSQL_TTYPE_PSEUDO        /* other pseudotypes */
00073 };
00074 
00075 /* ----------
00076  * Execution tree node types
00077  * ----------
00078  */
00079 enum PLpgSQL_stmt_types
00080 {
00081     PLPGSQL_STMT_BLOCK,
00082     PLPGSQL_STMT_ASSIGN,
00083     PLPGSQL_STMT_IF,
00084     PLPGSQL_STMT_CASE,
00085     PLPGSQL_STMT_LOOP,
00086     PLPGSQL_STMT_WHILE,
00087     PLPGSQL_STMT_FORI,
00088     PLPGSQL_STMT_FORS,
00089     PLPGSQL_STMT_FORC,
00090     PLPGSQL_STMT_FOREACH_A,
00091     PLPGSQL_STMT_EXIT,
00092     PLPGSQL_STMT_RETURN,
00093     PLPGSQL_STMT_RETURN_NEXT,
00094     PLPGSQL_STMT_RETURN_QUERY,
00095     PLPGSQL_STMT_RAISE,
00096     PLPGSQL_STMT_EXECSQL,
00097     PLPGSQL_STMT_DYNEXECUTE,
00098     PLPGSQL_STMT_DYNFORS,
00099     PLPGSQL_STMT_GETDIAG,
00100     PLPGSQL_STMT_OPEN,
00101     PLPGSQL_STMT_FETCH,
00102     PLPGSQL_STMT_CLOSE,
00103     PLPGSQL_STMT_PERFORM
00104 };
00105 
00106 
00107 /* ----------
00108  * Execution node return codes
00109  * ----------
00110  */
00111 enum
00112 {
00113     PLPGSQL_RC_OK,
00114     PLPGSQL_RC_EXIT,
00115     PLPGSQL_RC_RETURN,
00116     PLPGSQL_RC_CONTINUE
00117 };
00118 
00119 /* ----------
00120  * GET DIAGNOSTICS information items
00121  * ----------
00122  */
00123 enum
00124 {
00125     PLPGSQL_GETDIAG_ROW_COUNT,
00126     PLPGSQL_GETDIAG_RESULT_OID,
00127     PLPGSQL_GETDIAG_ERROR_CONTEXT,
00128     PLPGSQL_GETDIAG_ERROR_DETAIL,
00129     PLPGSQL_GETDIAG_ERROR_HINT,
00130     PLPGSQL_GETDIAG_RETURNED_SQLSTATE,
00131     PLPGSQL_GETDIAG_MESSAGE_TEXT
00132 };
00133 
00134 /* --------
00135  * RAISE statement options
00136  * --------
00137  */
00138 enum
00139 {
00140     PLPGSQL_RAISEOPTION_ERRCODE,
00141     PLPGSQL_RAISEOPTION_MESSAGE,
00142     PLPGSQL_RAISEOPTION_DETAIL,
00143     PLPGSQL_RAISEOPTION_HINT
00144 };
00145 
00146 /* --------
00147  * Behavioral modes for plpgsql variable resolution
00148  * --------
00149  */
00150 typedef enum
00151 {
00152     PLPGSQL_RESOLVE_ERROR,      /* throw error if ambiguous */
00153     PLPGSQL_RESOLVE_VARIABLE,   /* prefer plpgsql var to table column */
00154     PLPGSQL_RESOLVE_COLUMN      /* prefer table column to plpgsql var */
00155 } PLpgSQL_resolve_option;
00156 
00157 
00158 /**********************************************************************
00159  * Node and structure definitions
00160  **********************************************************************/
00161 
00162 
00163 typedef struct
00164 {                               /* Postgres data type */
00165     char       *typname;        /* (simple) name of the type */
00166     Oid         typoid;         /* OID of the data type */
00167     int         ttype;          /* PLPGSQL_TTYPE_ code */
00168     int16       typlen;         /* stuff copied from its pg_type entry */
00169     bool        typbyval;
00170     Oid         typrelid;
00171     Oid         typioparam;
00172     Oid         collation;      /* from pg_type, but can be overridden */
00173     FmgrInfo    typinput;       /* lookup info for typinput function */
00174     int32       atttypmod;      /* typmod (taken from someplace else) */
00175 } PLpgSQL_type;
00176 
00177 
00178 /*
00179  * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
00180  * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, and PLpgSQL_arrayelem
00181  */
00182 typedef struct
00183 {                               /* Generic datum array item     */
00184     int         dtype;
00185     int         dno;
00186 } PLpgSQL_datum;
00187 
00188 /*
00189  * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
00190  * fields
00191  */
00192 typedef struct
00193 {                               /* Scalar or composite variable */
00194     int         dtype;
00195     int         dno;
00196     char       *refname;
00197     int         lineno;
00198 } PLpgSQL_variable;
00199 
00200 typedef struct PLpgSQL_expr
00201 {                               /* SQL Query to plan and execute    */
00202     int         dtype;
00203     int         dno;
00204     char       *query;
00205     SPIPlanPtr  plan;
00206     Bitmapset  *paramnos;       /* all dnos referenced by this query */
00207 
00208     /* function containing this expr (not set until we first parse query) */
00209     struct PLpgSQL_function *func;
00210 
00211     /* namespace chain visible to this expr */
00212     struct PLpgSQL_nsitem *ns;
00213 
00214     /* fields for "simple expression" fast-path execution: */
00215     Expr       *expr_simple_expr;       /* NULL means not a simple expr */
00216     int         expr_simple_generation; /* plancache generation we checked */
00217     Oid         expr_simple_type;       /* result type Oid, if simple */
00218 
00219     /*
00220      * if expr is simple AND prepared in current transaction,
00221      * expr_simple_state and expr_simple_in_use are valid. Test validity by
00222      * seeing if expr_simple_lxid matches current LXID.  (If not,
00223      * expr_simple_state probably points at garbage!)
00224      */
00225     ExprState  *expr_simple_state;      /* eval tree for expr_simple_expr */
00226     bool        expr_simple_in_use;     /* true if eval tree is active */
00227     LocalTransactionId expr_simple_lxid;
00228 } PLpgSQL_expr;
00229 
00230 
00231 typedef struct
00232 {                               /* Scalar variable */
00233     int         dtype;
00234     int         dno;
00235     char       *refname;
00236     int         lineno;
00237 
00238     PLpgSQL_type *datatype;
00239     int         isconst;
00240     int         notnull;
00241     PLpgSQL_expr *default_val;
00242     PLpgSQL_expr *cursor_explicit_expr;
00243     int         cursor_explicit_argrow;
00244     int         cursor_options;
00245 
00246     Datum       value;
00247     bool        isnull;
00248     bool        freeval;
00249 } PLpgSQL_var;
00250 
00251 
00252 typedef struct
00253 {                               /* Row variable */
00254     int         dtype;
00255     int         dno;
00256     char       *refname;
00257     int         lineno;
00258 
00259     TupleDesc   rowtupdesc;
00260 
00261     /*
00262      * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
00263      *
00264      * Note: if the underlying rowtype contains a dropped column, the
00265      * corresponding fieldnames[] entry will be NULL, and there is no
00266      * corresponding var (varnos[] will be -1).
00267      */
00268     int         nfields;
00269     char      **fieldnames;
00270     int        *varnos;
00271 } PLpgSQL_row;
00272 
00273 
00274 typedef struct
00275 {                               /* Record variable (non-fixed structure) */
00276     int         dtype;
00277     int         dno;
00278     char       *refname;
00279     int         lineno;
00280 
00281     HeapTuple   tup;
00282     TupleDesc   tupdesc;
00283     bool        freetup;
00284     bool        freetupdesc;
00285 } PLpgSQL_rec;
00286 
00287 
00288 typedef struct
00289 {                               /* Field in record */
00290     int         dtype;
00291     int         dno;
00292     char       *fieldname;
00293     int         recparentno;    /* dno of parent record */
00294 } PLpgSQL_recfield;
00295 
00296 
00297 typedef struct
00298 {                               /* Element of array variable */
00299     int         dtype;
00300     int         dno;
00301     PLpgSQL_expr *subscript;
00302     int         arrayparentno;  /* dno of parent array variable */
00303     /* Remaining fields are cached info about the array variable's type */
00304     Oid         parenttypoid;   /* type of array variable; 0 if not yet set */
00305     int32       parenttypmod;   /* typmod of array variable */
00306     Oid         arraytypoid;    /* OID of actual array type */
00307     int32       arraytypmod;    /* typmod of array (and its elements too) */
00308     int16       arraytyplen;    /* typlen of array type */
00309     Oid         elemtypoid;     /* OID of array element type */
00310     int16       elemtyplen;     /* typlen of element type */
00311     bool        elemtypbyval;   /* element type is pass-by-value? */
00312     char        elemtypalign;   /* typalign of element type */
00313 } PLpgSQL_arrayelem;
00314 
00315 
00316 typedef struct PLpgSQL_nsitem
00317 {                               /* Item in the compilers namespace tree */
00318     int         itemtype;
00319     int         itemno;
00320     struct PLpgSQL_nsitem *prev;
00321     char        name[1];        /* actually, as long as needed */
00322 } PLpgSQL_nsitem;
00323 
00324 
00325 typedef struct
00326 {                               /* Generic execution node       */
00327     int         cmd_type;
00328     int         lineno;
00329 } PLpgSQL_stmt;
00330 
00331 
00332 typedef struct PLpgSQL_condition
00333 {                               /* One EXCEPTION condition name */
00334     int         sqlerrstate;    /* SQLSTATE code */
00335     char       *condname;       /* condition name (for debugging) */
00336     struct PLpgSQL_condition *next;
00337 } PLpgSQL_condition;
00338 
00339 typedef struct
00340 {
00341     int         sqlstate_varno;
00342     int         sqlerrm_varno;
00343     List       *exc_list;       /* List of WHEN clauses */
00344 } PLpgSQL_exception_block;
00345 
00346 typedef struct
00347 {                               /* One EXCEPTION ... WHEN clause */
00348     int         lineno;
00349     PLpgSQL_condition *conditions;
00350     List       *action;         /* List of statements */
00351 } PLpgSQL_exception;
00352 
00353 
00354 typedef struct
00355 {                               /* Block of statements          */
00356     int         cmd_type;
00357     int         lineno;
00358     char       *label;
00359     List       *body;           /* List of statements */
00360     int         n_initvars;
00361     int        *initvarnos;
00362     PLpgSQL_exception_block *exceptions;
00363 } PLpgSQL_stmt_block;
00364 
00365 
00366 typedef struct
00367 {                               /* Assign statement         */
00368     int         cmd_type;
00369     int         lineno;
00370     int         varno;
00371     PLpgSQL_expr *expr;
00372 } PLpgSQL_stmt_assign;
00373 
00374 typedef struct
00375 {                               /* PERFORM statement        */
00376     int         cmd_type;
00377     int         lineno;
00378     PLpgSQL_expr *expr;
00379 } PLpgSQL_stmt_perform;
00380 
00381 typedef struct
00382 {                               /* Get Diagnostics item     */
00383     int         kind;           /* id for diagnostic value desired */
00384     int         target;         /* where to assign it */
00385 } PLpgSQL_diag_item;
00386 
00387 typedef struct
00388 {                               /* Get Diagnostics statement        */
00389     int         cmd_type;
00390     int         lineno;
00391     bool        is_stacked;     /* STACKED or CURRENT diagnostics area? */
00392     List       *diag_items;     /* List of PLpgSQL_diag_item */
00393 } PLpgSQL_stmt_getdiag;
00394 
00395 
00396 typedef struct
00397 {                               /* IF statement             */
00398     int         cmd_type;
00399     int         lineno;
00400     PLpgSQL_expr *cond;         /* boolean expression for THEN */
00401     List       *then_body;      /* List of statements */
00402     List       *elsif_list;     /* List of PLpgSQL_if_elsif structs */
00403     List       *else_body;      /* List of statements */
00404 } PLpgSQL_stmt_if;
00405 
00406 typedef struct                  /* one ELSIF arm of IF statement */
00407 {
00408     int         lineno;
00409     PLpgSQL_expr *cond;         /* boolean expression for this case */
00410     List       *stmts;          /* List of statements */
00411 } PLpgSQL_if_elsif;
00412 
00413 
00414 typedef struct                  /* CASE statement */
00415 {
00416     int         cmd_type;
00417     int         lineno;
00418     PLpgSQL_expr *t_expr;       /* test expression, or NULL if none */
00419     int         t_varno;        /* var to store test expression value into */
00420     List       *case_when_list; /* List of PLpgSQL_case_when structs */
00421     bool        have_else;      /* flag needed because list could be empty */
00422     List       *else_stmts;     /* List of statements */
00423 } PLpgSQL_stmt_case;
00424 
00425 typedef struct                  /* one arm of CASE statement */
00426 {
00427     int         lineno;
00428     PLpgSQL_expr *expr;         /* boolean expression for this case */
00429     List       *stmts;          /* List of statements */
00430 } PLpgSQL_case_when;
00431 
00432 
00433 typedef struct
00434 {                               /* Unconditional LOOP statement     */
00435     int         cmd_type;
00436     int         lineno;
00437     char       *label;
00438     List       *body;           /* List of statements */
00439 } PLpgSQL_stmt_loop;
00440 
00441 
00442 typedef struct
00443 {                               /* WHILE cond LOOP statement        */
00444     int         cmd_type;
00445     int         lineno;
00446     char       *label;
00447     PLpgSQL_expr *cond;
00448     List       *body;           /* List of statements */
00449 } PLpgSQL_stmt_while;
00450 
00451 
00452 typedef struct
00453 {                               /* FOR statement with integer loopvar   */
00454     int         cmd_type;
00455     int         lineno;
00456     char       *label;
00457     PLpgSQL_var *var;
00458     PLpgSQL_expr *lower;
00459     PLpgSQL_expr *upper;
00460     PLpgSQL_expr *step;         /* NULL means default (ie, BY 1) */
00461     int         reverse;
00462     List       *body;           /* List of statements */
00463 } PLpgSQL_stmt_fori;
00464 
00465 
00466 /*
00467  * PLpgSQL_stmt_forq represents a FOR statement running over a SQL query.
00468  * It is the common supertype of PLpgSQL_stmt_fors, PLpgSQL_stmt_forc
00469  * and PLpgSQL_dynfors.
00470  */
00471 typedef struct
00472 {
00473     int         cmd_type;
00474     int         lineno;
00475     char       *label;
00476     PLpgSQL_rec *rec;
00477     PLpgSQL_row *row;
00478     List       *body;           /* List of statements */
00479 } PLpgSQL_stmt_forq;
00480 
00481 typedef struct
00482 {                               /* FOR statement running over SELECT    */
00483     int         cmd_type;
00484     int         lineno;
00485     char       *label;
00486     PLpgSQL_rec *rec;
00487     PLpgSQL_row *row;
00488     List       *body;           /* List of statements */
00489     /* end of fields that must match PLpgSQL_stmt_forq */
00490     PLpgSQL_expr *query;
00491 } PLpgSQL_stmt_fors;
00492 
00493 typedef struct
00494 {                               /* FOR statement running over cursor    */
00495     int         cmd_type;
00496     int         lineno;
00497     char       *label;
00498     PLpgSQL_rec *rec;
00499     PLpgSQL_row *row;
00500     List       *body;           /* List of statements */
00501     /* end of fields that must match PLpgSQL_stmt_forq */
00502     int         curvar;
00503     PLpgSQL_expr *argquery;     /* cursor arguments if any */
00504 } PLpgSQL_stmt_forc;
00505 
00506 typedef struct
00507 {                               /* FOR statement running over EXECUTE   */
00508     int         cmd_type;
00509     int         lineno;
00510     char       *label;
00511     PLpgSQL_rec *rec;
00512     PLpgSQL_row *row;
00513     List       *body;           /* List of statements */
00514     /* end of fields that must match PLpgSQL_stmt_forq */
00515     PLpgSQL_expr *query;
00516     List       *params;         /* USING expressions */
00517 } PLpgSQL_stmt_dynfors;
00518 
00519 
00520 typedef struct
00521 {                               /* FOREACH item in array loop */
00522     int         cmd_type;
00523     int         lineno;
00524     char       *label;
00525     int         varno;          /* loop target variable */
00526     int         slice;          /* slice dimension, or 0 */
00527     PLpgSQL_expr *expr;         /* array expression */
00528     List       *body;           /* List of statements */
00529 } PLpgSQL_stmt_foreach_a;
00530 
00531 
00532 typedef struct
00533 {                               /* OPEN a curvar                    */
00534     int         cmd_type;
00535     int         lineno;
00536     int         curvar;
00537     int         cursor_options;
00538     PLpgSQL_row *returntype;
00539     PLpgSQL_expr *argquery;
00540     PLpgSQL_expr *query;
00541     PLpgSQL_expr *dynquery;
00542     List       *params;         /* USING expressions */
00543 } PLpgSQL_stmt_open;
00544 
00545 
00546 typedef struct
00547 {                               /* FETCH or MOVE statement */
00548     int         cmd_type;
00549     int         lineno;
00550     PLpgSQL_rec *rec;           /* target, as record or row */
00551     PLpgSQL_row *row;
00552     int         curvar;         /* cursor variable to fetch from */
00553     FetchDirection direction;   /* fetch direction */
00554     long        how_many;       /* count, if constant (expr is NULL) */
00555     PLpgSQL_expr *expr;         /* count, if expression */
00556     bool        is_move;        /* is this a fetch or move? */
00557     bool        returns_multiple_rows;  /* can return more than one row? */
00558 } PLpgSQL_stmt_fetch;
00559 
00560 
00561 typedef struct
00562 {                               /* CLOSE curvar                     */
00563     int         cmd_type;
00564     int         lineno;
00565     int         curvar;
00566 } PLpgSQL_stmt_close;
00567 
00568 
00569 typedef struct
00570 {                               /* EXIT or CONTINUE statement           */
00571     int         cmd_type;
00572     int         lineno;
00573     bool        is_exit;        /* Is this an exit or a continue? */
00574     char       *label;          /* NULL if it's an unlabelled EXIT/CONTINUE */
00575     PLpgSQL_expr *cond;
00576 } PLpgSQL_stmt_exit;
00577 
00578 
00579 typedef struct
00580 {                               /* RETURN statement         */
00581     int         cmd_type;
00582     int         lineno;
00583     PLpgSQL_expr *expr;
00584     int         retvarno;
00585 } PLpgSQL_stmt_return;
00586 
00587 typedef struct
00588 {                               /* RETURN NEXT statement */
00589     int         cmd_type;
00590     int         lineno;
00591     PLpgSQL_expr *expr;
00592     int         retvarno;
00593 } PLpgSQL_stmt_return_next;
00594 
00595 typedef struct
00596 {                               /* RETURN QUERY statement */
00597     int         cmd_type;
00598     int         lineno;
00599     PLpgSQL_expr *query;        /* if static query */
00600     PLpgSQL_expr *dynquery;     /* if dynamic query (RETURN QUERY EXECUTE) */
00601     List       *params;         /* USING arguments for dynamic query */
00602 } PLpgSQL_stmt_return_query;
00603 
00604 typedef struct
00605 {                               /* RAISE statement          */
00606     int         cmd_type;
00607     int         lineno;
00608     int         elog_level;
00609     char       *condname;       /* condition name, SQLSTATE, or NULL */
00610     char       *message;        /* old-style message format literal, or NULL */
00611     List       *params;         /* list of expressions for old-style message */
00612     List       *options;        /* list of PLpgSQL_raise_option */
00613 } PLpgSQL_stmt_raise;
00614 
00615 typedef struct
00616 {                               /* RAISE statement option */
00617     int         opt_type;
00618     PLpgSQL_expr *expr;
00619 } PLpgSQL_raise_option;
00620 
00621 
00622 typedef struct
00623 {                               /* Generic SQL statement to execute */
00624     int         cmd_type;
00625     int         lineno;
00626     PLpgSQL_expr *sqlstmt;
00627     bool        mod_stmt;       /* is the stmt INSERT/UPDATE/DELETE? */
00628     /* note: mod_stmt is set when we plan the query */
00629     bool        into;           /* INTO supplied? */
00630     bool        strict;         /* INTO STRICT flag */
00631     PLpgSQL_rec *rec;           /* INTO target, if record */
00632     PLpgSQL_row *row;           /* INTO target, if row */
00633 } PLpgSQL_stmt_execsql;
00634 
00635 
00636 typedef struct
00637 {                               /* Dynamic SQL string to execute */
00638     int         cmd_type;
00639     int         lineno;
00640     PLpgSQL_expr *query;        /* string expression */
00641     bool        into;           /* INTO supplied? */
00642     bool        strict;         /* INTO STRICT flag */
00643     PLpgSQL_rec *rec;           /* INTO target, if record */
00644     PLpgSQL_row *row;           /* INTO target, if row */
00645     List       *params;         /* USING expressions */
00646 } PLpgSQL_stmt_dynexecute;
00647 
00648 
00649 typedef struct PLpgSQL_func_hashkey
00650 {                               /* Hash lookup key for functions */
00651     Oid         funcOid;
00652 
00653     bool        isTrigger;      /* true if called as a trigger */
00654 
00655     /* be careful that pad bytes in this struct get zeroed! */
00656 
00657     /*
00658      * For a trigger function, the OID of the relation triggered on is part of
00659      * the hash key --- we want to compile the trigger separately for each
00660      * relation it is used with, in case the rowtype is different.  Zero if
00661      * not called as a trigger.
00662      */
00663     Oid         trigrelOid;
00664 
00665     /*
00666      * We must include the input collation as part of the hash key too,
00667      * because we have to generate different plans (with different Param
00668      * collations) for different collation settings.
00669      */
00670     Oid         inputCollation;
00671 
00672     /*
00673      * We include actual argument types in the hash key to support polymorphic
00674      * PLpgSQL functions.  Be careful that extra positions are zeroed!
00675      */
00676     Oid         argtypes[FUNC_MAX_ARGS];
00677 } PLpgSQL_func_hashkey;
00678 
00679 typedef enum PLpgSQL_trigtype
00680 {
00681     PLPGSQL_DML_TRIGGER,
00682     PLPGSQL_EVENT_TRIGGER,
00683     PLPGSQL_NOT_TRIGGER
00684 } PLpgSQL_trigtype;
00685 
00686 typedef struct PLpgSQL_function
00687 {                               /* Complete compiled function     */
00688     char       *fn_signature;
00689     Oid         fn_oid;
00690     TransactionId fn_xmin;
00691     ItemPointerData fn_tid;
00692     PLpgSQL_trigtype fn_is_trigger;
00693     Oid         fn_input_collation;
00694     PLpgSQL_func_hashkey *fn_hashkey;   /* back-link to hashtable key */
00695     MemoryContext fn_cxt;
00696 
00697     Oid         fn_rettype;
00698     int         fn_rettyplen;
00699     bool        fn_retbyval;
00700     FmgrInfo    fn_retinput;
00701     Oid         fn_rettypioparam;
00702     bool        fn_retistuple;
00703     bool        fn_retset;
00704     bool        fn_readonly;
00705 
00706     int         fn_nargs;
00707     int         fn_argvarnos[FUNC_MAX_ARGS];
00708     int         out_param_varno;
00709     int         found_varno;
00710     int         new_varno;
00711     int         old_varno;
00712     int         tg_name_varno;
00713     int         tg_when_varno;
00714     int         tg_level_varno;
00715     int         tg_op_varno;
00716     int         tg_relid_varno;
00717     int         tg_relname_varno;
00718     int         tg_table_name_varno;
00719     int         tg_table_schema_varno;
00720     int         tg_nargs_varno;
00721     int         tg_argv_varno;
00722 
00723     /* for event triggers */
00724     int         tg_event_varno;
00725     int         tg_tag_varno;
00726 
00727     PLpgSQL_resolve_option resolve_option;
00728 
00729     int         ndatums;
00730     PLpgSQL_datum **datums;
00731     PLpgSQL_stmt_block *action;
00732 
00733     /* these fields change when the function is used */
00734     struct PLpgSQL_execstate *cur_estate;
00735     unsigned long use_count;
00736 } PLpgSQL_function;
00737 
00738 
00739 typedef struct PLpgSQL_execstate
00740 {                               /* Runtime execution data   */
00741     PLpgSQL_function *func;     /* function being executed */
00742 
00743     Datum       retval;
00744     bool        retisnull;
00745     Oid         rettype;        /* type of current retval */
00746 
00747     Oid         fn_rettype;     /* info about declared function rettype */
00748     bool        retistuple;
00749     bool        retisset;
00750 
00751     bool        readonly_func;
00752 
00753     TupleDesc   rettupdesc;
00754     char       *exitlabel;      /* the "target" label of the current EXIT or
00755                                  * CONTINUE stmt, if any */
00756     ErrorData  *cur_error;      /* current exception handler's error */
00757 
00758     Tuplestorestate *tuple_store;       /* SRFs accumulate results here */
00759     MemoryContext tuple_store_cxt;
00760     ResourceOwner tuple_store_owner;
00761     ReturnSetInfo *rsi;
00762 
00763     int         found_varno;
00764     int         ndatums;
00765     PLpgSQL_datum **datums;
00766 
00767     /* temporary state for results from evaluation of query or expr */
00768     SPITupleTable *eval_tuptable;
00769     uint32      eval_processed;
00770     Oid         eval_lastoid;
00771     ExprContext *eval_econtext; /* for executing simple expressions */
00772     PLpgSQL_expr *cur_expr;     /* current query/expr being evaluated */
00773 
00774     /* status information for error context reporting */
00775     PLpgSQL_stmt *err_stmt;     /* current stmt */
00776     const char *err_text;       /* additional state info */
00777 
00778     void       *plugin_info;    /* reserved for use by optional plugin */
00779 } PLpgSQL_execstate;
00780 
00781 
00782 /*
00783  * A PLpgSQL_plugin structure represents an instrumentation plugin.
00784  * To instrument PL/pgSQL, a plugin library must access the rendezvous
00785  * variable "PLpgSQL_plugin" and set it to point to a PLpgSQL_plugin struct.
00786  * Typically the struct could just be static data in the plugin library.
00787  * We expect that a plugin would do this at library load time (_PG_init()).
00788  * It must also be careful to set the rendezvous variable back to NULL
00789  * if it is unloaded (_PG_fini()).
00790  *
00791  * This structure is basically a collection of function pointers --- at
00792  * various interesting points in pl_exec.c, we call these functions
00793  * (if the pointers are non-NULL) to give the plugin a chance to watch
00794  * what we are doing.
00795  *
00796  *  func_setup is called when we start a function, before we've initialized
00797  *  the local variables defined by the function.
00798  *
00799  *  func_beg is called when we start a function, after we've initialized
00800  *  the local variables.
00801  *
00802  *  func_end is called at the end of a function.
00803  *
00804  *  stmt_beg and stmt_end are called before and after (respectively) each
00805  *  statement.
00806  *
00807  * Also, immediately before any call to func_setup, PL/pgSQL fills in the
00808  * error_callback and assign_expr fields with pointers to its own
00809  * plpgsql_exec_error_callback and exec_assign_expr functions.  This is
00810  * a somewhat ad-hoc expedient to simplify life for debugger plugins.
00811  */
00812 
00813 typedef struct
00814 {
00815     /* Function pointers set up by the plugin */
00816     void        (*func_setup) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
00817     void        (*func_beg) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
00818     void        (*func_end) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
00819     void        (*stmt_beg) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
00820     void        (*stmt_end) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
00821 
00822     /* Function pointers set by PL/pgSQL itself */
00823     void        (*error_callback) (void *arg);
00824     void        (*assign_expr) (PLpgSQL_execstate *estate, PLpgSQL_datum *target,
00825                                             PLpgSQL_expr *expr);
00826 } PLpgSQL_plugin;
00827 
00828 
00829 /* Struct types used during parsing */
00830 
00831 typedef struct
00832 {
00833     char       *ident;          /* palloc'd converted identifier */
00834     bool        quoted;         /* Was it double-quoted? */
00835 } PLword;
00836 
00837 typedef struct
00838 {
00839     List       *idents;         /* composite identifiers (list of String) */
00840 } PLcword;
00841 
00842 typedef struct
00843 {
00844     PLpgSQL_datum *datum;       /* referenced variable */
00845     char       *ident;          /* valid if simple name */
00846     bool        quoted;
00847     List       *idents;         /* valid if composite name */
00848 } PLwdatum;
00849 
00850 /**********************************************************************
00851  * Global variable declarations
00852  **********************************************************************/
00853 
00854 typedef enum
00855 {
00856     IDENTIFIER_LOOKUP_NORMAL,   /* normal processing of var names */
00857     IDENTIFIER_LOOKUP_DECLARE,  /* In DECLARE --- don't look up names */
00858     IDENTIFIER_LOOKUP_EXPR      /* In SQL expression --- special case */
00859 } IdentifierLookup;
00860 
00861 extern IdentifierLookup plpgsql_IdentifierLookup;
00862 
00863 extern int  plpgsql_variable_conflict;
00864 
00865 extern bool plpgsql_check_syntax;
00866 extern bool plpgsql_DumpExecTree;
00867 
00868 extern PLpgSQL_stmt_block *plpgsql_parse_result;
00869 
00870 extern int  plpgsql_nDatums;
00871 extern PLpgSQL_datum **plpgsql_Datums;
00872 
00873 extern char *plpgsql_error_funcname;
00874 
00875 extern PLpgSQL_function *plpgsql_curr_compile;
00876 extern MemoryContext compile_tmp_cxt;
00877 
00878 extern PLpgSQL_plugin **plugin_ptr;
00879 
00880 /**********************************************************************
00881  * Function declarations
00882  **********************************************************************/
00883 
00884 /* ----------
00885  * Functions in pl_comp.c
00886  * ----------
00887  */
00888 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
00889                 bool forValidator);
00890 extern PLpgSQL_function *plpgsql_compile_inline(char *proc_source);
00891 extern void plpgsql_parser_setup(struct ParseState *pstate,
00892                      PLpgSQL_expr *expr);
00893 extern bool plpgsql_parse_word(char *word1, const char *yytxt,
00894                    PLwdatum *wdatum, PLword *word);
00895 extern bool plpgsql_parse_dblword(char *word1, char *word2,
00896                       PLwdatum *wdatum, PLcword *cword);
00897 extern bool plpgsql_parse_tripword(char *word1, char *word2, char *word3,
00898                        PLwdatum *wdatum, PLcword *cword);
00899 extern PLpgSQL_type *plpgsql_parse_wordtype(char *ident);
00900 extern PLpgSQL_type *plpgsql_parse_cwordtype(List *idents);
00901 extern PLpgSQL_type *plpgsql_parse_wordrowtype(char *ident);
00902 extern PLpgSQL_type *plpgsql_parse_cwordrowtype(List *idents);
00903 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod,
00904                        Oid collation);
00905 extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
00906                        PLpgSQL_type *dtype,
00907                        bool add2namespace);
00908 extern PLpgSQL_rec *plpgsql_build_record(const char *refname, int lineno,
00909                      bool add2namespace);
00910 extern int plpgsql_recognize_err_condition(const char *condname,
00911                                 bool allow_sqlstate);
00912 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
00913 extern void plpgsql_adddatum(PLpgSQL_datum *new);
00914 extern int  plpgsql_add_initdatums(int **varnos);
00915 extern void plpgsql_HashTableInit(void);
00916 
00917 /* ----------
00918  * Functions in pl_handler.c
00919  * ----------
00920  */
00921 extern void _PG_init(void);
00922 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
00923 extern Datum plpgsql_inline_handler(PG_FUNCTION_ARGS);
00924 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
00925 
00926 /* ----------
00927  * Functions in pl_exec.c
00928  * ----------
00929  */
00930 extern Datum plpgsql_exec_function(PLpgSQL_function *func,
00931                       FunctionCallInfo fcinfo);
00932 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
00933                      TriggerData *trigdata);
00934 extern void plpgsql_exec_event_trigger(PLpgSQL_function *func,
00935                      EventTriggerData *trigdata);
00936 extern void plpgsql_xact_cb(XactEvent event, void *arg);
00937 extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
00938                    SubTransactionId parentSubid, void *arg);
00939 extern Oid exec_get_datum_type(PLpgSQL_execstate *estate,
00940                     PLpgSQL_datum *datum);
00941 extern void exec_get_datum_type_info(PLpgSQL_execstate *estate,
00942                          PLpgSQL_datum *datum,
00943                          Oid *typeid, int32 *typmod, Oid *collation);
00944 
00945 /* ----------
00946  * Functions for namespace handling in pl_funcs.c
00947  * ----------
00948  */
00949 extern void plpgsql_ns_init(void);
00950 extern void plpgsql_ns_push(const char *label);
00951 extern void plpgsql_ns_pop(void);
00952 extern PLpgSQL_nsitem *plpgsql_ns_top(void);
00953 extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
00954 extern PLpgSQL_nsitem *plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode,
00955                   const char *name1, const char *name2,
00956                   const char *name3, int *names_used);
00957 extern PLpgSQL_nsitem *plpgsql_ns_lookup_label(PLpgSQL_nsitem *ns_cur,
00958                         const char *name);
00959 
00960 /* ----------
00961  * Other functions in pl_funcs.c
00962  * ----------
00963  */
00964 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
00965 extern const char *plpgsql_getdiag_kindname(int kind);
00966 extern void plpgsql_free_function_memory(PLpgSQL_function *func);
00967 extern void plpgsql_dumptree(PLpgSQL_function *func);
00968 
00969 /* ----------
00970  * Scanner functions in pl_scanner.c
00971  * ----------
00972  */
00973 extern int  plpgsql_base_yylex(void);
00974 extern int  plpgsql_yylex(void);
00975 extern void plpgsql_push_back_token(int token);
00976 extern bool plpgsql_token_is_unreserved_keyword(int token);
00977 extern void plpgsql_append_source_text(StringInfo buf,
00978                            int startlocation, int endlocation);
00979 extern int  plpgsql_peek(void);
00980 extern void plpgsql_peek2(int *tok1_p, int *tok2_p, int *tok1_loc,
00981               int *tok2_loc);
00982 extern int  plpgsql_scanner_errposition(int location);
00983 extern void plpgsql_yyerror(const char *message);
00984 extern int  plpgsql_location_to_lineno(int location);
00985 extern int  plpgsql_latest_lineno(void);
00986 extern void plpgsql_scanner_init(const char *str);
00987 extern void plpgsql_scanner_finish(void);
00988 
00989 /* ----------
00990  * Externs in gram.y
00991  * ----------
00992  */
00993 extern int  plpgsql_yyparse(void);
00994 
00995 #endif   /* PLPGSQL_H */