Skip to content

Commit bbd5c20

Browse files
committed
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 <[email protected]> Reviewed-by: Peter Eisentraut <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/CAFj8pRDRCjN6rpM9ZccU7Ta_afsNX7mg9=n34F+r445Nt9v2tA@mail.gmail.com/
1 parent bf2fb2e commit bbd5c20

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/pl/plpgsql/src/pl_comp.c

+6
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ do_compile(FunctionCallInfo fcinfo,
368368

369369
function->fn_prokind = procStruct->prokind;
370370

371+
function->nstatements = 0;
372+
371373
/*
372374
* Initialize the compiler, particularly the namespace stack. The
373375
* outermost namespace contains function parameters and other special
@@ -880,6 +882,8 @@ plpgsql_compile_inline(char *proc_source)
880882
function->extra_warnings = 0;
881883
function->extra_errors = 0;
882884

885+
function->nstatements = 0;
886+
883887
plpgsql_ns_init();
884888
plpgsql_ns_push(func_name, PLPGSQL_LABEL_BLOCK);
885889
plpgsql_DumpExecTree = false;
@@ -1020,6 +1024,7 @@ add_dummy_return(PLpgSQL_function *function)
10201024

10211025
new = palloc0(sizeof(PLpgSQL_stmt_block));
10221026
new->cmd_type = PLPGSQL_STMT_BLOCK;
1027+
new->stmtid = ++function->nstatements;
10231028
new->body = list_make1(function->action);
10241029

10251030
function->action = new;
@@ -1031,6 +1036,7 @@ add_dummy_return(PLpgSQL_function *function)
10311036

10321037
new = palloc0(sizeof(PLpgSQL_stmt_return));
10331038
new->cmd_type = PLPGSQL_STMT_RETURN;
1039+
new->stmtid = ++function->nstatements;
10341040
new->expr = NULL;
10351041
new->retvarno = function->out_param_varno;
10361042

src/pl/plpgsql/src/pl_gram.y

+33
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ pl_block : decl_sect K_BEGIN proc_sect exception_sect K_END opt_label
414414

415415
new->cmd_type = PLPGSQL_STMT_BLOCK;
416416
new->lineno = plpgsql_location_to_lineno(@2);
417+
new->stmtid = ++plpgsql_curr_compile->nstatements;
417418
new->label = $1.label;
418419
new->n_initvars = $1.n_initvars;
419420
new->initvarnos = $1.initvarnos;
@@ -905,6 +906,7 @@ stmt_perform : K_PERFORM expr_until_semi
905906
new = palloc0(sizeof(PLpgSQL_stmt_perform));
906907
new->cmd_type = PLPGSQL_STMT_PERFORM;
907908
new->lineno = plpgsql_location_to_lineno(@1);
909+
new->stmtid = ++plpgsql_curr_compile->nstatements;
908910
new->expr = $2;
909911

910912
$$ = (PLpgSQL_stmt *)new;
@@ -918,6 +920,7 @@ stmt_call : K_CALL
918920
new = palloc0(sizeof(PLpgSQL_stmt_call));
919921
new->cmd_type = PLPGSQL_STMT_CALL;
920922
new->lineno = plpgsql_location_to_lineno(@1);
923+
new->stmtid = ++plpgsql_curr_compile->nstatements;
921924
new->expr = read_sql_stmt("CALL ");
922925
new->is_call = true;
923926

@@ -932,6 +935,7 @@ stmt_call : K_CALL
932935
new = palloc0(sizeof(PLpgSQL_stmt_call));
933936
new->cmd_type = PLPGSQL_STMT_CALL;
934937
new->lineno = plpgsql_location_to_lineno(@1);
938+
new->stmtid = ++plpgsql_curr_compile->nstatements;
935939
new->expr = read_sql_stmt("DO ");
936940
new->is_call = false;
937941

@@ -947,6 +951,7 @@ stmt_assign : assign_var assign_operator expr_until_semi
947951
new = palloc0(sizeof(PLpgSQL_stmt_assign));
948952
new->cmd_type = PLPGSQL_STMT_ASSIGN;
949953
new->lineno = plpgsql_location_to_lineno(@1);
954+
new->stmtid = ++plpgsql_curr_compile->nstatements;
950955
new->varno = $1->dno;
951956
new->expr = $3;
952957

@@ -962,6 +967,7 @@ stmt_getdiag : K_GET getdiag_area_opt K_DIAGNOSTICS getdiag_list ';'
962967
new = palloc0(sizeof(PLpgSQL_stmt_getdiag));
963968
new->cmd_type = PLPGSQL_STMT_GETDIAG;
964969
new->lineno = plpgsql_location_to_lineno(@1);
970+
new->stmtid = ++plpgsql_curr_compile->nstatements;
965971
new->is_stacked = $2;
966972
new->diag_items = $4;
967973

@@ -1149,6 +1155,7 @@ stmt_if : K_IF expr_until_then proc_sect stmt_elsifs stmt_else K_END K_IF ';'
11491155
new = palloc0(sizeof(PLpgSQL_stmt_if));
11501156
new->cmd_type = PLPGSQL_STMT_IF;
11511157
new->lineno = plpgsql_location_to_lineno(@1);
1158+
new->stmtid = ++plpgsql_curr_compile->nstatements;
11521159
new->cond = $2;
11531160
new->then_body = $3;
11541161
new->elsif_list = $4;
@@ -1253,6 +1260,7 @@ stmt_loop : opt_loop_label K_LOOP loop_body
12531260
new = palloc0(sizeof(PLpgSQL_stmt_loop));
12541261
new->cmd_type = PLPGSQL_STMT_LOOP;
12551262
new->lineno = plpgsql_location_to_lineno(@2);
1263+
new->stmtid = ++plpgsql_curr_compile->nstatements;
12561264
new->label = $1;
12571265
new->body = $3.stmts;
12581266

@@ -1270,6 +1278,7 @@ stmt_while : opt_loop_label K_WHILE expr_until_loop loop_body
12701278
new = palloc0(sizeof(PLpgSQL_stmt_while));
12711279
new->cmd_type = PLPGSQL_STMT_WHILE;
12721280
new->lineno = plpgsql_location_to_lineno(@2);
1281+
new->stmtid = ++plpgsql_curr_compile->nstatements;
12731282
new->label = $1;
12741283
new->cond = $3;
12751284
new->body = $4.stmts;
@@ -1290,6 +1299,7 @@ stmt_for : opt_loop_label K_FOR for_control loop_body
12901299

12911300
new = (PLpgSQL_stmt_fori *) $3;
12921301
new->lineno = plpgsql_location_to_lineno(@2);
1302+
new->stmtid = ++plpgsql_curr_compile->nstatements;
12931303
new->label = $1;
12941304
new->body = $4.stmts;
12951305
$$ = (PLpgSQL_stmt *) new;
@@ -1304,6 +1314,7 @@ stmt_for : opt_loop_label K_FOR for_control loop_body
13041314
/* forq is the common supertype of all three */
13051315
new = (PLpgSQL_stmt_forq *) $3;
13061316
new->lineno = plpgsql_location_to_lineno(@2);
1317+
new->stmtid = ++plpgsql_curr_compile->nstatements;
13071318
new->label = $1;
13081319
new->body = $4.stmts;
13091320
$$ = (PLpgSQL_stmt *) new;
@@ -1333,6 +1344,7 @@ for_control : for_variable K_IN
13331344

13341345
new = palloc0(sizeof(PLpgSQL_stmt_dynfors));
13351346
new->cmd_type = PLPGSQL_STMT_DYNFORS;
1347+
new->stmtid = ++plpgsql_curr_compile->nstatements;
13361348
if ($1.row)
13371349
{
13381350
new->var = (PLpgSQL_variable *) $1.row;
@@ -1378,6 +1390,7 @@ for_control : for_variable K_IN
13781390

13791391
new = (PLpgSQL_stmt_forc *) palloc0(sizeof(PLpgSQL_stmt_forc));
13801392
new->cmd_type = PLPGSQL_STMT_FORC;
1393+
new->stmtid = ++plpgsql_curr_compile->nstatements;
13811394
new->curvar = cursor->dno;
13821395

13831396
/* Should have had a single variable name */
@@ -1492,6 +1505,7 @@ for_control : for_variable K_IN
14921505

14931506
new = palloc0(sizeof(PLpgSQL_stmt_fori));
14941507
new->cmd_type = PLPGSQL_STMT_FORI;
1508+
new->stmtid = ++plpgsql_curr_compile->nstatements;
14951509
new->var = fvar;
14961510
new->reverse = reverse;
14971511
new->lower = expr1;
@@ -1526,6 +1540,7 @@ for_control : for_variable K_IN
15261540

15271541
new = palloc0(sizeof(PLpgSQL_stmt_fors));
15281542
new->cmd_type = PLPGSQL_STMT_FORS;
1543+
new->stmtid = ++plpgsql_curr_compile->nstatements;
15291544
if ($1.row)
15301545
{
15311546
new->var = (PLpgSQL_variable *) $1.row;
@@ -1626,6 +1641,7 @@ stmt_foreach_a : opt_loop_label K_FOREACH for_variable foreach_slice K_IN K_ARRA
16261641
new = palloc0(sizeof(PLpgSQL_stmt_foreach_a));
16271642
new->cmd_type = PLPGSQL_STMT_FOREACH_A;
16281643
new->lineno = plpgsql_location_to_lineno(@2);
1644+
new->stmtid = ++plpgsql_curr_compile->nstatements;
16291645
new->label = $1;
16301646
new->slice = $4;
16311647
new->expr = $7;
@@ -1672,6 +1688,7 @@ stmt_exit : exit_type opt_label opt_exitcond
16721688

16731689
new = palloc0(sizeof(PLpgSQL_stmt_exit));
16741690
new->cmd_type = PLPGSQL_STMT_EXIT;
1691+
new->stmtid = ++plpgsql_curr_compile->nstatements;
16751692
new->is_exit = $1;
16761693
new->lineno = plpgsql_location_to_lineno(@1);
16771694
new->label = $2;
@@ -1763,6 +1780,7 @@ stmt_raise : K_RAISE
17631780

17641781
new->cmd_type = PLPGSQL_STMT_RAISE;
17651782
new->lineno = plpgsql_location_to_lineno(@1);
1783+
new->stmtid = ++plpgsql_curr_compile->nstatements;
17661784
new->elog_level = ERROR; /* default */
17671785
new->condname = NULL;
17681786
new->message = NULL;
@@ -1907,6 +1925,7 @@ stmt_assert : K_ASSERT
19071925

19081926
new->cmd_type = PLPGSQL_STMT_ASSERT;
19091927
new->lineno = plpgsql_location_to_lineno(@1);
1928+
new->stmtid = ++plpgsql_curr_compile->nstatements;
19101929

19111930
new->cond = read_sql_expression2(',', ';',
19121931
", or ;",
@@ -1984,6 +2003,7 @@ stmt_dynexecute : K_EXECUTE
19842003
new = palloc(sizeof(PLpgSQL_stmt_dynexecute));
19852004
new->cmd_type = PLPGSQL_STMT_DYNEXECUTE;
19862005
new->lineno = plpgsql_location_to_lineno(@1);
2006+
new->stmtid = ++plpgsql_curr_compile->nstatements;
19872007
new->query = expr;
19882008
new->into = false;
19892009
new->strict = false;
@@ -2040,6 +2060,7 @@ stmt_open : K_OPEN cursor_variable
20402060
new = palloc0(sizeof(PLpgSQL_stmt_open));
20412061
new->cmd_type = PLPGSQL_STMT_OPEN;
20422062
new->lineno = plpgsql_location_to_lineno(@1);
2063+
new->stmtid = ++plpgsql_curr_compile->nstatements;
20432064
new->curvar = $2->dno;
20442065
new->cursor_options = CURSOR_OPT_FAST_PLAN;
20452066

@@ -2164,6 +2185,7 @@ stmt_close : K_CLOSE cursor_variable ';'
21642185
new = palloc(sizeof(PLpgSQL_stmt_close));
21652186
new->cmd_type = PLPGSQL_STMT_CLOSE;
21662187
new->lineno = plpgsql_location_to_lineno(@1);
2188+
new->stmtid = ++plpgsql_curr_compile->nstatements;
21672189
new->curvar = $2->dno;
21682190

21692191
$$ = (PLpgSQL_stmt *)new;
@@ -2184,6 +2206,7 @@ stmt_commit : K_COMMIT ';'
21842206
new = palloc(sizeof(PLpgSQL_stmt_commit));
21852207
new->cmd_type = PLPGSQL_STMT_COMMIT;
21862208
new->lineno = plpgsql_location_to_lineno(@1);
2209+
new->stmtid = ++plpgsql_curr_compile->nstatements;
21872210

21882211
$$ = (PLpgSQL_stmt *)new;
21892212
}
@@ -2196,6 +2219,7 @@ stmt_rollback : K_ROLLBACK ';'
21962219
new = palloc(sizeof(PLpgSQL_stmt_rollback));
21972220
new->cmd_type = PLPGSQL_STMT_ROLLBACK;
21982221
new->lineno = plpgsql_location_to_lineno(@1);
2222+
new->stmtid = ++plpgsql_curr_compile->nstatements;
21992223

22002224
$$ = (PLpgSQL_stmt *)new;
22012225
}
@@ -2208,6 +2232,8 @@ stmt_set : K_SET
22082232
new = palloc0(sizeof(PLpgSQL_stmt_set));
22092233
new->cmd_type = PLPGSQL_STMT_SET;
22102234
new->lineno = plpgsql_location_to_lineno(@1);
2235+
new->stmtid = ++plpgsql_curr_compile->nstatements;
2236+
22112237
new->expr = read_sql_stmt("SET ");
22122238

22132239
$$ = (PLpgSQL_stmt *)new;
@@ -2219,6 +2245,7 @@ stmt_set : K_SET
22192245
new = palloc0(sizeof(PLpgSQL_stmt_set));
22202246
new->cmd_type = PLPGSQL_STMT_SET;
22212247
new->lineno = plpgsql_location_to_lineno(@1);
2248+
new->stmtid = ++plpgsql_curr_compile->nstatements;
22222249
new->expr = read_sql_stmt("RESET ");
22232250

22242251
$$ = (PLpgSQL_stmt *)new;
@@ -3000,6 +3027,7 @@ make_execsql_stmt(int firsttoken, int location)
30003027
execsql = palloc(sizeof(PLpgSQL_stmt_execsql));
30013028
execsql->cmd_type = PLPGSQL_STMT_EXECSQL;
30023029
execsql->lineno = plpgsql_location_to_lineno(location);
3030+
execsql->stmtid = ++plpgsql_curr_compile->nstatements;
30033031
execsql->sqlstmt = expr;
30043032
execsql->into = have_into;
30053033
execsql->strict = have_strict;
@@ -3025,6 +3053,7 @@ read_fetch_direction(void)
30253053
*/
30263054
fetch = (PLpgSQL_stmt_fetch *) palloc0(sizeof(PLpgSQL_stmt_fetch));
30273055
fetch->cmd_type = PLPGSQL_STMT_FETCH;
3056+
fetch->stmtid = ++plpgsql_curr_compile->nstatements;
30283057
/* set direction defaults: */
30293058
fetch->direction = FETCH_FORWARD;
30303059
fetch->how_many = 1;
@@ -3177,6 +3206,7 @@ make_return_stmt(int location)
31773206
new = palloc0(sizeof(PLpgSQL_stmt_return));
31783207
new->cmd_type = PLPGSQL_STMT_RETURN;
31793208
new->lineno = plpgsql_location_to_lineno(location);
3209+
new->stmtid = ++plpgsql_curr_compile->nstatements;
31803210
new->expr = NULL;
31813211
new->retvarno = -1;
31823212

@@ -3264,6 +3294,7 @@ make_return_next_stmt(int location)
32643294
new = palloc0(sizeof(PLpgSQL_stmt_return_next));
32653295
new->cmd_type = PLPGSQL_STMT_RETURN_NEXT;
32663296
new->lineno = plpgsql_location_to_lineno(location);
3297+
new->stmtid = ++plpgsql_curr_compile->nstatements;
32673298
new->expr = NULL;
32683299
new->retvarno = -1;
32693300

@@ -3327,6 +3358,7 @@ make_return_query_stmt(int location)
33273358
new = palloc0(sizeof(PLpgSQL_stmt_return_query));
33283359
new->cmd_type = PLPGSQL_STMT_RETURN_QUERY;
33293360
new->lineno = plpgsql_location_to_lineno(location);
3361+
new->stmtid = ++plpgsql_curr_compile->nstatements;
33303362

33313363
/* check for RETURN QUERY EXECUTE */
33323364
if ((tok = yylex()) != K_EXECUTE)
@@ -3997,6 +4029,7 @@ make_case(int location, PLpgSQL_expr *t_expr,
39974029
new = palloc(sizeof(PLpgSQL_stmt_case));
39984030
new->cmd_type = PLPGSQL_STMT_CASE;
39994031
new->lineno = plpgsql_location_to_lineno(location);
4032+
new->stmtid = ++plpgsql_curr_compile->nstatements;
40004033
new->t_expr = t_expr;
40014034
new->t_varno = 0;
40024035
new->case_when_list = case_when_list;

0 commit comments

Comments
 (0)