pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes
4+ messages / 4 participants
[nested] [flat]

* BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes
@ 2016-02-08 16:32 bear2k@mail.ru
  2016-02-10 16:52 ` Re: BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-02-10 21:25 ` Re: BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes Andres Freund <andres@anarazel.de>
  0 siblings, 2 replies; 4+ messages in thread

From: bear2k@mail.ru @ 2016-02-08 16:32 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      13935
Logged by:          Alexey Makhmutov
Email address:      bear2k@mail.ru
PostgreSQL version: 9.4.4
Operating system:   Linux x86_64
Description:        

We are seeing a very strange situation - table pg_constrint has a duplicate
row for particular primary key constraint which is not accessible via
indexes but is visible during segment scan. There wasn't any specific
manipulation with catalog data, however the database is used with standby
node, so switchover from one instance to another is performed from time to
time.

Here is the PG version:
# select version();
                                                    version                 
                                  
---------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.4.4 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7
20120313 (Red Hat 4.4.7-11), 64-bit
(1 row)

First, select rows with segment scan:

# select oid,ctid,xmin,xmax,conrelid,contype from pg_constraint where
conrelid::int + 0 = 50621;
  oid   |  ctid   |  xmin   | xmax | conrelid | contype 
--------+---------+---------+------+----------+---------
 301952 | (6,136) | 4883898 |    0 |    50621 | p
 300466 | (7,1)   | 4786734 |    0 |    50621 | p
(2 rows)

# explain select oid,ctid,xmin,xmax,conrelid,contype from pg_constraint
where conrelid::int + 0 = 50621;
                          QUERY PLAN                           
---------------------------------------------------------------
 Seq Scan on pg_constraint  (cost=0.00..11.81 rows=1 width=23)
   Filter: (((conrelid)::integer + 0) = 50621)
(2 rows)

Here we can see two primary key constraints for the same relation, which is
very strange by itself. Now, let's try to access the same information by
using index scan:

# select oid,ctid,xmin,xmax,conrelid,contype from pg_constraint where
conrelid = 50621;
  oid   |  ctid   |  xmin   | xmax | conrelid | contype 
--------+---------+---------+------+----------+---------
 301952 | (6,136) | 4883898 |    0 |    50621 | p
(1 row)

# explain select oid,ctid,xmin,xmax,conrelid,contype from pg_constraint
where conrelid = 50621;
                                            QUERY PLAN                      
                      
---------------------------------------------------------------------------------------------------
 Index Scan using pg_constraint_conrelid_index on pg_constraint 
(cost=0.14..4.16 rows=1 width=23)
   Index Cond: (conrelid = 50621::oid)
(2 rows)

We can see only one row now. Moreover, if we try to access that row by its
oid it will be still not accessible via index:

# select oid,ctid,xmin,xmax,conrelid,contype from pg_constraint where oid in
(301952, 300466);
  oid   |  ctid   |  xmin   | xmax | conrelid | contype 
--------+---------+---------+------+----------+---------
 301952 | (6,136) | 4883898 |    0 |    50621 | p
(1 row)

# explain select oid,ctid,xmin,xmax,conrelid,contype from pg_constraint
where oid in (301952, 300466);
                                      QUERY PLAN                            
         
--------------------------------------------------------------------------------------
 Bitmap Heap Scan on pg_constraint  (cost=4.31..7.33 rows=2 width=23)
   Recheck Cond: (oid = ANY ('{301952,300466}'::oid[]))
   ->  Bitmap Index Scan on pg_constraint_oid_index  (cost=0.00..4.30 rows=2
width=0)
         Index Cond: (oid = ANY ('{301952,300466}'::oid[]))
(4 rows)

However, if accessed by full segment scan - it is visible:

# set enable_bitmapscan = off;
SET
# set enable_indexscan = off;
SET
# select oid,ctid,xmin,xmax,conrelid,contype from pg_constraint where oid in
(301952, 300466);
  oid   |  ctid   |  xmin   | xmax | conrelid | contype 
--------+---------+---------+------+----------+---------
 301952 | (6,136) | 4883898 |    0 |    50621 | p
 300466 | (7,1)   | 4786734 |    0 |    50621 | p
(2 rows)

# explain select oid,ctid,xmin,xmax,conrelid,contype from pg_constraint
where oid in (301952, 300466);
                          QUERY PLAN                           
