Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f0pqU-0006J4-FB for pgsql-general@arkaria.postgresql.org; Tue, 27 Mar 2018 14:45:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f0pqT-0006hX-Jo for pgsql-general@arkaria.postgresql.org; Tue, 27 Mar 2018 14:45:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f0pqT-0006hK-9T for pgsql-general@lists.postgresql.org; Tue, 27 Mar 2018 14:45:05 +0000 Received: from mail-pl0-x230.google.com ([2607:f8b0:400e:c01::230]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f0pqO-0006a9-Lg for pgsql-general@lists.postgresql.org; Tue, 27 Mar 2018 14:45:04 +0000 Received: by mail-pl0-x230.google.com with SMTP id p9-v6so14214895pls.2 for ; Tue, 27 Mar 2018 07:45:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=SG2DjXrdki6SAsS/Di205gnprZW4Ht0KDXDpO+4eQIo=; b=Gws/yXhiZccoUh2rVdZniNo4IeieczfJ+9JXCsrCKk/z8+mt+DkeyPdjx6qQ0ExkPW 2cEuZ3yHM+AIEbVWgxbEymEMCbxnB02raJE6Fiepj7lPSlx7l3dj6AVJCBW2CdiCTZge HFajIuxpC3ktvC32PgL4aS6QSCLAiq4WDaqPsgQArau9TX4tejh3rqxA/Ncn2Qu34UUJ xtNJgmPayOhsTG926aiJSDEtLDKPNKITJ6d3kO8YIDg1MhRKPoOJlhRSuS153mN2jUv6 jJEHzSasOzHFERZVnHOcc1nDPeCoXg7Pg5STlK1VZiRKO9CkVO8KOr+xqhfDbABBQSkW /HYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=SG2DjXrdki6SAsS/Di205gnprZW4Ht0KDXDpO+4eQIo=; b=MEvHD43nZnWZwl7gOFGHB8XqW5aQ+/fCbnePH3gdsQaBZKrUnyaFxTQjJiRp7D1flt M50lBw9rFsC8otpiiophoWWUz/RfOtPDmcE2xFiAw7QeABBGFGb8Px7JJ+mrYSmBPVI0 CBWBDojZmoyh7cWjoI/+X2A/vd6+usXJqGXerltnuBBfZOvjqbBU5Ewlka8LFYjlbqUx HbyBDrS21JIDiUoxoZnJPwSS/0TFKDEHmX9gO0BZI5KZo6SM2AeduxhYkSD2c9f1at0r GWipTs+3r9BshenyqODn02AJp6qyO+uQZ5qnMsS/D65/mqtQhNlsdNYIztCOJTxivys9 qhlg== X-Gm-Message-State: AElRT7F8wBXk44lTGNxlGNXQnlZs4914ih1JfHVjbxzO8QPzt4JqiBmn sMizbDkKSodeGbGLa939tezHHHfl X-Google-Smtp-Source: AG47ELt3eKGA6hjH1M5gqH2Cxfsf0JYcNKkqDTTBvU1aRH7YOZQUxSQR9c8iHc/hdRJDx1jmPRRFyw== X-Received: by 2002:a17:902:42a3:: with SMTP id h32-v6mr44336515pld.231.1522161897907; Tue, 27 Mar 2018 07:44:57 -0700 (PDT) Received: from [192.168.2.201] (static-50-53-109-254.bvtn.or.frontiernet.net. [50.53.109.254]) by smtp.googlemail.com with ESMTPSA id l64sm3067711pga.33.2018.03.27.07.44.57 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Mar 2018 07:44:57 -0700 (PDT) Subject: Re: Check constraints. To: pgsql-general@lists.postgresql.org References: From: Paul Jungwirth Message-ID: Date: Tue, 27 Mar 2018 07:44:56 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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#L117-L137 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