pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14691: Isolation failure in deferrable transaction concurrent with schema change
5+ messages / 4 participants
[nested] [flat]

* BUG #14691: Isolation failure in deferrable transaction concurrent with schema change
@ 2017-06-05 19:11 cpacejo@clearskydata.com
  2017-07-17 17:08 ` Re: BUG #14691: Isolation failure in deferrable transaction concurrent with schema change David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: cpacejo@clearskydata.com @ 2017-06-05 19:11 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      14691
Logged by:          Chris Pacejo
Email address:      cpacejo@clearskydata.com
PostgreSQL version: 9.5.7
Operating system:   CentOS 7
Description:        

Hi, issuing a SERIALIZABLE DEFERRABLE READ ONLY read from a table to which a
column is being added results in an isolation failure.

Example 1:

Session A (setup):

me=> CREATE TABLE foo(x integer);
CREATE TABLE
me=> INSERT INTO foo VALUES (1), (2), (3);
INSERT 0 3


Session B (normal read):

me=> BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE; TABLE foo;
COMMIT;
BEGIN
 x 
---
 1
 2
 3
(3 rows)

COMMIT


Session A (add column but do not commit):

me=> BEGIN; ALTER TABLE foo ADD COLUMN y integer DEFAULT 0;
BEGIN
ALTER TABLE


Session B (stalled read):

me=> BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE; TABLE foo;
COMMIT;
BEGIN


Session A (commit added column):

me=> COMMIT;
COMMIT


Session B (stalled read returns empty table; a second read is again
correct):

 x | y 
---+---
(0 rows)

COMMIT
me=> BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE; TABLE foo;
COMMIT;
BEGIN
 x | y 
---+---
 1 | 0
 2 | 0
 3 | 0
(3 rows)

COMMIT



Example 2:

Session A (setup):

me=> CREATE TABLE foo(x integer);
CREATE TABLE
me=> INSERT INTO foo VALUES (1), (2), (3);
INSERT 0 3


Session B (normal read):

me=> BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE; TABLE foo;
COMMIT;
BEGIN
 x 
---
 1
 2
 3
(3 rows)

COMMIT


Session A (add column but do not commit):

me=> BEGIN; ALTER TABLE foo ADD COLUMN y integer; UPDATE foo SET y = 0;
BEGIN
ALTER TABLE
UPDATE 3


Session B (stalled read):

me=> BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE; TABLE foo;
COMMIT;
BEGIN


Session A (commit added column):

me=> COMMIT;
COMMIT


Session B (stalled read returns empty column; a second read is again
correct):

 x | y 
---+---
 1 |  
 2 |  
 3 |  
(3 rows)

COMMIT
me=> BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE; TABLE foo;
COMMIT;
BEGIN
 x | y 
---+---
 1 | 0
 2 | 0
 3 | 0
(3 rows)

COMMIT


I would expect the "middle" result of each example (with missing data) not
to be visible at any isolation level.

This does not occur if the schema-modifying transaction is aborted, if a
column is deleted, with a view, or if a non-deferrable read transaction is
used.

Please let me know if there is any additional information I can give or if I
misunderstand the deferrable transaction mechanism.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


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

* Re: BUG #14691: Isolation failure in deferrable transaction concurrent with schema change
  2017-06-05 19:11 BUG #14691: Isolation failure in deferrable transaction concurrent with schema change cpacejo@clearskydata.com
@ 2017-07-17 17:08 ` David G. Johnston <david.g.johnston@gmail.com>
  2017-07-17 19:35   ` Re: BUG #14691: Isolation failure in deferrable transaction concurrent with schema change Thomas Munro <thomas.munro@enterprisedb.com>
  2017-07-19 17:39   ` Re: BUG #14691: Isolation failure in deferrable transaction concurrent with schema change Chris Pacejo <cpacejo@clearskydata.com>
  0 siblings, 2 replies; 5+ messages in thread

From: David G. Johnston @ 2017-07-17 17:08 UTC (permalink / raw)
  To: cpacejo@clearskydata.com; +Cc: pgsql-bugs

On Mon, Jun 5, 2017 at 12:11 PM, <cpacejo@clearskydata.com> wrote:

> The following bug has been logged on the website:
>
> Bug reference:      14691
> Logged by:          Chris Pacejo
> Email address:      cpacejo@clearskydata.com
> PostgreSQL version: 9.5.7
> Operating system:   CentOS 7
> Description:
>
> Hi, issuing a SERIALIZABLE DEFERRABLE READ ONLY read from a table to which
> a
> column is being added results in an isolation failure.
>


> me=> BEGIN; ALTER TABLE foo ADD COLUMN y integer DEFAULT 0;
>


