Re: how to correctly invalidate a constraint?

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: how to correctly invalidate a constraint?
Date: 2017-01-13 21:44:57
Message-ID: 20170113214457.uqduwgm7okmv2wqp@alvherre.pgsql
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Pavel Stehule wrote:
> Hi
>
> I would to do import without RI check - so I disable RI triggers.
>
> I would to invalidate constraint on Foreign Keys and then I would to use
> ALTER TABLE VALIDATE CONSTRAINT ...
>
> I didn't find how to invalidate constraint without direct update
> pg_constraint.
>
> Is there some clean way?

I think what you want is:
- set the constraint as "not valid", so that the following is a valid
operation
- set the RI trigger not to fire, to improve performance of bulk loads
- do the load
- activate the trigger
- validate the constraint

We have SQL commands for everything except the first step. Now my
question would be: do we want to support that operation as a stand-alone
thing so that you can construct the above from pieces, or do we want
some higher-level command so that the above is less cumbersome? The
main issue I see is that a single constraint involves several triggers,
and the triggers have internally-derived, very ugly names. So in my
mind the right way to provide this functionality is to have a command
that operates on the RI constraint and modifies the triggers status.

ALTER TABLE .. ALTER CONSTRAINT [name / ALL] DEACTIVATE
-- sets constraint as NOT VALID, also sets triggers inactive

[user bulkload occurs here]

ALTER TABLE .. ALTER CONSTRAINT [name / ALL] ACTIVATE
-- activates triggers, validates constraint

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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2017-01-13 22:08:30 Re: GSoC 2017
Previous Message Jeff Janes 2017-01-13 21:39:02 Re: postgres_fdw bug in 9.6