5

I have a table where I have a row that cannot be found by id. If I go around the PK index, the row is found. If I drop the index, the row is found. If I add a new index, row not found.

d5toqvrtbm8bbu=> SELECT id, created_at FROM widgets WHERE id = 1155301;
   id    |         created_at         
---------+----------------------------
 1155301 | 2014-01-10 02:59:47.856214
(1 row)

d5toqvrtbm8bbu=> CREATE UNIQUE INDEX widgets_pkey ON widgets(id);
CREATE INDEX
d5toqvrtbm8bbu=> SELECT id, created_at FROM widgets WHERE id = 1155301;
 id | created_at 
----+------------
(0 rows)

d5toqvrtbm8bbu=> SELECT id, created_at FROM widgets WHERE id - 1 + 1 = 1155301;
   id    |         created_at         
---------+----------------------------
 1155301 | 2014-01-10 02:59:47.856214
(1 row)

d5toqvrtbm8bbu=> DROP INDEX widgets_pkey;
DROP INDEX
d5toqvrtbm8bbu=> SELECT id, created_at FROM widgets WHERE id = 1155301;
   id    |         created_at         
---------+----------------------------
 1155301 | 2014-01-10 02:59:47.856214
(1 row)

d5toqvrtbm8bbu=> CREATE UNIQUE INDEX widgets_pkey ON widgets(id);
CREATE INDEX
d5toqvrtbm8bbu=> SELECT id, created_at FROM widgets WHERE id = 1155301;
 id | created_at 
----+------------
(0 rows)

This is on postgres 9.3

Any suggestions?

5
  • 1
    This looks really weird. You should post that to the Postgres mailing list. Commented Jan 15, 2014 at 17:07
  • It looks as if the index is corrupted as soon as it's created. What's the type of this id column and is there any custom code in your postgres installation? Commented Jan 15, 2014 at 18:58
  • Please show the \d+ for the table widgets, and explain for both with-index and without-index plans. Is id an integer / bigint? Or something else? What's the exact SELECT version() ? Can you post a self-contained database dump that demonstrates the problem. Commented Jan 16, 2014 at 4:48
  • A dump and restore fixes the problem, consistent with my conclusion that I've got a broken index. Commented Jan 24, 2014 at 20:12
  • I'm seeing almost the exact same problem right now myself. I tried REINDEX which seems as though it's meant for exactly this, but it's not helping. (Which I suppose is similar to how you're creating the index and it's instantly corrupt.) This is on Amazon RDS with version 3.3.3. Commented Aug 1, 2014 at 0:12

2 Answers 2

5

I had a similar problem with my database. I tried REINDEX but it didn't help.

VACUUM FULL widgets;

Totally fixed the problem though.

Sign up to request clarification or add additional context in comments.

Comments

0

Looping back on this:

I worked with Heroku support (this was Heroku Postgres hosted) and they told me there was a bug in 9.3.2 that could lead to this kind of corruption when a follower was promoted. (I experienced it on a couple of generations of promoted followers.)

They said the problem was fixed in 9.3.4, and I wound up being able to recreate the broken rows from backups, deleting the rows and then rebuilding.

So far no reoccurrence.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.