PREPARE

An SQL command for creating a prepared statement

PREPARE is a utility command for creating a prepared statement.

PREPARE was added in PostgreSQL 7.3.

Usage

PREPARE is used to create a named prepared statement, optionally with a list of parameters specified by position as $1, $2 etc..

The prepared statement created by this command is valid for the current session, and cannot be accessed from other sessions. It is removed automatically when the session ends, or can be manually reoved with the DEALLOCATE command.

The prepared statement can be executed with EXECUTE.

Change history

Examples

Basic usage example for PREPARE:

postgres=# PREPARE foo_ins (INT, TEXT) AS
             INSERT INTO foo VALUES ($1, $2);
PREPARE

postgres=# SELECT * FROM pg_prepared_statements;
-[ RECORD 1 ]---+-----------------------------------
name            | foo_ins
statement       | PREPARE foo_ins (INT, TEXT) AS    +
                |   INSERT INTO foo VALUES ($1, $2);
prepare_time    | 2022-08-04 23:17:04.706349+09
parameter_types | {integer,text}
result_types    | 
from_sql        | t
generic_plans   | 0
custom_plans    | 0

 

  • PostgreSQL documentation: PREPARE

Categories

Utility command

See also

EXECUTE, DEALLOCATE, plan_cache_mode