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)) {