pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Dirk Mika <Dirk.Mika@mikatiming.de>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Expression of check constraint
Date: Thu, 04 Jul 2019 12:32:04 +0200
Message-ID: <ea4efc94946a7e098786fa5a38c4d43f8da8871b.camel@cybertec.at> (raw)
In-Reply-To: <DDD8FEB9-CCE9-4951-9022-A58DCC2998C4@mikatiming.de>
References: <DDD8FEB9-CCE9-4951-9022-A58DCC2998C4@mikatiming.de>

Dirk Mika wrote:
> if I add the following check constraint to a table:
>  
> ALTER TABLE public.times_places
>    ADD CONSTRAINT ck_tp_ratified CHECK
>           (ratified IS NULL OR (ratified IN ('Y', 'N')));
>  
> It becomes the following when describing the table in psql:
>  
> Check constraints:
>     "ck_tp_ratified" CHECK (ratified IS NULL OR (ratified::text = ANY (ARRAY['Y'::character varying, 'N'::character varying]::text[])))
>  
> The behavior of the check constraint is logically identical and this seems plausible to me, but I still wonder why:
> 1.    does the expression x in (a, b) become the expression x = any(array(a, b)?

Because that's what the PostgreSQL query parser makes out of an IN list.

> 2.    why is the array expression casted so wildly? First to character varying and then to text[]?

Because "text" is the preferred string type, and there is no "=" operator for "character varying".
But don't worry, casting "character varying" to "text" doesn't cost anything, since the
types are binary coercible (the storage ist the same).

> 3.    The column ratified is of type character varying(1).  Why is it casted to text?

See 2. above.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com






view thread (5+ messages)  latest in thread

Message-ID: <ea4efc94946a7e098786fa5a38c4d43f8da8871b.camel@cybertec.at>
Permalink:  ../ea4efc94946a7e098786fa5a38c4d43f8da8871b.camel@cybertec.at/
Also on:    postgresql.org/message-id/ea4efc94946a7e098786fa5a38c4d43f8da8871b.camel@cybertec.at

 · 

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: laurenz.albe@cybertec.at, Dirk.Mika@mikatiming.de
  Subject: Re: Expression of check constraint
  In-Reply-To: <ea4efc94946a7e098786fa5a38c4d43f8da8871b.camel@cybertec.at>
  References: <DDD8FEB9-CCE9-4951-9022-A58DCC2998C4@mikatiming.de> <ea4efc94946a7e098786fa5a38c4d43f8da8871b.camel@cybertec.at>

* 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