Monthly Summary: Assertion Failure in LATERAL GRAPH_TABLE with Multi-label Pattern (May 2026)
Overview
A bug was reported and fixed in PostgreSQL's SQL/PGQ GRAPH_TABLE implementation where a LATERAL reference into a GRAPH_TABLE using multi-label patterns (e.g., a IS vl1 | vl2) triggers an assertion failure in the planner's create_lateral_join_info(). The fix progressed from initial diagnosis through patch v2 during this month.
Root Cause
When GRAPH_TABLE patterns use label alternation, rewriteGraphTable() expands them into multiple path queries combined via UNION ALL through generate_setop_from_pathqueries(). This introduces two levels of subquery nesting (one per-branch wrapper + one overall setop wrapper), but the rewriter only bumped varlevelsup on outer-referencing Vars once — correct for the single-path case but insufficient for multi-path expansion.
The under-bumped Var causes the lateral dependency to resolve to the GRAPH_TABLE's own RTE rather than the intended outer relation, producing a self-referential lateral dependency. The planner catches this via:
Assert(!bms_is_member(rti, lateral_relids)); /* initsplan.c:1428 */
On non-assert builds, this would silently produce wrong plans or wrong results.
The Fix
The fix calls IncrementVarSublevelsUp() with min_sublevels_up = 1 on each path query before it is wrapped into a subquery RTE during UNION ALL construction. This ensures only lateral-referencing Vars (those already pointing outside the path query) get bumped, while path-query-local Vars remain unaffected. The fix is placed inside generate_setop_from_pathqueries() itself — the function that introduces the extra wrapper is responsible for compensating varlevelsup.
Patch Progression
- v1 (Satya Narlapuram): Initial fix with correct approach but minor style issues.
- Style review (Ashutosh Bapat): Flagged non-standard multi-line comment formatting.
- v2 (Ayush Tiwari): Reformatted comments to PostgreSQL style, expanded commit message with full causal explanation, relocated regression test adjacent to existing lateral-reference tests. Patch appears to be converging toward commit readiness.
Status at Month End
No objections or alternative approaches have been raised. The patch is awaiting further review but appears technically sound and ready for commit consideration.