pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix Portal snapshot tracking to handle subtransactions properly.
5+ messages / 1 participants
[nested] [flat]

* pgsql: Fix Portal snapshot tracking to handle subtransactions properly.
@ 2021-10-01 15:10 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2021-10-01 15:10 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix Portal snapshot tracking to handle subtransactions properly.

Commit 84f5c2908 forgot to consider the possibility that
EnsurePortalSnapshotExists could run inside a subtransaction with
lifespan shorter than the Portal's.  In that case, the new active
snapshot would be popped at the end of the subtransaction, leaving
a dangling pointer in the Portal, with mayhem ensuing.

To fix, make sure the ActiveSnapshot stack entry is marked with
the same subtransaction nesting level as the associated Portal.
It's certainly safe to do so since we won't be here at all unless
the stack is empty; hence we can't create an out-of-order stack.

Let's also apply this logic in the case where PortalRunUtility
sets portalSnapshot, just to be sure that path can't cause similar
problems.  It's slightly less clear that that path can't create
an out-of-order stack, so add an assertion guarding it.

Report and patch by Bertrand Drouvot (with kibitzing by me).
Back-patch to v11, like the previous commit.

Discussion: https://postgr.es/m/ff82b8c5-77f4-3fe7-6028-fcf3303e82dd@amazon.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/7b5d4c29ed0262e537026cb3a85161d6cf98abcc

Modified Files
--------------
src/backend/access/transam/xact.c                  |  1 +
src/backend/tcop/pquery.c                          | 27 +++++++++++++++------
src/backend/utils/mmgr/portalmem.c                 |  4 ++++
src/backend/utils/time/snapmgr.c                   | 17 ++++++++++++-
src/include/utils/portal.h                         |  2 ++
src/include/utils/snapmgr.h                        |  1 +
.../plpgsql/src/expected/plpgsql_transaction.out   | 28 ++++++++++++++++++++++
src/pl/plpgsql/src/sql/plpgsql_transaction.sql     | 21 ++++++++++++++++
8 files changed, 93 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix Portal snapshot tracking to handle subtransactions properly.
@ 2021-10-01 15:10 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2021-10-01 15:10 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix Portal snapshot tracking to handle subtransactions properly.

Commit 84f5c2908 forgot to consider the possibility that
EnsurePortalSnapshotExists could run inside a subtransaction with
lifespan shorter than the Portal's.  In that case, the new active
snapshot would be popped at the end of the subtransaction, leaving
a dangling pointer in the Portal, with mayhem ensuing.

To fix, make sure the ActiveSnapshot stack entry is marked with
the same subtransaction nesting level as the associated Portal.
It's certainly safe to do so since we won't be here at all unless
the stack is empty; hence we can't create an out-of-order stack.

Let's also apply this logic in the case where PortalRunUtility
sets portalSnapshot, just to be sure that path can't cause similar
problems.  It's slightly less clear that that path can't create
an out-of-order stack, so add an assertion guarding it.

Report and patch by Bertrand Drouvot (with kibitzing by me).
Back-patch to v11, like the previous commit.

Discussion: https://postgr.es/m/ff82b8c5-77f4-3fe7-6028-fcf3303e82dd@amazon.com

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/e6adaa1795d593edd616b9541fff0920637f9721

Modified Files
--------------
src/backend/access/transam/xact.c                  |  1 +
src/backend/tcop/pquery.c                          | 27 +++++++++++++++------
src/backend/utils/mmgr/portalmem.c                 |  4 ++++
src/backend/utils/time/snapmgr.c                   | 17 ++++++++++++-
src/include/utils/portal.h                         |  4 ++++
src/include/utils/snapmgr.h                        |  1 +
.../plpgsql/src/expected/plpgsql_transaction.out   | 28 ++++++++++++++++++++++
src/pl/plpgsql/src/sql/plpgsql_transaction.sql     | 21 ++++++++++++++++
8 files changed, 95 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix Portal snapshot tracking to handle subtransactions properly.
@ 2021-10-01 15:10 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2021-10-01 15:10 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix Portal snapshot tracking to handle subtransactions properly.

Commit 84f5c2908 forgot to consider the possibility that
EnsurePortalSnapshotExists could run inside a subtransaction with
lifespan shorter than the Portal's.  In that case, the new active
snapshot would be popped at the end of the subtransaction, leaving
a dangling pointer in the Portal, with mayhem ensuing.

To fix, make sure the ActiveSnapshot stack entry is marked with
the same subtransaction nesting level as the associated Portal.
It's certainly safe to do so since we won't be here at all unless
the stack is empty; hence we can't create an out-of-order stack.

Let's also apply this logic in the case where PortalRunUtility
sets portalSnapshot, just to be sure that path can't cause similar
problems.  It's slightly less clear that that path can't create
an out-of-order stack, so add an assertion guarding it.

Report and patch by Bertrand Drouvot (with kibitzing by me).
Back-patch to v11, like the previous commit.

Discussion: https://postgr.es/m/ff82b8c5-77f4-3fe7-6028-fcf3303e82dd@amazon.com

Branch
------
REL_13_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/04ef2021e3ca7207b87ffce437b0b4db73a7f6b2

