Avoid unnecessary computation of pgbench's script line number.

ParseScript only needs the lineno for meta-commands, so let's not
bother computing it otherwise.  While this doesn't save much given
the previous patch, there's no point in doing unnecessary work.
While we're at it, avoid calling psql_scan_get_location() twice for
a meta-command.

One reason for making this change is that the line number computed
in ParseScript's main loop was actually wrong in most cases: it
would point just past the semicolon of the previous SQL command,
not at what the user thinks the current command's line number is.
We could add some code to skip whitespace before capturing the line
number, but it would be pretty pointless at present.  Just move the
call to avoid the temptation to rely on that value.  (Once we've
lexed the backslash, the computed line number will be right.)

This change also means that pgbench never inquires about the
location before it's lexed something, so that the care taken in
the previous patch to behave sanely in that case is unnecessary.
It seems best to keep that logic, though, as future callers
might depend on it.

Author: Daniel Vérité <daniel@manitou-mail.org>
Discussion: https://postgr.es/m/84a8a89e-adb8-47a9-9d34-c13f7150ee45@manitou-mail.org
This commit is contained in:
Tom Lane 2025-02-27 10:57:55 -05:00
parent c8c74ad7e1
commit 6eb8a1a4f9

View File

@ -5677,22 +5677,17 @@ postprocess_sql_command(Command *my_command)
* At call, we have scanned only the initial backslash.
*/
static Command *
process_backslash_command(PsqlScanState sstate, const char *source)
process_backslash_command(PsqlScanState sstate, const char *source,
int lineno, int start_offset)
{
Command *my_command;
PQExpBufferData word_buf;
int word_offset;
int offsets[MAX_ARGS]; /* offsets of argument words */
int start_offset;
int lineno;
int j;
initPQExpBuffer(&word_buf);
/* Remember location of the backslash */
psql_scan_get_location(sstate, &lineno, &start_offset);
start_offset--;
/* Collect first word of command */
if (!expr_lex_one_word(sstate, &word_buf, &word_offset))
{
@ -5980,16 +5975,12 @@ ParseScript(const char *script, const char *desc, int weight)
for (;;)
{
int lineno;
int start_offset;
PsqlScanResult sr;
promptStatus_t prompt;
Command *command = NULL;
resetPQExpBuffer(&line_buf);
psql_scan_get_location(sstate, &lineno, &start_offset);
sr = psql_scan(sstate, &line_buf, &prompt);
/* If we collected a new SQL command, process that */
@ -6002,7 +5993,15 @@ ParseScript(const char *script, const char *desc, int weight)
/* If we reached a backslash, process that */
if (sr == PSCAN_BACKSLASH)
{
command = process_backslash_command(sstate, desc);
int lineno;
int start_offset;
/* Capture location of the backslash */
psql_scan_get_location(sstate, &lineno, &start_offset);
start_offset--;
command = process_backslash_command(sstate, desc,
lineno, start_offset);
if (command)
{