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 1hGPdz-0007ts-L7 for pgsql-general@arkaria.postgresql.org; Tue, 16 Apr 2019 15:05:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hGPdy-0001LF-CQ for pgsql-general@arkaria.postgresql.org; Tue, 16 Apr 2019 15:05:06 +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 1hGPdy-0001L7-0m for pgsql-general@lists.postgresql.org; Tue, 16 Apr 2019 15:05:06 +0000 Received: from mail-oi1-x242.google.com ([2607:f8b0:4864:20::242]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGPdv-0000Zr-2Q for pgsql-general@lists.postgresql.org; Tue, 16 Apr 2019 15:05:05 +0000 Received: by mail-oi1-x242.google.com with SMTP id f196so6494292oib.7 for ; Tue, 16 Apr 2019 08:05:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=+5MChQ1FINZntasAfOu5FJUUOqSkO/b6zRcHISeMpyY=; b=SPKevqA/5771/6sh9OhNN5IakeIlvKbcx8kdC37V3jhXbSY6mqldKcaUCXsqfVhkwG I/2bgOrhpCfjrXTNGRJsQcLPJ5KL67jp6QqIhhGM3bKo1AvksYV7pt/URAgynz8CVl+y tR1tn532ZJqJuGXk2mN4B0Re1RhYboQdRzYJAdfPywN/13ADvHQTWW0nh2KGA3diguWf bVZSXe9ejsxIvCkgf8eRooMOKCUZlzA9QzEHGPRYFUoJez4pYGKGv/OGXC00hAb4KLJ1 89+yKG23Lhp19RTxx7Z1dneEbnpgXmfryLu0js6R0e/fIQ08MyPXLLv8y1kJj2sCvHtu VXvA== 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-transfer-encoding :content-language; bh=+5MChQ1FINZntasAfOu5FJUUOqSkO/b6zRcHISeMpyY=; b=ShcEkKL+tK4eO/wBEeKLfN9CPepmLzfjB7ENltbyqlRLh3awKoIXS9bkPFn4vqrUP+ pVOYNFhrtY2Pj1MEWIMlEPeva8Pi9au/ghHv6tfLHpvMaFeJ2tjur8Imh6pYqJkqZWJ1 R6TQXJO/KefIUV1ebyvbrgIOMSKzTJeKmWC+A+rFZcOB8Uo6jjzzgEhPU1LpqztmK8Mc nC+CfQJCkqHA/Un9vZyXqFFV+RnZOfri09lvbdWlyWj5VMoxd+lKlWLR94gApR3tX07M Okd7PZ6UMEEi/moDmxseqgmun/Wqyhmtq7FF1MuzjgsVo2m8r6ESht0cu+Qr3h3Kib/y +8lw== X-Gm-Message-State: APjAAAWF5VEtHHAUo4G1I3pqtmFuRRBXzkdYDwMhEgd34ZH4cFe/Ahf8 wDGra5NdHyXL5OT4MlQYemF4m4tzgtI= X-Google-Smtp-Source: APXvYqwb8qwz2WCSNjS/o2mKBXoeJdrSag4tmSMYX17Y2fsBbSpIbZVwlv2CsQwQymAUD/vaJF47AA== X-Received: by 2002:aca:55cb:: with SMTP id j194mr24755221oib.72.1555427100073; Tue, 16 Apr 2019 08:05:00 -0700 (PDT) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id c145sm21378793oig.0.2019.04.16.08.04.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Apr 2019 08:04:58 -0700 (PDT) Subject: Re: Alter domain type / avoiding table rewrite To: pgsql-general@lists.postgresql.org References: <54c9d5ab-9be5-1499-1c36-f45ea33cc322@gmail.com> <1bd403de-2552-7986-70ec-ac25e7717805@aklaver.com> From: Ron Message-ID: Date: Tue, 16 Apr 2019 10:04:58 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <1bd403de-2552-7986-70ec-ac25e7717805@aklaver.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 4/16/19 9:28 AM, Adrian Klaver wrote: > On 4/16/19 7:19 AM, Ron wrote: >> On 4/16/19 4:22 AM, Tim Kane wrote: >>> So I have a situation where I would like to modify a field that is >>> currently a domain type over a varchar(9) >>> >>> Specifically: >>> CREATE DOMAIN old_type AS varchar(9) >>> >>> This isn't ideal, let's just say.. legacy. >>> >>> >>> I wish to modify this type.. ideally to a text type with a length >>> constraint.. or even just a slightly larger varchar(12) would suffice.. >>> >>> CREATE DOMAIN new_type AS text; >>> ALTER DOMAIN new_type ADD CONSTRAINT check_len CHECK ((length(VALUE) <= >>> 12)) NOT VALID; >>> >>> ALTER TABLE target ALTER >>> COLUMN value SET DATA TYPE new_type; >>> >>> >>> But it seems impossible to achieve either without a full table rewrite. >> >> But the column only has -- at most -- 9 characters of data in it. Won't >> the CHECK constraint instantly fail?  (ISTM that you should add the check >> constraint AFTER modifying the length and updating your data.) >> > > Not sure how?: > > create table check_test (id integer, fld_1 varchar(12)); > CREATE TABLE > test=> insert into check_test values (1, '123456789'), (2, ''); > INSERT 0 2 > > test=> select length(fld_1) from check_test ; > >  length > > -------- > >       9 > >       0 > > (2 rows) > > The lengths would be less then or equal to 12. But there's no CHECK constraint. > > Also the NOT VALID will push the check into the future: > > https://www.postgresql.org/docs/9.6/sql-altertable.html > > "... If the constraint is marked NOT VALID, the potentially-lengthy > initial check to verify that all rows in the table satisfy the constraint > is skipped. The constraint will still be enforced against subsequent > inserts or updates NOT VALID is the part that obviates my concern. -- Angular momentum makes the world go 'round.