pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: ralf@rw7.de
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #14296: weird check constraint clause in pg_constraint
Date: Fri, 26 Aug 2016 20:00:27 +0100
Message-ID: <87y43j74jc.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <20160826144958.15674.41360@wrigleys.postgresql.org>
References: <20160826144958.15674.41360@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

>>>>> "ralf" == ralf  <ralf@rw7.de> writes:

 ralf> I created two tables

 ralf> CREATE Table tabgood ( col integer, CONSTRAINT checker CHECK ( col>0 ))
 ralf> CREATE Table tabbad  ( col integer, CONSTRAINT checker CHECK ( col>-1 ))

 ralf> and fetch the check constraints from pg_constraint

 ralf> SELECT ut.relname,uc.conname,uc.consrc

As a rule you should never look at consrc, adsrc, etc because they
become stale the instant anything gets renamed. The right way to get the
constraint definition is to use pg_get_constraintdef(oid), or
pg_get_expr(conbin,conrelid) for just the check expression.

 ralf> "tabgood";"checker";"(col > 0)"
 ralf> "tabbad";"checker";"(col > '-1'::integer)"

 ralf> The second clause is weird, although working. it should be just
 ralf> "col > -1".

 ralf> This problem is observed seen on PostgresSQL 9.5.4, it was not
 ralf> observed on 9.3.14.

This is intentional.

Quoth the comments in ruleutils.c:

            /*
             * INT4 can be printed without any decoration, unless it is
             * negative; in that case print it as '-nnn'::integer to ensure
             * that the output will re-parse as a constant, not as a constant
             * plus operator.  In most cases we could get away with printing
             * (-nnn) instead, because of the way that gram.y handles negative
             * literals; but that doesn't work for INT_MIN, and it doesn't
             * seem that much prettier anyway.
             */

And the accompanying commit message: (from 542320c2b)

    Be more careful about printing constants in ruleutils.c.
    
    The previous coding in get_const_expr() tried to avoid quoting integer,
    float, and numeric literals if at all possible.  While that looks nice,
    it means that dumped expressions might re-parse to something that's
    semantically equivalent but not the exact same parsetree; for example
    a FLOAT8 constant would re-parse as a NUMERIC constant with a cast to
    FLOAT8.  Though the result would be the same after constant-folding,
    this is problematic in certain contexts.  In particular, Jeff Davis
    pointed out that this could cause unexpected failures in ALTER INHERIT
    operations because of child tables having not-exactly-equivalent CHECK
    expressions.  Therefore, favor correctness over legibility and dump
    such constants in quotes except in the limited cases where they'll
    be interpreted as the same type even without any casting.
    
    This results in assorted small changes in the regression test outputs,
    and will affect display of user-defined views and rules similarly.
    The odds of that causing problems in the field seem non-negligible;
    given the lack of previous complaints, it seems best not to change
    this in the back branches.

So the objective is not to preserve the original input, but to return
text that will be parsed to produce the same expression tree that was
stored.

-- 
Andrew (irc:RhodiumToad)


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (3+ messages)  latest in thread

Message-ID: <87y43j74jc.fsf@news-spur.riddles.org.uk>
Permalink:  ../87y43j74jc.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87y43j74jc.fsf@news-spur.riddles.org.uk

 · 

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-bugs@postgresql.org
  Cc: andrew@tao11.riddles.org.uk, ralf@rw7.de
  Subject: Re: BUG #14296: weird check constraint clause in pg_constraint
  In-Reply-To: <87y43j74jc.fsf@news-spur.riddles.org.uk>
  References: <20160826144958.15674.41360@wrigleys.postgresql.org> <87y43j74jc.fsf@news-spur.riddles.org.uk>

* 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