Modified Files
--------------
src/backend/access/transam/xact.c                  |  1 +
src/backend/tcop/pquery.c                          | 27 +++++++++++++++------
src/backend/utils/mmgr/portalmem.c                 |  4 ++++
src/backend/utils/time/snapmgr.c                   | 17 ++++++++++++-
src/include/utils/portal.h                         |  4 ++++
src/include/utils/snapmgr.h                        |  1 +
.../plpgsql/src/expected/plpgsql_transaction.out   | 28 ++++++++++++++++++++++
src/pl/plpgsql/src/sql/plpgsql_transaction.sql     | 21 ++++++++++++++++
8 files changed, 95 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix Portal snapshot tracking to handle subtransactions properly.
@ 2021-10-01 15:10 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2021-10-01 15:10 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix Portal snapshot tracking to handle subtransactions properly.

Commit 84f5c2908 forgot to consider the possibility that
EnsurePortalSnapshotExists could run inside a subtransaction with
lifespan shorter than the Portal's.  In that case, the new active
snapshot would be popped at the end of the subtransaction, leaving
a dangling pointer in the Portal, with mayhem ensuing.

To fix, make sure the ActiveSnapshot stack entry is marked with
the same subtransaction nesting level as the associated Portal.
It's certainly safe to do so since we won't be here at all unless
the stack is empty; hence we can't create an out-of-order stack.

Let's also apply this logic in the case where PortalRunUtility
sets portalSnapshot, just to be sure that path can't cause similar
problems.  It's slightly less clear that that path can't create
an out-of-order stack, so add an assertion guarding it.

Report and patch by Bertrand Drouvot (with kibitzing by me).
Back-patch to v11, like the previous commit.

Discussion: https://postgr.es/m/ff82b8c5-77f4-3fe7-6028-fcf3303e82dd@amazon.com

Branch
------
REL_12_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/cded2c46099eecc9fc8572bc09675407fb178c44

Modified Files
--------------
src/backend/access/transam/xact.c                  |  1 +
src/backend/tcop/pquery.c                          | 27 +++++++++++++++------
src/backend/utils/mmgr/portalmem.c                 |  4 ++++
src/backend/utils/time/snapmgr.c                   | 17 ++++++++++++-
src/include/utils/portal.h                         |  4 ++++
src/include/utils/snapmgr.h                        |  1 +
.../plpgsql/src/expected/plpgsql_transaction.out   | 28 ++++++++++++++++++++++
src/pl/plpgsql/src/sql/plpgsql_transaction.sql     | 21 ++++++++++++++++
8 files changed, 95 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix Portal snapshot tracking to handle subtransactions properly.
@ 2021-10-01 15:10 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2021-10-01 15:10 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix Portal snapshot tracking to handle subtransactions properly.

Commit 84f5c2908 forgot to consider the possibility that
EnsurePortalSnapshotExists could run inside a subtransaction with
lifespan shorter than the Portal's.  In that case, the new active
snapshot would be popped at the end of the subtransaction, leaving
a dangling pointer in the Portal, with mayhem ensuing.

To fix, make sure the ActiveSnapshot stack entry is marked with
the same subtransaction nesting level as the associated Portal.
It's certainly safe to do so since we won't be here at all unless
the stack is empty; hence we can't create an out-of-order stack.

Let's also apply this logic in the case where PortalRunUtility
sets portalSnapshot, just to be sure that path can't cause similar
problems.  It's slightly less clear that that path can't create
an out-of-order stack, so add an assertion guarding it.

Report and patch by Bertrand Drouvot (with kibitzing by me).
Back-patch to v11, like the previous commit.

Discussion: https://postgr.es/m/ff82b8c5-77f4-3fe7-6028-fcf3303e82dd@amazon.com

Branch
------
REL_11_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/5abbda98594d2cd1f7687577030dbb7a3d1e3d13

Modified Files
--------------
src/backend/access/transam/xact.c                  |  1 +
src/backend/tcop/pquery.c                          | 27 +++++++++++++++------
src/backend/utils/mmgr/portalmem.c                 |  4 ++++
src/backend/utils/time/snapmgr.c                   | 17 ++++++++++++-
src/include/utils/portal.h                         |  4 ++++
src/include/utils/snapmgr.h                        |  1 +
.../plpgsql/src/expected/plpgsql_transaction.out   | 28 ++++++++++++++++++++++
src/pl/plpgsql/src/sql/plpgsql_transaction.sql     | 21 ++++++++++++++++
8 files changed, 95 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2021-10-01 15:10 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-10-01 15:10 pgsql: Fix Portal snapshot tracking to handle subtransactions properly. Tom Lane <tgl@sss.pgh.pa.us>
2021-10-01 15:10 pgsql: Fix Portal snapshot tracking to handle subtransactions properly. Tom Lane <tgl@sss.pgh.pa.us>
2021-10-01 15:10 pgsql: Fix Portal snapshot tracking to handle subtransactions properly. Tom Lane <tgl@sss.pgh.pa.us>
2021-10-01 15:10 pgsql: Fix Portal snapshot tracking to handle subtransactions properly. Tom Lane <tgl@sss.pgh.pa.us>
2021-10-01 15:10 pgsql: Fix Portal snapshot tracking to handle subtransactions properly. Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox