SET_TRANSACTION(7pg16)
β¨ SET_TRANSACTION π (PostgreSQL 16.15 edition, bestie!)
okay so picture this: you're in a PostgreSQL 16.15 session (yes, THE PostgreSQL, the icon herself) and you need to control how your transaction behaves. that's literally what SET_TRANSACTION is for. she sets the characteristics of the current transaction. it's giving main character energy for your transaction. 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 like, the SET TRANSACTION command sets the characteristics of the current transaction. that's it. it has no effect on any subsequent transactions, bestie β current only, no carryover, periodt. β¨
SET SESSION CHARACTERISTICS is the other girl β she sets the default transaction characteristics for subsequent transactions of a session. and those defaults? yeah, they can be overridden by SET TRANSACTION for an individual transaction. slay.
the available transaction characteristics are, drumroll please: π₯
- the transaction isolation level
- the transaction access mode (read/write or read-only)
- the deferrable mode
plus, you can select a snapshot, but only for the current transaction β not as a session default. k byee.
πΈ the isolation level tea πΈ
the isolation level of a transaction determines what data the transaction can see when other transactions are running concurrently. this is where it gets juicy:
READ COMMITTEDβ a statement can only see rows committed before it began. this is the default. lowkey 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. consistent vibe check. πSERIALIZABLEβ same visibility asREPEATABLE READ(rows committed before the first query or data-modification statement was executed in this transaction). BUT: 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 aserialization_failureerror. harsh but fair, not me crying π
the SQL standard defines one additional level, READ UNCOMMITTED. in PostgreSQL, READ UNCOMMITTED is treated as READ COMMITTED. so like, they're the same person, different fonts. π
β οΈ real talk: the transaction isolation level cannot be changed after the first query or data-modification statement (SELECT,INSERT,DELETE,UPDATE,MERGE,FETCH, orCOPY) of a transaction has been executed. see Chapter 13 for more info about transaction isolation and concurrency control. no cap, go read it. π
π¦ the access mode π¦
the transaction access mode determines whether the transaction is read/write or read-only. read/write is the default. iconic behavior only. when a transaction is read-only, the following SQL commands are disallowed, and yes i'm listing all of them:
INSERTUPDATEDELETEMERGECOPY FROMif the table they would write to is not a temporary table- all
CREATE,ALTER, andDROPcommands COMMENT,GRANT,REVOKE,TRUNCATEEXPLAIN ANALYZEandEXECUTEif 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 like, it's not a full security guard, she's more of a vibe curator. π
π§ the DEFERRABLE property π§
the DEFERRABLE transaction property has no effect unless the transaction is also SERIALIZABLE and READ ONLY. all three or nothing, bestie. 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. obsessed. π
π the SNAPSHOT moment π
the SET TRANSACTION SNAPSHOT command allows a new transaction to run with the same snapshot as an existing transaction. 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, MERGE, 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. so like, don't even try it. π
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 rules, bestie. π
π NOTES π
- if
SET TRANSACTIONis executed without a priorSTART TRANSACTIONorBEGIN, it emits a warning and otherwise has no effect. so like, she's talking but nobody's listening. π - it is possible to dispense with
SET TRANSACTIONby instead specifying the desiredtransaction_modesinBEGINorSTART TRANSACTION. but that option is not available forSET 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, anddefault_transaction_deferrable. (in factSET SESSION CHARACTERISTICSis just a verbose equivalent for setting these variables withSET.) this means the defaults can be set in the configuration file, viaALTER DATABASE, etc. consult Chapter 20 for more information. π - the current transaction's modes can similarly be set or examined via the configuration parameters
transaction_isolation,transaction_read_only, andtransaction_deferrable. setting one of these parameters acts the same as the correspondingSET TRANSACTIONoption, 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. so like, SQL only, no sneaky config file edits. π
π 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';
slay. now both transactions are on the same wavelength. β¨
π 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, it's fine. π
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 like, we're built different. π¦
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. not our thing, moving on. π
the SQL standard requires commas between successive transaction_modes, but for historical reasons PostgreSQL allows the commas to be omitted. we're flexible like that. π
and that's the whole manual, bestie. no notes. you're now a SET TRANSACTION expert, fr fr. go forth and set those characteristics. periodt. πβ¨
PostgreSQL 16.15 β written 2026, brought to you by the PostgreSQL documentation girlies. π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms