pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Paul Jungwirth <pj@illuminatedcomputing.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Check constraints.
Date: Tue, 27 Mar 2018 07:44:56 -0700
Message-ID: <cf4e649e-a025-f1a4-25e9-58d4747ebec9@illuminatedcomputing.com> (raw)
In-Reply-To: <a6c7c35e-07d5-6a6c-0d60-5e3c0b439003@yewtc.demon.co.uk>
References: <a6c7c35e-07d5-6a6c-0d60-5e3c0b439003@yewtc.demon.co.uk>

On 03/27/2018 04:23 AM, Steve Rogerson wrote:
> I am trying to add/change a constraint programmatically, but not if it
> already exists, and is the same as before.
> ...
> Is there a way of getting the "normalised" version of constraint so decide if
> I need to update the constraint if one already exists?

Hi Steve,

I wrote a Ruby gem to do this some years ago. Here is the SQL I used:

           SELECT  c.conname,
                   t.relname,
                   pg_get_expr(c.conbin, c.conrelid)
           FROM    pg_catalog.pg_constraint c,
                   pg_catalog.pg_class t,
                   pg_catalog.pg_namespace n
           WHERE   c.contype = 'c'
           AND     c.conrelid = t.oid
           AND     t.relkind = 'r'
           AND     n.oid = t.relnamespace
           AND     n.nspname NOT IN ('pg_catalog', 'pg_toast')
           AND     pg_catalog.pg_table_is_visible(t.oid)

https://github.com/pjungwir/db_leftovers/blob/master/lib/db_leftovers/postgres_database_interface.rb...

I haven't used it against the last few Postgres versions, but it 
probably still works or needs only minor adjustments.

-- 
Paul              ~{:-)
pj@illuminatedcomputing.com




view thread (4+ messages)  latest in thread

Message-ID: <cf4e649e-a025-f1a4-25e9-58d4747ebec9@illuminatedcomputing.com>
Permalink:  ../cf4e649e-a025-f1a4-25e9-58d4747ebec9@illuminatedcomputing.com/
Also on:    postgresql.org/message-id/cf4e649e-a025-f1a4-25e9-58d4747ebec9@illuminatedcomputing.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: pj@illuminatedcomputing.com
  Subject: Re: Check constraints.
  In-Reply-To: <cf4e649e-a025-f1a4-25e9-58d4747ebec9@illuminatedcomputing.com>
  References: <a6c7c35e-07d5-6a6c-0d60-5e3c0b439003@yewtc.demon.co.uk> <cf4e649e-a025-f1a4-25e9-58d4747ebec9@illuminatedcomputing.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox