pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Cases where alter table set type varchar(longer length) still needs table rewrite
7+ messages / 4 participants
[nested] [flat]

* Cases where alter table set type varchar(longer length) still needs table rewrite
@ 2020-02-17 14:04  Jeremy Finzel <finzelj@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Jeremy Finzel @ 2020-02-17 14:04 UTC (permalink / raw)
  To: pgsql-general

Good morning!

We are a little bit puzzled because running the following command on a 9.6
cluster is apparently requiring a table rewrite, or at least a very long
operation of some kind, even though the docs say that as of 9.2:


   -

   Increasing the length limit for a varchar or varbit column, or removing
   the limit altogether, no longer requires a table rewrite. Similarly,
   increasing the allowable precision of a numeric column, or changing a
   column from constrained numeric to unconstrained numeric, no longer
   requires a table rewrite. Table rewrites are also avoided in similar cases
   involving the interval, timestamp, and timestamptz types.

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);

I attempted the same operation instead cast to text - same problem.

Why do the docs seem wrong in our case?  I have a guess: if the table was
created prior to version 9.2, perhaps they are not binary coercible to text
after 9.2?  In any case, I would be very grateful for an explanation!


Thank you!
Jeremy

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Cases where alter table set type varchar(longer length) still needs table rewrite
@ 2020-02-17 14:21  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Jeremy Finzel <finzelj@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 2020-02-17 14:21 UTC (permalink / raw)
  To: Jeremy Finzel <finzelj@gmail.com>; +Cc: pgsql-general

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





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Cases where alter table set type varchar(longer length) still needs table rewrite
@ 2020-02-17 15:01  Jeremy Finzel <finzelj@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 7+ messages in thread

From: Jeremy Finzel @ 2020-02-17 15:01 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general

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?

Thanks!
Jeremy

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Cases where alter table set type varchar(longer length) still needs table rewrite
@ 2020-02-17 15:08  Ron <ronljohnsonjr@gmail.com>
  parent: Jeremy Finzel <finzelj@gmail.com>
  1 sibling, 0 replies; 7+ messages in thread

From: Ron @ 2020-02-17 15:08 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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 
> <mailto:tgl@sss.pgh.pa.us>> wrote:
>
>     Jeremy Finzel <finzelj@gmail.com <mailto: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.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Cases where alter table set type varchar(longer length) still needs table rewrite
@ 2020-02-17 16:46  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Jeremy Finzel <finzelj@gmail.com>
  1 sibling, 2 replies; 7+ messages in thread

From: Adrian Klaver @ 2020-02-17 16:46 UTC (permalink / raw)
  To: Jeremy Finzel <finzelj@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general

On 2/17/20 7:01 AM, Jeremy Finzel wrote:
> On Mon, Feb 17, 2020 at 8:21 AM Tom Lane <tgl@sss.pgh.pa.us 
> <mailto:tgl@sss.pgh.pa.us>> wrote:
> 
>     Jeremy Finzel <finzelj@gmail.com <mailto: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 about?:

https://www.postgresql.org/docs/9.6/sql-altertable.html

"Adding a column with a DEFAULT clause or 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. Adding or removing a system oid 
column also requires rewriting the entire table. Table and/or index 
rebuilds may take a significant amount of time for a large table; and 
will temporarily require as much as double the disk space."

> 
> Thanks!
> Jeremy


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Cases where alter table set type varchar(longer length) still needs table rewrite
@ 2020-02-17 18:19  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  1 sibling, 0 replies; 7+ messages in thread

From: Tom Lane @ 2020-02-17 18:19 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: Jeremy Finzel <finzelj@gmail.com>; pgsql-general

Adrian Klaver <adrian.klaver@aklaver.com> writes:
> On 2/17/20 7:01 AM, Jeremy Finzel wrote:
>> 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?

> [ it is already ]

Yeah.  In principle, we wouldn't need to rebuild the indexes in this
case, since there's no semantic difference between a value sourced
from a varchar(N) column and a varchar(some-other-N) column.  In general,
though, ALTER COLUMN TYPE doesn't know whether that's true; and there
are definitely binary-compatible cases where it *does* matter.
(An example is that coercing integer to OID is allowed without a
table rewrite, but an index rebuild is needed because the sort
order is different.)  So right now, any index mentioning the altered
column has to be rebuilt.

Maybe someday we'll figure out how to do better.  I'm kind of wondering
whether it wouldn't be safe to assume that changes that only change the
typmod and not the type OID don't require index rebuilds.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Cases where alter table set type varchar(longer length) still needs table rewrite
@ 2020-02-17 18:39  Jeremy Finzel <finzelj@gmail.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  1 sibling, 0 replies; 7+ messages in thread

From: Jeremy Finzel @ 2020-02-17 18:39 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-general

On Mon, Feb 17, 2020 at 10:46 AM Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

>
> How about?:
>
> https://www.postgresql.org/docs/9.6/sql-altertable.html
>
> "Adding a column with a DEFAULT clause or 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. Adding or removing a system oid
> column also requires rewriting the entire table. Table and/or index
> rebuilds may take a significant amount of time for a large table; and
> will temporarily require as much as double the disk space."
>
> >
> > Thanks!
> > Jeremy
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com


You mean the part "any indexes on the affected columns must still be
rebuilt"?  Yes, I guess that is pretty clear.  Thanks,

Jeremy

^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2020-02-17 18:39 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-02-17 14:04 Cases where alter table set type varchar(longer length) still needs table rewrite Jeremy Finzel <finzelj@gmail.com>
2020-02-17 14:21 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-02-17 15:01   ` Jeremy Finzel <finzelj@gmail.com>
2020-02-17 15:08     ` Ron <ronljohnsonjr@gmail.com>
2020-02-17 16:46     ` Adrian Klaver <adrian.klaver@aklaver.com>
2020-02-17 18:19       ` Tom Lane <tgl@sss.pgh.pa.us>
2020-02-17 18:39       ` Jeremy Finzel <finzelj@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox