From 280a408b48d5ee42969f981bceb9e9426c3a344c Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Sun, 24 Mar 2019 10:33:14 +0100 Subject: Transaction chaining Add command variants COMMIT AND CHAIN and ROLLBACK AND CHAIN, which start new transactions with the same transaction characteristics as the just finished one, per SQL standard. Support for transaction chaining in PL/pgSQL is also added. This functionality is especially useful when running COMMIT in a loop in PL/pgSQL. Reviewed-by: Fabien COELHO Discussion: https://www.postgresql.org/message-id/flat/28536681-324b-10dc-ade8-ab46f7645a5a@2ndquadrant.com --- doc/src/sgml/plpgsql.sgml | 14 ++++++++++++++ doc/src/sgml/ref/abort.sgml | 14 +++++++++++++- doc/src/sgml/ref/commit.sgml | 23 +++++++++++++++++++---- doc/src/sgml/ref/end.sgml | 14 +++++++++++++- doc/src/sgml/ref/rollback.sgml | 23 +++++++++++++++++++---- doc/src/sgml/spi.sgml | 29 +++++++++++++++++++++++++++-- 6 files changed, 105 insertions(+), 12 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index f8c6435c50e..50f1884afec 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -3490,6 +3490,20 @@ CALL transaction_test1(); + + chained transactions + in PL/pgSQL + + + + A new transaction starts out with default transaction characteristics such + as transaction isolation level. In cases where transactions are committed + in a loop, it might be desirable to start new transactions automatically + with the same characteristics as the previous one. The commands + COMMIT AND CHAIN and ROLLBACK AND + CHAIN accomplish this. + + Transaction control is only possible in CALL or DO invocations from the top level or nested diff --git a/doc/src/sgml/ref/abort.sgml b/doc/src/sgml/ref/abort.sgml index 21799d2a83f..03729133651 100644 --- a/doc/src/sgml/ref/abort.sgml +++ b/doc/src/sgml/ref/abort.sgml @@ -21,7 +21,7 @@ PostgreSQL documentation -ABORT [ WORK | TRANSACTION ] +ABORT [ WORK | TRANSACTION ] [ AND [ NO ] CHAIN ] @@ -51,6 +51,18 @@ ABORT [ WORK | TRANSACTION ] + + + AND CHAIN + + + If AND CHAIN is specified, a new transaction is + immediately started with the same transaction characteristics (see ) as the just finished one. Otherwise, + no new transaction is started. + + + diff --git a/doc/src/sgml/ref/commit.sgml b/doc/src/sgml/ref/commit.sgml index b2e8d5d1807..e4169cd2c62 100644 --- a/doc/src/sgml/ref/commit.sgml +++ b/doc/src/sgml/ref/commit.sgml @@ -21,7 +21,7 @@ PostgreSQL documentation -COMMIT [ WORK | TRANSACTION ] +COMMIT [ WORK | TRANSACTION ] [ AND [ NO ] CHAIN ] @@ -38,6 +38,10 @@ COMMIT [ WORK | TRANSACTION ] Parameters + + chained transactions + + WORK @@ -48,6 +52,18 @@ COMMIT [ WORK | TRANSACTION ] + + + AND CHAIN + + + If AND CHAIN is specified, a new transaction is + immediately started with the same transaction characteristics (see ) as the just finished one. Otherwise, + no new transaction is started. + + + @@ -79,9 +95,8 @@ COMMIT; Compatibility - The SQL standard only specifies the two forms - COMMIT and COMMIT - WORK. Otherwise, this command is fully conforming. + The command COMMIT conforms to the SQL standard. The + form COMMIT TRANSACTION is a PostgreSQL extension. diff --git a/doc/src/sgml/ref/end.sgml b/doc/src/sgml/ref/end.sgml index 7523315f344..8b8f4f0dbb9 100644 --- a/doc/src/sgml/ref/end.sgml +++ b/doc/src/sgml/ref/end.sgml @@ -21,7 +21,7 @@ PostgreSQL documentation -END [ WORK | TRANSACTION ] +END [ WORK | TRANSACTION ] [ AND [ NO ] CHAIN ] @@ -50,6 +50,18 @@ END [ WORK | TRANSACTION ] + + + AND CHAIN + + + If AND CHAIN is specified, a new transaction is + immediately started with the same transaction characteristics (see ) as the just finished one. Otherwise, + no new transaction is started. + + + diff --git a/doc/src/sgml/ref/rollback.sgml b/doc/src/sgml/ref/rollback.sgml index 3cafb848a9a..a5bbf25221c 100644 --- a/doc/src/sgml/ref/rollback.sgml +++ b/doc/src/sgml/ref/rollback.sgml @@ -21,7 +21,7 @@ PostgreSQL documentation -ROLLBACK [ WORK | TRANSACTION ] +ROLLBACK [ WORK | TRANSACTION ] [ AND [ NO ] CHAIN ] @@ -37,6 +37,10 @@ ROLLBACK [ WORK | TRANSACTION ] Parameters + + chained transactions + + WORK @@ -47,6 +51,18 @@ ROLLBACK [ WORK | TRANSACTION ] + + + AND CHAIN + + + If AND CHAIN is specified, a new transaction is + immediately started with the same transaction characteristics (see ) as the just finished one. Otherwise, + no new transaction is started. + + + @@ -78,9 +94,8 @@ ROLLBACK; Compatibility - The SQL standard only specifies the two forms - ROLLBACK and ROLLBACK - WORK. Otherwise, this command is fully conforming. + The command ROLLBACK conforms to the SQL standard. The + form ROLLBACK TRANSACTION is a PostgreSQL extension. diff --git a/doc/src/sgml/spi.sgml b/doc/src/sgml/spi.sgml index 6f4f3bae6ff..9b2f5169421 100644 --- a/doc/src/sgml/spi.sgml +++ b/doc/src/sgml/spi.sgml @@ -4376,6 +4376,7 @@ int SPI_freeplan(SPIPlanPtr plan) SPI_commit + SPI_commit_and_chain SPI_commit @@ -4384,12 +4385,17 @@ int SPI_freeplan(SPIPlanPtr plan) SPI_commit + SPI_commit_and_chain commit the current transaction void SPI_commit(void) + + + +void SPI_commit_and_chain(void) @@ -4406,7 +4412,14 @@ void SPI_commit(void) - This function can only be executed if the SPI connection has been set as + SPI_commit_and_chain is the same, but a new + transaction is immediately started with the same transaction + characteristics as the just finished one, like with the SQL command + COMMIT AND CHAIN. + + + + These functions can only be executed if the SPI connection has been set as nonatomic in the call to SPI_connect_ext. @@ -4416,6 +4429,7 @@ void SPI_commit(void) SPI_rollback + SPI_rollback_and_chain SPI_rollback @@ -4424,12 +4438,17 @@ void SPI_commit(void) SPI_rollback + SPI_rollback_and_chain abort the current transaction void SPI_rollback(void) + + + +void SPI_rollback_and_chain(void) @@ -4444,9 +4463,15 @@ void SPI_rollback(void) using SPI_start_transaction before further database actions can be executed. + + SPI_rollback_and_chain is the same, but a new + transaction is immediately started with the same transaction + characteristics as the just finished one, like with the SQL command + ROLLBACK AND CHAIN. + - This function can only be executed if the SPI connection has been set as + These functions can only be executed if the SPI connection has been set as nonatomic in the call to SPI_connect_ext. -- cgit v1.2.3