[php-src] PR #24353: ext/pdo_pgsql: Speed up the SQL scanner
| From: | brainexe | Date: | Thu, 15 Oct 2026 14:06:56 +0000 |
| Subject: | [php-src] PR #24353: ext/pdo_pgsql: Speed up the SQL scanner | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-39470@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/24353
Author: brainexe
The pgsql placeholder scanner excluded
e/E from plain text because they
can start an E'...' string. As a result, every e/E
in a query (SELECT, WHERE, created_at, …) ended the current
text token and became a one-character token of its own.
This PR:
- allows e/E inside text runs as long as a run doesn't end with one,
so E'...' strings are still recognized exactly as before
- generates the scanner with re2c bitmaps (-b) instead of a switch on
every input character
The scanner runs in PDO::prepare() and, with emulated prepares, again in
PDOStatement::execute().
## Benchmark
Instructions (perf stat -e instructions:u, mean of 5 runs) for 200k
PDO::prepare() calls, compared to master:
| Query | Emulated prepares | Native prepares |
|---|---|---|
| SELECT with 11 columns, 2 named params | −24% | −16% |
| INSERT with 8 named params | −15% | −7% |
For a whole emulated query (prepare, execute, fetch one row) against a local PostgreSQL server, PHP
executes 7–9% fewer instructions.
## Verification
- New test ext/pdo_pgsql/tests/pdo_pgsql_parser_e_strings.phpt, run with both emulated
and native prepares. It covers E'...' strings directly after text,
ESCAPE'!', date'...', placeholders after
E'...' strings and words ending in e/E.
- Differential fuzzing against master: ~13k random queries built from SQL fragments (quotes,
E'...' strings, dollar quotes, comments, ??, named and
positional placeholders), each executed with 8 binding variants. The rewritten SQL and the errors
were identical.
- Built with scanners generated by re2c 4.4 and re2c 1.3, the configure minimum being 1.0.3.
- ext/pdo and ext/pdo_pgsql test suites pass with native prepares. With
emulated prepares forced on (PDO_PGSQL_TEST_ATTR), the same tests fail as on master.