SET_TRANSACTION(7pg13)
π SET_TRANSACTION β bestie, let's talk transaction vibes
okay so this is the SET TRANSACTION(7) page from the PostgreSQL 13.23 docs, and it's genuinely the manual page for SET_TRANSACTION β the one that sets the characteristics of the current transaction. no cap, it's lowkey iconic how much control you get here. let's get into it π
β¨ SYNOPSIS β¨
SET TRANSACTION transaction_mode [, ...]
SET TRANSACTION SNAPSHOT snapshot_id
SET SESSION CHARACTERISTICS AS TRANSACTION transaction_mode [, ...]
and transaction_mode is one of these babes:
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. like, the one you're in right now. it does not do anything to any subsequent transactions β it's a one-transaction-influencer, not a trendsetter. meanwhile SET SESSION CHARACTERISTICS sets the default transaction characteristics for subsequent transactions of a session. and those defaults? yeah, they can be overridden by SET TRANSACTION for one individual transaction. main character gets to break the rules π
the transaction characteristics you can pick from are the transaction isolation level, the transaction access mode (read/write or read-only), and the deferrable mode. plus β bonus β a snapshot can be selected, but only for the current transaction, never as a session default. that one's strictly a "this transaction only" situation.
π isolation level β what can you even see
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, and honestly it's the chill one of the group.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. no surprise rows for you, bestie π«Ά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 spicy 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 aserialization_failureerror. it's giving strict teacher energy π but it's for your own good.
also the SQL standard defines one additional level, READ UNCOMMITTED. but in PostgreSQL, READ UNCOMMITTED is treated as READ COMMITTED. so, like, it exists but it's not really existing, yk?
β οΈ the transaction isolation level cannot be changed after the first query or data-modification statement (SELECT,INSERT,DELETE,UPDATE,FETCH, orCOPY) of a transaction has been executed. that ship has sailed, the vibes are locked in.
peep Chapter 13 for more information about transaction isolation and concurrency control, if you want the full lore.
π 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. when a transaction is read-only, the following SQL commands are disallowed:
INSERT,UPDATE,DELETE, andCOPY FROMif the table they would write to is not a temporary table- all
CREATE,ALTER, andDROPcommands COMMENT,GRANT,REVOKE,TRUNCATE- and
EXPLAIN ANALYZEandEXECUTEif the command they would execute is among those listed
ngl this is a high-level notion of read-only β it does not prevent all writes to disk. so don't get it twisted π
π§ DEFERRABLE β the situational icon
the DEFERRABLE transaction property has no effect unless the transaction is also SERIALIZABLE and READ ONLY. all three or it's literally doing nothing.
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. slay, honestly β that's the "leave me alone, I'm doing a big read" mode πΈ
π SET TRANSACTION SNAPSHOT
the SET TRANSACTION SNAPSHOT command allows a new transaction to run with the same snapshot as an existing transaction. cute, right? here's the fine print though:
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. periodt.
π 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 TRANSACTIONis executed without a priorSTART TRANSACTIONorBEGIN, it emits a warning and otherwise has no effect. like talking to a wall, but politely. - it is possible to dispense with
SET TRANSACTIONby instead specifying the desiredtransaction_modes inBEGINorSTART TRANSACTION. but that option is not available forSET TRANSACTION SNAPSHOT. sad but true. - 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 SQL only, no pre-recording π
π 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. chef's kiss π€β¨
β 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. homegrown excellence π
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. we do things our own way, respectfully.
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 if you were looking for it: it's not here, bestie π
the SQL standard requires commas between successive transaction_modes, but for historical reasons PostgreSQL allows the commas to be omitted. we're chill like that. the commas are optional, the vibes are mandatory π¦
okay that's the whole page!! this was SET_TRANSACTION(7) from PostgreSQL 13.23, dated 2025 β go forth and set your transaction characteristics with confidence, you gorgeous thing ππ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-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