From 6260cc550b0e60052168518a0338e440b67cf24e Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Thu, 10 Jan 2019 13:42:20 -0300 Subject: pgbench: add \cset and \gset commands These commands allow assignment of values produced by queries to pgbench variables, where they can be used by further commands. \gset terminates a command sequence (just like a bare semicolon); \cset separates multiple queries in a compound command, like an escaped semicolon (\;). A prefix can be provided to the \-command and is prepended to the name of each output column to produce the final variable name. This feature allows pgbench scripts to react meaningfully to the actual database contents, allowing more powerful benchmarks to be written. Authors: Fabien Coelho, Álvaro Herrera Reviewed-by: Amit Langote Reviewed-by: Stephen Frost Reviewed-by: Pavel Stehule Reviewed-by: Tom Lane Reviewed-by: Tatsuo Ishii Reviewed-by: Rafia Sabih Discussion: https://postgr.es/m/alpine.DEB.2.20.1607091005330.3412@sto --- src/fe_utils/psqlscan.l | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'src/fe_utils') diff --git a/src/fe_utils/psqlscan.l b/src/fe_utils/psqlscan.l index 9ea32c319f1..321744cddbb 100644 --- a/src/fe_utils/psqlscan.l +++ b/src/fe_utils/psqlscan.l @@ -693,8 +693,15 @@ other . * substitution. We want these before {self}, also. */ -"\\"[;:] { - /* Force a semicolon or colon into the query buffer */ +"\\"; { + /* Count semicolons in compound commands */ + cur_state->escaped_semicolons++; + /* Force a semicolon into the query buffer */ + psqlscan_emit(cur_state, yytext + 1, 1); + } + +"\\": { + /* Force a colon into the query buffer */ psqlscan_emit(cur_state, yytext + 1, 1); } @@ -1065,6 +1072,9 @@ psql_scan(PsqlScanState state, /* Set current output target */ state->output_buf = query_buf; + /* Reset number of escaped semicolons seen */ + state->escaped_semicolons = 0; + /* Set input source */ if (state->buffer_stack != NULL) yy_switch_to_buffer(state->buffer_stack->buf, state->scanner); @@ -1208,6 +1218,16 @@ psql_scan_reset(PsqlScanState state) state->dolqstart = NULL; } +/* + * Return the number of escaped semicolons in the lexed string seen by the + * previous psql_scan call. + */ +int +psql_scan_get_escaped_semicolons(PsqlScanState state) +{ + return state->escaped_semicolons; +} + /* * Reselect this lexer (psqlscan.l) after using another one. * -- cgit v1.2.3