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 1hGP4R-0005Ms-MS for pgsql-general@arkaria.postgresql.org; Tue, 16 Apr 2019 14:28:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hGP4Q-00064k-7K for pgsql-general@arkaria.postgresql.org; Tue, 16 Apr 2019 14:28:22 +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 1hGP4P-00062o-R6 for pgsql-general@lists.postgresql.org; Tue, 16 Apr 2019 14:28:21 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGP4N-00085L-74 for pgsql-general@lists.postgresql.org; Tue, 16 Apr 2019 14:28:21 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 0B84823648; Tue, 16 Apr 2019 10:28:18 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 16 Apr 2019 10:28:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=o q2tadycwpo8orHdJrC4GHHfFfROwZcJ7Sb+FvvlNRc=; b=mnsqnsX3F7x0BeQKz 0iooojkQ3nCqJjbrQqvoN5u8KqU/6A4UJYy+LttjLEWSQFTBnVOG8fZt9VdKzgJE xoCbWy/PbR9bf+EWg4aqiOLTP5SHYTQYGpXv/LB2yhOMzHmzQuAHGxpOYlXiARr2 8QoLdSRw8XbAUE+l9751XseUUBa00nPuiN9cbpP9DuHeTT+zS7epNUd5SRyhseX4 FL54J5jLbHSXS7gTdWRk7wEAqaNZEz1uKxUndybJyzsJR7z4XIhZP36U1y5oke1h 7Ij2PSSEv7uO5iYz9ux6QayohyoLF1vNIaIcjxQwGvQXjNbZIV96m+trFCvQpKME ccUKQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=oq2tadycwpo8orHdJrC4GHHfFfROwZcJ7Sb+FvvlN Rc=; b=p10plGcVbfFFzwsYksDJYMmVliJOhJcB7wto6I7aXhifS4zP+xYPl28uR B4CXVAohDRoX56FPQa7STn3dlIRaRtgC1bkClb0LbHzVJpRJ9R05tVAFLvi6y0nA epl2P5/9ipog2AfjXaI0ZYUkSEhMuGZhq9WG8eexQ2unwBH0lBstrAxzVUZ0NF6r njgfO8rVUr7FCvELY0WbhCX3uwijpnxMyl+At4yza7NBB0dJJpOkom//kVbnzKy2 N2O624YQr2AvLrYVSQvPAUHErl7t4Bx21Z0jhdS9uMwssoaqaL3o6Q88cV1LElNP 5qGEGi6/xtUC0+CyFepq9lXoPrbug== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrfedugdejjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthekre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucffohhmrghinhepphhoshhtghhrvghsqh hlrdhorhhgnecukfhppeejuddrvdduvddrudehtddrvddtvdenucfrrghrrghmpehmrghi lhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlh hushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 150FCE4AD4; Tue, 16 Apr 2019 10:28:16 -0400 (EDT) Subject: Re: Alter domain type / avoiding table rewrite To: Ron , pgsql-general@lists.postgresql.org References: <54c9d5ab-9be5-1499-1c36-f45ea33cc322@gmail.com> From: Adrian Klaver Message-ID: <1bd403de-2552-7986-70ec-ac25e7717805@aklaver.com> Date: Tue, 16 Apr 2019 07:28:16 -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: <54c9d5ab-9be5-1499-1c36-f45ea33cc322@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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. 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 (that is, they'll fail unless there is a matching row in the referenced table, in the case of foreign keys; and they'll fail unless the new row matches the specified check constraints). But the database will not assume that the constraint holds for all rows in the table, until it is validated by using the VALIDATE CONSTRAINT option." -- Adrian Klaver adrian.klaver@aklaver.com