3

I am trying to add a simple index with the following SQL in Postgres, but the command keeps timing out:

CREATE INDEX playlist_tracklinks_playlist_enid ON playlist_tracklinks (playlist_enid);

The table definition is as follows:

=> \d playlist_tracklinks
         Table "public.playlist_tracklinks"
     Column     |     Type      |     Modifiers
----------------+---------------+--------------------
 playlist_enid  | numeric(20,0) | not null default 0
 tracklink_enid | numeric(20,0) | not null default 0
 position       | integer       | not null default 1

There are around 2.2 billion rows in the table, and it fails with the following error:

ERROR:  canceling statement due to user request

I tried increasing the query timeout time with the following:

SET statement_timeout TO 360000000;

However it still hits that threshold. I have tried with and without CONCURRENTLY, and am sort of at a loss for what to do. Any suggestions would be greatly appreciated.

6
  • 2
    This error is not caused by statement_timeout, but by a user cancel request. What interface do you use to access PostgreSQL? Try setting the s_t value to 0, which turns this off. Commented Sep 17, 2013 at 1:45
  • I'm using psql to access the server remotely (this is Heroku's Postgres offering, so I don't have direct server access). Commented Sep 17, 2013 at 14:04
  • I'll try creating the index with statement_timeout set at 0 Commented Sep 17, 2013 at 14:06
  • I tried with statment_timeout set to 0, and it still failed... I'm not really sure why... Commented Sep 17, 2013 at 21:27
  • Looks like Heroku is killing your connection, check with their support if the really do that. Also, try increasing maintenance_work_mem to the higher value you can, this will improve the index creating. Commented Sep 24, 2013 at 18:13

3 Answers 3

1

You could try indexing a part-piece of the table, say the first 10k rows using the WHERE statement. Then you might be able to see if that works and how long it takes. Reference for using WHERE with CREATE INDEX here: http://www.postgresql.org/docs/9.1/static/sql-createindex.html

Is it possible your column contains non-unique numbers? That could potentially cause an issue (I'm not sure if an index requires unique values on a column in this case).

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

5 Comments

It definitely does have non-unique numbers - I want to create a simple index vs a unique index. I don't think it requires unique values with this syntax (it worked on smaller tables).
@JacobWG Thanks for clearing that up. Perhaps try the partial index?
after I create the partial index, then what? Do I create multiple partial indexes? Can I combine them into one?
Because your data contains non-unique numbers, it usually indicates a common value (perhaps the default of 0?) which will not need indexing if 90% of the values are 0. Therefore your partial index could cover values greater than 0. More info here: postgresql.org/docs/8.1/static/indexes-partial.html
Yeah, they all need indexing... :/ This is a join table between two other tables, so each field refers to a primary key of another table.
1

Arithmetic with numerics is very slow. This includes the comparisons needed to build and use the indexes. I suggest that you change the enid types to char(20) or just varchar if you do not do any arithmetic (other than comparisons) on them, and perhaps bigint if you do. Bigint isn't quite enough for the largest possible 20-digit number—I don't know what sort of information this ids carry around, if they can really be that big.

Comments

1

It was Heroku killing connections (the server ran out of temporary space). Contacting Heroku support was the solution eventually...

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.