Executing dynamic statements

Non-PL/pgSQL statements are sent to the server engine as regular SQL statements. A few examples in the previous sections have already performed some queries within code blocks. It is possible for a code block to construct an SQL statement dynamically to be executed by the server engine. This can be achieved using the EXECUTE statement.

An EXECUTE statement is made of three parts:

  • mandatory query string, which represents the SQL statement that will be executed
  • An optional INTO (or INTO STRICT) clause, which allows us to store values from the previous statement in variables
  • An optional USING clause, which provides positional values for parameter substitution

The query string will not be interpolated, which means ...

Get PostgreSQL 11 Server Side Programming Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.