> me=> BEGIN; ALTER TABLE foo ADD COLUMN y integer; UPDATE foo SET y = 0;
>


>  x | y
> ---+---
>  1 |
>  2 |
>  3 |
> (3 rows)
>

​This is a documented limitation.

​https://www.postgresql.org/docs/9.5/static/mvcc-caveats.html

"Some DDL commands, currently only TRUNCATE and the table-rewriting forms
of ALTER TABLE, are not MVCC-safe."

The first command is "table-rewriting".

The second ALTER TABLE doesn't rewrite the table but does change its
structure - so you get rows but the newly added column is null.  This would
seem to be a reasonable behavior.

David J.

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

* Re: BUG #14691: Isolation failure in deferrable transaction concurrent with schema change
  2017-06-05 19:11 BUG #14691: Isolation failure in deferrable transaction concurrent with schema change cpacejo@clearskydata.com
  2017-07-17 17:08 ` Re: BUG #14691: Isolation failure in deferrable transaction concurrent with schema change David G. Johnston <david.g.johnston@gmail.com>
@ 2017-07-17 19:35   ` Thomas Munro <thomas.munro@enterprisedb.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Thomas Munro @ 2017-07-17 19:35 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-bugs

On Tue, Jul 18, 2017 at 5:08 AM, David G. Johnston
<david.g.johnston@gmail.com> wrote:
> On Mon, Jun 5, 2017 at 12:11 PM, <cpacejo@clearskydata.com> wrote:
>>
>> The following bug has been logged on the website:
>>
>> Bug reference:      14691
>> Logged by:          Chris Pacejo
>> Email address:      cpacejo@clearskydata.com
>> PostgreSQL version: 9.5.7
>> Operating system:   CentOS 7
>> Description:
>>
>> Hi, issuing a SERIALIZABLE DEFERRABLE READ ONLY read from a table to which
>> a
>> column is being added results in an isolation failure.
>
>
>>
>> me=> BEGIN; ALTER TABLE foo ADD COLUMN y integer DEFAULT 0;
>
>
>>
>> me=> BEGIN; ALTER TABLE foo ADD COLUMN y integer; UPDATE foo SET y = 0;
>
>
>>
>>  x | y
>> ---+---
>>  1 |
>>  2 |
>>  3 |
>> (3 rows)
>
>
> This is a documented limitation.
>
> https://www.postgresql.org/docs/9.5/static/mvcc-caveats.html
>
> "Some DDL commands, currently only TRUNCATE and the table-rewriting forms of
> ALTER TABLE, are not MVCC-safe."
>
> The first command is "table-rewriting".
>
> The second ALTER TABLE doesn't rewrite the table but does change its
> structure - so you get rows but the newly added column is null.  This would
> seem to be a reasonable behavior.

It is quite weird that you see the new column (DDL effects) but not
the new values (DML effects) that were produced by the same
transaction.  That happens even if the altering session runs in
SERIALIZABLE.  There are defences against seeing anything weird when a
concurrent serializable transaction TRUNCATEs (see the call to
CheckTableForSerializableConflictIn(rel)), but those defences don't
simply nuke concurrent transactions: they use the usual SSI algorithm
so that the schedule might be allowed.

The same approach won't work here, as the attached patch shows.  If we
add a call to CheckTableForSerializableConflict(rel) in ATExecCmd (one
of the ALTER TABLE workhorse functions) then I think a conflict
between the overlapping transactions is detected, but there is no
cycle: the snapshot is judged safe and appears to run before the
ALTER, even though it sees the table definition with the new column,
ie after the ALTER.  To fix that maybe we'd need to do SSI on system
catalogs...  Then the attached patch should cause a serialization
failure in the second permutation, because a cycle would exist between
s2 and s3 (s2 read a catalog entry that s3 wrote, and s3 wrote to the
whole relation by running ALTER TABLE and s2 read the whole table).

-- 
Thomas Munro
http://www.enterprisedb.com

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

