1PREPARE TRANSACTION() SQL Commands PREPARE TRANSACTION()
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, those
54 effects persist after PREPARE TRANSACTION, and will not be affected by
55 any later COMMIT PREPARED or ROLLBACK PREPARED. Thus, in this one
56 respect PREPARE TRANSACTION acts more like COMMIT than ROLLBACK.
57
58 All currently available prepared transactions are listed in the pg_pre‐
59 pared_xacts system view.
60
61 From a performance standpoint, it is unwise to leave transactions in
62 the prepared state for a long time: this will for instance interfere
63 with the ability of VACUUM to reclaim storage. Keep in mind also that
64 the transaction continues to hold whatever locks it held. The intended
65 usage of the feature is that a prepared transaction will normally be
66 committed or rolled back as soon as an external transaction manager has
67 verified that other databases are also prepared to commit.
68
69 If you make any serious use of prepared transactions, you will probably
70 want to increase the value of max_prepared_transactions, as the default
71 setting is quite small (to avoid wasting resources for those who don't
72 use it). It is recommendable to make it at least equal to max_connec‐
73 tions, so that every session can have a prepared transaction pending.
74
76 Prepare the current transaction for two-phase commit, using foobar as
77 the transaction identifier:
78
79 PREPARE TRANSACTION 'foobar';
80
81
83 COMMIT PREPARED [commit_prepared(7)], ROLLBACK PREPARED [rollback_pre‐
84 pared(l)]
85
86
87
88SQL - Language Statements 2008-06-08 PREPARE TRANSACTION()