Consistent multi-query reads
A request needs several independent reads to share one transactional snapshot.
Neon · Module
sql.transaction() executes multiple queries together in one non-interactive transaction. It accepts an array of queries or a synchronous query-producing function, with optional transaction settings.
The query function exposes transaction() for grouping multiple SQL queries into one non-interactive transaction. Pass either an array of tagged-template queries or a synchronous function that receives the query function and returns such an array. Optional transaction settings are supplied in a second argument.
transaction(queriesOrFn, options)| Name | Type | Description |
|---|---|---|
queriesOrFn | Query[] | (sql) => Query[] | An array of SQL queries, or a synchronous function receiving the query function and returning an array of queries. |
options | TransactionOptions | Optional query and transaction configuration, including isolationLevel, readOnly, and deferrable. |
A promise resolving to an array containing the result of each query, in the same order.
import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL);
const [users, posts] = await sql.transaction([
sql`SELECT * FROM users`,
sql`SELECT * FROM posts`
]);Runs both queries as one non-interactive transaction.
A request needs several independent reads to share one transactional snapshot.
Several related statements must be submitted as one HTTP operation.
A read workload needs explicit isolation or read-only behavior.
transaction() groups multiple queries into one non-interactive transaction. Its first argument can be an array of queries or a synchronous function that returns an array of queries.
Follow-up: When should WebSockets be used instead? Use Pool or Client for interactive transactions, sessions, or node-postgres compatibility.
The second argument accepts ordinary query options plus isolationLevel, readOnly, and deferrable. These options configure how the transaction executes.
Follow-up: What combination is required for a deferrable transaction? readOnly must be true and isolationLevel must be Serializable, in addition to deferrable being true.
The HTTP neon() function is intended for single queries and non-interactive transactions. Pool or Client over WebSockets is intended when the transaction needs interaction across multiple steps or session behavior.
Follow-up: Why not use HTTP for an interactive transaction? HTTP executes the submitted batch rather than maintaining the persistent session required for interactive steps.
neon()PoolClientisolationLevelreadOnlydeferrable