SET_TRANSACTION(7)
๐ SET_TRANSACTION โ setting the vibe of ur current transaction โจ
okay bestie, so this is the SET TRANSACTION page from the PostgreSQL 18.6 docs. it's giving "how do i tell my transaction how to behave" and honestly? iconic. let's get into it ๐ซถ
๐ธ NAME ๐ธ
SET_TRANSACTION โ literally just: set the characteristics of the current transaction. that's the whole job. no cap. ๐
โจ SYNOPSIS โจ
SET TRANSACTION transaction_mode [, ...]
SET TRANSACTION SNAPSHOT snapshot_id
SET SESSION CHARACTERISTICS AS TRANSACTION transaction_mode [, ...]
and transaction_mode is one of these lil guys:
ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
READ WRITE | READ ONLY
[ NOT ] DEFERRABLE
๐ฆ DESCRIPTION ๐ฆ
so SET TRANSACTION sets the characteristics of the current transaction. key word: current. it does not touch any subsequent transactions, bestie. it's a one-transaction wonder. โจ
meanwhile SET SESSION CHARACTERISTICS sets the default transaction characteristics for subsequent transactions in a session. so it's the "set the vibe for the rest of the night" option. and those defaults can still be overridden by SET TRANSACTION for one individual transaction. ๐
the available transaction characteristics are:
- 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. that's a this-transaction-only kinda deal, fr fr.
๐ง isolation level: what can u even see
the isolation level of a transaction determines what data the transaction can see when other transactions are running concurrently. so basically: how much chaos from other people's queries are you exposed to.
READ COMMITTEDโ a statement can only see rows committed before it began. this is the default. yes, the default is 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, 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. so like, somebody's getting cancelled and it might be you ๐ญ
the SQL standard defines one additional level, READ UNCOMMITTED. in PostgreSQL, READ UNCOMMITTED is treated as READ COMMITTED. so it's the fake one. it's giving costume jewelry. ๐
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. so decide early or don't decide at all, bestie.
see Chapter 13 for more information about transaction isolation and concurrency control. ๐
๐ access mode: read/write or read-only
the transaction access mode determines whether the transaction is read/write or read-only. read/write is the default. periodt.
when a transaction is read-only, these SQL commands are disallowed:
INSERT,UPDATE,DELETE,MERGE, andCOPY FROMโ if the table they would write to is not a temporary table- all
CREATE,ALTER, andDROPcommands COMMENT,GRANT,REVOKE,TRUNCATEEXPLAIN ANALYZEandEXECUTEโ if the command they would execute is among those listed
heads up: this is a high-level notion of read-only that does not prevent all writes to disk. so it's not a security boundary, it's a vibe boundary. ๐
๐ the DEFERRABLE thing
the DEFERRABLE transaction property has no effect unless the transaction is also SERIALIZABLE and READ ONLY. all three or it's 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. slay. โจ
this mode is well suited for long-running reports or backups. the "i'm just gonna sit here and read everything for an hour" mode. ๐
๐ซถ SET TRANSACTION SNAPSHOT
the SET TRANSACTION SNAPSHOT command allows a new transaction to run with the same snapshot as an existing transaction. matching snapshots, matching vibes. ๐ฆ
- the pre-existing transaction must have exported its snapshot with the
pg_export_snapshotfunction (see Section 9.28.5) - that function returns a snapshot identifier, which must be given to
SET TRANSACTION SNAPSHOTto 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 SNAPSHOTcan only be executed at the start of a transaction, before the first query or data-modification statement (SELECT,INSERT,DELETE,UPDATE,MERGE,FETCH, orCOPY) of the transaction- furthermore, the transaction must already be set to
SERIALIZABLEorREPEATABLE READisolation level โ otherwise, the snapshot would be discarded immediately, sinceREAD COMMITTEDmode takes a new snapshot for each command - if the importing transaction uses
SERIALIZABLEisolation 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
๐ NOTES ๐
- if
SET TRANSACTIONis executed without a priorSTART TRANSACTIONorBEGIN, it emits a warning and otherwise has no effect. so it's just gonna yell into the void. ๐ญ - it is possible to dispense with
SET TRANSACTIONby instead specifying the desiredtransaction_modesinBEGINorSTART TRANSACTION. but that option is not available forSET TRANSACTION SNAPSHOT. - 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 19 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. live only, bestie. ๐
โญ 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 that. two transactions, one shared snapshot. main character energy. ๐
๐ฅ COMPATIBILITY ๐ฅ
- these commands are defined in the SQL standard, except for the
DEFERRABLEtransaction mode and theSET TRANSACTION SNAPSHOTform, which are PostgreSQL extensions. homegrown. ๐ฑ SERIALIZABLEis the default transaction isolation level in the standard. in PostgreSQL the default is ordinarilyREAD COMMITTED, but you can change it as mentioned above.- 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. so it's just not here. ๐
- the SQL standard requires commas between successive
transaction_modes, but for historical reasons PostgreSQL allows the commas to be omitted. the commas are optional, the drama is not. โจ
and that's the whole page, bestie!! this was PostgreSQL 18.6, documented back in 2026, and you just read SET TRANSACTION(7). go forth and set ur transaction characteristics responsibly. mwah ๐๐๐ฆ
๐ฆ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 ยท PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 ยท PostgreSQL
- arch rolling: postgresql 18.6-4 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-client-18 18.6-0ubuntu0.26.04.1 ยท PostgreSQL
- ubuntu stonking: postgresql-client-18 18.6-3build2 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms