plpgsql.h 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*-------------------------------------------------------------------------
  2. *
  3. * plpgsql.h - Definitions for the PL/pgSQL
  4. * procedural language
  5. *
  6. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. *
  10. * IDENTIFICATION
  11. * src/pl/plpgsql/src/plpgsql.h
  12. *
  13. *-------------------------------------------------------------------------
  14. */
  15. #ifndef PLPGSQL_H
  16. #define PLPGSQL_H
  17. #include "postgres.h"
  18. #include "access/xact.h"
  19. #include "commands/event_trigger.h"
  20. #include "commands/trigger.h"
  21. #include "executor/spi.h"
  22. /**********************************************************************
  23. * Definitions
  24. **********************************************************************/
  25. /* define our text domain for translations */
  26. #undef TEXTDOMAIN
  27. #define TEXTDOMAIN PG_TEXTDOMAIN("plpgsql")
  28. #undef _
  29. #define _(x) dgettext(TEXTDOMAIN, x)
  30. /* ----------
  31. * Compiler's namespace item types
  32. * ----------
  33. */
  34. enum
  35. {
  36. PLPGSQL_NSTYPE_LABEL,
  37. PLPGSQL_NSTYPE_VAR,
  38. PLPGSQL_NSTYPE_ROW,
  39. PLPGSQL_NSTYPE_REC
  40. };
  41. /* ----------
  42. * A PLPGSQL_NSTYPE_LABEL stack entry must be one of these types
  43. * ----------
  44. */
  45. enum PLpgSQL_label_types
  46. {
  47. PLPGSQL_LABEL_BLOCK, /* DECLARE/BEGIN block */
  48. PLPGSQL_LABEL_LOOP, /* looping construct */
  49. PLPGSQL_LABEL_OTHER /* anything else */
  50. };
  51. /* ----------
  52. * Datum array node types
  53. * ----------
  54. */
  55. enum
  56. {
  57. PLPGSQL_DTYPE_VAR,
  58. PLPGSQL_DTYPE_ROW,
  59. PLPGSQL_DTYPE_REC,
  60. PLPGSQL_DTYPE_RECFIELD,
  61. PLPGSQL_DTYPE_ARRAYELEM,
  62. PLPGSQL_DTYPE_EXPR
  63. };
  64. /* ----------
  65. * Variants distinguished in PLpgSQL_type structs
  66. * ----------
  67. */
  68. enum
  69. {
  70. PLPGSQL_TTYPE_SCALAR, /* scalar types and domains */
  71. PLPGSQL_TTYPE_ROW, /* composite types */
  72. PLPGSQL_TTYPE_REC, /* RECORD pseudotype */
  73. PLPGSQL_TTYPE_PSEUDO /* other pseudotypes */
  74. };
  75. /* ----------
  76. * Execution tree node types
  77. * ----------
  78. */
  79. enum PLpgSQL_stmt_types
  80. {
  81. PLPGSQL_STMT_BLOCK,
  82. PLPGSQL_STMT_ASSIGN,
  83. PLPGSQL_STMT_IF,
  84. PLPGSQL_STMT_CASE,
  85. PLPGSQL_STMT_LOOP,
  86. PLPGSQL_STMT_WHILE,
  87. PLPGSQL_STMT_FORI,
  88. PLPGSQL_STMT_FORS,
  89. PLPGSQL_STMT_FORC,
  90. PLPGSQL_STMT_FOREACH_A,
  91. PLPGSQL_STMT_EXIT,
  92. PLPGSQL_STMT_RETURN,
  93. PLPGSQL_STMT_RETURN_NEXT,
  94. PLPGSQL_STMT_RETURN_QUERY,
  95. PLPGSQL_STMT_RAISE,
  96. PLPGSQL_STMT_ASSERT,
  97. PLPGSQL_STMT_EXECSQL,
  98. PLPGSQL_STMT_DYNEXECUTE,
  99. PLPGSQL_STMT_DYNFORS,
  100. PLPGSQL_STMT_GETDIAG,
  101. PLPGSQL_STMT_OPEN,
  102. PLPGSQL_STMT_FETCH,
  103. PLPGSQL_STMT_CLOSE,
  104. PLPGSQL_STMT_PERFORM
  105. };
  106. /* ----------
  107. * Execution node return codes
  108. * ----------
  109. */
  110. enum
  111. {
  112. PLPGSQL_RC_OK,
  113. PLPGSQL_RC_EXIT,
  114. PLPGSQL_RC_RETURN,
  115. PLPGSQL_RC_CONTINUE
  116. };
  117. /* ----------
  118. * GET DIAGNOSTICS information items
  119. * ----------
  120. */
  121. enum
  122. {
  123. PLPGSQL_GETDIAG_ROW_COUNT,
  124. PLPGSQL_GETDIAG_RESULT_OID,
  125. PLPGSQL_GETDIAG_CONTEXT,
  126. PLPGSQL_GETDIAG_ERROR_CONTEXT,
  127. PLPGSQL_GETDIAG_ERROR_DETAIL,
  128. PLPGSQL_GETDIAG_ERROR_HINT,
  129. PLPGSQL_GETDIAG_RETURNED_SQLSTATE,
  130. PLPGSQL_GETDIAG_COLUMN_NAME,
  131. PLPGSQL_GETDIAG_CONSTRAINT_NAME,
  132. PLPGSQL_GETDIAG_DATATYPE_NAME,
  133. PLPGSQL_GETDIAG_MESSAGE_TEXT,
  134. PLPGSQL_GETDIAG_TABLE_NAME,
  135. PLPGSQL_GETDIAG_SCHEMA_NAME
  136. };
  137. /* --------
  138. * RAISE statement options
  139. * --------
  140. */
  141. enum
  142. {
  143. PLPGSQL_RAISEOPTION_ERRCODE,
  144. PLPGSQL_RAISEOPTION_MESSAGE,
  145. PLPGSQL_RAISEOPTION_DETAIL,
  146. PLPGSQL_RAISEOPTION_HINT,
  147. PLPGSQL_RAISEOPTION_COLUMN,
  148. PLPGSQL_RAISEOPTION_CONSTRAINT,
  149. PLPGSQL_RAISEOPTION_DATATYPE,
  150. PLPGSQL_RAISEOPTION_TABLE,
  151. PLPGSQL_RAISEOPTION_SCHEMA
  152. };
  153. /* --------
  154. * Behavioral modes for plpgsql variable resolution
  155. * --------
  156. */
  157. typedef enum
  158. {
  159. PLPGSQL_RESOLVE_ERROR, /* throw error if ambiguous */
  160. PLPGSQL_RESOLVE_VARIABLE, /* prefer plpgsql var to table column */
  161. PLPGSQL_RESOLVE_COLUMN /* prefer table column to plpgsql var */
  162. } PLpgSQL_resolve_option;
  163. /**********************************************************************
  164. * Node and structure definitions
  165. **********************************************************************/
  166. typedef struct
  167. { /* Postgres data type */
  168. char *typname; /* (simple) name of the type */
  169. Oid typoid; /* OID of the data type */
  170. int ttype; /* PLPGSQL_TTYPE_ code */
  171. int16 typlen; /* stuff copied from its pg_type entry */
  172. bool typbyval;
  173. char typtype;
  174. Oid typrelid;
  175. Oid collation; /* from pg_type, but can be overridden */
  176. bool typisarray; /* is "true" array, or domain over one */
  177. int32 atttypmod; /* typmod (taken from someplace else) */
  178. } PLpgSQL_type;
  179. /*
  180. * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
  181. * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, and PLpgSQL_arrayelem
  182. */
  183. typedef struct
  184. { /* Generic datum array item */
  185. int dtype;
  186. int dno;
  187. } PLpgSQL_datum;
  188. /*
  189. * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
  190. * fields
  191. */
  192. typedef struct
  193. { /* Scalar or composite variable */
  194. int dtype;
  195. int dno;
  196. char *refname;
  197. int lineno;
  198. } PLpgSQL_variable;
  199. typedef struct PLpgSQL_expr
  200. { /* SQL Query to plan and execute */
  201. int dtype;
  202. int dno;
  203. char *query;
  204. SPIPlanPtr plan;
  205. Bitmapset *paramnos; /* all dnos referenced by this query */
  206. int rwparam; /* dno of read/write param, or -1 if none */
  207. /* function containing this expr (not set until we first parse query) */
  208. struct PLpgSQL_function *func;
  209. /* namespace chain visible to this expr */
  210. struct PLpgSQL_nsitem *ns;
  211. /* fields for "simple expression" fast-path execution: */
  212. Expr *expr_simple_expr; /* NULL means not a simple expr */
  213. int expr_simple_generation; /* plancache generation we checked */
  214. Oid expr_simple_type; /* result type Oid, if simple */
  215. int32 expr_simple_typmod; /* result typmod, if simple */
  216. /*
  217. * if expr is simple AND prepared in current transaction,
  218. * expr_simple_state and expr_simple_in_use are valid. Test validity by
  219. * seeing if expr_simple_lxid matches current LXID. (If not,
  220. * expr_simple_state probably points at garbage!)
  221. */
  222. ExprState *expr_simple_state; /* eval tree for expr_simple_expr */
  223. bool expr_simple_in_use; /* true if eval tree is active */
  224. LocalTransactionId expr_simple_lxid;
  225. } PLpgSQL_expr;
  226. typedef struct
  227. { /* Scalar variable */
  228. int dtype;
  229. int dno;
  230. char *refname;
  231. int lineno;
  232. PLpgSQL_type *datatype;
  233. int isconst;
  234. int notnull;
  235. PLpgSQL_expr *default_val;
  236. PLpgSQL_expr *cursor_explicit_expr;
  237. int cursor_explicit_argrow;
  238. int cursor_options;
  239. Datum value;
  240. bool isnull;
  241. bool freeval;
  242. } PLpgSQL_var;
  243. typedef struct
  244. { /* Row variable */
  245. int dtype;
  246. int dno;
  247. char *refname;
  248. int lineno;
  249. TupleDesc rowtupdesc;
  250. /*
  251. * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
  252. *
  253. * Note: if the underlying rowtype contains a dropped column, the
  254. * corresponding fieldnames[] entry will be NULL, and there is no
  255. * corresponding var (varnos[] will be -1).
  256. */
  257. int nfields;
  258. char **fieldnames;
  259. int *varnos;
  260. } PLpgSQL_row;
  261. typedef struct
  262. { /* Record variable (non-fixed structure) */
  263. int dtype;
  264. int dno;
  265. char *refname;
  266. int lineno;
  267. HeapTuple tup;
  268. TupleDesc tupdesc;
  269. bool freetup;
  270. bool freetupdesc;
  271. } PLpgSQL_rec;
  272. typedef struct
  273. { /* Field in record */
  274. int dtype;
  275. int dno;
  276. char *fieldname;
  277. int recparentno; /* dno of parent record */
  278. } PLpgSQL_recfield;
  279. typedef struct
  280. { /* Element of array variable */
  281. int dtype;
  282. int dno;
  283. PLpgSQL_expr *subscript;
  284. int arrayparentno; /* dno of parent array variable */
  285. /* Remaining fields are cached info about the array variable's type */
  286. Oid parenttypoid; /* type of array variable; 0 if not yet set */
  287. int32 parenttypmod; /* typmod of array variable */
  288. Oid arraytypoid; /* OID of actual array type */
  289. int32 arraytypmod; /* typmod of array (and its elements too) */
  290. int16 arraytyplen; /* typlen of array type */
  291. Oid elemtypoid; /* OID of array element type */
  292. int16 elemtyplen; /* typlen of element type */
  293. bool elemtypbyval; /* element type is pass-by-value? */
  294. char elemtypalign; /* typalign of element type */
  295. } PLpgSQL_arrayelem;
  296. typedef struct PLpgSQL_nsitem
  297. { /* Item in the compilers namespace tree */
  298. int itemtype;
  299. int itemno;
  300. /* For labels, itemno is a value of enum PLpgSQL_label_types. */
  301. /* For other itemtypes, itemno is the associated PLpgSQL_datum's dno. */
  302. struct PLpgSQL_nsitem *prev;
  303. char name[FLEXIBLE_ARRAY_MEMBER]; /* nul-terminated string */
  304. } PLpgSQL_nsitem;
  305. typedef struct
  306. { /* Generic execution node */
  307. int cmd_type;
  308. int lineno;
  309. } PLpgSQL_stmt;
  310. typedef struct PLpgSQL_condition
  311. { /* One EXCEPTION condition name */
  312. int sqlerrstate; /* SQLSTATE code */
  313. char *condname; /* condition name (for debugging) */
  314. struct PLpgSQL_condition *next;
  315. } PLpgSQL_condition;
  316. typedef struct
  317. {
  318. int sqlstate_varno;
  319. int sqlerrm_varno;
  320. List *exc_list; /* List of WHEN clauses */
  321. } PLpgSQL_exception_block;
  322. typedef struct
  323. { /* One EXCEPTION ... WHEN clause */
  324. int lineno;
  325. PLpgSQL_condition *conditions;
  326. List *action; /* List of statements */
  327. } PLpgSQL_exception;
  328. typedef struct
  329. { /* Block of statements */
  330. int cmd_type;
  331. int lineno;
  332. char *label;
  333. List *body; /* List of statements */
  334. int n_initvars;
  335. int *initvarnos;
  336. PLpgSQL_exception_block *exceptions;
  337. } PLpgSQL_stmt_block;
  338. typedef struct
  339. { /* Assign statement */
  340. int cmd_type;
  341. int lineno;
  342. int varno;
  343. PLpgSQL_expr *expr;
  344. } PLpgSQL_stmt_assign;
  345. typedef struct
  346. { /* PERFORM statement */
  347. int cmd_type;
  348. int lineno;
  349. PLpgSQL_expr *expr;
  350. } PLpgSQL_stmt_perform;
  351. typedef struct
  352. { /* Get Diagnostics item */
  353. int kind; /* id for diagnostic value desired */
  354. int target; /* where to assign it */
  355. } PLpgSQL_diag_item;
  356. typedef struct
  357. { /* Get Diagnostics statement */
  358. int cmd_type;
  359. int lineno;
  360. bool is_stacked; /* STACKED or CURRENT diagnostics area? */
  361. List *diag_items; /* List of PLpgSQL_diag_item */
  362. } PLpgSQL_stmt_getdiag;
  363. typedef struct
  364. { /* IF statement */
  365. int cmd_type;
  366. int lineno;
  367. PLpgSQL_expr *cond; /* boolean expression for THEN */
  368. List *then_body; /* List of statements */
  369. List *elsif_list; /* List of PLpgSQL_if_elsif structs */
  370. List *else_body; /* List of statements */
  371. } PLpgSQL_stmt_if;
  372. typedef struct /* one ELSIF arm of IF statement */
  373. {
  374. int lineno;
  375. PLpgSQL_expr *cond; /* boolean expression for this case */
  376. List *stmts; /* List of statements */
  377. } PLpgSQL_if_elsif;
  378. typedef struct /* CASE statement */
  379. {
  380. int cmd_type;
  381. int lineno;
  382. PLpgSQL_expr *t_expr; /* test expression, or NULL if none */
  383. int t_varno; /* var to store test expression value into */
  384. List *case_when_list; /* List of PLpgSQL_case_when structs */
  385. bool have_else; /* flag needed because list could be empty */
  386. List *else_stmts; /* List of statements */
  387. } PLpgSQL_stmt_case;
  388. typedef struct /* one arm of CASE statement */
  389. {
  390. int lineno;
  391. PLpgSQL_expr *expr; /* boolean expression for this case */
  392. List *stmts; /* List of statements */
  393. } PLpgSQL_case_when;
  394. typedef struct
  395. { /* Unconditional LOOP statement */
  396. int cmd_type;
  397. int lineno;
  398. char *label;
  399. List *body; /* List of statements */
  400. } PLpgSQL_stmt_loop;
  401. typedef struct
  402. { /* WHILE cond LOOP statement */
  403. int cmd_type;
  404. int lineno;
  405. char *label;
  406. PLpgSQL_expr *cond;
  407. List *body; /* List of statements */
  408. } PLpgSQL_stmt_while;
  409. typedef struct
  410. { /* FOR statement with integer loopvar */
  411. int cmd_type;
  412. int lineno;
  413. char *label;
  414. PLpgSQL_var *var;
  415. PLpgSQL_expr *lower;
  416. PLpgSQL_expr *upper;
  417. PLpgSQL_expr *step; /* NULL means default (ie, BY 1) */
  418. int reverse;
  419. List *body; /* List of statements */
  420. } PLpgSQL_stmt_fori;
  421. /*
  422. * PLpgSQL_stmt_forq represents a FOR statement running over a SQL query.
  423. * It is the common supertype of PLpgSQL_stmt_fors, PLpgSQL_stmt_forc
  424. * and PLpgSQL_dynfors.
  425. */
  426. typedef struct
  427. {
  428. int cmd_type;
  429. int lineno;
  430. char *label;
  431. PLpgSQL_rec *rec;
  432. PLpgSQL_row *row;
  433. List *body; /* List of statements */
  434. } PLpgSQL_stmt_forq;
  435. typedef struct
  436. { /* FOR statement running over SELECT */
  437. int cmd_type;
  438. int lineno;
  439. char *label;
  440. PLpgSQL_rec *rec;
  441. PLpgSQL_row *row;
  442. List *body; /* List of statements */
  443. /* end of fields that must match PLpgSQL_stmt_forq */
  444. PLpgSQL_expr *query;
  445. } PLpgSQL_stmt_fors;
  446. typedef struct
  447. { /* FOR statement running over cursor */
  448. int cmd_type;
  449. int lineno;
  450. char *label;
  451. PLpgSQL_rec *rec;
  452. PLpgSQL_row *row;
  453. List *body; /* List of statements */
  454. /* end of fields that must match PLpgSQL_stmt_forq */
  455. int curvar;
  456. PLpgSQL_expr *argquery; /* cursor arguments if any */
  457. } PLpgSQL_stmt_forc;
  458. typedef struct
  459. { /* FOR statement running over EXECUTE */
  460. int cmd_type;
  461. int lineno;
  462. char *label;
  463. PLpgSQL_rec *rec;
  464. PLpgSQL_row *row;
  465. List *body; /* List of statements */
  466. /* end of fields that must match PLpgSQL_stmt_forq */
  467. PLpgSQL_expr *query;
  468. List *params; /* USING expressions */
  469. } PLpgSQL_stmt_dynfors;
  470. typedef struct
  471. { /* FOREACH item in array loop */
  472. int cmd_type;
  473. int lineno;
  474. char *label;
  475. int varno; /* loop target variable */
  476. int slice; /* slice dimension, or 0 */
  477. PLpgSQL_expr *expr; /* array expression */
  478. List *body; /* List of statements */
  479. } PLpgSQL_stmt_foreach_a;
  480. typedef struct
  481. { /* OPEN a curvar */
  482. int cmd_type;
  483. int lineno;
  484. int curvar;
  485. int cursor_options;
  486. PLpgSQL_row *returntype;
  487. PLpgSQL_expr *argquery;
  488. PLpgSQL_expr *query;
  489. PLpgSQL_expr *dynquery;
  490. List *params; /* USING expressions */
  491. } PLpgSQL_stmt_open;
  492. typedef struct
  493. { /* FETCH or MOVE statement */
  494. int cmd_type;
  495. int lineno;
  496. PLpgSQL_rec *rec; /* target, as record or row */
  497. PLpgSQL_row *row;
  498. int curvar; /* cursor variable to fetch from */
  499. FetchDirection direction; /* fetch direction */
  500. long how_many; /* count, if constant (expr is NULL) */
  501. PLpgSQL_expr *expr; /* count, if expression */
  502. bool is_move; /* is this a fetch or move? */
  503. bool returns_multiple_rows; /* can return more than one row? */
  504. } PLpgSQL_stmt_fetch;
  505. typedef struct
  506. { /* CLOSE curvar */
  507. int cmd_type;
  508. int lineno;
  509. int curvar;
  510. } PLpgSQL_stmt_close;
  511. typedef struct
  512. { /* EXIT or CONTINUE statement */
  513. int cmd_type;
  514. int lineno;
  515. bool is_exit; /* Is this an exit or a continue? */
  516. char *label; /* NULL if it's an unlabelled EXIT/CONTINUE */
  517. PLpgSQL_expr *cond;
  518. } PLpgSQL_stmt_exit;
  519. typedef struct
  520. { /* RETURN statement */
  521. int cmd_type;
  522. int lineno;
  523. PLpgSQL_expr *expr;
  524. int retvarno;
  525. } PLpgSQL_stmt_return;
  526. typedef struct
  527. { /* RETURN NEXT statement */
  528. int cmd_type;
  529. int lineno;
  530. PLpgSQL_expr *expr;
  531. int retvarno;
  532. } PLpgSQL_stmt_return_next;
  533. typedef struct
  534. { /* RETURN QUERY statement */
  535. int cmd_type;
  536. int lineno;
  537. PLpgSQL_expr *query; /* if static query */
  538. PLpgSQL_expr *dynquery; /* if dynamic query (RETURN QUERY EXECUTE) */
  539. List *params; /* USING arguments for dynamic query */
  540. } PLpgSQL_stmt_return_query;
  541. typedef struct
  542. { /* RAISE statement */
  543. int cmd_type;
  544. int lineno;
  545. int elog_level;
  546. char *condname; /* condition name, SQLSTATE, or NULL */
  547. char *message; /* old-style message format literal, or NULL */
  548. List *params; /* list of expressions for old-style message */
  549. List *options; /* list of PLpgSQL_raise_option */
  550. } PLpgSQL_stmt_raise;
  551. typedef struct
  552. { /* RAISE statement option */
  553. int opt_type;
  554. PLpgSQL_expr *expr;
  555. } PLpgSQL_raise_option;
  556. typedef struct
  557. { /* ASSERT statement */
  558. int cmd_type;
  559. int lineno;
  560. PLpgSQL_expr *cond;
  561. PLpgSQL_expr *message;
  562. } PLpgSQL_stmt_assert;
  563. typedef struct
  564. { /* Generic SQL statement to execute */
  565. int cmd_type;
  566. int lineno;
  567. PLpgSQL_expr *sqlstmt;
  568. bool mod_stmt; /* is the stmt INSERT/UPDATE/DELETE? */
  569. /* note: mod_stmt is set when we plan the query */
  570. bool into; /* INTO supplied? */
  571. bool strict; /* INTO STRICT flag */
  572. PLpgSQL_rec *rec; /* INTO target, if record */
  573. PLpgSQL_row *row; /* INTO target, if row */
  574. } PLpgSQL_stmt_execsql;
  575. typedef struct
  576. { /* Dynamic SQL string to execute */
  577. int cmd_type;
  578. int lineno;
  579. PLpgSQL_expr *query; /* string expression */
  580. bool into; /* INTO supplied? */
  581. bool strict; /* INTO STRICT flag */
  582. PLpgSQL_rec *rec; /* INTO target, if record */
  583. PLpgSQL_row *row; /* INTO target, if row */
  584. List *params; /* USING expressions */
  585. } PLpgSQL_stmt_dynexecute;
  586. typedef struct PLpgSQL_func_hashkey
  587. { /* Hash lookup key for functions */
  588. Oid funcOid;
  589. bool isTrigger; /* true if called as a trigger */
  590. /* be careful that pad bytes in this struct get zeroed! */
  591. /*
  592. * For a trigger function, the OID of the relation triggered on is part of
  593. * the hash key --- we want to compile the trigger separately for each
  594. * relation it is used with, in case the rowtype is different. Zero if
  595. * not called as a trigger.
  596. */
  597. Oid trigrelOid;
  598. /*
  599. * We must include the input collation as part of the hash key too,
  600. * because we have to generate different plans (with different Param
  601. * collations) for different collation settings.
  602. */
  603. Oid inputCollation;
  604. /*
  605. * We include actual argument types in the hash key to support polymorphic
  606. * PLpgSQL functions. Be careful that extra positions are zeroed!
  607. */
  608. Oid argtypes[FUNC_MAX_ARGS];
  609. } PLpgSQL_func_hashkey;
  610. typedef enum PLpgSQL_trigtype
  611. {
  612. PLPGSQL_DML_TRIGGER,
  613. PLPGSQL_EVENT_TRIGGER,
  614. PLPGSQL_NOT_TRIGGER
  615. } PLpgSQL_trigtype;
  616. typedef struct PLpgSQL_function
  617. { /* Complete compiled function */
  618. char *fn_signature;
  619. Oid fn_oid;
  620. TransactionId fn_xmin;
  621. ItemPointerData fn_tid;
  622. PLpgSQL_trigtype fn_is_trigger;
  623. Oid fn_input_collation;
  624. PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
  625. MemoryContext fn_cxt;
  626. Oid fn_rettype;
  627. int fn_rettyplen;
  628. bool fn_retbyval;
  629. bool fn_retistuple;
  630. bool fn_retset;
  631. bool fn_readonly;
  632. int fn_nargs;
  633. int fn_argvarnos[FUNC_MAX_ARGS];
  634. int out_param_varno;
  635. int found_varno;
  636. int new_varno;
  637. int old_varno;
  638. int tg_name_varno;
  639. int tg_when_varno;
  640. int tg_level_varno;
  641. int tg_op_varno;
  642. int tg_relid_varno;
  643. int tg_relname_varno;
  644. int tg_table_name_varno;
  645. int tg_table_schema_varno;
  646. int tg_nargs_varno;
  647. int tg_argv_varno;
  648. /* for event triggers */
  649. int tg_event_varno;
  650. int tg_tag_varno;
  651. PLpgSQL_resolve_option resolve_option;
  652. bool print_strict_params;
  653. /* extra checks */
  654. int extra_warnings;
  655. int extra_errors;
  656. /* the datums representing the function's local variables */
  657. int ndatums;
  658. PLpgSQL_datum **datums;
  659. Bitmapset *resettable_datums; /* dnos of non-simple vars */
  660. /* function body parsetree */
  661. PLpgSQL_stmt_block *action;
  662. /* these fields change when the function is used */
  663. struct PLpgSQL_execstate *cur_estate;
  664. unsigned long use_count;
  665. } PLpgSQL_function;
  666. typedef struct PLpgSQL_execstate
  667. { /* Runtime execution data */
  668. PLpgSQL_function *func; /* function being executed */
  669. Datum retval;
  670. bool retisnull;
  671. Oid rettype; /* type of current retval */
  672. Oid fn_rettype; /* info about declared function rettype */
  673. bool retistuple;
  674. bool retisset;
  675. bool readonly_func;
  676. TupleDesc rettupdesc;
  677. char *exitlabel; /* the "target" label of the current EXIT or
  678. * CONTINUE stmt, if any */
  679. ErrorData *cur_error; /* current exception handler's error */
  680. Tuplestorestate *tuple_store; /* SRFs accumulate results here */
  681. MemoryContext tuple_store_cxt;
  682. ResourceOwner tuple_store_owner;
  683. ReturnSetInfo *rsi;
  684. /* the datums representing the function's local variables */
  685. int found_varno;
  686. int ndatums;
  687. PLpgSQL_datum **datums;
  688. /* we pass datums[i] to the executor, when needed, in paramLI->params[i] */
  689. ParamListInfo paramLI;
  690. bool params_dirty; /* T if any resettable datum has been passed */
  691. /* EState to use for "simple" expression evaluation */
  692. EState *simple_eval_estate;
  693. /* Lookup table to use for executing type casts */
  694. HTAB *cast_hash;
  695. MemoryContext cast_hash_context;
  696. /* temporary state for results from evaluation of query or expr */
  697. SPITupleTable *eval_tuptable;
  698. uint64 eval_processed;
  699. Oid eval_lastoid;
  700. ExprContext *eval_econtext; /* for executing simple expressions */
  701. /* status information for error context reporting */
  702. PLpgSQL_stmt *err_stmt; /* current stmt */
  703. const char *err_text; /* additional state info */
  704. void *plugin_info; /* reserved for use by optional plugin */
  705. } PLpgSQL_execstate;
  706. /*
  707. * A PLpgSQL_plugin structure represents an instrumentation plugin.
  708. * To instrument PL/pgSQL, a plugin library must access the rendezvous
  709. * variable "PLpgSQL_plugin" and set it to point to a PLpgSQL_plugin struct.
  710. * Typically the struct could just be static data in the plugin library.
  711. * We expect that a plugin would do this at library load time (_PG_init()).
  712. * It must also be careful to set the rendezvous variable back to NULL
  713. * if it is unloaded (_PG_fini()).
  714. *
  715. * This structure is basically a collection of function pointers --- at
  716. * various interesting points in pl_exec.c, we call these functions
  717. * (if the pointers are non-NULL) to give the plugin a chance to watch
  718. * what we are doing.
  719. *
  720. * func_setup is called when we start a function, before we've initialized
  721. * the local variables defined by the function.
  722. *
  723. * func_beg is called when we start a function, after we've initialized
  724. * the local variables.
  725. *
  726. * func_end is called at the end of a function.
  727. *
  728. * stmt_beg and stmt_end are called before and after (respectively) each
  729. * statement.
  730. *
  731. * Also, immediately before any call to func_setup, PL/pgSQL fills in the
  732. * error_callback and assign_expr fields with pointers to its own
  733. * plpgsql_exec_error_callback and exec_assign_expr functions. This is
  734. * a somewhat ad-hoc expedient to simplify life for debugger plugins.
  735. */
  736. typedef struct
  737. {
  738. /* Function pointers set up by the plugin */
  739. void (*func_setup) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
  740. void (*func_beg) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
  741. void (*func_end) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
  742. void (*stmt_beg) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
  743. void (*stmt_end) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
  744. /* Function pointers set by PL/pgSQL itself */
  745. void (*error_callback) (void *arg);
  746. void (*assign_expr) (PLpgSQL_execstate *estate, PLpgSQL_datum *target,
  747. PLpgSQL_expr *expr);
  748. } PLpgSQL_plugin;
  749. /* Struct types used during parsing */
  750. typedef struct
  751. {
  752. char *ident; /* palloc'd converted identifier */
  753. bool quoted; /* Was it double-quoted? */
  754. } PLword;
  755. typedef struct
  756. {
  757. List *idents; /* composite identifiers (list of String) */
  758. } PLcword;
  759. typedef struct
  760. {
  761. PLpgSQL_datum *datum; /* referenced variable */
  762. char *ident; /* valid if simple name */
  763. bool quoted;
  764. List *idents; /* valid if composite name */
  765. } PLwdatum;
  766. /**********************************************************************
  767. * Global variable declarations
  768. **********************************************************************/
  769. typedef enum
  770. {
  771. IDENTIFIER_LOOKUP_NORMAL, /* normal processing of var names */
  772. IDENTIFIER_LOOKUP_DECLARE, /* In DECLARE --- don't look up names */
  773. IDENTIFIER_LOOKUP_EXPR /* In SQL expression --- special case */
  774. } IdentifierLookup;
  775. extern IdentifierLookup plpgsql_IdentifierLookup;
  776. extern int plpgsql_variable_conflict;
  777. extern bool plpgsql_print_strict_params;
  778. extern bool plpgsql_check_asserts;
  779. /* extra compile-time checks */
  780. #define PLPGSQL_XCHECK_NONE 0
  781. #define PLPGSQL_XCHECK_SHADOWVAR 1
  782. #define PLPGSQL_XCHECK_ALL ((int) ~0)
  783. extern int plpgsql_extra_warnings;
  784. extern int plpgsql_extra_errors;
  785. extern bool plpgsql_check_syntax;
  786. extern bool plpgsql_DumpExecTree;
  787. extern PLpgSQL_stmt_block *plpgsql_parse_result;
  788. extern int plpgsql_nDatums;
  789. extern PLpgSQL_datum **plpgsql_Datums;
  790. extern char *plpgsql_error_funcname;
  791. extern PLpgSQL_function *plpgsql_curr_compile;
  792. extern MemoryContext plpgsql_compile_tmp_cxt;
  793. extern PLpgSQL_plugin **plpgsql_plugin_ptr;
  794. /**********************************************************************
  795. * Function declarations
  796. **********************************************************************/
  797. /* ----------
  798. * Functions in pl_comp.c
  799. * ----------
  800. */
  801. extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
  802. bool forValidator);
  803. extern PLpgSQL_function *plpgsql_compile_inline(char *proc_source);
  804. extern void plpgsql_parser_setup(struct ParseState *pstate,
  805. PLpgSQL_expr *expr);
  806. extern bool plpgsql_parse_word(char *word1, const char *yytxt,
  807. PLwdatum *wdatum, PLword *word);
  808. extern bool plpgsql_parse_dblword(char *word1, char *word2,
  809. PLwdatum *wdatum, PLcword *cword);
  810. extern bool plpgsql_parse_tripword(char *word1, char *word2, char *word3,
  811. PLwdatum *wdatum, PLcword *cword);
  812. extern PLpgSQL_type *plpgsql_parse_wordtype(char *ident);
  813. extern PLpgSQL_type *plpgsql_parse_cwordtype(List *idents);
  814. extern PLpgSQL_type *plpgsql_parse_wordrowtype(char *ident);
  815. extern PLpgSQL_type *plpgsql_parse_cwordrowtype(List *idents);
  816. extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod,
  817. Oid collation);
  818. extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
  819. PLpgSQL_type *dtype,
  820. bool add2namespace);
  821. extern PLpgSQL_rec *plpgsql_build_record(const char *refname, int lineno,
  822. bool add2namespace);
  823. extern int plpgsql_recognize_err_condition(const char *condname,
  824. bool allow_sqlstate);
  825. extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
  826. extern void plpgsql_adddatum(PLpgSQL_datum *new);
  827. extern int plpgsql_add_initdatums(int **varnos);
  828. extern void plpgsql_HashTableInit(void);
  829. /* ----------
  830. * Functions in pl_handler.c
  831. * ----------
  832. */
  833. extern void _PG_init(void);
  834. /* ----------
  835. * Functions in pl_exec.c
  836. * ----------
  837. */
  838. extern Datum plpgsql_exec_function(PLpgSQL_function *func,
  839. FunctionCallInfo fcinfo,
  840. EState *simple_eval_estate);
  841. extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
  842. TriggerData *trigdata);
  843. extern void plpgsql_exec_event_trigger(PLpgSQL_function *func,
  844. EventTriggerData *trigdata);
  845. extern void plpgsql_xact_cb(XactEvent event, void *arg);
  846. extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
  847. SubTransactionId parentSubid, void *arg);
  848. extern Oid plpgsql_exec_get_datum_type(PLpgSQL_execstate *estate,
  849. PLpgSQL_datum *datum);
  850. extern void plpgsql_exec_get_datum_type_info(PLpgSQL_execstate *estate,
  851. PLpgSQL_datum *datum,
  852. Oid *typeid, int32 *typmod, Oid *collation);
  853. /* ----------
  854. * Functions for namespace handling in pl_funcs.c
  855. * ----------
  856. */
  857. extern void plpgsql_ns_init(void);
  858. extern void plpgsql_ns_push(const char *label,
  859. enum PLpgSQL_label_types label_type);
  860. extern void plpgsql_ns_pop(void);
  861. extern PLpgSQL_nsitem *plpgsql_ns_top(void);
  862. extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
  863. extern PLpgSQL_nsitem *plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode,
  864. const char *name1, const char *name2,
  865. const char *name3, int *names_used);
  866. extern PLpgSQL_nsitem *plpgsql_ns_lookup_label(PLpgSQL_nsitem *ns_cur,
  867. const char *name);
  868. extern PLpgSQL_nsitem *plpgsql_ns_find_nearest_loop(PLpgSQL_nsitem *ns_cur);
  869. /* ----------
  870. * Other functions in pl_funcs.c
  871. * ----------
  872. */
  873. extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
  874. extern const char *plpgsql_getdiag_kindname(int kind);
  875. extern void plpgsql_free_function_memory(PLpgSQL_function *func);
  876. extern void plpgsql_dumptree(PLpgSQL_function *func);
  877. /* ----------
  878. * Scanner functions in pl_scanner.c
  879. * ----------
  880. */
  881. extern int plpgsql_base_yylex(void);
  882. extern int plpgsql_yylex(void);
  883. extern void plpgsql_push_back_token(int token);
  884. extern bool plpgsql_token_is_unreserved_keyword(int token);
  885. extern void plpgsql_append_source_text(StringInfo buf,
  886. int startlocation, int endlocation);
  887. extern int plpgsql_peek(void);
  888. extern void plpgsql_peek2(int *tok1_p, int *tok2_p, int *tok1_loc,
  889. int *tok2_loc);
  890. extern int plpgsql_scanner_errposition(int location);
  891. extern void plpgsql_yyerror(const char *message) pg_attribute_noreturn();
  892. extern int plpgsql_location_to_lineno(int location);
  893. extern int plpgsql_latest_lineno(void);
  894. extern void plpgsql_scanner_init(const char *str);
  895. extern void plpgsql_scanner_finish(void);
  896. /* ----------
  897. * Externs in gram.y
  898. * ----------
  899. */
  900. extern int plpgsql_yyparse(void);
  901. #endif /* PLPGSQL_H */