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 1hGOt7-0004ZB-Ij for pgsql-general@arkaria.postgresql.org; Tue, 16 Apr 2019 14:16:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hGOt6-0003Ah-9z for pgsql-general@arkaria.postgresql.org; Tue, 16 Apr 2019 14:16:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGOt5-00034j-Qd for pgsql-general@lists.postgresql.org; Tue, 16 Apr 2019 14:16:39 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGOt2-0005sO-ME for pgsql-general@postgresql.org; Tue, 16 Apr 2019 14:16:38 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 689662221C; Tue, 16 Apr 2019 10:16:35 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 16 Apr 2019 10:16:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=U X5XewMJPvhj367Lw3te62PPHO9E4JzJJ8/WQqgJ0Qc=; b=aemI5Vb1mXIHOTz8W um3jiJ5Vz/xL1damv1JTfgjJiPg2lgjUVqJtH0jVK1Z1UlXA7lBtjKIq1eaWBvM9 5OEQeVz2gG5AFVb9HuRmZbdauJYUeCXLxGEIM4i3Ea/yrTbaMoxZ+7vlcRyjBrVH h3tIQujkZ0Fe+h5QoNVChLOGXf7gvGpj/DYz7DVp3iG4jSGgFj49n8JTMLb3q2i6 ScrSymVs5s/6a4dYmTtndBNskEP+1ZCsSPepQB8KSiYipjtiLn2qiLT8PIiNQvEn 5KBFPMvZJLBa7T1fpBP5pXCuywSuzIPiSQZhnS98aJT+hsQpTbGtgGUPNI9H+RF7 SZDeA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=UX5XewMJPvhj367Lw3te62PPHO9E4JzJJ8/WQqgJ0 Qc=; b=ZMe5b2ox90VQ0cky0U4+Q1Xi1sXGLkj8Wrbp+mSZ77y1rqIXnKpiRlSAc 2lVn4gKfq77Q64qFL7tkGwnPkKW0ECCxsLhtktlhQ/1MSJtw6Ifw2inLJttbET9d kOfH3MrbhXj4xvs20kQ3mAwR40c0Z1lyZK4cbYeV19JWeu6GirpnMDSY5dUzDYpY AzHUVFRuhv2bzDWp8TbScIBe1hY86Cts9pRgLjOlQI64S1dwNlCiKgUbY2oMj3WS mwNx2QJn14LVKCW9cjoTpRRBTST8emCo5HCm61BrjMm7FYWfWyy6pX9hsfCeXC2z R6tAh9xgou69eoWaniDOOAOg2xrtw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrfedugdejhecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecukfhppeejuddrvdduvddrudehtddrvddtvdenucfrrghrrghmpehmrghilhhfrhho mheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvg hrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 287F0E4788; Tue, 16 Apr 2019 10:16:34 -0400 (EDT) Subject: Re: Alter domain type / avoiding table rewrite To: Tom Lane , Tim Kane Cc: PostgreSQL mailing lists References: <26216.1555423922@sss.pgh.pa.us> From: Adrian Klaver Message-ID: <9dc52f4b-94b5-813c-52ba-be22dec1cb93@aklaver.com> Date: Tue, 16 Apr 2019 07:16:33 -0700 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: <26216.1555423922@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 4/16/19 7:12 AM, Tom Lane wrote: > Tim Kane writes: >> 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) > >> 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. > > No, that's not going to work: coercing to a domain that has any > constraints is considered to require a rewrite. > > You could cast down to varchar(9) without a rewrite, and you could cast > from there to varchar(12) without a rewrite, and it should work to do that > in one step. I suspect the OP wants the type to text with a CHECK constraint to allow for increasing the length of field values in the future by just changing the CHECK setting. If that is the case would changing the type to text and then adding a CHECK NOT VALID work without too much pain? > > If you really want a domain in there, I'd try creating the domain without > any constraint, then doing the ALTER TABLE, then adding the constraint > with ALTER DOMAIN. But TBH, that "new_type" is going to be a huge > performance drag compared to plain varchar(12). I'd only recommend > using a domain when there is no other way to get the check you need. > PG just doesn't support domains very well (especially before the work > I did for v12...) > > regards, tom lane > > > -- Adrian Klaver adrian.klaver@aklaver.com