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 1iufok-0005WW-L0 for pgsql-general@arkaria.postgresql.org; Thu, 23 Jan 2020 16:58:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iufoj-0003ae-5f for pgsql-general@arkaria.postgresql.org; Thu, 23 Jan 2020 16:58:53 +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 1iufoi-0003aX-PL for pgsql-general@lists.postgresql.org; Thu, 23 Jan 2020 16:58:52 +0000 Received: from mail-ot1-x342.google.com ([2607:f8b0:4864:20::342]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iufof-000497-Tr for pgsql-general@lists.postgresql.org; Thu, 23 Jan 2020 16:58:52 +0000 Received: by mail-ot1-x342.google.com with SMTP id g15so45343otp.3 for ; Thu, 23 Jan 2020 08:58:49 -0800 (PST) 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-language; bh=0QMUlb2QMEfKQzSvPkBsc+zjrfupf4yFf5PZorbihs8=; b=jC9KZ0taBV2qdwAEOL9inlo/e4pfHgcnc/QjlgFmu7xrdChsIuJop5KZgVxw9SFp5W 4azUZ95xgkzs75pcT+0mrSpSjZdAzIXs02KWgNnV3o9xxXVLW5NA7fJybJmWJyqpT4mb pptd+hZhiDgrbdGBJv832KVMT1lvEIVylHfThiDe7PGp8+gZ3TmY6mpcuWXf2pF43B6P vRbMPnl7w470EuRWPDP7pwbjgxDSq3pIJx5cflTPB7T3tyrfS10ijVPyT9g1tNRnSW1S F/opu6z/oM+gthj4/+zjqOb9ENl5AEOWZ32OMCZzQTQRg0GEaXKC9pM2wp8UIRInBXAx D5Ng== 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-language; bh=0QMUlb2QMEfKQzSvPkBsc+zjrfupf4yFf5PZorbihs8=; b=ocjVZW/DSmtAXKvoOAVvTzkNgioKGzE/Qn1MThtJQGkxUf38SN0ZTwF2EOo7Dw4xf2 0Lo6aTlWZSKsUlECT1vZ1BQdt1FoOfiCRsoct+1iwVE9laKdRXb+a2YdzbfGTc2SXZJV 1gRWXI3BGjiipmSAh8YF15Mw4hrSS4TRO25ZZuuODZvkGfDUoQKtrPjV0cP0EVCoOI2o GBTdX39tqE7Bwa/m5P3YB6f+NvwyCZXr/j/8JFrQAZ1EQ51jKNUhqhSsvt6t1AMHtir9 X49mHjxEGks30cKhmvfPYH23KffYJbTPq3B7qwrGnszyV8vKaa1gvidqUlpWPNKouFfz 3Ibw== X-Gm-Message-State: APjAAAWkhemEOU2VxSOD/Sqv7sJWv37+to77KKZA+nQ6+QB7/TEbDDUw 9V8zCOE4ezCs1SBg2XwQghMF86+A8l8= X-Google-Smtp-Source: APXvYqxZ0bPqUpZw38hFUqwMnUHL6ZVdjE2foUTzJIiwRcAGsdQpe/Nz/AeERwmh9K1zlneT0gHg2Q== X-Received: by 2002:a9d:6859:: with SMTP id c25mr12144353oto.205.1579798727946; Thu, 23 Jan 2020 08:58:47 -0800 (PST) Received: from [192.168.1.10] (ip70-186-126-197.no.no.cox.net. [70.186.126.197]) by smtp.googlemail.com with ESMTPSA id i6sm845038oie.12.2020.01.23.08.58.47 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 23 Jan 2020 08:58:47 -0800 (PST) Subject: Re: Does converting an indexed varchar to text rewrite its index? Docs say so, tests say no. To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <21b1efcb-a31f-1f05-c9ee-98e118965a69@gmail.com> Date: Thu, 23 Jan 2020 10:58:46 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------C0E5F97A69B10CFF4381EDAF" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------C0E5F97A69B10CFF4381EDAF Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Since you just built the index, and it's relatively small, maybe all the data is still cached. On 1/23/20 10: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?)* > > Thanks! > > Mike -- Angular momentum makes the world go 'round. --------------C0E5F97A69B10CFF4381EDAF Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit Since you just built the index, and it's relatively small, maybe all the data is still cached.

On 1/23/20 10: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:


> [...] 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?)

Thanks!

Mike

--
Angular momentum makes the world go 'round.
--------------C0E5F97A69B10CFF4381EDAF--