Repository navigation
Refuse an unparsed VACUUM after empty statements, keep an NXDOMAIN final, and move to sqlparser 0.64 - #29
Conversation
Postgres drops empty statements, so `;VACUUM (FULL)` runs as a single VACUUM of every table. first_word stopped at the `;` and returned "", so rejects_unparsed forwarded it while a table was gated. Skip `;` along with whitespace and comments. Co-authored-by: Perplexity <noreply@perplexity.ai>
artem-pplx
left a comment
There was a problem hiding this comment.
Raphael review of 5f2391f: CLEAR. This was 2 passes, Opus High and then a fresh Astra High, both on this head. D1 from #28 is fixed.
Checked:
- Every iteration of
first_wordnow drops;together with whitespace (Rust'sis_whitespacecovers Postgres's[ \t\n\r\f\v]), nested block comments and CR/LF line comments. - No prefix that Postgres accepts still bypasses the check.
- These return
vacuum:/* a */;/* b */VACUUM,-- a\n;-- b\n;VACUUM,;\f;\vVACUUM, and;/**/;/* x /* y */ z */;\nVACUUM(FULL). ;select 1; vacuumreturnsselect. That's correct, because two non-empty statements run in an implicit transaction block, where VACUUM errors.
- These return
- There is no over-refusal of valid SQL and no new panic path (
;is ASCII).
Evidence:
- The focused test passes. With
:82reverted it fails at:3713on;VACUUM (FULL). cargo fmt --checkis clean.- The 383-test lib receipt is reused from the identical base tree.
Description: accurate, and it correctly replaces #28's "only the first word…" sentence.
Nonblocking: one optional test addition, inline.
Created with Perplexity
Add `/* a */;/* b */VACUUM (FULL)` and a form feed and vertical tab prefix to the unparsed VACUUM test, so the order in which first_word skips comments, `;` and whitespace stays covered. Co-authored-by: Perplexity <noreply@perplexity.ai>
hickory-resolver 0.24 built each system nameserver with trust_negative_responses off, so an NXDOMAIN from one server made it ask the others. 0.26 turns it on, so one server that doesn't know the database host makes the lookup fail. Read the system configuration and turn it off for every nameserver before building the resolver. Co-authored-by: Perplexity <noreply@perplexity.ai>
artem-pplx
left a comment
There was a problem hiding this comment.
Review of c6e9a37: CLEAR. Two passes: Opus High, then a fresh Astra High, both on this head.
- A1, the NXDOMAIN fallback, is fixed.
resolver_withmakes the same callsbuilder_tokio()made in hickory-resolver 0.26.2. In order:read_system_conf, thenbuilder_with_configwith the default Tokio provider, then the options are replaced. The only addition istrust_negative_responses = falseon every nameserver.- Both new DNS tests pass. The fallback test fails when the trust loop is removed.
- D1 test additions: both new VACUUM prefixes pass, and each one fails when the fix is reverted.
- Focused suite: 9/9 pass at this head. The author's fmt, clippy, 385 lib + 4 doc test results are reused, since nothing else changed.
- Title and description: accurate, with the restoration explicitly scoped to NXDOMAIN.
Nonblocking:
- One test-strength nit, inline.
- Two follow-ups in hickory 0.26 that this PR doesn't change, in the separate comment below.
Created with Perplexity
|
Follow-ups beyond this PR (hickory-resolver 0.26 behavior, nonblocking, from the
Created with Perplexity |
The test ordered the two answers with a 100 ms delay, so a scheduling stall could let the A answer arrive first and the test pass without the fix. Ask one nameserver at a time, in the given order, drop the delay, and check that each nameserver got one query. Co-authored-by: Perplexity <noreply@perplexity.ai>
|
Both items are out of scope for #29, so there are no code changes for them here. This PR restores only the pre-0.26 NXDOMAIN fallback. Both items are hickory-resolver 0.26 behavior, and they're deferred upstream, to be tracked as follow-ups:
Created with Perplexity |
artem-pplx
left a comment
There was a problem hiding this comment.
Review of 392b6ea: CLEAR. Two passes: Opus High, then a fresh Astra High, both on this head.
- Test-only: the code outside
#[cfg(test)]indns_cache.rsis byte-identical toc6e9a37. - The P4 nit is fixed. The fallback test asks the servers one at a time in the given order, and the mock has no delay or sleep. The counters increment before the reply is sent, so the count assertions are race-free.
- Evidence: the DNS tests pass 8/8 at this head. With the trust loop removed, the unchanged fallback test fails with NXDOMAIN.
- Follow-ups: the two ledger items are deferred as upstream hickory behavior (noted).
- Description: the testing section matches this head.
Created with Perplexity
c6e9a37 had hickory-resolver ask the other nameservers after an NXDOMAIN, as 0.24 did. 0.26 limits a lookup to one timeout per server and then returns a timeout instead of the error it already has, so an NXDOMAIN followed by a nameserver that doesn't answer became a timeout, retried for about 30 s with the default options. glibc, which resolves the host when the connection is opened, takes the first NXDOMAIN as final. Leave 0.26's default, so the cache gets the same answer at once. Co-authored-by: Perplexity <noreply@perplexity.ai>
|
This supersedes my earlier reply, which deferred both follow-ups. The goal is now behavior that matches the glibc stub resolver, which opens the connection through
The PR body has the full table and the test results. Created with Perplexity |
On 0.62 a query's parse time grows exponentially with nesting: f( and ROW( double it with each level (6.5 s at depth 20 in a debug build), POSITION( about triples it, and ARRAY[ hangs once it nests as deep as the recursion limit. 0.63 and 0.64 parse all of them at depth 30 in milliseconds and refuse ARRAY[ nested 48 deep at once. 0.64 reads ONLY before a table in FROM, UPDATE, DELETE and MERGE, and `t *`, as markers on the real table, so plugins now check that table instead of the query being refused. The parser still reads `INSERT INTO ONLY t` as a table named ONLY, so that's still refused. relations.rs follows the AST changes (a TABLE command's names are Idents, SELECT INTO has a list of targets, index_hints is now index_directives) without changing its labels. Co-authored-by: Perplexity <noreply@perplexity.ai>
artem-pplx
left a comment
There was a problem hiding this comment.
Verdict at 5d3ba0e1: not clear, 1 blocker. This covers the delta since the cleared 392b6eaa: 4efff4c (DNS) and 5d3ba0e (sqlparser 0.64).
- Blocker:
Cargo.toml:32:ALTER TABLE ... ATTACH PARTITION <gated>newly parses and the partition name isn't checked (inline). - Nonblocking:
nxdomain_is_not_held_for_a_silent_nameserverneeds a time bound (inline). One description item is in a separate comment.
What holds:
- The exponential parse is fixed. In a debug build,
f(×30 takes 2.1 ms,ROW(×30 0.6 ms andPOSITION(×30 0.6 ms, andARRAY[×48 hits the recursion limit in 8.6 ms. - No
InList/InSubquery/InUnnestmatches remain. - A 165-query probe compared 392b6ea with this head (parse required,
secretsgated). Every other change went from refused or Allow to Deny: ONLY andt *in every position, views,BEGIN ATOMIC, rules,REFRESHandOVERRIDING. - Treating NXDOMAIN as final matches glibc and
main. cargo fmt --checkis clean and the lib suite passes (388/388,--features test-plugins). Clippy shows no warnings on changed lines.
Limits. The probes were unit-level, through QueryRouter::parse and the plugins, not a live PostgreSQL. String function bodies (CREATE FUNCTION ... AS $$ ... $$) are unchecked on 0.62 as well as 0.64. That's pre-existing and is listed for follow-up at the end of the review.
Pass 1 of 2 (Opus, high effort). A second, independent pass with a different model follows on the fixed head.
Created with Perplexity
|
Nonblocking: one description item at
The plugins only see the relations that sqlparser visits. At this head, table_access doesn't see the partition named by Suggested wording once the blocker is fixed: "...routed by kind and checked by plugins, which see the relations sqlparser visits. table_access also checks the partition named by The other claims I checked match the head: the ONLY forms, the view change, the test count, the timing table, and "doesn't change DNS behavior relative to Created with Perplexity |
sqlparser 0.64 parses ATTACH PARTITION, DETACH PARTITION, INHERIT and NO INHERIT in ALTER TABLE, and INHERITS in CREATE FOREIGN TABLE, which 0.62 didn't. It visits only the table altered or created, not the one these name. So with a table gated and parse required, `ALTER TABLE p ATTACH PARTITION secrets DEFAULT; SELECT * FROM p` went from refused to allowed, and read secrets through p. table_access now checks those names, in any statement. nxdomain_is_not_held_for_a_silent_nameserver now waits at most 2 s for the NXDOMAIN, so it fails if the answer is held, not only if it changes. Co-authored-by: Perplexity <noreply@perplexity.ai>
Co-authored-by: Perplexity <noreply@perplexity.ai>
|
Updated the description at 57cd0c1 with that wording. It also names Created with Perplexity |
artem-pplx
left a comment
There was a problem hiding this comment.
Verdict at 57cd0c17: clear, no blockers. This covers the delta from the twice-cleared 392b6eaa.
- The
ATTACH PARTITIONblocker is fixed. The check is recursive (visit_statements), and schema-qualified and quoted names are denied. Mutation-tested: stubbing the supplement failstest_partitions_and_parents_are_checkedon the chain. - The DNS test is now bounded at 2 s.
- The exponential parse is fixed.
f(,ROW(andPOSITION(nested ×30 parse in 0.4–3.7 ms (debug build), andARRAY[×48 errors in 2.5 ms. cargo +stable fmt --checkis clean and the lib suite passes (389/389,--features test-plugins).- PostgreSQL 18.6 confirms that an unquoted
BEGIN ATOMICbody rejects COPY, DROP and ALTER atCREATE FUNCTION. The router's top-level-only COPY check therefore has no executable gap there.
Nonblocking: CREATE [UNLOGGED] TABLE ... INHERITS and DROP FOREIGN TABLE names (inline, recommended in this PR); description items (separate comment). Pre-existing items are in a separate ledger comment.
Limits. These were unit-level router and plugin probes, plus one disposable PostgreSQL 18.6 cluster for DDL semantics. There was no end-to-end proxy run, low-privilege role matrix or loopback FDW setup.
Two passes: Opus (high effort) on 5d3ba0e, an Opus (high effort) check of the 57cd0c1 delta, and a fresh independent final with Astra (high effort) on this exact head.
Created with Perplexity
|
Nonblocking: description items at
The title is accurate. Created with Perplexity |
|
Follow-up ledger (nonblocking, outside this PR's scope; posted once at the end of the review). All of these were already present on
Created with Perplexity |
The visitor doesn't report the parents in CREATE [UNLOGGED] TABLE ... INHERITS or the tables in DROP FOREIGN TABLE. 0.64 newly parses CREATE UNLOGGED TABLE and DROP FOREIGN TABLE, so with plugins_require_parse on, these were refused as unparsed before and allowed after the upgrade. Rows inserted into a child table are read from its parent, so inheriting from a gated table adds rows that its readers see. table_access now checks both, as it checks CREATE FOREIGN TABLE ... INHERITS. Co-authored-by: Perplexity <noreply@perplexity.ai>
Co-authored-by: Perplexity <noreply@perplexity.ai>
artem-pplx
left a comment
There was a problem hiding this comment.
Verdict at 9af00e68: clear, no findings. This covers the delta 57cd0c1..9af00e6.
- The
CreateTable.inheritsandDrop { ForeignTable }arms are correct. - Each arm is independently mutation-tested: removing it fails
test_partitions_and_parents_are_checked. - 28 more boundary probes passed: nested statements, qualified and quoted names,
IF EXISTS/CASCADE/RESTRICT, multiple targets, and ungated controls. PARTITION OFparents are already visited on 0.62 and 0.64, so they're denied.- The lib suite passes (389/389), and the description matches the head.
Security tier: an Opus (high effort) delta check, then a fresh independent final with Astra (high effort) on this exact head.
Created with Perplexity
micahjz
left a comment
There was a problem hiding this comment.
Verdict: APPROVE
PR/head: #29 at 9af00e6 against main/fac54a927739a29e52a0b1a3e1097491f71160ee.
Change/flow: Reviewed the full base-to-head diff, Query/Parse admission and unparsed refusal paths, table_access relation traversal, metrics AST port, and the resolver construction against hickory-resolver 0.26.2 source. The explicit resolver construction preserves the previous builder options and nameserver behavior.
Findings: No new blocking findings. ONLY checks the actual relation; the recursive supplement covers the added partition/inheritance/foreign-table paths. Empty statements no longer hide unparsed VACUUM. The existing COPY-tail gap is still present at this head and is closed by the separately reviewed #30; do not treat #29 alone as closing it.
Validation: Independently ran cargo test --locked --features test-plugins on a fresh package build of this head: 389 unit tests and 4 doc-tests pass. Also ran 32 SQL boundary cases through both Q and P messages against main, this head, and #30. The semicolon/comment VACUUM probe changes from forwarded to refused; the partition/parent cases remain denied or refused. All four nested-parser probes exceeded a 1-second deadline on main; this head completes them in approximately 2–19 ms, including the bounded ARRAY recursion error. Formatting and diff whitespace checks pass. Toolchain: rustc 1.100.0, debug info disabled. Current reported GitHub checks pass; an existing human approval is present.
Compatibility/rollout: Merge #29 before #30 and deploy the completed stack for COPY-tail coverage. External Rust plugins must port/rebuild with sqlparser 0.64 because Statement is part of the compile-time interface; this is not a new PostgreSQL wire or stored-data format. Rolling back restores the old parser/security behavior.
Exclusions: No code changes, merge, queue, deployment, live PostgreSQL/proxy integration test, private-plugin build, or production verification performed. This is a source-review approval, not deployment certification.
Created with Perplexity
Fixes a follow-up from the review of #28, settles how the DNS cache treats negative answers, and moves the query parser to sqlparser 0.64.
Unparsed VACUUM after empty statements
From this review comment (5f2391f, 682ea2e).
Postgres drops empty statements, so
;VACUUM (FULL)runs as a single VACUUM of every table, outside any implicit transaction block.first_wordstopped at the leading;and returned"", sorejects_unparsedforwarded the statement while a table was gated. It now skips;along with whitespace and comments, repeatedly.This also corrects a sentence in #28's description, which said only the first word matters because of the implicit transaction block. The first word is now found after any empty statements. A message with more than one non-empty statement still runs in an implicit transaction block, where VACUUM can't run.
Negative DNS answers
From this review comment and the follow-ups to it (c6e9a37, 392b6ea, 4efff4c).
c6e9a37 turned
trust_negative_responsesoff on every system nameserver, as hickory-resolver 0.24 had it, so an NXDOMAIN from one server made the resolver ask the others. In 0.26 that has a cost 0.24 didn't have. Each attempt gets onetimeoutacross all servers (name_server_pool.rs), and when it runs out the pool returns a timeout instead of the NXDOMAIN it already has. So an NXDOMAIN followed by a nameserver that doesn't answer became a timeout, retried for about 30 s with the default options (three tries each for A and AAAA, 5 s per try).The DNS cache is meant to track what the connection itself will resolve. pgcat opens the connection through Tokio's
lookup_host, which callsgetaddrinfo, so the reference is the glibc stub resolver (res_send.c). These are the results from mock nameservers on localhost, for each kind of answer from the first server, with the servers asked one at a time. With the default options the first two servers are asked at once, and the first answer to arrive counts.timeouttimeouttimeout4efff4c removes the loop, so NXDOMAIN is final again, as in glibc and in #28 as merged. The resolver is now built exactly as
builder_tokio()builds it, plus the three option overrides from #28, so this PR doesn't change DNS behavior relative tomain. It adds the tests and a doc comment that explains the choice.The cost is the case from the review: with nameservers that see different zones, an NXDOMAIN from one of them is final, although another might resolve the host. glibc gives the same answer when that server is asked first.
The other rows can't be reached through hickory-resolver 0.26's configuration:
name_server_pool.rs). Upstreammainmoves on after SERVFAIL, but no release has that yet.timeout, so with servers asked one at a time a silent first server ends the attempt.sqlparser 0.64
(5d3ba0e, 57cd0c1, 9af00e6)
On 0.62, a query's parse time grows exponentially with nesting. 0.63 fixes several of these cases (upstream #2344, #2350, #2352, #2372 and #2392; changelog), and 0.64 fixes nested
POSITION(#2619; changelog). The times below are from debug builds, with the parse on a worker thread bounded byrecv_timeout:SELECT+ nestingf(× 16 / 18 / 20f(× 30ROW(× 16 / 18ROW(× 30POSITION(× 12 / 14POSITION(× 30ARRAY[× 47ARRAY[× 48The port keeps pgcat's behavior except where 0.64 parses differently. Those changes are:
ONLYand*(#2770).FROM ONLY t,FROM ONLY (t),JOIN ONLY s.t,UPDATE ONLY t,DELETE FROM ONLY t,MERGE INTO ONLY tandt *now parse as markers on the real table. On 0.62 the parser readFROM ONLY tas a table named ONLY, aliased t, which pgcat refused, and some of the other forms didn't parse. Plugins now check the real table. Withsecretsgated,SELECT * FROM ONLY secretsis read and denied by table_access, andSELECT * FROM ONLY otheris allowed. The parser still readsINSERT INTO ONLY t(which PostgreSQL rejects) as a table named ONLY, so that form is still refused.ALTER TABLE ONLY,LOCK TABLE ONLYandTRUNCATE ONLYparse as before.CREATE [OR REPLACE] [MATERIALIZED] VIEW vnow visitsvas a relation (#2442), so table_access denies creating a view with a gated table's name, as it already did forCREATE TABLE.ABORT,ALTER DEFAULT PRIVILEGES,ALTER [MATERIALIZED] VIEW,ALTER FOREIGN TABLE,ALTER TABLEwithATTACH/DETACH PARTITION,[NO] INHERIT,RESET (...)orSET LOGGED,ALTER TRIGGER,COMMENT ON FOREIGN TABLE,CREATE EXTENSION ... SCHEMA,CREATE/DROP FOREIGN TABLE,BEGIN ATOMICfunction bodies,CREATE/DROP GROUP,CREATE OR REPLACE SCHEMA,CREATE/DROP RULE,EXCLUDEconstraints,CREATE UNLOGGED TABLE,CREATE USER MAPPING, roleGRANT/REVOKE,GRANT ... ALL ROUTINES,INSERT ... OVERRIDING,ON CONFLICT (a) WHERE,MERGE ... DO NOTHING,REFRESH MATERIALIZED VIEWandRESET SESSION AUTHORIZATION. sqlparser doesn't visit the partition named byATTACH/DETACH PARTITION, the parent named by[NO] INHERIT, the parents inCREATE FOREIGN TABLE ... INHERITS, or the tables inDROP FOREIGN TABLE, so table_access now checks those itself, in any statement (57cd0c1, 9af00e6). The parents inCREATE [UNLOGGED] TABLE ... INHERITSare also checked explicitly (9af00e6). Without those checks,ALTER TABLE p ATTACH PARTITION secrets DEFAULT; SELECT * FROM p,CREATE UNLOGGED TABLE f (a int) INHERITS (secrets)andDROP FOREIGN TABLE secretswould have gone from refused to allowed. PlainCREATE TABLE ... INHERITSalready parsed on 0.62, so with a gated parent it goes from allowed onmainto denied.COMMENT ON FOREIGN TABLEwas refused as unparsed before and is now parsed and allowed, likeCOMMENT ON TABLE. As onmain, table_access doesn't see the tables named byCOMMENT ONor by otherDROPstatements, or the new name inALTER [MATERIALIZED] VIEW ... RENAME TO, which matchesALTER TABLE ... RENAME TO.SELECT,IS JSON,TRIM(FROM ...),XMLPARSE,ORDER BY ... USING,::text COLLATE, right-nested joins,IN ((subquery), 2),FOR UPDATEbeforeLIMIT,BETWEENwith a comparison andSELECT ... INTO a, b.SELECT 1 AS 'x', which PostgreSQL rejects; #2668), a string literal continued on the next line (SELECT 'a'+ newline +'b', which PostgreSQL reads as one string and 0.62 read as a string with an alias),SELECT * FROM limit, andARRAY[nested exactly 47 deep.relations.rs(the metrics classifier) follows the AST changes: a TABLE command's names are nowIdents,SELECT INTOhas a list of targets, andindex_hintsis nowindex_directives. Its labels are unchanged.Plugins receive the parsed
Vec<Statement>, so plugins built outside this repository must move to 0.64 too. The changes they're most likely to hit are theINvariants merged intoExpr::In(#2679),TableFactor::Table's newinheritancefield andindex_hintsrenamed toindex_directives,SelectInto::targets,Table's names asIdents,Statement::Copy's newpayload, and the new statement variants. The plugins needed no changes for the AST API port. table_access adds explicit checks for relation names the visitor skips (57cd0c1, 9af00e6).The port also turned up statements after
COPY ... FROM STDINthat plugins don't see. That's existing behavior, so it's fixed separately in #30, which is stacked on this PR.Testing
On rustc 1.99.1:
test_unparsed_vacuum_is_rejected_while_a_table_is_gatednow also covers these prefixes, through both a simple Query and a Parse message:;,; ;,;;,/* c */;,; -- c\n,/* a */;/* b */, and form feed,;, vertical tab. With the oldtrim_start()line restored, each of these fails.nxdomain_from_the_first_nameserver_is_finalasks two mock UDP nameservers on localhost one at a time, in the given order. The first answers NXDOMAIN and the second an A record. The lookup must fail with NXDOMAIN, and the second server must get no queries. With c6e9a37's loop restored, it fails.nxdomain_is_not_held_for_a_silent_nameserverasks a mock nameserver that answers NXDOMAIN and one that never answers, both at once (the default options). The lookup must fail with NXDOMAIN within 2 s (tokio::time::timeout), well before the silent server's 5 s timeout. With c6e9a37's loop restored, it fails after 2 s.nxdomain_from_every_nameserver_failschecks that NXDOMAIN from every server is still an NXDOMAIN error.test_nested_calls_parse_in_bounded_timeparsesf(,ROW(andPOSITION(nested 30 deep, each on a worker thread, and needs each to parse within 1 s (recv_timeout, no sleeps). It also needsARRAY[nested 48 deep to return an error within 1 s. With the bump and the port reverted to 0.62, all four time out.test_only_checks_the_real_tableneeds the ONLY and*forms above withsecretsgated to parse and be denied, andSELECT * FROM ONLY otherto be allowed. On 0.62, it fails because the query is refused as naming a table ONLY.test_partitions_and_parents_are_checkedgatessecretsand requires a parse. TheATTACH PARTITIONchain above, thepublic.secrets, quoted andFOR VALUES WITHforms,DETACH PARTITION,INHERIT,NO INHERITafter another operation,ALTER FOREIGN TABLE ... INHERIT,CREATE FOREIGN TABLE ... INHERITS, anINHERITinside aBEGIN ATOMICbody,CREATE [UNLOGGED] TABLE ... INHERITS(withsecrets,public.secretsorpublic."secrets"among the parents) andDROP FOREIGN TABLE(secrets, andIF EXISTS other, secrets CASCADE) must each be denied.ATTACH PARTITION other,ATTACH PARTITION "Secrets",INHERIT other,INHERITS (other)for foreign, plain and unlogged tables, andDROP FOREIGN TABLE othermust be allowed. With 57cd0c1's table_access change reverted, it fails on the chain. With only 9af00e6'sCREATE TABLEandDROParms removed, it fails onCREATE TABLE f INHERITS (secrets), and with only theDROParm removed, onDROP FOREIGN TABLE secrets.test_plugins_require_parsenow usesINSERT INTO ONLYfor its ONLY cases, because theFROM ONLYandUPDATE ONLYcases it had now parse.cargo fmt --check: clean.cargo test --locked --features test-plugins: 389 unit tests and 4 doc-tests pass.cargo clippy --locked --lib --examples --tests --features test-plugins: no warnings on the changed lines, and the same warning set asmain.CARGO_PROFILE_DEV_DEBUG=0,CARGO_PROFILE_TEST_DEBUG=0) to save disk space.Created with Perplexity