Rebuilding large GIN indexes

takes time. A lot of time. With rebuilding, I mean when you for example have to UPDATE all your tsvector fields due to a change in tsearch2 configuration. (Yes, I had to do that because I had a slightly incorrect tsearch2 configuration for the archives search database). So don't do it. Instead use the fact that PostgreSQL has nice transactional DDL and do something like this:

BEGIN TRANSACTION;

CREATE TABLE messages_new AS SELECT id,txt,to_tsvector(txt) AS fti

  FROM messages;

CREATE INDEX messages_new_fti ON messages_new USING gin(fti);

ANALYZE messags_new;

ALTER TABLE messages RENAME TO messages_old;

ALTER TABLE messages_new RENAME TO messages;

COMMIT;

DROP TABLE messages_old;

ALTER INDEX messages_new_fti RENAME TO messages_fti;

(apologies for any typos, I didn't bother to actually type these commands into the database again, and I lost my cut-and-paste of what I ran)

This way, the messages table can still serve up searches without any disruption to the searches at all. And creating the new index is a lot faster than updating the existing one if you have to touch all rows.


Add comment

New comments can no longer be posted on this entry.

Conferences

I speak at and organize conferences around Open Source in general and PostgreSQL in particular.

Upcoming

PGConf.EU 2026
Oct 20-23, 2026
Valencia, Spain
FOSDEM PGDay 2027
Jan 28-31, 2027
Brussels, Belgium
Nordic PGDay 2027
Mar 16, 2027
Stockholm, Sweden

Past

PGDay Lowlands 2026
Sep 10, 2026
Utrecht, Netherlands
PGDay.UK 2026
Sep 8, 2026
London, UK
pgconf.dev 2026
May 19-22, 2026
Vancouver, Canada
pgDay.paris 2026
Mar 26, 2026
Paris, France
Nordic PGDay 2026
Mar 24, 2026
Helsinki, Finland
More past conferences