Commit bbd5c207 authored by Peter Eisentraut's avatar Peter Eisentraut

PL/pgSQL: Add statement ID to statement structures

This can be used by a profiler as the index for an array of
per-statement metrics.

Author: Pavel Stehule <pavel.stehule@gmail.com>
Reviewed-by: default avatarPeter Eisentraut <peter.eisentraut@2ndquadrant.com>
Discussion: https://www.postgresql.org/message-id/flat/CAFj8pRDRCjN6rpM9ZccU7Ta_afsNX7mg9=n34F+r445Nt9v2tA@mail.gmail.com/
parent bf2fb2e0
...@@ -368,6 +368,8 @@ do_compile(FunctionCallInfo fcinfo, ...@@ -368,6 +368,8 @@ do_compile(FunctionCallInfo fcinfo,
function->fn_prokind = procStruct->prokind; function->fn_prokind = procStruct->prokind;
function->nstatements = 0;
/* /*
* Initialize the compiler, particularly the namespace stack. The * Initialize the compiler, particularly the namespace stack. The
* outermost namespace contains function parameters and other special * outermost namespace contains function parameters and other special
...@@ -880,6 +882,8 @@ plpgsql_compile_inline(char *proc_source) ...@@ -880,6 +882,8 @@ plpgsql_compile_inline(char *proc_source)
function->extra_warnings = 0; function->extra_warnings = 0;
function->extra_errors = 0; function->extra_errors = 0;
function->nstatements = 0;
plpgsql_ns_init(); plpgsql_ns_init();
plpgsql_ns_push(func_name, PLPGSQL_LABEL_BLOCK); plpgsql_ns_push(func_name, PLPGSQL_LABEL_BLOCK);
plpgsql_DumpExecTree = false; plpgsql_DumpExecTree = false;
...@@ -1020,6 +1024,7 @@ add_dummy_return(PLpgSQL_function *function) ...@@ -1020,6 +1024,7 @@ add_dummy_return(PLpgSQL_function *function)
new = palloc0(sizeof(PLpgSQL_stmt_block)); new = palloc0(sizeof(PLpgSQL_stmt_block));
new->cmd_type = PLPGSQL_STMT_BLOCK; new->cmd_type = PLPGSQL_STMT_BLOCK;
new->stmtid = ++function->nstatements;
new->body = list_make1(function->action); new->body = list_make1(function->action);
function->action = new; function->action = new;
...@@ -1031,6 +1036,7 @@ add_dummy_return(PLpgSQL_function *function) ...@@ -1031,6 +1036,7 @@ add_dummy_return(PLpgSQL_function *function)
new = palloc0(sizeof(PLpgSQL_stmt_return)); new = palloc0(sizeof(PLpgSQL_stmt_return));
new->cmd_type = PLPGSQL_STMT_RETURN; new->cmd_type = PLPGSQL_STMT_RETURN;
new->stmtid = ++function->nstatements;
new->expr = NULL; new->expr = NULL;
new->retvarno = function->out_param_varno; new->retvarno = function->out_param_varno;
......
This diff is collapsed.
...@@ -447,6 +447,13 @@ typedef struct PLpgSQL_stmt ...@@ -447,6 +447,13 @@ typedef struct PLpgSQL_stmt
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
/*
* Unique statement ID in this function (starting at 1; 0 is invalid/not
* set). This can be used by a profiler as the index for an array of
* per-statement metrics.
*/
unsigned int stmtid;
} PLpgSQL_stmt; } PLpgSQL_stmt;
/* /*
...@@ -486,6 +493,7 @@ typedef struct PLpgSQL_stmt_block ...@@ -486,6 +493,7 @@ typedef struct PLpgSQL_stmt_block
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
char *label; char *label;
List *body; /* List of statements */ List *body; /* List of statements */
int n_initvars; /* Length of initvarnos[] */ int n_initvars; /* Length of initvarnos[] */
...@@ -500,6 +508,7 @@ typedef struct PLpgSQL_stmt_assign ...@@ -500,6 +508,7 @@ typedef struct PLpgSQL_stmt_assign
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
int varno; int varno;
PLpgSQL_expr *expr; PLpgSQL_expr *expr;
} PLpgSQL_stmt_assign; } PLpgSQL_stmt_assign;
...@@ -511,6 +520,7 @@ typedef struct PLpgSQL_stmt_perform ...@@ -511,6 +520,7 @@ typedef struct PLpgSQL_stmt_perform
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
PLpgSQL_expr *expr; PLpgSQL_expr *expr;
} PLpgSQL_stmt_perform; } PLpgSQL_stmt_perform;
...@@ -521,6 +531,7 @@ typedef struct PLpgSQL_stmt_call ...@@ -521,6 +531,7 @@ typedef struct PLpgSQL_stmt_call
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
PLpgSQL_expr *expr; PLpgSQL_expr *expr;
bool is_call; bool is_call;
PLpgSQL_variable *target; PLpgSQL_variable *target;
...@@ -533,6 +544,7 @@ typedef struct PLpgSQL_stmt_commit ...@@ -533,6 +544,7 @@ typedef struct PLpgSQL_stmt_commit
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
} PLpgSQL_stmt_commit; } PLpgSQL_stmt_commit;
/* /*
...@@ -542,6 +554,7 @@ typedef struct PLpgSQL_stmt_rollback ...@@ -542,6 +554,7 @@ typedef struct PLpgSQL_stmt_rollback
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
} PLpgSQL_stmt_rollback; } PLpgSQL_stmt_rollback;
/* /*
...@@ -551,6 +564,7 @@ typedef struct PLpgSQL_stmt_set ...@@ -551,6 +564,7 @@ typedef struct PLpgSQL_stmt_set
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
PLpgSQL_expr *expr; PLpgSQL_expr *expr;
} PLpgSQL_stmt_set; } PLpgSQL_stmt_set;
...@@ -570,6 +584,7 @@ typedef struct PLpgSQL_stmt_getdiag ...@@ -570,6 +584,7 @@ typedef struct PLpgSQL_stmt_getdiag
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
bool is_stacked; /* STACKED or CURRENT diagnostics area? */ bool is_stacked; /* STACKED or CURRENT diagnostics area? */
List *diag_items; /* List of PLpgSQL_diag_item */ List *diag_items; /* List of PLpgSQL_diag_item */
} PLpgSQL_stmt_getdiag; } PLpgSQL_stmt_getdiag;
...@@ -581,6 +596,7 @@ typedef struct PLpgSQL_stmt_if ...@@ -581,6 +596,7 @@ typedef struct PLpgSQL_stmt_if
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
PLpgSQL_expr *cond; /* boolean expression for THEN */ PLpgSQL_expr *cond; /* boolean expression for THEN */
List *then_body; /* List of statements */ List *then_body; /* List of statements */
List *elsif_list; /* List of PLpgSQL_if_elsif structs */ List *elsif_list; /* List of PLpgSQL_if_elsif structs */
...@@ -604,6 +620,7 @@ typedef struct PLpgSQL_stmt_case ...@@ -604,6 +620,7 @@ typedef struct PLpgSQL_stmt_case
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
PLpgSQL_expr *t_expr; /* test expression, or NULL if none */ PLpgSQL_expr *t_expr; /* test expression, or NULL if none */
int t_varno; /* var to store test expression value into */ int t_varno; /* var to store test expression value into */
List *case_when_list; /* List of PLpgSQL_case_when structs */ List *case_when_list; /* List of PLpgSQL_case_when structs */
...@@ -628,6 +645,7 @@ typedef struct PLpgSQL_stmt_loop ...@@ -628,6 +645,7 @@ typedef struct PLpgSQL_stmt_loop
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
char *label; char *label;
List *body; /* List of statements */ List *body; /* List of statements */
} PLpgSQL_stmt_loop; } PLpgSQL_stmt_loop;
...@@ -639,6 +657,7 @@ typedef struct PLpgSQL_stmt_while ...@@ -639,6 +657,7 @@ typedef struct PLpgSQL_stmt_while
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
char *label; char *label;
PLpgSQL_expr *cond; PLpgSQL_expr *cond;
List *body; /* List of statements */ List *body; /* List of statements */
...@@ -651,6 +670,7 @@ typedef struct PLpgSQL_stmt_fori ...@@ -651,6 +670,7 @@ typedef struct PLpgSQL_stmt_fori
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
char *label; char *label;
PLpgSQL_var *var; PLpgSQL_var *var;
PLpgSQL_expr *lower; PLpgSQL_expr *lower;
...@@ -669,6 +689,7 @@ typedef struct PLpgSQL_stmt_forq ...@@ -669,6 +689,7 @@ typedef struct PLpgSQL_stmt_forq
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
char *label; char *label;
PLpgSQL_variable *var; /* Loop variable (record or row) */ PLpgSQL_variable *var; /* Loop variable (record or row) */
List *body; /* List of statements */ List *body; /* List of statements */
...@@ -681,6 +702,7 @@ typedef struct PLpgSQL_stmt_fors ...@@ -681,6 +702,7 @@ typedef struct PLpgSQL_stmt_fors
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
char *label; char *label;
PLpgSQL_variable *var; /* Loop variable (record or row) */ PLpgSQL_variable *var; /* Loop variable (record or row) */
List *body; /* List of statements */ List *body; /* List of statements */
...@@ -695,6 +717,7 @@ typedef struct PLpgSQL_stmt_forc ...@@ -695,6 +717,7 @@ typedef struct PLpgSQL_stmt_forc
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
char *label; char *label;
PLpgSQL_variable *var; /* Loop variable (record or row) */ PLpgSQL_variable *var; /* Loop variable (record or row) */
List *body; /* List of statements */ List *body; /* List of statements */
...@@ -710,6 +733,7 @@ typedef struct PLpgSQL_stmt_dynfors ...@@ -710,6 +733,7 @@ typedef struct PLpgSQL_stmt_dynfors
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
char *label; char *label;
PLpgSQL_variable *var; /* Loop variable (record or row) */ PLpgSQL_variable *var; /* Loop variable (record or row) */
List *body; /* List of statements */ List *body; /* List of statements */
...@@ -725,6 +749,7 @@ typedef struct PLpgSQL_stmt_foreach_a ...@@ -725,6 +749,7 @@ typedef struct PLpgSQL_stmt_foreach_a
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
char *label; char *label;
int varno; /* loop target variable */ int varno; /* loop target variable */
int slice; /* slice dimension, or 0 */ int slice; /* slice dimension, or 0 */
...@@ -739,6 +764,7 @@ typedef struct PLpgSQL_stmt_open ...@@ -739,6 +764,7 @@ typedef struct PLpgSQL_stmt_open
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
int curvar; int curvar;
int cursor_options; int cursor_options;
PLpgSQL_expr *argquery; PLpgSQL_expr *argquery;
...@@ -754,6 +780,7 @@ typedef struct PLpgSQL_stmt_fetch ...@@ -754,6 +780,7 @@ typedef struct PLpgSQL_stmt_fetch
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
PLpgSQL_variable *target; /* target (record or row) */ PLpgSQL_variable *target; /* target (record or row) */
int curvar; /* cursor variable to fetch from */ int curvar; /* cursor variable to fetch from */
FetchDirection direction; /* fetch direction */ FetchDirection direction; /* fetch direction */
...@@ -770,6 +797,7 @@ typedef struct PLpgSQL_stmt_close ...@@ -770,6 +797,7 @@ typedef struct PLpgSQL_stmt_close
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
int curvar; int curvar;
} PLpgSQL_stmt_close; } PLpgSQL_stmt_close;
...@@ -780,6 +808,7 @@ typedef struct PLpgSQL_stmt_exit ...@@ -780,6 +808,7 @@ typedef struct PLpgSQL_stmt_exit
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
bool is_exit; /* Is this an exit or a continue? */ bool is_exit; /* Is this an exit or a continue? */
char *label; /* NULL if it's an unlabelled EXIT/CONTINUE */ char *label; /* NULL if it's an unlabelled EXIT/CONTINUE */
PLpgSQL_expr *cond; PLpgSQL_expr *cond;
...@@ -792,6 +821,7 @@ typedef struct PLpgSQL_stmt_return ...@@ -792,6 +821,7 @@ typedef struct PLpgSQL_stmt_return
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
PLpgSQL_expr *expr; PLpgSQL_expr *expr;
int retvarno; int retvarno;
} PLpgSQL_stmt_return; } PLpgSQL_stmt_return;
...@@ -803,6 +833,7 @@ typedef struct PLpgSQL_stmt_return_next ...@@ -803,6 +833,7 @@ typedef struct PLpgSQL_stmt_return_next
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
PLpgSQL_expr *expr; PLpgSQL_expr *expr;
int retvarno; int retvarno;
} PLpgSQL_stmt_return_next; } PLpgSQL_stmt_return_next;
...@@ -814,6 +845,7 @@ typedef struct PLpgSQL_stmt_return_query ...@@ -814,6 +845,7 @@ typedef struct PLpgSQL_stmt_return_query
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
PLpgSQL_expr *query; /* if static query */ PLpgSQL_expr *query; /* if static query */
PLpgSQL_expr *dynquery; /* if dynamic query (RETURN QUERY EXECUTE) */ PLpgSQL_expr *dynquery; /* if dynamic query (RETURN QUERY EXECUTE) */
List *params; /* USING arguments for dynamic query */ List *params; /* USING arguments for dynamic query */
...@@ -826,6 +858,7 @@ typedef struct PLpgSQL_stmt_raise ...@@ -826,6 +858,7 @@ typedef struct PLpgSQL_stmt_raise
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
int elog_level; int elog_level;
char *condname; /* condition name, SQLSTATE, or NULL */ char *condname; /* condition name, SQLSTATE, or NULL */
char *message; /* old-style message format literal, or NULL */ char *message; /* old-style message format literal, or NULL */
...@@ -849,6 +882,7 @@ typedef struct PLpgSQL_stmt_assert ...@@ -849,6 +882,7 @@ typedef struct PLpgSQL_stmt_assert
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
PLpgSQL_expr *cond; PLpgSQL_expr *cond;
PLpgSQL_expr *message; PLpgSQL_expr *message;
} PLpgSQL_stmt_assert; } PLpgSQL_stmt_assert;
...@@ -860,6 +894,7 @@ typedef struct PLpgSQL_stmt_execsql ...@@ -860,6 +894,7 @@ typedef struct PLpgSQL_stmt_execsql
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
PLpgSQL_expr *sqlstmt; PLpgSQL_expr *sqlstmt;
bool mod_stmt; /* is the stmt INSERT/UPDATE/DELETE? Note: bool mod_stmt; /* is the stmt INSERT/UPDATE/DELETE? Note:
* mod_stmt is set when we plan the query */ * mod_stmt is set when we plan the query */
...@@ -875,6 +910,7 @@ typedef struct PLpgSQL_stmt_dynexecute ...@@ -875,6 +910,7 @@ typedef struct PLpgSQL_stmt_dynexecute
{ {
PLpgSQL_stmt_type cmd_type; PLpgSQL_stmt_type cmd_type;
int lineno; int lineno;
unsigned int stmtid;
PLpgSQL_expr *query; /* string expression */ PLpgSQL_expr *query; /* string expression */
bool into; /* INTO supplied? */ bool into; /* INTO supplied? */
bool strict; /* INTO STRICT flag */ bool strict; /* INTO STRICT flag */
...@@ -963,6 +999,9 @@ typedef struct PLpgSQL_function ...@@ -963,6 +999,9 @@ typedef struct PLpgSQL_function
int extra_warnings; int extra_warnings;
int extra_errors; int extra_errors;
/* count of statements inside function */
unsigned int nstatements;
/* the datums representing the function's local variables */ /* the datums representing the function's local variables */
int ndatums; int ndatums;
PLpgSQL_datum **datums; PLpgSQL_datum **datums;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment