pgsql: Transaction control in PL procedures

Поиск
Список
Период
Сортировка
От Peter Eisentraut
Тема pgsql: Transaction control in PL procedures
Дата
Msg-id E1edcRB-0004zs-B6@gemulon.postgresql.org
обсуждение исходный текст
Список pgsql-committers
Transaction control in PL procedures

In each of the supplied procedural languages (PL/pgSQL, PL/Perl,
PL/Python, PL/Tcl), add language-specific commit and rollback
functions/commands to control transactions in procedures in that
language.  Add similar underlying functions to SPI.  Some additional
cleanup so that transaction commit or abort doesn't blow away data
structures still used by the procedure call.  Add execution context
tracking to CALL and DO statements so that transaction control commands
can only be issued in top-level procedure and block calls, not function
calls or other procedure or block calls.

- SPI

Add a new function SPI_connect_ext() that is like SPI_connect() but
allows passing option flags.  The only option flag right now is
SPI_OPT_NONATOMIC.  A nonatomic SPI connection can execute transaction
control commands, otherwise it's not allowed.  This is meant to be
passed down from CALL and DO statements which themselves know in which
context they are called.  A nonatomic SPI connection uses different
memory management.  A normal SPI connection allocates its memory in
TopTransactionContext.  For nonatomic connections we use PortalContext
instead.  As the comment in SPI_connect_ext() (previously SPI_connect())
indicates, one could potentially use PortalContext in all cases, but it
seems safest to leave the existing uses alone, because this stuff is
complicated enough already.

SPI also gets new functions SPI_start_transaction(), SPI_commit(), and
SPI_rollback(), which can be used by PLs to implement their transaction
control logic.

- portalmem.c

Some adjustments were made in the code that cleans up portals at
transaction abort.  The portal code could already handle a command
*committing* a transaction and continuing (e.g., VACUUM), but it was not
quite prepared for a command *aborting* a transaction and continuing.

In AtAbort_Portals(), remove the code that marks an active portal as
failed.  As the comment there already predicted, this doesn't work if
the running command wants to keep running after transaction abort.  And
it's actually not necessary, because pquery.c is careful to run all
portal code in a PG_TRY block and explicitly runs MarkPortalFailed() if
there is an exception.  So the code in AtAbort_Portals() is never used
anyway.

In AtAbort_Portals() and AtCleanup_Portals(), we need to be careful not
to clean up active portals too much.  This mirrors similar code in
PreCommit_Portals().

- PL/Perl

Gets new functions spi_commit() and spi_rollback()

- PL/pgSQL

Gets new commands COMMIT and ROLLBACK.

Update the PL/SQL porting example in the documentation to reflect that
transactions are now possible in procedures.

- PL/Python

Gets new functions plpy.commit and plpy.rollback.

- PL/Tcl

Gets new commands commit and rollback.

Reviewed-by: Andrew Dunstan <andrew.dunstan@2ndquadrant.com>

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/8561e4840c81f7e345be2df170839846814fa004

Modified Files
--------------
doc/src/sgml/plperl.sgml                           |  54 +++++
doc/src/sgml/plpgsql.sgml                          |  91 ++++----
doc/src/sgml/plpython.sgml                         |  41 ++++
doc/src/sgml/pltcl.sgml                            |  41 ++++
doc/src/sgml/ref/call.sgml                         |   7 +
doc/src/sgml/ref/create_procedure.sgml             |   7 +
doc/src/sgml/ref/do.sgml                           |   7 +
doc/src/sgml/spi.sgml                              | 177 +++++++++++++++
src/backend/commands/functioncmds.c                |  47 +++-
src/backend/executor/spi.c                         | 102 ++++++++-
src/backend/tcop/utility.c                         |   6 +-
src/backend/utils/mmgr/portalmem.c                 |  49 +++--
src/include/commands/defrem.h                      |   4 +-
src/include/executor/spi.h                         |   7 +
src/include/executor/spi_priv.h                    |   4 +
src/include/nodes/nodes.h                          |   3 +-
src/include/nodes/parsenodes.h                     |   7 +
src/include/utils/portal.h                         |   1 +
src/pl/plperl/GNUmakefile                          |   2 +-
src/pl/plperl/SPI.xs                               |   9 +
src/pl/plperl/expected/plperl_transaction.out      | 133 ++++++++++++
src/pl/plperl/plperl.c                             |  69 +++++-
src/pl/plperl/plperl.h                             |   2 +
src/pl/plperl/sql/plperl_transaction.sql           | 120 ++++++++++
src/pl/plpgsql/src/Makefile                        |   2 +-
.../plpgsql/src/expected/plpgsql_transaction.out   | 241 +++++++++++++++++++++
src/pl/plpgsql/src/pl_exec.c                       |  66 +++++-
src/pl/plpgsql/src/pl_funcs.c                      |  44 ++++
src/pl/plpgsql/src/pl_gram.y                       |  34 +++
src/pl/plpgsql/src/pl_handler.c                    |   9 +-
src/pl/plpgsql/src/pl_scanner.c                    |   2 +
src/pl/plpgsql/src/plpgsql.h                       |  22 +-
src/pl/plpgsql/src/sql/plpgsql_transaction.sql     | 215 ++++++++++++++++++
src/pl/plpython/Makefile                           |   1 +
src/pl/plpython/expected/plpython_test.out         |   4 +-
src/pl/plpython/expected/plpython_transaction.out  | 135 ++++++++++++
src/pl/plpython/plpy_main.c                        |  21 +-
src/pl/plpython/plpy_plpymodule.c                  |  49 +++++
src/pl/plpython/sql/plpython_transaction.sql       | 115 ++++++++++
src/pl/tcl/Makefile                                |   2 +-
src/pl/tcl/expected/pltcl_transaction.out          | 100 +++++++++
src/pl/tcl/pltcl.c                                 |  95 +++++++-
src/pl/tcl/sql/pltcl_transaction.sql               |  98 +++++++++
43 files changed, 2149 insertions(+), 96 deletions(-)


В списке pgsql-committers по дате отправления:

Предыдущее
От: Thomas Munro
Дата:
Сообщение: Re: pgsql: Add parallel-aware hash joins.
Следующее
От: Tom Lane
Дата:
Сообщение: Re: pgsql: Add parallel-aware hash joins.