1PREPARE TRANSACTION(7) SQL Commands PREPARE TRANSACTION(7)
2
3
4
6 PREPARE TRANSACTION - prepare the current transaction for two-phase
7 commit
8
9
11 PREPARE TRANSACTION transaction_id
12
13
15 PREPARE TRANSACTION prepares the current transaction for two-phase com‐
16 mit. After this command, the transaction is no longer associated with
17 the current session; instead, its state is fully stored on disk, and
18 there is a very high probability that it can be committed successfully,
19 even if a database crash occurs before the commit is requested.
20
21 Once prepared, a transaction can later be committed or rolled back with
22 COMMIT PREPARED [commit_prepared(7)] or ROLLBACK PREPARED [roll‐
23 back_prepared(7)], respectively. Those commands can be issued from any
24 session, not only the one that executed the original transaction.
25
26 From the point of view of the issuing session, PREPARE TRANSACTION is
27 not unlike a ROLLBACK command: after executing it, there is no active
28 current transaction, and the effects of the prepared transaction are no
29 longer visible. (The effects will become visible again if the transac‐
30 tion is committed.)
31
32 If the PREPARE TRANSACTION command fails for any reason, it becomes a
33 ROLLBACK: the current transaction is canceled.
34
36 transaction_id
37 An arbitrary identifier that later identifies this transaction
38 for COMMIT PREPARED or ROLLBACK PREPARED. The identifier must
39 be written as a string literal, and must be less than 200 bytes
40 long. It must not be the same as the identifier used for any
41 currently prepared transaction.
42
44 This command must be used inside a transaction block. Use BEGIN
45 [begin(7)] to start one.
46
47 It is not currently allowed to PREPARE a transaction that has executed
48 any operations involving temporary tables, created any cursors WITH
49 HOLD, or executed LISTEN or UNLISTEN. Those features are too tightly
50 tied to the current session to be useful in a transaction to be pre‐
51 pared.
52
53 If the transaction modified any run-time parameters with SET (without
54 the LOCAL option), those effects persist after PREPARE TRANSACTION, and
55 will not be affected by any later COMMIT PREPARED or ROLLBACK PREPARED.
56 Thus, in this one respect PREPARE TRANSACTION acts more like COMMIT
57 than ROLLBACK.
58
59 All currently available prepared transactions are listed in the pg_pre‐
60 pared_xacts system view.
61
62 Caution:
63
64 It is unwise to leave transactions in the prepared state for a
65 long time. This will interfere with the ability of VACUUM to
66 reclaim storage, and in extreme cases could cause the database
67 to shut down to prevent transaction ID wraparound (see in the
68 documentation). Keep in mind also that the transaction continues
69 to hold whatever locks it held. The intended usage of the fea‐
70 ture is that a prepared transaction will normally be committed
71 or rolled back as soon as an external transaction manager has
72 verified that other databases are also prepared to commit.
73
74 If you have not set up an external transaction manager to track
75 prepared transactions and ensure they get closed out promptly,
76 it is best to keep the prepared-transaction feature disabled by
77 setting max_prepared_transactions to zero. This will prevent
78 accidental creation of prepared transactions that might then be
79 forgotten and eventually cause problems.
80
81
83 Prepare the current transaction for two-phase commit, using foobar as
84 the transaction identifier:
85
86 PREPARE TRANSACTION 'foobar';
87
88
90 COMMIT PREPARED [commit_prepared(7)], ROLLBACK PREPARED [rollback_pre‐
91 pared(7)]
92
93
94
95SQL - Language Statements 2014-02-17 PREPARE TRANSACTION(7)