]> begriffs open source - ai-pg/blob - full-docs/txt/sql-savepoint.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / sql-savepoint.txt
1
2 SAVEPOINT
3
4    SAVEPOINT — define a new savepoint within the current transaction
5
6 Synopsis
7
8 SAVEPOINT savepoint_name
9
10 Description
11
12    SAVEPOINT establishes a new savepoint within the current transaction.
13
14    A savepoint is a special mark inside a transaction that allows all
15    commands that are executed after it was established to be rolled back,
16    restoring the transaction state to what it was at the time of the
17    savepoint.
18
19 Parameters
20
21    savepoint_name
22           The name to give to the new savepoint. If savepoints with the
23           same name already exist, they will be inaccessible until newer
24           identically-named savepoints are released.
25
26 Notes
27
28    Use ROLLBACK TO to rollback to a savepoint. Use RELEASE SAVEPOINT to
29    destroy a savepoint, keeping the effects of commands executed after it
30    was established.
31
32    Savepoints can only be established when inside a transaction block.
33    There can be multiple savepoints defined within a transaction.
34
35 Examples
36
37    To establish a savepoint and later undo the effects of all commands
38    executed after it was established:
39 BEGIN;
40     INSERT INTO table1 VALUES (1);
41     SAVEPOINT my_savepoint;
42     INSERT INTO table1 VALUES (2);
43     ROLLBACK TO SAVEPOINT my_savepoint;
44     INSERT INTO table1 VALUES (3);
45 COMMIT;
46
47    The above transaction will insert the values 1 and 3, but not 2.
48
49    To establish and later destroy a savepoint:
50 BEGIN;
51     INSERT INTO table1 VALUES (3);
52     SAVEPOINT my_savepoint;
53     INSERT INTO table1 VALUES (4);
54     RELEASE SAVEPOINT my_savepoint;
55 COMMIT;
56
57    The above transaction will insert both 3 and 4.
58
59    To use a single savepoint name:
60 BEGIN;
61     INSERT INTO table1 VALUES (1);
62     SAVEPOINT my_savepoint;
63     INSERT INTO table1 VALUES (2);
64     SAVEPOINT my_savepoint;
65     INSERT INTO table1 VALUES (3);
66
67     -- rollback to the second savepoint
68     ROLLBACK TO SAVEPOINT my_savepoint;
69     SELECT * FROM table1;               -- shows rows 1 and 2
70
71     -- release the second savepoint
72     RELEASE SAVEPOINT my_savepoint;
73
74     -- rollback to the first savepoint
75     ROLLBACK TO SAVEPOINT my_savepoint;
76     SELECT * FROM table1;               -- shows only row 1
77 COMMIT;
78
79    The above transaction shows row 3 being rolled back first, then row 2.
80
81 Compatibility
82
83    SQL requires a savepoint to be destroyed automatically when another
84    savepoint with the same name is established. In PostgreSQL, the old
85    savepoint is kept, though only the more recent one will be used when
86    rolling back or releasing. (Releasing the newer savepoint with RELEASE
87    SAVEPOINT will cause the older one to again become accessible to
88    ROLLBACK TO SAVEPOINT and RELEASE SAVEPOINT.) Otherwise, SAVEPOINT is
89    fully SQL conforming.
90
91 See Also
92
93    BEGIN, COMMIT, RELEASE SAVEPOINT, ROLLBACK, ROLLBACK TO SAVEPOINT