Query Format

Cypher queries are executed via the cypher function in the ag_catalog schema, which returns a SETOF records.

cypher()

The cypher() function executes a Cypher query passed as an argument.

Syntax: cypher(graph_name, query_string, parameters)

Returns:

A SETOF records

Parameters:

Name Description
graph_name The target graph for the Cypher query.
query_string The Cypher query to execute.
parameters Optional parameter map for prepared statements; defaults to NULL.

Note!

  • Even if the Cypher query produces no results, a record definition must still be provided.
  • The parameters map is valid only with prepared statements; using it otherwise raises an error.

Example query:

SELECT * FROM cypher('graph_name', $$ 
/* Cypher query goes here */ 
$$) AS (result1 agtype, result2 agtype);

Using Cypher Within Expressions

Cypher cannot be used directly inside SQL expressions. Instead, use a subquery. For details on embedding Cypher queries in expressions, see [Advanced Usage]().

In the SELECT Clause

Calling cypher() as a standalone column in the SELECT clause is not allowed. However, cypher() may be used within conditional clauses (e.g., WHERE, HAVING).

Unsupported usage:

SELECT 
    cypher('graph_name', $$
         MATCH (v:Person)
         RETURN v.name
     $$);

Error message:

ERROR:  cypher(...) in expressions is not supported
LINE 3:         cypher('graph_name', $$
                ^
HINT:  Use subquery instead if possible.