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 1hGOoh-0004DS-Vb for pgsql-general@arkaria.postgresql.org; Tue, 16 Apr 2019 14:12:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hGOog-00048n-LT for pgsql-general@arkaria.postgresql.org; Tue, 16 Apr 2019 14:12: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 1hGOog-00048g-CB for pgsql-general@lists.postgresql.org; Tue, 16 Apr 2019 14:12:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGOoe-0007dS-5V for pgsql-general@postgresql.org; Tue, 16 Apr 2019 14:12:06 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x3GEC2Jj026217; Tue, 16 Apr 2019 10:12:02 -0400 From: Tom Lane To: Tim Kane cc: PostgreSQL mailing lists Subject: Re: Alter domain type / avoiding table rewrite In-reply-to: References: Comments: In-reply-to Tim Kane message dated "Tue, 16 Apr 2019 10:22:27 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <26215.1555423922.1@sss.pgh.pa.us> Date: Tue, 16 Apr 2019 10:12:02 -0400 Message-ID: <26216.1555423922@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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. 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