diff options
author | Tom Lane | 2025-02-27 15:53:38 +0000 |
---|---|---|
committer | Tom Lane | 2025-02-27 15:53:38 +0000 |
commit | c8c74ad7e1cbc71b616f8ae786776c521729976b (patch) | |
tree | a6dbcc5abaa8aa43df77f923a12b5b1cc34463f8 /src/include/fe_utils/psqlscan.h | |
parent | e167191dc146b65146fbd32e147be30dd8f1f166 (diff) |
Get rid of O(N^2) script-parsing overhead in pgbench.
pgbench wants to record the starting line number of each command
in its scripts. It was computing that by scanning from the script
start and counting newlines, so that O(N^2) work had to be done
for an N-command script. In a script with 50K lines, this adds
up to about 10 seconds on my machine.
To add insult to injury, the results were subtly wrong, because
expr_scanner_offset() scanned to find the NUL that flex inserts
at the end of the current token --- and before the first yylex
call, no such NUL has been inserted. So we ended by computing the
script's last line number not its first one. This was visible only
in case of \gset at the start of a script, which perhaps accounts
for the lack of complaints.
To fix, steal an idea from plpgsql and track the current lexer
ending position and line count as we advance through the script.
(It's a bit simpler than plpgsql since we can't need to back up.)
Also adjust a couple of other places that were invoking scans
from script start when they didn't really need to. I made a new
psqlscan function psql_scan_get_location() that replaces both
expr_scanner_offset() and expr_scanner_get_lineno(), since in
practice expr_scanner_get_lineno() was only being invoked to find
the line number of the current lexer end position.
Reported-by: Daniel Vérité <[email protected]>
Author: Tom Lane <[email protected]>
Discussion: https://postgr.es/m/[email protected]
Diffstat (limited to 'src/include/fe_utils/psqlscan.h')
-rw-r--r-- | src/include/fe_utils/psqlscan.h | 3 |
1 files changed, 3 insertions, 0 deletions
diff --git a/src/include/fe_utils/psqlscan.h b/src/include/fe_utils/psqlscan.h index 81f792b1733..39d2065fe98 100644 --- a/src/include/fe_utils/psqlscan.h +++ b/src/include/fe_utils/psqlscan.h @@ -87,4 +87,7 @@ extern void psql_scan_reselect_sql_lexer(PsqlScanState state); extern bool psql_scan_in_quote(PsqlScanState state); +extern void psql_scan_get_location(PsqlScanState state, + int *lineno, int *offset); + #endif /* PSQLSCAN_H */ |