Skip to content

Make get_vertex see clause writes after a CALLed function runs statements - #2615

Open
ksaiharshavinay wants to merge 1 commit into
apache:masterfrom
ksaiharshavinay:fix/2605-stale-vertex-read
Open

ksaiharshavinay wants to merge 1 commit into
apache:masterfrom
ksaiharshavinay:fix/2605-stale-vertex-read

Conversation

@ksaiharshavinay

Copy link
Copy Markdown

Fixes #2605.

In the reported query, a CALL to a PL/pgSQL function that creates
an index on an unrelated label makes a later SET n0.k4 = NULL
invisible to properties(endNode(r)): it returns the pre-update
properties even though the stored vertex is correct.

Root cause: any statement executed inside the called function goes
through SPI, which calls CommandCounterIncrement() when it finishes.
That advances the global command id but not the curcid of the
snapshot the executor is using. The executor's clause machinery
tracks curcid manually and assumes it stays in step with the global
command id, so after such a CALL the snapshot's curcid lags behind.
The SET update is then stamped with a command id the snapshot cannot
see, and the scan in get_vertex() returns the old tuple version.
(The same happens with a plain INSERT inside the called function -
DDL is not required.)

The executor's own entity fetch (entity_exists() in cypher_utils.c)
already guards against this by temporarily advancing the snapshot's
curcid to the current command id (and restoring it afterwards).
get_vertex() - used by startNode(), endNode() and
_get_vertex_by_graphid - did not. This change applies the same
pattern there: save, advance with Max, restore immediately after the
tuple fetch so no error path can leave the snapshot raised.

Regression tests in cypher_call.sql: the reported shape with a
no-op CALL as control, an index-creating CALL, and an
INSERT-executing CALL. Full suite: all 43 tests pass (PG 18.6, AGE
master).

Disclosure: this fix was developed with AI assistance (Claude), and
every change was reproduced, tested and reviewed locally by me.

…ents

A statement executed inside a function called with CALL goes through
SPI, whose CommandCounterIncrement advances the global command id
but not the curcid of the snapshot used by the executor. The clause
machinery tracks curcid manually and assumes it stays in step, so
after such a CALL the snapshot lags: a SET update later in the same
query is stamped with a command id the snapshot cannot see, and
properties(endNode(r)) returns the pre-update tuple even though the
stored vertex is correct. A plain INSERT inside the called function
triggers it too; DDL is not required.

Apply the same save / advance-with-Max / restore pattern the
executor's entity fetch in cypher_utils.c already uses, in
get_vertex(), which backs startNode(), endNode() and
_get_vertex_by_graphid.

Add a regression test in cypher_call.sql with a no-op CALL as
control and an index-creating CALL as the failing case.

Fixes apache#2605
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Creating an index on an unrelated label during a Cypher query makes properties(endNode(r)) return pre-update properties

1 participant