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 1hiz2B-0004nf-LT for pgsql-general@arkaria.postgresql.org; Thu, 04 Jul 2019 10:32:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hiz2A-0000Dv-II for pgsql-general@arkaria.postgresql.org; Thu, 04 Jul 2019 10:32:10 +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 1hiz2A-0008Vy-1h for pgsql-general@lists.postgresql.org; Thu, 04 Jul 2019 10:32:10 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hiz27-000470-EI for pgsql-general@lists.postgresql.org; Thu, 04 Jul 2019 10:32:09 +0000 Received: by mail-wr1-x442.google.com with SMTP id c27so6083886wrb.2 for ; Thu, 04 Jul 2019 03:32:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=1SehaTJhCpAiakIYeZRScwPtNEDJDf4VMOCrBaozaUw=; b=FzYXkduX9pXT3Ys3aOhotAaavyYULYCSLSODIuDdHW+diENOXj9/0cYbOtFuq3mZzw s+VaOSTkXJeb1X8Z4Huy0tRlzuLQDn/bhKte1p77OWcQYPEAF0jgtLugpr4KIp7eDpuX tNe15OWAMmVfqkhgofmwvmCGeC3QPmq7rET0OCUdOmMszbOeqaeUjQNzvvRqfia4y8p9 tbF1KPzPwKLHDy+YAVn+sKzS+b3DyLZ4SvWkge6E0v3/6fDhxCbMydcbyYKClZt50Ysi sazgyqLvYwHuwNImMEUie85WL86eksgZ8FvNUHUssAzAX97ymcsRbeigYm1ROqHv4Y1e Xq6Q== 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=1SehaTJhCpAiakIYeZRScwPtNEDJDf4VMOCrBaozaUw=; b=iipY2LXTdzFUfciKKx4IlDc8ckpEpt4yDhRy9ouYNq2Tbguha6mogBhgC/bGzXkaOe tXXJjy6SVFIo/sQJaR2WMsCD6d/T0yzvdbBIrdZjKdvMQAEvP384TGOAAJxsRfY4Khdv 9dEa1i4mokm7H+UAgY66R2Yt7IkaK48L4kAWoOv/qTdtPIRndVpZWqzNeNHAvBbOYGtl pTSzOB5jiW0cNY5BIVGb6dXQ1WlDODClyW6ekcg5xRXPQvYsuC0eXQyc33iLjbysC2I6 +P87pO7W2FSzhq2nh1IQkw0tIXqkylIgYnVqXU5tm4bcQNafwoTLit98rbt7hQ7Kp4aw UirQ== X-Gm-Message-State: APjAAAVOEbAbwmeYPDWmiwdB38hk0Qt1GaFLsciR0QG0uENR3HaxMWcC tqINS4pwOjM27GjXpN/jv9dNIA== X-Google-Smtp-Source: APXvYqxAAwAjXZwLYxCqPHJ5o2yfCXN5XCJWKBWfth5Avr6Ypjg9vDHi3izJXp7itASHgAzQftMtTg== X-Received: by 2002:a5d:6949:: with SMTP id r9mr31635724wrw.73.1562236326088; Thu, 04 Jul 2019 03:32:06 -0700 (PDT) Received: from localhost.localdomain (217-149-172-165.nat.highway.telekom.at. [217.149.172.165]) by smtp.gmail.com with ESMTPSA id c14sm2259479wrr.56.2019.07.04.03.32.05 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 04 Jul 2019 03:32:05 -0700 (PDT) Message-ID: Subject: Re: Expression of check constraint From: Laurenz Albe To: Dirk Mika , "pgsql-general@lists.postgresql.org" Date: Thu, 04 Jul 2019 12:32:04 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.30.5 (3.30.5-1.fc29) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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