pg_restore foreign keys NOT VALID, or [assume] VALID; VALIDATE CONSTRAINT CONCURRENTLY

From: Tomasz Ostrowski <tometzky+pg(at)ato(dot)waw(dot)pl>
To: pgsql-hackers(at)postgresql(dot)org
Subject: pg_restore foreign keys NOT VALID, or [assume] VALID; VALIDATE CONSTRAINT CONCURRENTLY
Date: 2017-02-04 10:49:32
Message-ID: d9b87a42-21bf-67f5-53b5-247210dd1003@ato.waw.pl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi.

A lot of time during pg_restore of a large database is spent on
validating all the foreign keys. In contrast to importing data and
creating indexes this operation does not parallelize well. So large
percentage of parallel restore time ends up using single worker to
validate foreign keys for the largest table.

If we'd have a option to restore the table without validating foreign
keys and leaving them in NOT VALID state, the downtime needed for us to
restore would decrease significantly.

If we'd also have an option to avoid blocking updates on the table
during (potentially long) validating, for example:
ALTER TABLE distributors VALIDATE CONSTRAINT CONCURRENTLY distfk;
Then we could postpone it and do it during normal operation of the
database, out of precious disaster recovery time.

Alternatively maybe it should be allowed to do for example:
ALTER TABLE distributor ADD CONSTRAINT distfk
FOREIGN KEY (address) REFERENCES addresses (address) VALID;
It would mean that the database should assume that this constraint is
valid. Should be possible to turn it on using some pg_restore option (or
pg_dump option when dumping to text format), though maybe only when
restoring whole database, not single table. Though there's a possibility
that a partially failed restore could leave database in inconsistent
state. So I'd rather prefer the above option (NOT VALID + VALIDATE
CONCURRENTLY).

Any comments on this? Does it look like a good idea? It shouldn't be
hard to implement.

--
Tomasz "Tometzky" Ostrowski

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Verite 2017-02-04 11:37:24 Re: \if, \elseif, \else, \endif (was Re: PSQL commands: \quit_if, \quit_unless)
Previous Message Ashutosh Sharma 2017-02-04 10:38:24 Re: pageinspect: Hash index support