pgsql: Fix subtransaction cleanup after an outer-subtransaction portal

Поиск
Список
Период
Сортировка
От Tom Lane
Тема pgsql: Fix subtransaction cleanup after an outer-subtransaction portal
Дата
Msg-id E1ZXuve-0006q7-9V@gemulon.postgresql.org
обсуждение исходный текст
Список pgsql-committers
Fix subtransaction cleanup after an outer-subtransaction portal fails.

Formerly, we treated only portals created in the current subtransaction as
having failed during subtransaction abort.  However, if the error occurred
while running a portal created in an outer subtransaction (ie, a cursor
declared before the last savepoint), that has to be considered broken too.

To allow reliable detection of which ones those are, add a bookkeeping
field to struct Portal that tracks the innermost subtransaction in which
each portal has actually been executed.  (Without this, we'd end up
failing portals containing functions that had called the subtransaction,
thereby breaking plpgsql exception blocks completely.)

In addition, when we fail an outer-subtransaction Portal, transfer its
resources into the subtransaction's resource owner, so that they're
released early in cleanup of the subxact.  This fixes a problem reported by
Jim Nasby in which a function executed in an outer-subtransaction cursor
could cause an Assert failure or crash by referencing a relation created
within the inner subtransaction.

The proximate cause of the Assert failure is that AtEOSubXact_RelationCache
assumed it could blow away a relcache entry without first checking that the
entry had zero refcount.  That was a bad idea on its own terms, so add such
a check there, and to the similar coding in AtEOXact_RelationCache.  This
provides an independent safety measure in case there are still ways to
provoke the situation despite the Portal-level changes.

This has been broken since subtransactions were invented, so back-patch
to all supported branches.

Tom Lane and Michael Paquier

Branch
------
REL9_4_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/37d10c524c73cebc88ec9c69adc44917a3d67dcd

Modified Files
--------------
src/backend/access/transam/xact.c          |    1 +
src/backend/commands/portalcmds.c          |    6 +-
src/backend/tcop/pquery.c                  |   12 +---
src/backend/utils/cache/relcache.c         |   35 +++++++++++-
src/backend/utils/mmgr/portalmem.c         |   82 +++++++++++++++++++++++++---
src/include/utils/portal.h                 |   18 +++++-
src/test/regress/expected/transactions.out |   46 ++++++++++++++++
src/test/regress/sql/transactions.sql      |   32 +++++++++++
8 files changed, 203 insertions(+), 29 deletions(-)


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

Предыдущее
От: Tom Lane
Дата:
Сообщение: pgsql: Fix subtransaction cleanup after an outer-subtransaction portal
Следующее
От: Tatsuo Ishii
Дата:
Сообщение: pgsql: Fix brin index summarizing while vacuuming.