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 1iugh2-0008EF-1X for pgsql-general@arkaria.postgresql.org; Thu, 23 Jan 2020 17:55:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iugh0-0004DH-Qj for pgsql-general@arkaria.postgresql.org; Thu, 23 Jan 2020 17:54:58 +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 1iuggy-0004CE-QL for pgsql-general@lists.postgresql.org; Thu, 23 Jan 2020 17:54:58 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iuggt-0004ZO-UL for pgsql-general@lists.postgresql.org; Thu, 23 Jan 2020 17:54:56 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 36E7E220E4; Thu, 23 Jan 2020 12:54:50 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 23 Jan 2020 12:54:50 -0500 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=K iy0aIy/HPdsGlZJDIYWcOihwt1UpDECL2iNyTauJio=; b=N6X3TEbwvHZvxI2Ao aaRrkjsyy8VuHLJMnp9H3ShrrNc+tEpHA5yhGcWQMQXSVDUl11quaEdyK71ChkXn 6RVQuz0DLae22N1X9HJIVqH75afKfcWrckmoWiEfTfJIJReOuBS+gfKh4RmFy4Yu hUxwSZVo779edHWM2UAd7IkFjP5iZ3w9DoIz03yiXex67sp9I/HFUX0O/VFmgn7w mUHfbIxzlRWuhIwMGOudEJce1FUREl6ZpwayusQycnP3Plx3pcfdlE592hjNk4AI OT7GQPKTRfcaAjVICkvYrPrzBlA96oQm8OPbOcGd56Enct2+QOfT7+Ek8z2NxLyV iQfdg== 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=fm1; bh=Kiy0aIy/HPdsGlZJDIYWcOihwt1UpDECL2iNyTauJ io=; b=t+unFw+oPFUVbi4OOfe1VP/BDBSRUrRDS8VcMieuRpCElXHukJ7IsB/C+ O0OGKv2H/qnbEXEcxSYDeradJtPm0Nh2BT2W5SExrjj/+5Sb5QUzQTFJhogzGx4w MM5AhLlRDiD71aMzevOrfRTR42Dbqo0DrLLKnBRAwmyQxcJmRSkmnPd8iVtMTk8Q VxKV2Zfuniu1Cr/UsANVNQJDWDaqjoUxe9hx0PYU3/VMl5oMq4901YZGegUImrs9 mY8ODmW3eHhHpQAgJ+sG9Pe0qaFAwH4Pxp5CdUiGdF5yOO9PhWc6LEWrnpLzu/HC kyPtFgHWrlhgtLPgwX9kOA3HrWq9w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrvddvgdejvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepjedurddvuddv rdduvdelrdduleefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilh hfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id 62FA43280059; Thu, 23 Jan 2020 12:54:49 -0500 (EST) Subject: Re: Does converting an indexed varchar to text rewrite its index? Docs say so, tests say no. To: Mike Lissner , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: <18e832ea-4bae-8c47-fdfe-e2cf316f52ce@aklaver.com> Date: Thu, 23 Jan 2020 09:54:48 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.1 MIME-Version: 1.0 In-Reply-To: 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 1/23/20 8:55 AM, Mike Lissner wrote: > I think the docs say that if you convert a varchar to text, it'll > rewrite the index, but my test doesn't seem to indicate that. Is the > test or the documentation wrong? > > If the docs, I'll be happy to make a fix my first contribution to > postgresql. :) > > Here are the docs: > > (https://www.postgresql.org/docs/10/sql-altertable.html) > > > [...] changing the type of an existing column will require the entire > table and its indexes to be rewritten. As an exception when changing the > type of an existing column, if the USING clause does not change the > column contents and the old type is either binary coercible to the new > type or an unconstrained domain over the new type, a table rewrite is > not needed; but *any indexes on the affected columns must still be rebuilt.* > > And the test: > > postgres=# CREATE TABLE t1 (id serial PRIMARY KEY, name character > varying(30)); > CREATE TABLE > Time: 25.927 ms > postgres=# INSERT INTO t1 (id) SELECT generate_series(1,1000000) i; > INSERT 0 1000000 > Time: 2080.416 ms (00:02.080) > postgres=# CREATE INDEX ON t1 (name); > CREATE INDEX > Time: 463.373 ms *<-- Index takes ~500ms to build* > postgres=# ALTER TABLE t1 ALTER COLUMN name TYPE text; > ALTER TABLE > Time: 19.698 ms *<-- Alter takes 20ms to run (no rebuild, right?)* I going to say it is the exception to the exception, in that in Postgres varchar and text are essentially the same type. FYI there is a reindex going on: test=> set client_min_messages = debug1; test=> CREATE TABLE t1 (id serial PRIMARY KEY, name character varying(30)); LOG: statement: CREATE TABLE t1 (id serial PRIMARY KEY, name character varying(30)); DEBUG: CREATE TABLE will create implicit sequence "t1_id_seq" for serial column "t1.id" DEBUG: CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1" DEBUG: building index "t1_pkey" on table "t1" serially CREATE TABLE test=> INSERT INTO t1 (id) SELECT generate_series(1,1000000) i; LOG: statement: INSERT INTO t1 (id) SELECT generate_series(1,1000000) i; INSERT 0 1000000 test=> CREATE INDEX ON t1 (name); LOG: statement: CREATE INDEX ON t1 (name); DEBUG: building index "t1_name_idx" on table "t1" with request for 1 parallel worker CREATE INDEX test=> ALTER TABLE t1 ALTER COLUMN name TYPE text; LOG: statement: ALTER TABLE t1 ALTER COLUMN name TYPE text; DEBUG: building index "pg_toast_37609_index" on table "pg_toast_37609" serially ALTER TABLE > > Thanks! > > Mike > ** -- Adrian Klaver adrian.klaver@aklaver.com