pg.ddx.io pgsql-committers@postgresql.org mailing list archive
help / color / mirror / Atom feedpgsql: Account for catalog snapshot in PGXACT->xmin updates.
12+ messages / 4 participants
[nested] [flat]
Commits from this thread:
2017-11-05 bab3a714b6 2168f37c4d 1cac62dac0 7932891ab9 2a2e2e85e0 Ignore CatalogSnapshot when checking COPY FREEZE prerequisites.
* pgsql: Account for catalog snapshot in PGXACT->xmin updates.
@ 2016-11-15 20:55 Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 12+ messages in thread
From: Tom Lane @ 2016-11-15 20:55 UTC (permalink / raw)
To: pgsql-committers
Account for catalog snapshot in PGXACT->xmin updates.
The CatalogSnapshot was not plugged into SnapshotResetXmin()'s accounting
for whether MyPgXact->xmin could be cleared or advanced. In normal
transactions this was masked by the fact that the transaction snapshot
would be older, but during backend startup and certain utility commands
it was possible to re-use the CatalogSnapshot after MyPgXact->xmin had
been cleared, meaning that recently-deleted rows could be pruned even
though this snapshot could still see them, causing unexpected catalog
lookup failures. This effect appears to be the explanation for a recent
failure on buildfarm member piculet.
To fix, add the CatalogSnapshot to the RegisteredSnapshots heap whenever
it is valid.
In the previous logic, it was possible for the CatalogSnapshot to remain
valid across waits for client input, but with this change that would mean
it delays advance of global xmin in cases where it did not before. To
avoid possibly causing new table-bloat problems with clients that sit idle
for long intervals, add code to invalidate the CatalogSnapshot before
waiting for client input. (When the backend is busy, it's unlikely that
the CatalogSnapshot would be the oldest snap for very long, so we don't
worry about forcing early invalidation of it otherwise.)
In passing, remove the CatalogSnapshotStale flag in favor of using
"CatalogSnapshot != NULL" to represent validity, as we do for the other
special snapshots in snapmgr.c. And improve some obsolete comments.
No regression test because I don't know a deterministic way to cause this
failure. But the stress test shown in the original discussion provokes
"cache lookup failed for relation 1255" within a few dozen seconds for me.
Back-patch to 9.4 where MVCC catalog scans were introduced. (Note: it's
quite easy to produce similar failures with the same test case in branches
before 9.4. But MVCC catalog scans were supposed to fix that.)
Discussion: <16447.1478818294@sss.pgh.pa.us>
Branch
------
master
Details
-------
http://git.postgresql.org/pg/commitdiff/ffaa44cb559db332baeee7d25dedd74a61974203
Modified Files
--------------
src/backend/tcop/postgres.c | 8 ++-
src/backend/utils/time/snapmgr.c | 122 ++++++++++++++++++++++++++++-----------
src/include/utils/snapmgr.h | 1 +
3 files changed, 95 insertions(+), 36 deletions(-)
--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers
^ permalink raw reply [nested|flat] 12+ messages in thread
* pgsql: Account for catalog snapshot in PGXACT->xmin updates.
@ 2016-11-15 20:55 Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 12+ messages in thread
From: Tom Lane @ 2016-11-15 20:55 UTC (permalink / raw)
To: pgsql-committers
Account for catalog snapshot in PGXACT->xmin updates.
The CatalogSnapshot was not plugged into SnapshotResetXmin()'s accounting
for whether MyPgXact->xmin could be cleared or advanced. In normal
transactions this was masked by the fact that the transaction snapshot
would be older, but during backend startup and certain utility commands
it was possible to re-use the CatalogSnapshot after MyPgXact->xmin had
been cleared, meaning that recently-deleted rows could be pruned even
though this snapshot could still see them, causing unexpected catalog
lookup failures. This effect appears to be the explanation for a recent
failure on buildfarm member piculet.
To fix, add the CatalogSnapshot to the RegisteredSnapshots heap whenever
it is valid.
In the previous logic, it was possible for the CatalogSnapshot to remain
valid across waits for client input, but with this change that would mean
it delays advance of global xmin in cases where it did not before. To
avoid possibly causing new table-bloat problems with clients that sit idle
for long intervals, add code to invalidate the CatalogSnapshot before
waiting for client input. (When the backend is busy, it's unlikely that
the CatalogSnapshot would be the oldest snap for very long, so we don't
worry about forcing early invalidation of it otherwise.)
In passing, remove the CatalogSnapshotStale flag in favor of using
"CatalogSnapshot != NULL" to represent validity, as we do for the other
special snapshots in snapmgr.c. And improve some obsolete comments.
No regression test because I don't know a deterministic way to cause this
failure. But the stress test shown in the original discussion provokes
"cache lookup failed for relation 1255" within a few dozen seconds for me.
Back-patch to 9.4 where MVCC catalog scans were introduced. (Note: it's
quite easy to produce similar failures with the same test case in branches
before 9.4. But MVCC catalog scans were supposed to fix that.)
Discussion: <16447.1478818294@sss.pgh.pa.us>
Branch
------
REL9_6_STABLE
Details
-------
http://git.postgresql.org/pg/commitdiff/8aa3e47510b969354ea02aff25d2cae7bde9cbf1
Modified Files
--------------
src/backend/tcop/postgres.c | 6 ++
src/backend/utils/time/snapmgr.c | 122 ++++++++++++++++++++++++++++-----------
src/include/utils/snapmgr.h | 1 +
3 files changed, 94 insertions(+), 35 deletions(-)
--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers
^ permalink raw reply [nested|flat] 12+ messages in thread
* pgsql: Account for catalog snapshot in PGXACT->xmin updates.
@ 2016-11-15 20:55 Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 12+ messages in thread
From: Tom Lane @ 2016-11-15 20:55 UTC (permalink / raw)
To: pgsql-committers
Account for catalog snapshot in PGXACT->xmin updates.
The CatalogSnapshot was not plugged into SnapshotResetXmin()'s accounting
for whether MyPgXact->xmin could be cleared or advanced. In normal
transactions this was masked by the fact that the transaction snapshot
would be older, but during backend startup and certain utility commands
it was possible to re-use the CatalogSnapshot after MyPgXact->xmin had
been cleared, meaning that recently-deleted rows could be pruned even
though this snapshot could still see them, causing unexpected catalog
lookup failures. This effect appears to be the explanation for a recent
failure on buildfarm member piculet.
To fix, add the CatalogSnapshot to the RegisteredSnapshots heap whenever
it is valid.
In the previous logic, it was possible for the CatalogSnapshot to remain
valid across waits for client input, but with this change that would mean
it delays advance of global xmin in cases where it did not before. To
avoid possibly causing new table-bloat problems with clients that sit idle
for long intervals, add code to invalidate the CatalogSnapshot before
waiting for client input. (When the backend is busy, it's unlikely that
the CatalogSnapshot would be the oldest snap for very long, so we don't
worry about forcing early invalidation of it otherwise.)
In passing, remove the CatalogSnapshotStale flag in favor of using
"CatalogSnapshot != NULL" to represent validity, as we do for the other
special snapshots in snapmgr.c. And improve some obsolete comments.
No regression test because I don't know a deterministic way to cause this
failure. But the stress test shown in the original discussion provokes
"cache lookup failed for relation 1255" within a few dozen seconds for me.
Back-patch to 9.4 where MVCC catalog scans were introduced. (Note: it's
quite easy to produce similar failures with the same test case in branches
before 9.4. But MVCC catalog scans were supposed to fix that.)
Discussion: <16447.1478818294@sss.pgh.pa.us>
Branch
------
REL9_5_STABLE
Details
-------
http://git.postgresql.org/pg/commitdiff/0bc3ed98c08d01da1138886a06829a93f97c923e
Modified Files
--------------
src/backend/tcop/postgres.c | 6 ++
src/backend/utils/time/snapmgr.c | 124 +++++++++++++++++++++++++++------------
src/include/utils/snapmgr.h | 1 +
3 files changed, 95 insertions(+), 36 deletions(-)
--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers
^ permalink raw reply [nested|flat] 12+ messages in thread
* pgsql: Account for catalog snapshot in PGXACT->xmin updates.
@ 2016-11-15 20:55 Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 14:46 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 12+ messages in thread
From: Tom Lane @ 2016-11-15 20:55 UTC (permalink / raw)
To: pgsql-committers
Account for catalog snapshot in PGXACT->xmin updates.
The CatalogSnapshot was not plugged into SnapshotResetXmin()'s accounting
for whether MyPgXact->xmin could be cleared or advanced. In normal
transactions this was masked by the fact that the transaction snapshot
would be older, but during backend startup and certain utility commands
it was possible to re-use the CatalogSnapshot after MyPgXact->xmin had
been cleared, meaning that recently-deleted rows could be pruned even
though this snapshot could still see them, causing unexpected catalog
lookup failures. This effect appears to be the explanation for a recent
failure on buildfarm member piculet.
To fix, add the CatalogSnapshot to the RegisteredSnapshots heap whenever
it is valid.
In the previous logic, it was possible for the CatalogSnapshot to remain
valid across waits for client input, but with this change that would mean
it delays advance of global xmin in cases where it did not before. To
avoid possibly causing new table-bloat problems with clients that sit idle
for long intervals, add code to invalidate the CatalogSnapshot before
waiting for client input. (When the backend is busy, it's unlikely that
the CatalogSnapshot would be the oldest snap for very long, so we don't
worry about forcing early invalidation of it otherwise.)
In passing, remove the CatalogSnapshotStale flag in favor of using
"CatalogSnapshot != NULL" to represent validity, as we do for the other
special snapshots in snapmgr.c. And improve some obsolete comments.
No regression test because I don't know a deterministic way to cause this
failure. But the stress test shown in the original discussion provokes
"cache lookup failed for relation 1255" within a few dozen seconds for me.
Back-patch to 9.4 where MVCC catalog scans were introduced. (Note: it's
quite easy to produce similar failures with the same test case in branches
before 9.4. But MVCC catalog scans were supposed to fix that.)
Discussion: <16447.1478818294@sss.pgh.pa.us>
Branch
------
REL9_4_STABLE
Details
-------
http://git.postgresql.org/pg/commitdiff/3e844a34b80355570a9cfb25becac561aee7cf82
Modified Files
--------------
src/backend/tcop/postgres.c | 6 +++
src/backend/utils/time/snapmgr.c | 101 ++++++++++++++++++++++++++++-----------
src/include/utils/snapmgr.h | 1 +
3 files changed, 79 insertions(+), 29 deletions(-)
--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates.
2016-11-15 20:55 pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
@ 2016-12-06 14:46 ` Robert Haas <robertmhaas@gmail.com>
2016-12-06 18:36 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 12+ messages in thread
From: Robert Haas @ 2016-12-06 14:46 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
On Tue, Nov 15, 2016 at 3:55 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Account for catalog snapshot in PGXACT->xmin updates.
>
> The CatalogSnapshot was not plugged into SnapshotResetXmin()'s accounting
> for whether MyPgXact->xmin could be cleared or advanced. In normal
> transactions this was masked by the fact that the transaction snapshot
> would be older, but during backend startup and certain utility commands
> it was possible to re-use the CatalogSnapshot after MyPgXact->xmin had
> been cleared, meaning that recently-deleted rows could be pruned even
> though this snapshot could still see them, causing unexpected catalog
> lookup failures. This effect appears to be the explanation for a recent
> failure on buildfarm member piculet.
>
> To fix, add the CatalogSnapshot to the RegisteredSnapshots heap whenever
> it is valid.
It seems to me that this makes it possible for
ThereAreNoPriorRegisteredSnapshots() to fail spuriously. The only
core code that calls that function is in copy.c, and apparently we
never reach that point with the catalog snapshot set. But that seems
fragile.
--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates.
2016-11-15 20:55 pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 14:46 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
@ 2016-12-06 18:36 ` Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 18:59 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 12+ messages in thread
From: Tom Lane @ 2016-12-06 18:36 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
Robert Haas <robertmhaas@gmail.com> writes:
> On Tue, Nov 15, 2016 at 3:55 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Account for catalog snapshot in PGXACT->xmin updates.
> It seems to me that this makes it possible for
> ThereAreNoPriorRegisteredSnapshots() to fail spuriously. The only
> core code that calls that function is in copy.c, and apparently we
> never reach that point with the catalog snapshot set. But that seems
> fragile.
Hm. If there were some documentation explaining what
ThereAreNoPriorRegisteredSnapshots is supposed to do, it would be possible
for us to have a considered argument as to whether this patch broke it or
not. As things stand, it is not obvious whether it ought to be ignoring
the catalog snapshot or not; one could construct plausible arguments
either way. It's not even very obvious why both 0 and 1 registered
snapshots should be considered okay; that looks a lot more like a hack
than reasoned-out behavior. So I'm satisfied if COPY FREEZE does what
it's supposed to do, which it still appears to do.
IOW, I'm not interested in touching this unless someone first provides
adequate documentation for the pre-existing kluge here. There is no
API spec at all on the function itself, and the comment in front of the
one call site is too muddle-headed to provide any insight.
regards, tom lane
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates.
2016-11-15 20:55 pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 14:46 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
2016-12-06 18:36 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
@ 2016-12-06 18:59 ` Robert Haas <robertmhaas@gmail.com>
2017-08-19 19:54 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Noah Misch <noah@leadboat.com>
0 siblings, 1 reply; 12+ messages in thread
From: Robert Haas @ 2016-12-06 18:59 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
On Tue, Dec 6, 2016 at 1:36 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Robert Haas <robertmhaas@gmail.com> writes:
>> On Tue, Nov 15, 2016 at 3:55 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>>> Account for catalog snapshot in PGXACT->xmin updates.
>
>> It seems to me that this makes it possible for
>> ThereAreNoPriorRegisteredSnapshots() to fail spuriously. The only
>> core code that calls that function is in copy.c, and apparently we
>> never reach that point with the catalog snapshot set. But that seems
>> fragile.
>
> Hm. If there were some documentation explaining what
> ThereAreNoPriorRegisteredSnapshots is supposed to do, it would be possible
> for us to have a considered argument as to whether this patch broke it or
> not. As things stand, it is not obvious whether it ought to be ignoring
> the catalog snapshot or not; one could construct plausible arguments
> either way. It's not even very obvious why both 0 and 1 registered
> snapshots should be considered okay; that looks a lot more like a hack
> than reasoned-out behavior. So I'm satisfied if COPY FREEZE does what
> it's supposed to do, which it still appears to do.
>
> IOW, I'm not interested in touching this unless someone first provides
> adequate documentation for the pre-existing kluge here. There is no
> API spec at all on the function itself, and the comment in front of the
> one call site is too muddle-headed to provide any insight.
COPY FREEZE is designed to be usable only in situations where the new
tuples won't be visible earlier than would otherwise be the case.
Thus, copy.c has a check that the relfilenode was created by our
(sub)transaction, which guards against the possibility that other
transactions might see the data early, and also a check that there are
no other registered snapshots or ready portals, which guarantees that,
for example, a cursor belonging to the current subtransaction but with
a lower command-ID doesn't see the rows early. I am fuzzy why we need
to check for both snapshots and portals, but the overall intent seems
pretty clear to me. What are you confused about? It seems quite
obvious to me that no matter how old the catalog snapshot is, it's
nothing that we need to worry about here because it'll get invalidated
and refreshed before use if anything changes meanwhile.
--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates.
2016-11-15 20:55 pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 14:46 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
2016-12-06 18:36 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 18:59 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
@ 2017-08-19 19:54 ` Noah Misch <noah@leadboat.com>
2017-08-21 06:41 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Simon Riggs <simon@2ndquadrant.com>
0 siblings, 1 reply; 12+ messages in thread
From: Noah Misch @ 2017-08-19 19:54 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>; simon@2ndquadrant.com
On Tue, Dec 06, 2016 at 01:59:18PM -0500, Robert Haas wrote:
> On Tue, Dec 6, 2016 at 1:36 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> > Robert Haas <robertmhaas@gmail.com> writes:
> >> On Tue, Nov 15, 2016 at 3:55 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> >>> Account for catalog snapshot in PGXACT->xmin updates.
> >
> >> It seems to me that this makes it possible for
> >> ThereAreNoPriorRegisteredSnapshots() to fail spuriously. The only
> >> core code that calls that function is in copy.c, and apparently we
> >> never reach that point with the catalog snapshot set. But that seems
> >> fragile.
I recently noticed this by way of the copy2 regression test failing, in 9.4
and later, under REPEATABLE READ and SERIALIZABLE. That failure started with
$SUBJECT. Minimal example:
CREATE TABLE vistest (a text);
BEGIN ISOLATION LEVEL REPEATABLE READ;
TRUNCATE vistest;
COPY vistest FROM stdin CSV FREEZE;
x
\.
Before $SUBJECT, the registered snapshot count was one. Since $SUBJECT, the
catalog snapshot raises the count to two.
> > Hm. If there were some documentation explaining what
> > ThereAreNoPriorRegisteredSnapshots is supposed to do, it would be possible
> > for us to have a considered argument as to whether this patch broke it or
> > not. As things stand, it is not obvious whether it ought to be ignoring
> > the catalog snapshot or not; one could construct plausible arguments
The rows COPY FREEZE writes will be visible (until deleted) to every possible
catalog snapshot, so whether CatalogSnapshot==NULL doesn't matter. It may be
useful to error out if a catalog scan is in-flight, but the registration for
CatalogSnapshot doesn't confirm or refute that.
> > either way. It's not even very obvious why both 0 and 1 registered
> > snapshots should be considered okay; that looks a lot more like a hack
> > than reasoned-out behavior. So I'm satisfied if COPY FREEZE does what
Fair summary. ThereAreNoPriorRegisteredSnapshots() has functioned that way
since an early submission[1], and I don't see that we ever discussed it
explicitly. Adding Simon for his recollection. I think the goal was to raise
an error if any scan of the COPY-target table might use an older CommandId;
this prevents a way of seeing tuples that the scan would not see after COPY
without FREEZE. Allowing one registered snapshot was a satisfactory heuristic
at the time. It rejected running plain queries, which have been registering
two snapshots. It did not reject otherwise-idle REPEATABLE READ transactions,
which register FirstXactSnapshot once; that has been okay, because any use of
FirstXactSnapshot in a query pushes or registers a copy. I think the code was
wrong to consider registered snapshots and ignore ActiveSnapshot, though. (We
must ignore COPY's own ActiveSnapshot, but it should be the only one.) I
don't blame $SUBJECT for changing this landscape; COPY should not attribute
meaning to a particular nonzero registered snapshot count.
> > it's supposed to do, which it still appears to do.
> >
> > IOW, I'm not interested in touching this unless someone first provides
> > adequate documentation for the pre-existing kluge here. There is no
> > API spec at all on the function itself, and the comment in front of the
> > one call site is too muddle-headed to provide any insight.
>
> COPY FREEZE is designed to be usable only in situations where the new
> tuples won't be visible earlier than would otherwise be the case.
> Thus, copy.c has a check that the relfilenode was created by our
> (sub)transaction, which guards against the possibility that other
> transactions might see the data early, and also a check that there are
> no other registered snapshots or ready portals, which guarantees that,
> for example, a cursor belonging to the current subtransaction but with
> a lower command-ID doesn't see the rows early. I am fuzzy why we need
> to check for both snapshots and portals, but the overall intent seems
The snapshot check helps in this case:
CREATE TABLE vistest (a text);
BEGIN ISOLATION LEVEL READ COMMITTED;
CREATE FUNCTION copy_vistest() RETURNS void LANGUAGE plpgsql
AS $$BEGIN COPY vistest FROM '/dev/null' CSV FREEZE; END$$;
CREATE FUNCTION count_vistest() RETURNS int LANGUAGE plpgsql STABLE
AS $$BEGIN RETURN (SELECT count(*) FROM vistest); END$$;
TRUNCATE vistest;
SELECT copy_vistest(), count_vistest();
ROLLBACK;
Other sessions still see rows they ideally would not see[2], the same kind of
anomaly the portal and snapshot tests exist to prevent. It's odd that we
protect visibility solely within the COPY transaction, the transaction most
likely to know what it's doing. I'm therefore skeptical of having these tests
at all, but I hesitate to remove them in back branches. Even in master, we
may want them later if we add visibility protection for other transactions.
I'm tempted to add this hack, ...
inline patch 1: src/backend/commands/copy.c (+1/-0 lines) download inline-1.patch
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -2445,2 +2445,3 @@ CopyFrom(CopyState cstate)
{
+ InvalidateCatalogSnapshot();
if (!ThereAreNoPriorRegisteredSnapshots() || !ThereAreNoReadyPortals())
... along with comments and renaming ThereAreNoPriorRegisteredSnapshots().
That restores the pre-$SUBJECT semantics. I suspect those semantics allow
COPY FREEZE more than intended, but again that is mostly harmless. If we want
to make the restrictions more principled, ThereAreNoPriorRegisteredSnapshots()
could give way to a test like this (pseudocode):
n_relevant_registered_snap = cardinality(RegisteredSnapshots)
/* transaction/catalog snapshots are pushed/registered before use in query */
if (FirstXactSnapshot) n_relevant_registered_snap--
if (CatalogSnapshot) n_relevant_registered_snap--
/* expect one ActiveSnapshot for COPY itself */
can_freeze = cardinality(ActiveSnapshot) <= 1 && n_relevant_registered_snap == 0
Preferences?
[1] https://postgr.es/m/CA+U5nMJ8CdApEcm0+Ln0WXVx194k3wJHihb=-0xDHen1v0hhDg@mail.gmail.com
[2] https://postgr.es/m/20121206191823.GW5162@tamriel.snowman.net
> pretty clear to me. What are you confused about? It seems quite
> obvious to me that no matter how old the catalog snapshot is, it's
> nothing that we need to worry about here because it'll get invalidated
> and refreshed before use if anything changes meanwhile.
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates.
2016-11-15 20:55 pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 14:46 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
2016-12-06 18:36 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 18:59 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
2017-08-19 19:54 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Noah Misch <noah@leadboat.com>
@ 2017-08-21 06:41 ` Simon Riggs <simon@2ndquadrant.com>
2017-11-04 07:49 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Noah Misch <noah@leadboat.com>
0 siblings, 1 reply; 12+ messages in thread
From: Simon Riggs @ 2017-08-21 06:41 UTC (permalink / raw)
To: Noah Misch <noah@leadboat.com>; +Cc: Robert Haas <robertmhaas@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
On 19 August 2017 at 20:54, Noah Misch <noah@leadboat.com> wrote:
> On Tue, Dec 06, 2016 at 01:59:18PM -0500, Robert Haas wrote:
>> On Tue, Dec 6, 2016 at 1:36 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> > Robert Haas <robertmhaas@gmail.com> writes:
>> >> On Tue, Nov 15, 2016 at 3:55 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> >>> Account for catalog snapshot in PGXACT->xmin updates.
>> >
>> >> It seems to me that this makes it possible for
>> >> ThereAreNoPriorRegisteredSnapshots() to fail spuriously. The only
>> >> core code that calls that function is in copy.c, and apparently we
>> >> never reach that point with the catalog snapshot set. But that seems
>> >> fragile.
>
> I recently noticed this by way of the copy2 regression test failing, in 9.4
> and later, under REPEATABLE READ and SERIALIZABLE. That failure started with
> $SUBJECT. Minimal example:
>
> CREATE TABLE vistest (a text);
> BEGIN ISOLATION LEVEL REPEATABLE READ;
> TRUNCATE vistest;
> COPY vistest FROM stdin CSV FREEZE;
> x
> \.
>
> Before $SUBJECT, the registered snapshot count was one. Since $SUBJECT, the
> catalog snapshot raises the count to two.
>
>> > Hm. If there were some documentation explaining what
>> > ThereAreNoPriorRegisteredSnapshots is supposed to do, it would be possible
>> > for us to have a considered argument as to whether this patch broke it or
>> > not. As things stand, it is not obvious whether it ought to be ignoring
>> > the catalog snapshot or not; one could construct plausible arguments
>
> The rows COPY FREEZE writes will be visible (until deleted) to every possible
> catalog snapshot, so whether CatalogSnapshot==NULL doesn't matter. It may be
> useful to error out if a catalog scan is in-flight, but the registration for
> CatalogSnapshot doesn't confirm or refute that.
>
>> > either way. It's not even very obvious why both 0 and 1 registered
>> > snapshots should be considered okay; that looks a lot more like a hack
>> > than reasoned-out behavior. So I'm satisfied if COPY FREEZE does what
>
> Fair summary. ThereAreNoPriorRegisteredSnapshots() has functioned that way
> since an early submission[1], and I don't see that we ever discussed it
> explicitly. Adding Simon for his recollection.
The intention, IIRC, was to allow the current snapshot (i.e. 1) but no
earlier (i.e. prior) snapshots (so not >=2)
The name was chosen so it was (hopefully) clear what it did --
ThereAreNoPriorRegisteredSnapshots
...but that was before we had MVCC scans on catalogs, which changed
things in unforeseen ways.
The right fix is surely to do a more principled approach and renaming
of the function so that it reflects the current snapshot types.
As Noah mentions there are potential anomalies in other transactions
but this was understood and hence why we have a specific command
keyword to acknowledge user acceptance of these behaviours.
--
Simon Riggs http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates.
2016-11-15 20:55 pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 14:46 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
2016-12-06 18:36 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 18:59 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
2017-08-19 19:54 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Noah Misch <noah@leadboat.com>
2017-08-21 06:41 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Simon Riggs <simon@2ndquadrant.com>
@ 2017-11-04 07:49 ` Noah Misch <noah@leadboat.com>
2017-11-04 16:23 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 12+ messages in thread
From: Noah Misch @ 2017-11-04 07:49 UTC (permalink / raw)
To: Simon Riggs <simon@2ndquadrant.com>; +Cc: Robert Haas <robertmhaas@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
On Mon, Aug 21, 2017 at 07:41:52AM +0100, Simon Riggs wrote:
> On 19 August 2017 at 20:54, Noah Misch <noah@leadboat.com> wrote:
> > On Tue, Dec 06, 2016 at 01:59:18PM -0500, Robert Haas wrote:
> >> On Tue, Dec 6, 2016 at 1:36 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> >> > Robert Haas <robertmhaas@gmail.com> writes:
> >> >> On Tue, Nov 15, 2016 at 3:55 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> >> >>> Account for catalog snapshot in PGXACT->xmin updates.
> >> >
> >> >> It seems to me that this makes it possible for
> >> >> ThereAreNoPriorRegisteredSnapshots() to fail spuriously. The only
> >> >> core code that calls that function is in copy.c, and apparently we
> >> >> never reach that point with the catalog snapshot set. But that seems
> >> >> fragile.
> >
> > I recently noticed this by way of the copy2 regression test failing, in 9.4
> > and later, under REPEATABLE READ and SERIALIZABLE. That failure started with
> > $SUBJECT.
> > Fair summary. ThereAreNoPriorRegisteredSnapshots() has functioned that way
> > since an early submission[1], and I don't see that we ever discussed it
> > explicitly. Adding Simon for his recollection.
>
> The intention, IIRC, was to allow the current snapshot (i.e. 1) but no
> earlier (i.e. prior) snapshots (so not >=2)
>
> The name was chosen so it was (hopefully) clear what it did --
> ThereAreNoPriorRegisteredSnapshots
I now understand the function name, so I added a comment but didn't rename it.
I plan to use the attached patch after the minor release tags land. If
there's significant support, I could instead push before the wrap.
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Attachments:
ThereAreNoPriorRegisteredSnapshots-CatalogSnapshot-v1.patch (text/plain, 2.9K) download
commit d68eed7 (HEAD, ThereAreNoPriorRegisteredSnapshots-CatalogSnapshot)
Author: Noah Misch <noah@leadboat.com>
AuthorDate: Sat Nov 4 00:08:04 2017 -0700
Commit: Noah Misch <noah@leadboat.com>
CommitDate: Sat Nov 4 00:33:37 2017 -0700
Ignore CatalogSnapshot when checking COPY FREEZE prerequisites.
This restores the ability, essentially lost in commit
ffaa44cb559db332baeee7d25dedd74a61974203, to use COPY FREEZE under
REPEATABLE READ isolation. Back-patch to 9.4, like that commit.
Discussion: https://postgr.es/m/CA+TgmoahWDm-7fperBxzU9uZ99LPMUmEpSXLTw9TmrOgzwnORw@mail.gmail.com
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 8006df3..1bdd492 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -2394,13 +2394,25 @@ CopyFrom(CopyState cstate)
/*
* Optimize if new relfilenode was created in this subxact or one of its
* committed children and we won't see those rows later as part of an
- * earlier scan or command. This ensures that if this subtransaction
- * aborts then the frozen rows won't be visible after xact cleanup. Note
+ * earlier scan or command. The subxact test ensures that if this subxact
+ * aborts then the frozen rows won't be visible after xact cleanup. Note
* that the stronger test of exactly which subtransaction created it is
- * crucial for correctness of this optimization.
+ * crucial for correctness of this optimization. The test for an earlier
+ * scan or command tolerates false negatives. FREEZE causes other sessions
+ * to see rows they would not see under MVCC, and a false negative merely
+ * spreads that anomaly to the current session.
*/
if (cstate->freeze)
{
+ /*
+ * Tolerate one registration for the benefit of FirstXactSnapshot.
+ * Scan-bearing queries generally create at least two registrations,
+ * though relying on that is fragile, as is ignoring ActiveSnapshot.
+ * Clear CatalogSnapshot to avoid counting its registration. We'll
+ * still detect ongoing catalog scans, each of which separately
+ * registers the snapshot it uses.
+ */
+ InvalidateCatalogSnapshot();
if (!ThereAreNoPriorRegisteredSnapshots() || !ThereAreNoReadyPortals())
ereport(ERROR,
(errcode(ERRCODE_INVALID_TRANSACTION_STATE),
diff --git a/src/backend/utils/time/snapmgr.c b/src/backend/utils/time/snapmgr.c
index 294ab70..addf87d 100644
--- a/src/backend/utils/time/snapmgr.c
+++ b/src/backend/utils/time/snapmgr.c
@@ -1645,6 +1645,14 @@ DeleteAllExportedSnapshotFiles(void)
FreeDir(s_dir);
}
+/*
+ * ThereAreNoPriorRegisteredSnapshots
+ * Is the registered snapshot count less than or equal to one?
+ *
+ * Don't use this to settle important decisions. While zero registrations and
+ * no ActiveSnapshot would confirm a certain idleness, the system makes no
+ * guarantees about the significance of one registered snapshot.
+ */
bool
ThereAreNoPriorRegisteredSnapshots(void)
{
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates.
2016-11-15 20:55 pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 14:46 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
2016-12-06 18:36 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 18:59 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
2017-08-19 19:54 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Noah Misch <noah@leadboat.com>
2017-08-21 06:41 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Simon Riggs <simon@2ndquadrant.com>
2017-11-04 07:49 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Noah Misch <noah@leadboat.com>
@ 2017-11-04 16:23 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-11-05 17:40 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Noah Misch <noah@leadboat.com>
0 siblings, 1 reply; 12+ messages in thread
From: Tom Lane @ 2017-11-04 16:23 UTC (permalink / raw)
To: Noah Misch <noah@leadboat.com>; +Cc: Simon Riggs <simon@2ndquadrant.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
Noah Misch <noah@leadboat.com> writes:
> I plan to use the attached patch after the minor release tags land. If
> there's significant support, I could instead push before the wrap.
This looks fine to me --- I think you should push now.
(which reminds me, I'd better get on with making release notes.)
regards, tom lane
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates.
2016-11-15 20:55 pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 14:46 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
2016-12-06 18:36 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 18:59 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Robert Haas <robertmhaas@gmail.com>
2017-08-19 19:54 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Noah Misch <noah@leadboat.com>
2017-08-21 06:41 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Simon Riggs <simon@2ndquadrant.com>
2017-11-04 07:49 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Noah Misch <noah@leadboat.com>
2017-11-04 16:23 ` Re: [COMMITTERS] pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
@ 2017-11-05 17:40 ` Noah Misch <noah@leadboat.com>
0 siblings, 0 replies; 12+ messages in thread
From: Noah Misch @ 2017-11-05 17:40 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Simon Riggs <simon@2ndquadrant.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
On Sat, Nov 04, 2017 at 12:23:36PM -0400, Tom Lane wrote:
> Noah Misch <noah@leadboat.com> writes:
> > I plan to use the attached patch after the minor release tags land. If
> > there's significant support, I could instead push before the wrap.
>
> This looks fine to me --- I think you should push now.
Done.
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 12+ messages in thread
end of thread, other threads:[~2017-11-05 17:40 UTC | newest]
Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-11-15 20:55 pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-11-15 20:55 pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-11-15 20:55 pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-11-15 20:55 pgsql: Account for catalog snapshot in PGXACT->xmin updates. Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 14:46 ` Robert Haas <robertmhaas@gmail.com>
2016-12-06 18:36 ` Tom Lane <tgl@sss.pgh.pa.us>
2016-12-06 18:59 ` Robert Haas <robertmhaas@gmail.com>
2017-08-19 19:54 ` Noah Misch <noah@leadboat.com>
2017-08-21 06:41 ` Simon Riggs <simon@2ndquadrant.com>
2017-11-04 07:49 ` Noah Misch <noah@leadboat.com>
2017-11-04 16:23 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-11-05 17:40 ` Noah Misch <noah@leadboat.com>
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