SET_TRANSACTION(7pg14)

πŸ“¦ version 14.24, in opensuse-leap 16.0

✨ SET_TRANSACTION πŸ’…

okay so bestie, this is the PostgreSQL 14.24 documentation for the SET TRANSACTION command, and honestly? it's giving main character energy for your database transactions. we're talking isolation levels, read/write modes, snapshots, the whole vibe check. let's get into it 🫢


🌸 SYNOPSIS 🌸

SET TRANSACTION transaction_mode [, ...]
SET TRANSACTION SNAPSHOT snapshot_id
SET SESSION CHARACTERISTICS AS TRANSACTION transaction_mode [, ...]

where transaction_mode is one of:

ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
READ WRITE | READ ONLY
[ NOT ] DEFERRABLE

πŸ’– DESCRIPTION πŸ’–

so the SET TRANSACTION command sets the characteristics of the current transaction. it has no effect on any subsequent transactions. periodt. meanwhile, SET SESSION CHARACTERISTICS sets the default transaction characteristics for subsequent transactions of a session. these defaults can be overridden by SET TRANSACTION for an individual transaction β€” lowkey iconic how they layer like that.

the available transaction characteristics are the transaction isolation level, the transaction access mode (read/write or read-only), and the deferrable mode. in addition, a snapshot can be selected, though only for the current transaction, not as a session default. no cap.

πŸ¦‹ isolation levels, explained

the isolation level of a transaction determines what data the transaction can see when other transactions are running concurrently:

  • READ COMMITTED β€” a statement can only see rows committed before it began. this is the default. ngl it's the chill one.
  • REPEATABLE READ β€” all statements of the current transaction can only see rows committed before the first query or data-modification statement was executed in this transaction.
  • SERIALIZABLE β€” all statements of the current transaction can only see rows committed before the first query or data-modification statement was executed in this transaction. and here's the dramatic part: if a pattern of reads and writes among concurrent serializable transactions would create a situation which could not have occurred for any serial (one-at-a-time) execution of those transactions, one of them will be rolled back with a serialization_failure error. πŸ’€ so serious.

the SQL standard defines one additional level, READ UNCOMMITTED. in PostgreSQL, READ UNCOMMITTED is treated as READ COMMITTED. not us pretending that level doesn't exist, but also... yes exactly that.

⭐ the transaction isolation level cannot be changed after the first query or data-modification statement (SELECT, INSERT, DELETE, UPDATE, FETCH, or COPY) of a transaction has been executed. like, the door is closed, bestie. see Chapter 13 for more information about transaction isolation and concurrency control.

πŸ’… access mode

the transaction access mode determines whether the transaction is read/write or read-only. read/write is the default.

when a transaction is read-only, the following SQL commands are disallowed:

  • INSERT, UPDATE, DELETE, and COPY FROM if the table they would write to is not a temporary table
  • all CREATE, ALTER, and DROP commands
  • COMMENT, GRANT, REVOKE, TRUNCATE
  • and EXPLAIN ANALYZE and EXECUTE if the command they would execute is among those listed

this is a high-level notion of read-only that does not prevent all writes to disk. so don't @ me, it's not a full lockdown, it's more of a vibe restriction ✨.

πŸŽ€ the DEFERRABLE property

the DEFERRABLE transaction property has no effect unless the transaction is also SERIALIZABLE and READ ONLY. like, all three or it's not happening.

when all three of these properties are selected for a transaction, the transaction may block when first acquiring its snapshot, after which it is able to run without the normal overhead of a SERIALIZABLE transaction and without any risk of contributing to or being canceled by a serialization failure. this mode is well suited for long-running reports or backups. honestly? chef's kiss for that use case. πŸ‘‘

🧚 SET TRANSACTION SNAPSHOT

the SET TRANSACTION SNAPSHOT command allows a new transaction to run with the same snapshot as an existing transaction. no way, right?

the pre-existing transaction must have exported its snapshot with the pg_export_snapshot function (see Section 9.27.5). that function returns a snapshot identifier, which must be given to SET TRANSACTION SNAPSHOT to specify which snapshot is to be imported. the identifier must be written as a string literal in this command, for example '00000003-0000001B-1'.

⚠️ SET TRANSACTION SNAPSHOT can only be executed at the start of a transaction, before the first query or data-modification statement (SELECT, INSERT, DELETE, UPDATE, FETCH, or COPY) of the transaction.

furthermore, the transaction must already be set to SERIALIZABLE or REPEATABLE READ isolation level (otherwise, the snapshot would be discarded immediately, since READ COMMITTED mode takes a new snapshot for each command β€” it's just how she operates).

if the importing transaction uses SERIALIZABLE isolation level, then the transaction that exported the snapshot must also use that isolation level. also, a non-read-only serializable transaction cannot import a snapshot from a read-only transaction. the rules are the rules, bestie. πŸ“œ


🌈 NOTES 🌈

if SET TRANSACTION is executed without a prior START TRANSACTION or BEGIN, it emits a warning and otherwise has no effect. so like, it's shouting into the void. 😭

it is possible to dispense with SET TRANSACTION by instead specifying the desired transaction_modes in BEGIN or START TRANSACTION. but that option is not available for SET TRANSACTION SNAPSHOT. fyi.

the session default transaction modes can also be set or examined via the configuration parameters default_transaction_isolation, default_transaction_read_only, and default_transaction_deferrable. (in fact SET SESSION CHARACTERISTICS is just a verbose equivalent for setting these variables with SET.) this means the defaults can be set in the configuration file, via ALTER DATABASE, etc. consult Chapter 20 for more information. iykyk.

the current transaction's modes can similarly be set or examined via the configuration parameters transaction_isolation, transaction_read_only, and transaction_deferrable. setting one of these parameters acts the same as the corresponding SET TRANSACTION option, with the same restrictions on when it can be done. however, these parameters cannot be set in the configuration file, or from any source other than live SQL. live SQL only, no pre-recorded vibes allowed. 🎬


πŸ“ EXAMPLES πŸ“

to begin a new transaction with the same snapshot as an already existing transaction, first export the snapshot from the existing transaction. that will return the snapshot identifier, for example:

BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT pg_export_snapshot();
 pg_export_snapshot
---------------------
 00000003-0000001B-1
(1 row)

then give the snapshot identifier in a SET TRANSACTION SNAPSHOT command at the beginning of the newly opened transaction:

BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SET TRANSACTION SNAPSHOT '00000003-0000001B-1';

look at us, creating matching-snapshot sibling transactions. iconic behavior only. πŸ’–


πŸ‘‘ COMPATIBILITY πŸ‘‘

these commands are defined in the SQL standard, except for the DEFERRABLE transaction mode and the SET TRANSACTION SNAPSHOT form, which are PostgreSQL extensions. we do a little extra, as a treat.

SERIALIZABLE is the default transaction isolation level in the standard. in PostgreSQL the default is ordinarily READ COMMITTED, but you can change it as mentioned above. so we're a little different, but in a cute way.

in the SQL standard, there is one other transaction characteristic that can be set with these commands: the size of the diagnostics area. this concept is specific to embedded SQL, and therefore is not implemented in the PostgreSQL server. we simply do not do that here. πŸ™…β€β™€οΈ

the SQL standard requires commas between successive transaction_modes, but for historical reasons PostgreSQL allows the commas to be omitted. an icon of flexibility, honestly. ✨


🌈 that's the whole vibe from the PostgreSQL 14.24 documentation, dated 2026, bestie β€” go forth and transact accordingly πŸ’…πŸ’–

πŸ“¦ where this page comes from