1PREPARE TRANSACTION(7) PostgreSQL 16.1 Documentation PREPARE TRANSACTION(7)
2
3
4
6 PREPARE_TRANSACTION - prepare the current transaction for two-phase
7 commit
8
10 PREPARE TRANSACTION transaction_id
11
13 PREPARE TRANSACTION prepares the current transaction for two-phase
14 commit. After this command, the transaction is no longer associated
15 with the current session; instead, its state is fully stored on disk,
16 and there is a very high probability that it can be committed
17 successfully, even if a database crash occurs before the commit is
18 requested.
19
20 Once prepared, a transaction can later be committed or rolled back with
21 COMMIT PREPARED or ROLLBACK PREPARED, respectively. Those commands can
22 be issued from any session, not only the one that executed the original
23 transaction.
24
25 From the point of view of the issuing session, PREPARE TRANSACTION is
26 not unlike a ROLLBACK command: after executing it, there is no active
27 current transaction, and the effects of the prepared transaction are no
28 longer visible. (The effects will become visible again if the
29 transaction is committed.)
30
31 If the PREPARE TRANSACTION command fails for any reason, it becomes a
32 ROLLBACK: the current transaction is canceled.
33
35 transaction_id
36 An arbitrary identifier that later identifies this transaction for
37 COMMIT PREPARED or ROLLBACK PREPARED. The identifier must be
38 written as a string literal, and must be less than 200 bytes long.
39 It must not be the same as the identifier used for any currently
40 prepared transaction.
41
43 PREPARE TRANSACTION is not intended for use in applications or
44 interactive sessions. Its purpose is to allow an external transaction
45 manager to perform atomic global transactions across multiple databases
46 or other transactional resources. Unless you're writing a transaction
47 manager, you probably shouldn't be using PREPARE TRANSACTION.
48
49 This command must be used inside a transaction block. Use BEGIN to
50 start one.
51
52 It is not currently allowed to PREPARE a transaction that has executed
53 any operations involving temporary tables or the session's temporary
54 namespace, created any cursors WITH HOLD, or executed LISTEN, UNLISTEN,
55 or NOTIFY. Those features are too tightly tied to the current session
56 to be useful in a transaction to be prepared.
57
58 If the transaction modified any run-time parameters with SET (without
59 the LOCAL option), those effects persist after PREPARE TRANSACTION, and
60 will not be affected by any later COMMIT PREPARED or ROLLBACK PREPARED.
61 Thus, in this one respect PREPARE TRANSACTION acts more like COMMIT
62 than ROLLBACK.
63
64 All currently available prepared transactions are listed in the
65 pg_prepared_xacts system view.
66
67 Caution
68 It is unwise to leave transactions in the prepared state for a long
69 time. This will interfere with the ability of VACUUM to reclaim
70 storage, and in extreme cases could cause the database to shut down
71 to prevent transaction ID wraparound (see Section 25.1.5). Keep in
72 mind also that the transaction continues to hold whatever locks it
73 held. The intended usage of the feature is that a prepared
74 transaction will normally be committed or rolled back as soon as an
75 external transaction manager has verified that other databases are
76 also prepared to commit.
77
78 If you have not set up an external transaction manager to track
79 prepared transactions and ensure they get closed out promptly, it
80 is best to keep the prepared-transaction feature disabled by
81 setting max_prepared_transactions to zero. This will prevent
82 accidental creation of prepared transactions that might then be
83 forgotten and eventually cause problems.
84
86 Prepare the current transaction for two-phase commit, using foobar as
87 the transaction identifier:
88
89 PREPARE TRANSACTION 'foobar';
90
92 PREPARE TRANSACTION is a PostgreSQL extension. It is intended for use
93 by external transaction management systems, some of which are covered
94 by standards (such as X/Open XA), but the SQL side of those systems is
95 not standardized.
96
98 COMMIT PREPARED (COMMIT_PREPARED(7)), ROLLBACK PREPARED
99 (ROLLBACK_PREPARED(7))
100
101
102
103PostgreSQL 16.1 2023 PREPARE TRANSACTION(7)