Prepared statements let you run parameterized Cypher read queries in PolarDB for PostgreSQL. The database parses and analyzes the query once at preparation time, then reuses that work on each execution—reducing overhead and keeping parameter values out of the query string.
For the Cypher query format, see Cypher query format.
Cypher parameter format
A Cypher parameter is a $ sign followed by an identifier. Unlike PostgreSQL parameters, Cypher parameters start with a letter and can be followed by an alphanumeric string of any length.
Example: $parameter_name
Prepare and run a prepared statement
Prepared statements in Cypher extend the stored procedure system of PostgreSQL. The structure requires two distinct parameter types:
Cypher parameters (such as
$name): placed inside the Cypher query stringPostgreSQL parameter (such as
$1): placed as the third argument of thecypher()function call, not inside the Cypher query
The following example prepares and runs a statement that matches a person by name:
-- Prepare the statement
PREPARE cypher_stored_procedure(agtype) AS
SELECT *
FROM cypher('expr', $$
MATCH (v:Person)
WHERE v.name = $name -- Cypher parameter
RETURN v
$$, $1) -- PostgreSQL parameter: passes the agtype map at execution time
AS (v agtype);
-- Run the statement
EXECUTE cypher_prepared_statement('{"name": "Tobias"}');When running EXECUTE, pass an agtype map that contains the parameter values. The key "name" in the map corresponds to the Cypher parameter $name—omit the $ prefix in the map keys. If the value is not an agtype map, an error is returned.