Postgres: ON/OFF option values and PostgreSQL 16-17 EXPLAIN, VACUUM and ANALYZE options do not parse
Search before asking
- I searched the issues and found no similar issues.
What Happened
The postgres dialect reads a boolean option value with BooleanLiteralGrammar in EXPLAIN, ANALYZE, REINDEX, COPY, CREATE DATABASE, ALTER DATABASE and CREATE COLLATION. The two DATABASE statements use it for ALLOW_CONNECTIONS and IS_TEMPLATE. CREATE COLLATION uses it for DETERMINISTIC. That grammar is only TRUE or FALSE. PostgreSQL also accepts ON, OFF, 1 and 0 for these options. The EXPLAIN page lists all six values. In gram.y, utility_option_arg, copy_generic_opt_arg and createdb_opt_item read the value as opt_boolean_or_string or NumericOnly. CREATE COLLATION reads the value as a def_arg in gram.y, and DefineCollation converts it with defGetBoolean.
The grammar also has no rule for these options from PostgreSQL 16 and 17:
| statement | option | added in |
|---|---|---|
EXPLAIN |
GENERIC_PLAN |
16 |
EXPLAIN |
SERIALIZE, MEMORY |
17 |
VACUUM |
PROCESS_MAIN, SKIP_DATABASE_STATS, ONLY_DATABASE_STATS, BUFFER_USAGE_LIMIT |
16 |
ANALYZE |
BUFFER_USAGE_LIMIT |
16 |
The grammar is in src/sqlfluff/dialects/dialect_postgres.py:
ExplainOptionSegment(line 2341)CreateDatabaseStatementSegment(line 3684)AlterDatabaseStatementSegment(line 3757)VacuumStatementSegment(line 3940)ReindexStatementSegment(line 4825)AnalyzeStatementSegment(line 5168)CopyStatementSegment(line 6021)CreateCollationStatementSegment(line 6707)
Expected Behaviour
Each statement in the table below parses.
Observed Behaviour
Measured on main at 4d259e33f with sqlfluff lint --dialect postgres --rules LT01, one statement at a time:
| statement | result |
|---|---|
EXPLAIN (ANALYZE ON, BUFFERS OFF) SELECT 1; |
unparsable from position 1 |
EXPLAIN (GENERIC_PLAN) SELECT 1; |
unparsable from position 1 |
EXPLAIN (ANALYZE, SERIALIZE BINARY, MEMORY) SELECT 1; |
unparsable from position 1 |
VACUUM (SKIP_DATABASE_STATS); |
unparsable from position 8 |
VACUUM (PROCESS_MAIN FALSE, BUFFER_USAGE_LIMIT 256) t; |
unparsable from position 8 |
ANALYZE (BUFFER_USAGE_LIMIT 256) t; |
unparsable from position 9 |
ANALYZE (VERBOSE ON) t; |
unparsable from position 9 |
REINDEX (VERBOSE 1) TABLE t; |
unparsable from position 1 |
COPY t FROM STDIN WITH (FORMAT csv, HEADER ON); |
unparsable from position 24 |
CREATE DATABASE d WITH IS_TEMPLATE ON; |
unparsable from position 24 |
ALTER DATABASE d WITH ALLOW_CONNECTIONS OFF; |
unparsable from position 18 |
CREATE DATABASE d IS_TEMPLATE = 1 ALLOW_CONNECTIONS = off CONNECTION LIMIT 5; |
unparsable from position 19 |
ALTER DATABASE d IS_TEMPLATE ON CONNECTION LIMIT 5; |
unparsable from position 18 |
CREATE COLLATION c (provider = icu, locale = 'und', deterministic = off); |
unparsable from position 1 |
The output for the first statement includes this violation:
L: 1 | P: 1 | PRS | Line 1, Position 1: Found unparsable section: 'EXPLAIN
| (ANALYZE ON, BUFFERS OFF) SELECT...'EXPLAIN (ANALYZE TRUE, COSTS FALSE) SELECT 1; parses. CREATE DATABASE d WITH IS_TEMPLATE TRUE ALLOW_CONNECTIONS = false; parses. CREATE COLLATION c (provider = icu, locale = 'und', deterministic = false); parses. VACUUM (INDEX_CLEANUP off) t; also parses, because the VACUUM value grammar accepts an identifier.
How to reproduce
for q in \
'EXPLAIN (ANALYZE ON, BUFFERS OFF) SELECT 1;' \
'VACUUM (SKIP_DATABASE_STATS);' \
'ANALYZE (BUFFER_USAGE_LIMIT 256) t;' \
'CREATE DATABASE d WITH IS_TEMPLATE ON;' \
'ALTER DATABASE d WITH ALLOW_CONNECTIONS OFF;' \
"CREATE COLLATION c (provider = icu, locale = 'und', deterministic = off);"
do
printf '%s\n' "$q" | sqlfluff lint --dialect postgres --rules LT01 -
doneDialect
postgres
Version
sqlfluff 4.3.0 from main at 4d259e33f, Python 3.12.14.
Configuration
None. The command line gives the dialect.
Are you willing to work on and submit a PR to address the issue?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct
Source: sqlfluff/sqlfluff