chrisjaimon2012 opened a new issue, #2629:
URL: https://github.com/apache/age/issues/2629
### Configuration
- Docker images: `apache/age:release_PG18_1.8.0`
(`sha256:47a0b054c3663a3e64a25fc0c5a982f1b58348019274b326a691cdf77f78eb58`,
PostgreSQL 18.6) and `apache/age:dev_snapshot_master`
(`sha256:dd7d8f022a70cb2331e863998f30b7c0311d32e7c3f6816c7dc5fee448da2341`,
built 2026-11-23, PostgreSQL 18.7)
- AGE extension version: 1.8.0
- Platform: linux/arm64
- Query interface: `psql`
### Description
When one `cypher()` statement updates the same vertex or edge a second time,
the second update is dropped without an error. Every write after the first is
lost in each of these shapes:
- a `SET` over several `UNWIND` rows that reach the same entity;
- a `SET` after `MERGE`'s `ON CREATE SET` or `ON MATCH SET` has written the
same entity;
- a later `UNWIND` row whose `MERGE` matches an entity an earlier row
created: its `ON MATCH SET` is lost. This is the shape of #2620, which may be
the same defect.
Two `SET` clauses on a matched entity work, as do writes to different
entities.
### Steps to reproduce
```sql
LOAD 'age';
SET search_path = ag_catalog, "$user", public;
SELECT create_graph('repro');
-- 1. A SET over two UNWIND rows that match the same vertex
SELECT * FROM cypher('repro', $$ CREATE (:A {id: 1}) $$) AS (r agtype);
SELECT * FROM cypher('repro', $$ UNWIND [3, 4] AS v MATCH (n:A {id: 1}) SET
n.d = v $$) AS (r agtype);
SELECT * FROM cypher('repro', $$ MATCH (n:A {id: 1}) RETURN n.d $$) AS (d
agtype);
-- 2. The same with an increment
SELECT * FROM cypher('repro', $$ CREATE (:B {id: 1, c: 5}) $$) AS (r agtype);
SELECT * FROM cypher('repro', $$ UNWIND [1, 1, 1] AS v MATCH (n:B {id: 1})
SET n.c = n.c + v $$) AS (r agtype);
SELECT * FROM cypher('repro', $$ MATCH (n:B {id: 1}) RETURN n.c $$) AS (c
agtype);
-- 3. MERGE creates the vertex, ON CREATE SET writes it, then SET writes it
again
SELECT * FROM cypher('repro', $$ MERGE (n:C {id: 1}) ON CREATE SET n.f = 1
SET n.d = 2 $$) AS (r agtype);
SELECT * FROM cypher('repro', $$ MATCH (n:C {id: 1}) RETURN n.f, n.d $$) AS
(f agtype, d agtype);
-- 4. MERGE matches the vertex, ON MATCH SET writes it, then SET writes it
again
SELECT * FROM cypher('repro', $$ CREATE (:D {id: 1, c: 5}) $$) AS (r agtype);
SELECT * FROM cypher('repro', $$ MERGE (n:D {id: 1}) ON MATCH SET n.c = 7
SET n.d = 1 $$) AS (r agtype);
SELECT * FROM cypher('repro', $$ MATCH (n:D {id: 1}) RETURN n.c, n.d $$) AS
(c agtype, d agtype);
-- 5. The same on an edge
SELECT * FROM cypher('repro', $$ CREATE (:E {id: 1}), (:E {id: 2}) $$) AS (r
agtype);
SELECT * FROM cypher('repro', $$
MATCH (a:E {id: 1}), (b:E {id: 2}) MERGE (a)-[r:R]->(b) ON CREATE SET
r.f = 1 SET r.c = 2
$$) AS (r agtype);
SELECT * FROM cypher('repro', $$ MATCH (:E)-[r:R]->(:E) RETURN r.f, r.c $$)
AS (f agtype, c agtype);
-- 6. Two UNWIND rows MERGE the same vertex (the shape of #2620)
SELECT * FROM cypher('repro', $$ UNWIND [1, 1] AS k MERGE (n:F {id: k}) ON
CREATE SET n.c = 1 ON MATCH SET n.c = 2 $$) AS (r agtype);
SELECT * FROM cypher('repro', $$ MATCH (n:F) RETURN count(n), collect(n.c)
$$) AS (n agtype, c agtype);
-- Controls, which work: two SET clauses on a matched vertex; one SET after
MERGE; UNWIND rows on different vertices
SELECT * FROM cypher('repro', $$ CREATE (:G {id: 1, c: 5}) $$) AS (r agtype);
SELECT * FROM cypher('repro', $$ MATCH (n:G {id: 1}) SET n.c = 7 SET n.d = 1
$$) AS (r agtype);
SELECT * FROM cypher('repro', $$ MATCH (n:G {id: 1}) RETURN n.c, n.d $$) AS
(c agtype, d agtype);
SELECT * FROM cypher('repro', $$ MERGE (n:H {id: 1}) SET n.f = coalesce(n.f,
1), n.d = 2 $$) AS (r agtype);
SELECT * FROM cypher('repro', $$ MATCH (n:H {id: 1}) RETURN n.f, n.d $$) AS
(f agtype, d agtype);
SELECT * FROM cypher('repro', $$ CREATE (:I {id: 1}), (:I {id: 2}) $$) AS (r
agtype);
SELECT * FROM cypher('repro', $$ UNWIND [[1, 3], [2, 4]] AS p MATCH (n:I
{id: p[0]}) SET n.d = p[1] $$) AS (r agtype);
SELECT * FROM cypher('repro', $$ MATCH (n:I) RETURN n.id, n.d ORDER BY n.id
$$) AS (id agtype, d agtype);
```
### Expected and actual behaviour
Expected is what Neo4j 5.26.34 Community returns for the same Cypher, as
openCypher defines it: each row's `SET` applies in turn, so the last write wins
and increments add up.
| Case | Expected (Neo4j 5.26) | Actual (AGE 1.8.0 and master) |
| --- | --- | --- |
| 1. `UNWIND [3, 4]`, `SET n.d = v` on one vertex | `d = 4` | `d = 3` |
| 2. `UNWIND [1, 1, 1]`, `SET n.c = n.c + v` from 5 | `c = 8` | `c = 6` |
| 3. `ON CREATE SET n.f = 1 SET n.d = 2` | `f = 1, d = 2` | `f = 1, d =
null` |
| 4. `ON MATCH SET n.c = 7 SET n.d = 1` | `c = 7, d = 1` | `c = 7, d = null`
|
| 5. Case 3 on an edge | `f = 1, c = 2` | `f = 1, c = null` |
| 6. `UNWIND [1, 1]`, `MERGE … ON CREATE SET n.c = 1 ON MATCH SET n.c = 2` |
one vertex, `[2]` | one vertex, `[1]` |
| Control: `SET n.c = 7 SET n.d = 1` | `c = 7, d = 1` | `c = 7, d = 1` |
| Control: `MERGE … SET n.f = coalesce(n.f, 1), n.d = 2` | `f = 1, d = 2` |
`f = 1, d = 2` |
| Control: `UNWIND` rows on two vertices | `3` and `4` | `3` and `4` |
No error, warning or notice is raised, and the server log is clean. Both
images give identical output.
### Where it seems to come from
On master at 2c8be68, `update_entity_tuple` in
`src/backend/executor/cypher_set.c` returns without writing when
`table_tuple_update` (lines 187-203) or `heap_lock_tuple` (lines 223-231)
reports `TM_SelfModified` with `hufd.cmax == cid`. In other words, the row was
already updated under the same command id. That's PostgreSQL's rule for SQL,
where one `UPDATE` changes a row at most once. In Cypher, though, each row's
`SET` must apply.
A terminal `SET` processes every input row in `process_all_tuples` before
its single `CommandCounterIncrement` (around line 962). So the first row's
update takes the command id, and every later row that reaches the same entity
hits that branch. The `MERGE` cases look like the same branch reached from the
actions' update followed by the `SET`'s update. I haven't traced that path.
### Workarounds
- For `MERGE`: write `SET n.p = coalesce(n.p, <value>)` in place of `ON
CREATE SET n.p = <value>`, as in the second control. That covers `ON CREATE
SET`, but `ON MATCH SET` followed by another `SET` has no equivalent.
- For `UNWIND`: aggregate per entity before the `SET`, so that each entity
is written once.
Neither helps code that runs Cypher written for Neo4j, where these shapes
are common. Cartography's ingestion statements, for example, use `MERGE … ON
CREATE SET n.firstseen = timestamp() SET n.lastupdated = $update_tag, …`
throughout.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]