From 30855ec2f87944d9d74c84840d44f4e59cb35750 Mon Sep 17 00:00:00 2001 From: Sai Harsha Vinay Kudikala Date: Mon, 16 Nov 2026 07:26:05 -0500 Subject: [PATCH] Make get_vertex see clause writes after a CALLed function runs statements 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 #2605 --- regress/expected/cypher_call.out | 79 +++++++++++++++++++++++++++++++- regress/sql/cypher_call.sql | 58 +++++++++++++++++++++++ src/backend/utils/adt/agtype.c | 24 ++++++++++ 3 files changed, 160 insertions(+), 1 deletion(-) diff --git a/regress/expected/cypher_call.out b/regress/expected/cypher_call.out index 08f97ba41..effd24686 100644 --- a/regress/expected/cypher_call.out +++ b/regress/expected/cypher_call.out @@ -296,10 +296,87 @@ HINT: No function matches the given name and argument types. You might need to DROP FUNCTION myfunc; DROP SCHEMA call_stmt_test CASCADE; NOTICE: drop cascades to function call_stmt_test.add_agtype(agtype,agtype) +/* + * A statement executed inside a CALLed function advances the command + * counter outside the cypher executor. A SET later in the same query + * must still be visible to endNode() (issue #2605) + */ +CREATE FUNCTION pg_temp.issue_2605_noop() RETURNS boolean +LANGUAGE plpgsql VOLATILE AS $$ BEGIN RETURN true; END $$; +CREATE FUNCTION pg_temp.issue_2605_make_index() RETURNS boolean +LANGUAGE plpgsql VOLATILE AS $$ +BEGIN + EXECUTE 'CREATE INDEX issue_2605_idx ON cypher_call."Other" USING gin (properties)'; + RETURN true; +END $$; +CREATE TABLE pg_temp.issue_2605_sink (x int); +CREATE FUNCTION pg_temp.issue_2605_insert() RETURNS boolean +LANGUAGE plpgsql VOLATILE AS $$ +BEGIN + INSERT INTO pg_temp.issue_2605_sink VALUES (1); + RETURN true; +END $$; +SELECT * FROM cypher('cypher_call', $$ + CREATE (:T {id: 1, k4: 'x'}), (:T {id: 11, k4: 'x'}), + (:T {id: 21, k4: 'x'}), (:Other {id: 2}) +$$) AS (v agtype); + v +--- +(0 rows) + +-- control: the CALL performs no statement +SELECT * FROM cypher('cypher_call', $$ + MATCH (n0:T {id: 1}) + CALL pg_temp.issue_2605_noop() YIELD issue_2605_noop AS ignored + WITH n0 + CREATE (n0)-[r:R {id: 12}]->(n0) + WITH n0, r + SET n0.k4 = NULL + RETURN properties(endNode(r)) AS ep +$$) AS (ep agtype); + ep +----------- + {"id": 1} +(1 row) + +-- the CALL creates an index on an unrelated label of the same graph +SELECT * FROM cypher('cypher_call', $$ + MATCH (n0:T {id: 11}) + CALL pg_temp.issue_2605_make_index() YIELD issue_2605_make_index AS ignored + WITH n0 + CREATE (n0)-[r:R {id: 13}]->(n0) + WITH n0, r + SET n0.k4 = NULL + RETURN properties(endNode(r)) AS ep +$$) AS (ep agtype); + ep +------------ + {"id": 11} +(1 row) + +-- a plain INSERT inside the CALLed function has the same effect; +-- no DDL is required to trigger the bug +SELECT * FROM cypher('cypher_call', $$ + MATCH (n0:T {id: 21}) + CALL pg_temp.issue_2605_insert() YIELD issue_2605_insert AS ignored + WITH n0 + CREATE (n0)-[r:R {id: 14}]->(n0) + WITH n0, r + SET n0.k4 = NULL + RETURN properties(endNode(r)) AS ep +$$) AS (ep agtype); + ep +------------ + {"id": 21} +(1 row) + SELECT drop_graph('cypher_call', true); -NOTICE: drop cascades to 2 other objects +NOTICE: drop cascades to 5 other objects DETAIL: drop cascades to table cypher_call._ag_label_vertex drop cascades to table cypher_call._ag_label_edge +drop cascades to table cypher_call."T" +drop cascades to table cypher_call."Other" +drop cascades to table cypher_call."R" NOTICE: graph "cypher_call" has been dropped drop_graph ------------ diff --git a/regress/sql/cypher_call.sql b/regress/sql/cypher_call.sql index ee1468755..9c4fbd53a 100644 --- a/regress/sql/cypher_call.sql +++ b/regress/sql/cypher_call.sql @@ -130,4 +130,62 @@ SELECT * FROM cypher('cypher_call', $$ CALL ag_catalog.myfunz(25) YIELD myfunc R DROP FUNCTION myfunc; DROP SCHEMA call_stmt_test CASCADE; + +/* + * A statement executed inside a CALLed function advances the command + * counter outside the cypher executor. A SET later in the same query + * must still be visible to endNode() (issue #2605) + */ +CREATE FUNCTION pg_temp.issue_2605_noop() RETURNS boolean +LANGUAGE plpgsql VOLATILE AS $$ BEGIN RETURN true; END $$; +CREATE FUNCTION pg_temp.issue_2605_make_index() RETURNS boolean +LANGUAGE plpgsql VOLATILE AS $$ +BEGIN + EXECUTE 'CREATE INDEX issue_2605_idx ON cypher_call."Other" USING gin (properties)'; + RETURN true; +END $$; +CREATE TABLE pg_temp.issue_2605_sink (x int); +CREATE FUNCTION pg_temp.issue_2605_insert() RETURNS boolean +LANGUAGE plpgsql VOLATILE AS $$ +BEGIN + INSERT INTO pg_temp.issue_2605_sink VALUES (1); + RETURN true; +END $$; + +SELECT * FROM cypher('cypher_call', $$ + CREATE (:T {id: 1, k4: 'x'}), (:T {id: 11, k4: 'x'}), + (:T {id: 21, k4: 'x'}), (:Other {id: 2}) +$$) AS (v agtype); +-- control: the CALL performs no statement +SELECT * FROM cypher('cypher_call', $$ + MATCH (n0:T {id: 1}) + CALL pg_temp.issue_2605_noop() YIELD issue_2605_noop AS ignored + WITH n0 + CREATE (n0)-[r:R {id: 12}]->(n0) + WITH n0, r + SET n0.k4 = NULL + RETURN properties(endNode(r)) AS ep +$$) AS (ep agtype); +-- the CALL creates an index on an unrelated label of the same graph +SELECT * FROM cypher('cypher_call', $$ + MATCH (n0:T {id: 11}) + CALL pg_temp.issue_2605_make_index() YIELD issue_2605_make_index AS ignored + WITH n0 + CREATE (n0)-[r:R {id: 13}]->(n0) + WITH n0, r + SET n0.k4 = NULL + RETURN properties(endNode(r)) AS ep +$$) AS (ep agtype); +-- a plain INSERT inside the CALLed function has the same effect; +-- no DDL is required to trigger the bug +SELECT * FROM cypher('cypher_call', $$ + MATCH (n0:T {id: 21}) + CALL pg_temp.issue_2605_insert() YIELD issue_2605_insert AS ignored + WITH n0 + CREATE (n0)-[r:R {id: 14}]->(n0) + WITH n0, r + SET n0.k4 = NULL + RETURN properties(endNode(r)) AS ep +$$) AS (ep agtype); + SELECT drop_graph('cypher_call', true); diff --git a/src/backend/utils/adt/agtype.c b/src/backend/utils/adt/agtype.c index cc9cc7717..5089ac2f2 100644 --- a/src/backend/utils/adt/agtype.c +++ b/src/backend/utils/adt/agtype.c @@ -6134,6 +6134,7 @@ static Datum get_vertex(const char *graph, const char *vertex_label, Oid graph_namespace_oid; Oid vertex_label_table_oid; Snapshot snapshot; + CommandId saved_curcid; /* get the specific graph namespace (schema) */ graph_namespace_oid = get_namespace_oid(graph, false); @@ -6151,6 +6152,26 @@ static Datum get_vertex(const char *graph, const char *vertex_label, aclcheck_error(aclresult, OBJECT_TABLE, vertex_label); } + /* + * Temporarily advance the snapshot's curcid so that vertices written + * by preceding clauses (e.g., SET) in the same query are visible. + * When this runs inside a cypher query, the active snapshot is the + * executor's es_snapshot, whose curcid the clause machinery manages + * manually. CommandCounterIncrement() calls made outside that + * machinery, e.g., by statements executed inside a function called + * with CALL, advance the global command id without advancing this + * snapshot's curcid, which would leave it behind the command id + * that stamped those writes and make this scan return the + * pre-update tuple. + * + * Use Max to ensure we never decrease curcid. Visibility is decided + * when the tuple is fetched below, so restore the original curcid + * right after the fetch, before any error path can leave it raised. + * Same pattern as entity_exists() in cypher_utils.c. + */ + saved_curcid = snapshot->curcid; + snapshot->curcid = Max(saved_curcid, GetCurrentCommandId(false)); + /* open the relation (table) */ graph_vertex_label = table_open(vertex_label_table_oid, ShareLock); @@ -6192,6 +6213,9 @@ static Datum get_vertex(const char *graph, const char *vertex_label, tuple = heap_getnext(scan_desc, ForwardScanDirection); } + /* the fetch decided visibility; restore the snapshot's curcid */ + snapshot->curcid = saved_curcid; + /* bail if the tuple isn't valid */ if (!HeapTupleIsValid(tuple)) {