Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hixRY-0007sv-4a for pgsql-general@arkaria.postgresql.org; Thu, 04 Jul 2019 08:50:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hixRV-0002mh-BD for pgsql-general@arkaria.postgresql.org; Thu, 04 Jul 2019 08:50:13 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1hixRU-0002l0-VW for pgsql-general@lists.postgresql.org; Thu, 04 Jul 2019 08:50:13 +0000 Received: from mail-pg1-x52f.google.com ([2607:f8b0:4864:20::52f]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hixRP-0002fB-1I for pgsql-general@lists.postgresql.org; Thu, 04 Jul 2019 08:50:12 +0000 Received: by mail-pg1-x52f.google.com with SMTP id g15so2600202pgi.4 for ; Thu, 04 Jul 2019 01:50:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=EkxAwdfk+9ofWSCn1Nbj+a+U5+jjAVZLB+/rvWes1fc=; b=EMU3TQRa9yk+7FEXTmuaPcTej9U0COTuHUvh75ClLAkopVErOCay1I8w4zTBIRvByJ 1EgXupgT9s65JMaSuhg3+R8XLZa2RJsfwL6uIpus0ssVMaXHqvi+0jHimukPT7Ulc5J4 c7k7itZFqgg/DcuUQxtJmj47QIU+7QX8kHdJMzXzmoUmqMAdUoI7t7Kdrf/zzyiotMt3 R/hvE9X+wCrlo40NfZT9sriuCcoNs+UiTWgPcBHFiwP9cRcnmldfITEkaZri41aTKLs6 /sZDLtMgxer/pzqWr+HDZlwLYB2r78oUadQTh6J+n6SniagB0qnp7eCxAQqp1wUQy7Cz eU9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=EkxAwdfk+9ofWSCn1Nbj+a+U5+jjAVZLB+/rvWes1fc=; b=e/ey/mwT1ks5qL7bPgoHpSQzB4MnJS/gqpauaFjMwy5uHwsN0C+vomdCeNg1pdc/Z6 KGbbACq+gyFbvY94WiSet4ch9DTQeXLrFjlsD9XIEY+UYVnngZ1LbMSTpiwDEJEziTUC sZ0hJayQwYmSYY43oNhFaNbVlJivdCS0EUprVp/6E1ijSPccBDb+XsKTcVUAyBMRcsQM iix/0G/w09sJMxZ7TMJheVJ8Jx98s8ZbRfk2kQJzp9FCSlM/gNCxJ0N6BRKYmrVawk/6 KYLUIOD9F+pjlPPWDq2c5WDfWaYBc/nivgQVg1V7rUWsFgFQPCtSd7T8l7Pa5MOEiuFN oIrQ== X-Gm-Message-State: APjAAAVtgLdq1n7yeBYdGDbbx1kwumOp6QoevZhyI5QksRgLS+HvJUvP EpwNJml4GcHuHhxWX7vs5DI= X-Google-Smtp-Source: APXvYqygifvXM5C7uGkkREQ8vvWkRF/VWDMsCifRXPbZRYBI+1KCMsQIc820psh9OmGJOpEIxGYjbg== X-Received: by 2002:a63:1a5c:: with SMTP id a28mr42230009pgm.418.1562230204436; Thu, 04 Jul 2019 01:50:04 -0700 (PDT) Received: from roblaptop (194-193-62-104.tpgi.com.au. [194.193.62.104]) by smtp.gmail.com with ESMTPSA id 64sm7232995pfe.128.2019.07.04.01.50.02 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 04 Jul 2019 01:50:03 -0700 (PDT) Message-ID: <421a8b3052781f2419b0928f4523fb3731e8a913.camel@gmail.com> Subject: Re: Expression of check constraint From: rob stone To: Dirk Mika , "pgsql-general@lists.postgresql.org" Date: Thu, 04 Jul 2019 18:49:59 +1000 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.30.5-1.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, On Thu, 2019-07-04 at 05:58 +0000, Dirk Mika wrote: > Hi, > > 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)? > 2. why is the array expression casted so wildly? First to > character varying and then to text[]? > 3. The column ratified is of type character varying(1). Why is it > casted to text? > > Dirk > -- > Dirk Mika > Software Developer > > Why don't you define "ratified" as CHAR(1)? AFAIK, constraint evaluation is based upon the column's underlying data type. Cheers, Robert