GIN jsonpath scans miss rows when methods follow a predicate
Hi,
Codex reported me an issue where GIN scans can miss matching rows when
methods are applied to a jsonpath predicate's result. For example:
------------------------------------------
CREATE TEMP TABLE t AS SELECT '2'::jsonb AS j;
-- Returns one row.
SELECT * FROM t WHERE j @@ '($ == 1).size().boolean()';
CREATE INDEX ON t USING gin (j);
SET enable_seqscan = off;
-- Returns no rows, but should return one.
SELECT * FROM t WHERE j @@ '($ == 1).size().boolean()';
RESET enable_seqscan;
------------------------------------------
In the default lax mode, "($ == 1).size().boolean()" evaluates to true.
But, extract_jsp_bool_expr() ignores the following methods and
incorrectly extracts an index condition from "$ == 1", causing the issue.
The attached patch fixes this by skipping index condition extraction for
predicates with following items.
Regards,
--
Fujii Masao
Fujii Masao <masao.fujii@gmail.com> writes:
Codex reported me an issue where GIN scans can miss matching rows when
methods are applied to a jsonpath predicate's result.
SELECT * FROM t WHERE j @@ '($ == 1).size().boolean()';
This is related to one of the bugs I fixed in b28a2b2bf, but I think
it's different. I fear your patch might be too sweeping though ---
are you sure it doesn't reject cases that would be valid? I'd prefer
to inject the no-nextPos test on a per-node-type basis.
regards, tom lane
On Wed, Oct 14, 2026 at 10:25 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
This is related to one of the bugs I fixed in b28a2b2bf, but I think
it's different. I fear your patch might be too sweeping though ---
are you sure it doesn't reject cases that would be valid? I'd prefer
to inject the no-nextPos test on a per-node-type basis.
Yes, you're right. Seems we can safely extract an index condition if
the combined effect of the items following a predicate cannot turn false
or unknown predicate result into true. For example,
"($ == 1).string().lower().boolean()" can still use its index condition.
So I've updated the patch to check the entire chain of items following
a predicate. It allows chains that are safe as a whole and conservatively
skips extraction otherwise.
I've identified 13 such item types so far, but of course I may have
missed some. Thoughts?
Regards,
--
Fujii Masao