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 1j3i0N-00056y-Au for pgsql-general@arkaria.postgresql.org; Mon, 17 Feb 2020 15:08:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j3i0L-0000nT-V0 for pgsql-general@arkaria.postgresql.org; Mon, 17 Feb 2020 15:08:13 +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 1j3i0L-0000nM-Iz for pgsql-general@lists.postgresql.org; Mon, 17 Feb 2020 15:08:13 +0000 Received: from mail-ot1-x330.google.com ([2607:f8b0:4864:20::330]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1j3i0F-0008T7-2n for pgsql-general@lists.postgresql.org; Mon, 17 Feb 2020 15:08:13 +0000 Received: by mail-ot1-x330.google.com with SMTP id j20so16382894otq.3 for ; Mon, 17 Feb 2020 07:08:06 -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=BX2VXepKzABkj1MCSX7yPAhiJNa1g8JyTuykotztpgo=; b=SUZA67PBxQ7T2itG25NMY0P/xUaUXXGD5svqUXx0QJfGuumgfLDozrhAv4YwCkbsmf PdvNbY2QqLcUhcNlryyebjyJ6FqHg85nHrljqixLzKTrACv7Vlg/tD9rSrmi5JdIK55g den9+gN1bGv2Rezva+/gRRO0zZvPDIk8aVzHrQragcWF5fNUqPdeGesEYuZmKy5/mx3n joljHxDl6yQcknh8FgCDLneqgbKuhjGrwHqmscDLm6sLf/WDNPeOoMZFWQ5s5Vv59tpm lWWBN/6MbIwLrtF5MHRvUVev70zkHfBgAqR57XA+i2T41kJZRn/qp3uK7XnPWzV91M4g LFcg== 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=BX2VXepKzABkj1MCSX7yPAhiJNa1g8JyTuykotztpgo=; b=d7Cdpkwz2HGN6XfBxT8dl7RWHlc+BOEElwI80xZlAs4DB2jbzjD1vVhX/4c76k45cS 3+lk9dVuNAWG2CPIB25cUw10lbHP9HjjOUeeHSwrr+vEQ17CB5BbhfO+JkrM7tQaisEZ 8TjIP4tzCEHwQzUjokPBU+QsoJw9DE7S9mS3r3oNRCK6vkq1avUgSc1t6um9lSkM2a/h 6/9dGC+TosdvRnHb1OUzw19Fg5b8yU5FAEpKxI+jKxfd7G3qap2wCbON1y3SzesdDd9u 3ScPPwf5QMVB2znLSswvxzyeqhiR0G0n4/BaP4UQpYBuE7d/yk+1UgRCz+PNcVwQzM5E MUuA== X-Gm-Message-State: APjAAAWACbe26qmZfCENfuv4IPl1SnJqaLKLyZmyS0JNaR+ZxlLDfvN1 u0RiXuEtmzn+i2do8EK+47pPphADx4w= X-Google-Smtp-Source: APXvYqzQPL+a2Pc4qtZnMtSMQ+qbSfH5bvDXKLJ8ufn3ruHz1O1lVIp8ft2I+lRE69jum/iwMscOZg== X-Received: by 2002:a05:6830:c7:: with SMTP id x7mr11888237oto.272.1581952084786; Mon, 17 Feb 2020 07:08:04 -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 b145sm199063oii.31.2020.02.17.07.08.04 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 17 Feb 2020 07:08:04 -0800 (PST) Subject: Re: Cases where alter table set type varchar(longer length) still needs table rewrite To: pgsql-general@lists.postgresql.org References: <573.1581949272@sss.pgh.pa.us> From: Ron Message-ID: Date: Mon, 17 Feb 2020 09:08:03 -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="------------CB3D4B1E0A72ADE47E0CE989" 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. --------------CB3D4B1E0A72ADE47E0CE989 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 2/17/20 9:01 AM, Jeremy Finzel wrote: > On Mon, Feb 17, 2020 at 8:21 AM Tom Lane > wrote: > > Jeremy Finzel > writes: > > I have a table foo with 100 million rows, and a column: > >    - id character varying(20) > > The following command is the one that we expect to execute very > quickly (we > > are not seeing any locking), but it is instead taking a very long time: > >    - ALTER TABLE foo ALTER COLUMN id TYPE varchar(100); > > Hm, the code is supposed to avoid a table rewrite, but I wonder if > there's something else that's not being avoided, such as an index > rebuild or foreign-key verification.  Could we see the whole table > definition, eg from psql \d+ ? > >                         regards, tom lane > > > Based on your feedback, I quickly identified that indeed, the following > index is causing the re-type to be slow: > > "id_idx" btree ("substring"(id::text, 4, 7)) > > I'm still not sure why a rebuild of this index would be required, > technically speaking.  But perhaps in any case the docs should have > something to the effect that expression indexes may require rebuild under > specific circumstances? How much faster would "it" be if you dropped the index, ran ALTER and rebuilt the index?  Or is it too late? -- Angular momentum makes the world go 'round. --------------CB3D4B1E0A72ADE47E0CE989 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit On 2/17/20 9:01 AM, Jeremy Finzel wrote:
On Mon, Feb 17, 2020 at 8:21 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Jeremy Finzel <finzelj@gmail.com> writes:
> I have a table foo with 100 million rows, and a column:
>    - id character varying(20)
> The following command is the one that we expect to execute very quickly (we
> are not seeing any locking), but it is instead taking a very long time:
>    - ALTER TABLE foo ALTER COLUMN id TYPE varchar(100);

Hm, the code is supposed to avoid a table rewrite, but I wonder if
there's something else that's not being avoided, such as an index
rebuild or foreign-key verification.  Could we see the whole table
definition, eg from psql \d+ ?

                        regards, tom lane

Based on your feedback, I quickly identified that indeed, the following index is causing the re-type to be slow:

"id_idx" btree ("substring"(id::text, 4, 7))

I'm still not sure why a rebuild of this index would be required, technically speaking.  But perhaps in any case the docs should have something to the effect that expression indexes may require rebuild under specific circumstances?

How much faster would "it" be if you dropped the index, ran ALTER and rebuilt the index?  Or is it too late?

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