Repository navigation
Description
MERGE re-executes ON CREATE for an entity created earlier in the same query (ON MATCH never fires)
Configuration
Docker Image: apache/age:release_PG18_1.8.0
Image Digest: sha256:47a0b054c3663a3e64a25fc0c5a982f1b58348019274b326a691cdf77f78eb58
PostgreSQL Version: 18.6
Apache AGE Extension Version: 1.8.0
Operating System: WSL2, Ubuntu 24.04.1 LTS
Installation Method: Docker
Query Interface: psql (SELECT * FROM cypher('xgdb', $$ ... $$) AS (...))
Description
Within a single query, MERGE re-executes its ON CREATE branch for an entity
created earlier in the same query instead of taking the ON MATCH branch. In the issue shape an
UNWIND of two elements feeds two iterations into the same MERGE: the second iteration finds
the edge created by the first (no duplicate edge is created) but still executes
ON CREATE SET rel.v = -12, so ON MATCH SET rel.v = 34 never fires and the final property
value stays at the ON CREATE value.
Steps to reproduce
- Start the AGE container and connect with
psql. - Create one vertex, run the issue-shaped query, then inspect the edge:
SELECT * FROM cypher('xgdb', $$ CREATE (a {k: 1}) $$) AS (r agtype);
SELECT * FROM cypher('xgdb', $$
UNWIND [{v: 1}, {v: -2}] AS relData
MATCH (src), (dest)
WITH relData, src, dest
MERGE (src)-[rel:R]->(dest)
ON CREATE SET rel.v = -12
ON MATCH SET rel.v = 34
$$) AS (r agtype);
SELECT * FROM cypher('xgdb', $$ MATCH ()-[r:R]->() RETURN count(r) AS edges, collect(r.v) AS vs $$) AS (edges agtype, vs agtype);- Control: run the same
MERGEstatement twice as two separate statements (fresh typeRX);
the second statement takesON MATCH(Q2).
Expected behaviour
openCypher MERGE is read-after-write consistent within a query: the first iteration creates the
edge (ON CREATE), the second iteration must find it and take ON MATCH, giving
edges = 1 and vs = [34].
Actual behaviour
Both fresh-container runs show the ON CREATE branch re-executed:
edges | vs
-------+-------
1 | [-12]
(1 row)
The edge count proves the second iteration did match the existing edge (no duplicate), yet the
ON CREATE value was written again. Cross-checks:
- Cross-statement control Q2: second
MERGEstatement correctly givesvs = [34, 34, 34, 34]
style ON MATCH values - the ON branch classification works across statements. - Two-vertex, four-combination shape Q1/Q1b: 4 edges, all stuck at
-12
(vs = [-12, -12, -12, -12]). - MERGE chain Q4: nodes
P/Qeach match correctly (1 each) but theTedge also stays at
-12.
Backend alive (RestartCount=0), no FATAL/PANIC in the server log.
Minimal reproduction
SELECT * FROM cypher('xgdb', $$
UNWIND [{v: 1}, {v: -2}] AS relData
MATCH (src), (dest)
WITH relData, src, dest
MERGE (src)-[rel:R]->(dest)
ON CREATE SET rel.v = -12
ON MATCH SET rel.v = 34
$$) AS (r agtype);
SELECT * FROM cypher('xgdb', $$ MATCH ()-[r:R]->() RETURN count(r) AS edges, collect(r.v) AS vs $$) AS (edges agtype, vs agtype);One vertex is enough (src and dest both bind it).
Why this appears to be a bug
MERGE is defined to act on the current query's own writes. Taking ON CREATE for an entity
that the same query already created - while still suppressing the duplicate creation - is a
contradictory state that silently corrupts results. The cross-statement control proves the ON
branch logic itself works, so the defect is specific to within-query visibility.
Activity