---------------------------------------------------------------
 Seq Scan on pg_constraint  (cost=0.00..11.18 rows=2 width=23)
   Filter: (oid = ANY ('{301952,300466}'::oid[]))
(2 rows)

From my perspective this is something which shouldn't be observed in the
system, so it looks like some kind of bug. Is there any known problem which
may lead to such behavior?

Thanks,
Alexey Makhmutov


-- 
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] 4+ messages in thread

* Re: BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes
  2016-02-08 16:32 BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes bear2k@mail.ru
@ 2016-02-10 16:52 ` Alvaro Herrera <alvherre@2ndquadrant.com>
  2016-02-10 18:01   ` Re: BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 1 reply; 4+ messages in thread

From: Alvaro Herrera @ 2016-02-10 16:52 UTC (permalink / raw)
  To: bear2k@mail.ru; +Cc: pgsql-bugs

bear2k@mail.ru wrote:

> First, select rows with segment scan:
> 
> # select oid,ctid,xmin,xmax,conrelid,contype from pg_constraint where
> conrelid::int + 0 = 50621;
>   oid   |  ctid   |  xmin   | xmax | conrelid | contype 
> --------+---------+---------+------+----------+---------
>  301952 | (6,136) | 4883898 |    0 |    50621 | p
>  300466 | (7,1)   | 4786734 |    0 |    50621 | p
> (2 rows)

We've had reports of duplicate rows appearing in user tables, where only
one copy is visible through indexes, but most updates of pg_constraint
don't go through the normal executor, but rather direct catalog
manipulation of DDL implementation, so I'd be surprising if it's the
same thing.  Also, the OIDs of the two rows are different, so really the
two physical rows are not update copies of the same logical row.

I wonder if you got into this state by adding primary keys concurrently
somehow.  That should of course not be allowed, but of course there is
no UNIQUE constraint on conrelid itself, so the normal unique-ification
code in btree does not fire for this situation.

Perhaps we have a bug in some ALTER TABLE command.  Can you tell us how
you created the PK in the affected table?

-- 
Álvaro Herrera                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


-- 
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] 4+ messages in thread

* Re: BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes
  2016-02-08 16:32 BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes bear2k@mail.ru
  2016-02-10 16:52 ` Re: BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2016-02-10 18:01   ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2016-02-10 18:01 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: bear2k@mail.ru; pgsql-bugs

Alvaro Herrera <alvherre@2ndquadrant.com> writes:
> I wonder if you got into this state by adding primary keys concurrently
> somehow.  That should of course not be allowed, but of course there is
> no UNIQUE constraint on conrelid itself, so the normal unique-ification
> code in btree does not fire for this situation.

I thought maybe Simon's changes to reduce lock levels in ALTER TABLE
had gone too far, but simple experiment shows that ALTER ADD PRIMARY KEY
commands still block each other (and then the second one fails as
expected).  So there isn't an obvious hole here.

Given that the OIDs are different, it seems more likely that this is the
result of a primary key being dropped and then re-created, and later
somehow the commit state of the original row got reverted.

			regards, tom lane


-- 
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] 4+ messages in thread

* Re: BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes
  2016-02-08 16:32 BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes bear2k@mail.ru
@ 2016-02-10 21:25 ` Andres Freund <andres@anarazel.de>
  1 sibling, 0 replies; 4+ messages in thread

From: Andres Freund @ 2016-02-10 21:25 UTC (permalink / raw)
  To: bear2k@mail.ru; +Cc: pgsql-bugs

On 2016-02-08 16:32:22 +0000, bear2k@mail.ru wrote:
> We are seeing a very strange situation - table pg_constrint has a duplicate
> row for particular primary key constraint which is not accessible via
> indexes but is visible during segment scan. There wasn't any specific
> manipulation with catalog data, however the database is used with standby
> node, so switchover from one instance to another is performed from time to
> time.

Did your have any failovers or restore from backup, at some point in the
history of the cluster? If so, what were the exact procedures you used?

Greetings,

Andres Freund


-- 
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] 4+ messages in thread


end of thread, other threads:[~2016-02-10 21:25 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-02-08 16:32 BUG #13935: Duplicate row in pg_constraint table which is not accessible via indexes bear2k@mail.ru
2016-02-10 16:52 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2016-02-10 18:01   ` Tom Lane <tgl@sss.pgh.pa.us>
2016-02-10 21:25 ` Andres Freund <andres@anarazel.de>

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