You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Single-node labeled pattern (a:Label) does not filter by label (vertex-only EXISTS skips label quals) #2443
Single-node labeled pattern expressions (a:Label) are accepted as boolean expressions but do not filter by label — they evaluate as trivially true. This is a pre-existing limitation in the vertex-only pattern transform, surfaced by #2360 (which lets a bare pattern appear anywhere an expression is valid).
Reproduction
SELECT*FROM cypher('g', $$ CREATE (:Person {name:'Alice'}), (:Animal {name:'Rex'}) $$) AS (r agtype);
-- expected: only "Alice"; actual: "Alice" AND "Rex"SELECT*FROM cypher('g', $$ MATCH (a) WHERE (a:Person) RETURN a.name $$) AS (n agtype);
-- expected: a boolean reflecting whether a has label Person; actual: always trueSELECT*FROM cypher('g', $$ MATCH (a:Person) RETURN (a:Person) $$) AS (b agtype);
Root cause
make_path_join_quals() in src/backend/parser/cypher_clause.c:5454 early-returns for vertex-only patterns:
/* for vertex only queries, there is no work to do */if (list_length(entities) <3)
{
returnNIL;
}
The label-filter quals (_extract_label_id(id) = label_id) are only emitted inside make_join_condition_for_edge(), which is never reached without a relationship. So a single-vertex pattern desugars to an EXISTS subquery with no label constraint → trivially true.
Emit the label-filter qual for single bound vertices before the early return in make_path_join_quals (or hoist the label-filter logic out of make_join_condition_for_edge so it applies to vertex-only patterns), plus a regression test asserting WHERE (a:Person) filters and RETURN (a:Label) reflects the label.
Correction — the root cause above is incomplete. Verified by instrumenting the transform and inspecting the generated plans; the real cause is a missing correlation, not the make_path_join_quals early-return on its own.
What actually happens: a single-node labeled pattern desugars to an EXISTS sub-pattern. The inner vertex is not correlated to the outer variable — it is handled like any vertex, and with no edge to carry a correlation qual, the sub-pattern references nothing from the enclosing query. The planner therefore produces an uncorrelated one-time InitPlan that is true whenever any vertex of that label exists, independent of the outer row:
InitPlan 1
-> Result
One-Time Filter: ((InitPlan 1).col1)::agtype
Relationship patterns work precisely because the edge-driven join (start_id = a.id) correlates them to the outer variable — confirmed by identical node-level state in both cases but a correlated SubPlan only for the relationship form.
So make_path_join_qualsis the right place to fix, but the fix is to emit a correlated label filter, not merely "add the label qual": for a vertex-only pattern whose vertex has a non-default label and whose variable is bound in an ancestor parse state, emit _extract_label_id(<id>) = <label_id> via make_qual — whose name-based id reference resolves to the outer variable, which both correlates the sub-pattern and enforces the label. After the fix the plan is a correlated SubPlan:
SubPlan 1
-> Result
One-Time Filter: ((_extract_label_id(a_1.id))::integer = 3)
Fix + regression coverage in #2444. Note: RETURN (a:Label) in a projection on an unlabeled bound variable is a separate, pre-existing "multiple labels" guard, orthogonal to this issue and left unchanged.
Fix is up and ready for review: #2444. It makes single-node labeled patterns a correlated label predicate (WHERE / EXISTS), verified against a clean PG18 build with the full regression suite green (41/41). Merging #2444 will close this.
Activity
gregfelice commentedon Jun 22, 2026
Correction — the root cause above is incomplete. Verified by instrumenting the transform and inspecting the generated plans; the real cause is a missing correlation, not the
make_path_join_qualsearly-return on its own.What actually happens: a single-node labeled pattern desugars to an
EXISTSsub-pattern. The inner vertex is not correlated to the outer variable — it is handled like any vertex, and with no edge to carry a correlation qual, the sub-pattern references nothing from the enclosing query. The planner therefore produces an uncorrelated one-timeInitPlanthat is true whenever any vertex of that label exists, independent of the outer row:Relationship patterns work precisely because the edge-driven join (
start_id = a.id) correlates them to the outer variable — confirmed by identical node-level state in both cases but a correlatedSubPlanonly for the relationship form.So
make_path_join_qualsis the right place to fix, but the fix is to emit a correlated label filter, not merely "add the label qual": for a vertex-only pattern whose vertex has a non-default label and whose variable is bound in an ancestor parse state, emit_extract_label_id(<id>) = <label_id>viamake_qual— whose name-based id reference resolves to the outer variable, which both correlates the sub-pattern and enforces the label. After the fix the plan is a correlatedSubPlan:Fix + regression coverage in #2444. Note:
RETURN (a:Label)in a projection on an unlabeled bound variable is a separate, pre-existing "multiple labels" guard, orthogonal to this issue and left unchanged.Fix single-node labeled pattern expressions not filtering by label (a…
gregfelice commentedon Jun 22, 2026
Fix is up and ready for review: #2444. It makes single-node labeled patterns a correlated label predicate (WHERE / EXISTS), verified against a clean PG18 build with the full regression suite green (41/41). Merging #2444 will close this.
Fix single-node labeled pattern expressions not filtering by label (#…
Master to pg19 (#2456)
Fix single-node labeled pattern expressions not filtering by label (#…
Fix single-node labeled pattern expressions not filtering by label (#…