Generic-plan LIMIT functions can discard valid ranked search rows
perfloop-oss/paradedb · Unsound pruning
https://perfloop.ai/t/oss/case_d37hp5gv34
Verdict
VERIFIED · settled 2026-11-20
What happened: The check fails before this change and passes with it.
Hypothesis
Generic score-ordered searches can return too few rows because limit pushdown uses a function's argument instead of its result. A local SQL integration check reproduced missing rows. The prevalence of these expression shapes in customer queries is unknown.
At revision 0d959cf438855c1f57ef2af58111c21fc30a562f, unwrap_to_extern_param_id follows every FuncExpr with one argument without checking its function. ParameterizedValue::<i64>::from_node then retains only the external parameter ID. LimitOffset::from_parse and from_root pass that value to BaseScan::create_custom_path, whose parameter branch permits pushdown. TopKScanExecState::init resolves the raw parameter, and static_limit clamps a negative value to zero. This does not preserve abs($1) for a negative bigint.
The new, provisional Rust integration file tests/tests/hunt_limit_expression_semantics.rs used the repository's rstest and SQL fixtures, eight matching rows, force_generic_plan, no parallel workers, and one abs(bigint) wrapper. The bounded search covered integers -8..=8 in increasing magnitude and stopped after 0, 1, -1. Command: cargo test --package tests --test hunt_limit_expression_semantics bounded_abs_limit_search -- --nocapture --test-threads=1. On PostgreSQL 18.7, output was 'abs LIMIT input=-1 reference_rows=1 pushed_rows=Ok(0) TopK=true', followed by an assertion failure and exit 101. A separate command with the abs_negative_one_replay filter reproduced the same 0-versus-1 result and exit 101. The search did not complete its remaining inputs.
The witness prepares SELECT id FROM hunt_limit_docs WHERE body ||| 'needle' ORDER BY pdb.score(id) DESC LIMIT abs($1), then executes it with -1::bigint. The reference materializes the same scored matches before applying ORDER BY and LIMIT, preventing the limit from reaching the scan. EXPLAIN showed TopKScanExecState for the witness and ColumnarExecState beneath a CTE and Sort for the reference. The scratch test file and private cluster were removed after the run. No existing repository file was edited.
The independent repair proof is that supported generic-plan LIMIT expressions preserve PostgreSQL's result and error behavior, beginning with this deterministic witness; a safe fallback is acceptable. The checker layout is provisional, not an additional fixed repair requirement. CONTRIBUTING.md requires a reproducer and a failing-then-passing regression test for bug fixes; this needs no new public API.
Current upstream inspection found a distinct generic-plan predicate change in https://github.com/paradedb/paradedb/pull/7536: its actual patch routes parameter-containing NullTest predicates through HeapExpr, not LIMIT decoding. Recent path history and the merged implementation in https://github.com/paradedb/paradedb/pull/7427 show parameter-fetch-hook support already landed, but its patch leaves wrapper stripping unchanged. CONTRIBUTING.md identifies GitHub pull requests as the change-review venue. Neither change establishes that an arbitrary single-argument function is an identity conversion.
Change to test: Preserve the SQL value of a generic-plan LIMIT expression before using it to cap the scan. Evaluate retained expressions with PostgreSQL or decline their pushdown unless their value-preserving behavior is established, while retaining support for ordinary bigint parameters.
Where it lives
perfloop-oss/paradedb · pg_search/src/postgres/customscan/hook.rs
Evidence
The check fails before this change and passes with it: `With eight matching documents, default query settings and force_generic_plan, a score-ordered search with LIMIT abs($1) for -1::bigint returns the same one row as the corresponding literal LIMIT abs(-1::bigint).`
The check fails before this change and passes with it: `With eight matching documents, default query settings and force_generic_plan, a score-ordered search with LIMIT 1 OFFSET abs($1) for -1::bigint returns the same one row as the corresponding literal OFFSET abs(-1::bigint).`
The check fails before this change and passes with it: `With eight matching documents, default query settings and force_generic_plan, a score-ordered search with LIMIT $1 OFFSET ($2+0) for bigint parameters (1,1) returns the same one row as the corresponding literal bounds.`
The check fails before this change and passes with it: `With eight matching documents, default query settings and force_generic_plan, a score-ordered search with a NULL bigint LIMIT parameter returns all eight matching rows, like literal LIMIT NULL, without an internal error.`
The check fails before this change and passes with it: `With eight matching documents, default query settings and force_generic_plan, a score-ordered search with LIMIT 1 and a NULL bigint OFFSET parameter returns the same one row as literal LIMIT 1 OFFSET NULL, without an internal error.`
Checks: 5 of 5 passed. Verification: no defect found.
Timeline
2026-11-20· Case opened