SQL API
The pg_redis extension exposes SQL functions in schema redis so PostgreSQL transactions, stored procedures, and triggers can execute Redis commands in the same transaction as relational tables. Every function returns wire-formatted RESP replies as bytea.
For step-by-step examples, see SQL rollback, Binary keys and values, and Transactional outbox.
Command functions
| Function | Arguments | Description |
|---|---|---|
redis.command(text[], integer DEFAULT 0) | Command and arguments as text[], followed by logical database number (0..15) | Executes one command and returns a RESP2 reply as bytea. |
redis.command_bin(bytea[], integer DEFAULT 0) | Command and arguments as bytea[], followed by logical database number (0..15) | Binary-safe entry point for NUL (\x00) bytes or non-UTF-8 payloads; returns a RESP2 reply as bytea. |
Calling redis.command invokes redis.command_bin internally, so the calling PostgreSQL role requires USAGE ON SCHEMA redis, EXECUTE on both functions, and EXECUTE on the required capability marker (redis._read(), redis._write(), or redis._admin()). See Authentication and permissions for the complete GRANT list.
Errors, savepoints, and transaction scope
- Shared transaction: Both functions execute inside the caller’s
READ COMMITTEDPostgreSQL transaction (REPEATABLE READandSERIALIZABLEare rejected). Even when invoked from a SQLSELECT, a mutating command such asSETwrites rows and requiresredis._write(). - SQLSTATE codes: Redis command errors (such as
WRONGTYPEor syntax errors) raise SQLSTATERD001. Commands blocked byCLIENT PAUSEraise SQLSTATERD002. PostgreSQL engine errors (such as deadlocks or constraint failures) retain their native PostgreSQL SQLSTATE codes. - Savepoint recovery: Because an unhandled
RD001aborts the current SQL transaction block, wrap fallible Redis calls in aSAVEPOINT(or a PL/pgSQLBEGIN ... EXCEPTION WHEN SQLSTATE 'RD001' THEN ... ENDblock) to recover without losing earlier SQL work. - Per-call database index: The
dbparameter applies only to that function call. ExecutingSELECTinsideredis.commanddoes not change the logical database for subsequent SQL calls.
Blocking and Pub/Sub session API
Ordinary SQL calls cannot hold a transaction open while waiting for another transaction to publish a message or push a list item. For blocking commands (BLPOP, BLMOVE, BZPOPMIN, XREAD BLOCK, XREADGROUP BLOCK, WAIT) and Pub/Sub subscriptions across multiple SQL transactions, use the session functions:
| Function | Positional parameters | Return value (bytea) |
|---|---|---|
redis.blocking_begin(bytea[], bytea, integer DEFAULT 0) | args, token, db=0 | Registers a blocking command with RESP2 reply encoding; returns a 2-element RESP2 array [flag, result]. |
redis.blocking_begin(bytea[], bytea, integer, integer) | args, token, db, protocol | Registers a blocking command with explicit reply protocol (2 or 3); returns [flag, result]. |
redis.blocking_poll(bytea, integer DEFAULT 0) | token, db=0 | Polls for completion and renews the lease; returns [flag, result]. |
redis.blocking_cancel(bytea, integer DEFAULT 0) | token, db=0 | Deletes the registration and returns +OK\r\n. |
redis.pubsub_open(bytea, integer DEFAULT 2) | token, protocol=2 | Opens a Pub/Sub session with protocol 2 or 3; returns +OK\r\n. |
redis.pubsub_change(bytea, bytea[]) | token, args | Applies a subscription command (SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, SSUBSCRIBE, etc.); returns [count, frames]. |
redis.pubsub_poll(bytea) | token | Dequeues pending messages in the current transaction; returns [count, frames]. |
redis.pubsub_cancel(bytea) | token | Closes the Pub/Sub session and deletes queued messages; returns +OK\r\n. |
Parameter and lifecycle rules:
tokenis a unique 32-bytebyteagenerated by the caller for each blocking wait or Pub/Sub session. All subsequentpoll,change, andcancelcalls must use the sameLOGINand active role.- For blocking operations,
[flag, result]hasflag = 0(with a null bulk string) while waiting, andflag = 1once fulfilled. Register withblocking_beginand commit first, then callblocking_pollin subsequent transactions. Repeated polls after completion return the fixed result without popping a second element. - For Pub/Sub,
pubsub_polldeletes delivered rows fromredis._pubsub_eventinside the caller’s transaction; rolling back that transaction restores the messages to the queue.