experiment.patch (application/octet-stream, 3.1K) download diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index bb00858ad1..c1774f854d 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -4061,6 +4061,9 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel, break; } + /* Experiment for bug report 14691. This doesn't do anything useful. */ + CheckTableForSerializableConflictIn(rel); + /* * Report the subcommand to interested event triggers. */ diff --git a/src/test/isolation/expected/add-column-concurrently.out b/src/test/isolation/expected/add-column-concurrently.out new file mode 100644 index 0000000000..ec70adadb1 --- /dev/null +++ b/src/test/isolation/expected/add-column-concurrently.out @@ -0,0 +1,31 @@ +Parsed test spec with 3 sessions + +starting permutation: s1b s1a s1u s2b s2s s1c s2c +step s1b: BEGIN ISOLATION LEVEL READ COMMITTED; +step s1a: ALTER TABLE foo ADD COLUMN y integer; +step s1u: UPDATE foo SET y = 0; +step s2b: BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE; +step s2s: TABLE foo; <waiting ...> +step s1c: COMMIT; +step s2s: <... completed> +x y + +1 +2 +3 +step s2c: COMMIT; + +starting permutation: s3b s3a s3u s2b s2s s3c s2c +step s3b: BEGIN ISOLATION LEVEL SERIALIZABLE; +step s3a: ALTER TABLE foo ADD COLUMN y integer; +step s3u: UPDATE foo SET y = 0; +step s2b: BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE; +step s2s: TABLE foo; <waiting ...> +step s3c: COMMIT; +step s2s: <... completed> +x y + +1 +2 +3 +step s2c: COMMIT; diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule index 32c965b2a0..33e9ff80bf 100644 --- a/src/test/isolation/isolation_schedule +++ b/src/test/isolation/isolation_schedule @@ -1,3 +1,4 @@ +test: add-column-concurrently test: read-only-anomaly test: read-only-anomaly-2 test: read-only-anomaly-3 diff --git a/src/test/isolation/specs/add-column-concurrently.spec b/src/test/isolation/specs/add-column-concurrently.spec new file mode 100644 index 0000000000..98b54c9274 --- /dev/null +++ b/src/test/isolation/specs/add-column-concurrently.spec @@ -0,0 +1,34 @@ +# ADD COLUMN CONCURRENTLY +# +# Example from bug report 14691. + +setup +{ + CREATE TABLE foo(x integer); + INSERT INTO foo VALUES (1), (2), (3); +} + +teardown +{ + DROP TABLE foo; +} + +session "s1" +step "s1b" { BEGIN ISOLATION LEVEL READ COMMITTED; } +step "s1a" { ALTER TABLE foo ADD COLUMN y integer; } +step "s1u" { UPDATE foo SET y = 0; } +step "s1c" { COMMIT; } + +session "s2" +step "s2b" { BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE; } +step "s2s" { TABLE foo; } +step "s2c" { COMMIT; } + +session "s3" +step "s3b" { BEGIN ISOLATION LEVEL SERIALIZABLE; } +step "s3a" { ALTER TABLE foo ADD COLUMN y integer; } +step "s3u" { UPDATE foo SET y = 0; } +step "s3c" { COMMIT; } + +permutation "s1b" "s1a" "s1u" "s2b" "s2s" "s1c" "s2c" +permutation "s3b" "s3a" "s3u" "s2b" "s2s" "s3c" "s2c"
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: BUG #14691: Isolation failure in deferrable transaction concurrent with schema change
  2017-06-05 19:11 BUG #14691: Isolation failure in deferrable transaction concurrent with schema change cpacejo@clearskydata.com
  2017-07-17 17:08 ` Re: BUG #14691: Isolation failure in deferrable transaction concurrent with schema change David G. Johnston <david.g.johnston@gmail.com>
@ 2017-07-19 17:39   ` Chris Pacejo <cpacejo@clearskydata.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Chris Pacejo @ 2017-07-19 17:39 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-bugs

On Mon, Jul 17, 2017 at 1:08 PM, David G. Johnston <
david.g.johnston@gmail.com> wrote:

> "Some DDL commands, currently only TRUNCATE and the table-rewriting forms
> of ALTER TABLE, are not MVCC-safe."
>

Ah thank you, I missed that.  This makes sense, since the deferrable
transaction isn't taking any locks.  This information helps me to create a
better workaround.

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

* Re: BUG #14691: Isolation failure in deferrable transaction concurrent with schema change
@ 2017-07-17 15:56 Chris Pacejo <cpacejo@clearskydata.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Chris Pacejo @ 2017-07-17 15:56 UTC (permalink / raw)
  To: pgsql-bugs

Hi, just making sure this bug report hasn't gotten lost.  That this is an
isolation failure makes this a rather serious bug for us, and the
workarounds are somewhat unpleasant.  If any more information is needed
please let me know.  Thanks.

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


end of thread, other threads:[~2017-07-19 17:39 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-06-05 19:11 BUG #14691: Isolation failure in deferrable transaction concurrent with schema change cpacejo@clearskydata.com
2017-07-17 17:08 ` David G. Johnston <david.g.johnston@gmail.com>
2017-07-17 19:35   ` Thomas Munro <thomas.munro@enterprisedb.com>
2017-07-19 17:39   ` Chris Pacejo <cpacejo@clearskydata.com>
2017-07-17 15:56 Re: BUG #14691: Isolation failure in deferrable transaction concurrent with schema change Chris Pacejo <cpacejo@clearskydata.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