Skip to content

MATCH directly after OPTIONAL MATCH is rejected with "MATCH cannot follow OPTIONAL MATCH" #2622

Open

Description

@zihaozeng0021

MATCH directly after OPTIONAL MATCH is rejected with "MATCH cannot follow OPTIONAL MATCH"

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

A MATCH clause placed directly after an OPTIONAL MATCH clause is rejected by
the server with ERROR: MATCH cannot follow OPTIONAL MATCH. openCypher allows consecutive
reading clauses (MATCH / OPTIONAL MATCH in any order) inside the same read scope; only
updating clauses require a separating WITH. The rejection is inconsistent with the sibling
forms that AGE accepts on the same server: OPTIONAL MATCH -> OPTIONAL MATCH, MATCH -> MATCH,
and OPTIONAL MATCH -> WITH -> MATCH all execute and return correct counts on empty and
non-empty graphs.


Steps to reproduce

  1. Start the AGE container and connect with psql.
  2. Create the graph and run the chained read-clause query on an empty graph:
DO $ddl$ BEGIN
  IF NOT EXISTS (SELECT 1 FROM ag_catalog.ag_graph WHERE name = 'xgdb') THEN
    PERFORM ag_catalog.create_graph('xgdb');
  END IF;
END $ddl$;
SELECT * FROM cypher('xgdb', $$ OPTIONAL MATCH (n) MATCH (m) RETURN count(m) $$) AS (c agtype);
  1. As contrast, run the sibling clause-order forms in the same graph. On a non-empty graph
    (2 vertices, 1 edge) the legal variants return count 1 while the OPTIONAL MATCH -> MATCH query is rejected.

Expected behaviour

The query is legal openCypher: consecutive reading clauses may be chained in one scope. It should
execute without error and return count = 0 on an empty graph (and the matching count on a
non-empty graph). Neo4j and Memgraph execute the same shape.


Actual behaviour

The whole statement is rejected on every run (two fresh-container runs):

ERROR:  MATCH cannot follow OPTIONAL MATCH
LINE 1: SELECT * FROM cypher('xgdb', $$ OPTIONAL MATCH (n) MATCH (m)...
                                      ^
exit=3

On the non-empty graph the legal variants answer correctly while the OPTIONAL MATCH -> MATCH query is refused:

 b_count
---------
 1
(1 row)

ERROR:  MATCH cannot follow OPTIONAL MATCH
LINE 1: SELECT * FROM cypher('xgdb', $$ OPTIONAL MATCH (n1)-[:rt3]->...
                                      ^

The backend stays alive (RestartCount=0); the server log contains no FATAL/PANIC lines.


Minimal reproduction

SELECT * FROM cypher('xgdb', $$ OPTIONAL MATCH (n) MATCH (m) RETURN count(m) $$) AS (c agtype);

Works on an empty or a non-empty graph; no data required.


Why this appears to be a bug

A documented, openCypher-legal read-clause combination is refused by a server-side clause-order
validation, while semantically adjacent combinations are accepted. Valid queries become impossible
to express directly and users must insert a no-op WITH. The behavior contradicts both the
openCypher grammar and the behavior of other openCypher engines.

Activity

  1. added
    bug
    on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions