March 6th, 2011 by depesz | Tags: , , , , , , , , , | 4 comments »
Did it help? If yes - maybe you can help me?

On 12th of February, Robert Haas committed patch:

Teach ALTER TABLE .. SET DATA TYPE to avoid some table rewrites.
 
When the old type is binary coercible to the new type and the using
clause does not change the column contents, we can avoid a full table
rewrite, though any indexes on the affected columns will still need
to be rebuilt. This applies, for example, when changing a varchar
column to be of type text.
 
The prior coding assumed that the set of operations that force a
rewrite is identical to the set of operations that must be propagated
to tables making use of the affected table's rowtype. This is
no longer true: even though the tuples in those tables wouldn't
need to be modified, the data type change invalidate indexes built
using those composite type columns. Indexes on the table we're
actually modifying can be invalidated too, of course, but the
existing machinery is sufficient to handle that case.
 
Along the way, add some debugging messages that make it possible
to understand what operations ALTER TABLE is actually performing
in these cases.
 
Noah Misch and Robert Haas

Later on, on 15th, he committed second patch with few more cases where rewrite can be avoided.

This is not really new functionality, but rather (partial) removal of one of the biggest problems I ever had with PostgreSQL.

Consider table:

CREATE TABLE test (
x varchar(16)
);

If you'll decide that you need longer strings. Being smart guy, I assume that since I already had to increase it once, I will probably need to increase it again in the future, so I decide to use TEXT datatype.

ALTER TABLE test ALTER COLUMN x SET DATA TYPE text;

it will require full table lock, and rewrite, which can take very long time.

At least – up to 9.0.

So, let's see how it works. First, sanity check on 9.0:

$ CREATE TABLE test ( x varchar(16) );
CREATE TABLE
 
$ insert into test select i::text from generate_series(1,1000000) i;
INSERT 0 1000000

After these two commands, this table row in pg_class looks like this:

$ select * from pg_class where relname = 'test';
-[ RECORD 1 ]---+-------
relname | test
relnamespace | 2200
reltype | 16482
reloftype | 0
relowner | 16385
relam | 0
relfilenode | 16480
reltablespace | 0
relpages | 0
reltuples | 0
reltoastrelid | 0
reltoastidxid | 0
relhasindex | f
relisshared | f
relistemp | f
relkind | r
relnatts | 1
relchecks | 0
relhasoids | f
relhaspkey | f
relhasexclusion | f
relhasrules | f
relhastriggers | f
relhassubclass | f
relfrozenxid | 723
relacl | [null]
reloptions | [null]

Now, let's alter the table:

$ begin;
BEGIN
 
*$ alter table test alter column x set data type text;
ALTER TABLE
Time: 1001.682 ms
 
*$ select relation::regclass, mode from pg_locks where pid = pg_backend_pid() and relation is not null;
relation | mode
----------+---------------------
pg_locks | AccessShareLock
test | ShareLock
test | AccessExclusiveLock
16483 | AccessExclusiveLock
(4 rows)
 
*$ commit;

As you can see alter table took 1 second, and it obtained AccessExclusiveLock of test table.

Table row from pg_class:

$ select * from pg_class where relname = 'test';
-[ RECORD 1 ]---+-------
relname | test
relnamespace | 2200
reltype | 16482
reloftype | 0
relowner | 16385
relam | 0
relfilenode | 16483
reltablespace | 0
relpages | 0
reltuples | 0
reltoastrelid | 0
reltoastidxid | 0
relhasindex | f
relisshared | f
relistemp | f
relkind | r
relnatts | 1
relchecks | 0
relhasoids | f
relhaspkey | f
relhasexclusion | f
relhasrules | f
relhastriggers | f
relhassubclass | f
relfrozenxid | 726
relacl | [null]
reloptions | [null]

OK. So, sanity check complete, version 9.0 behaved as I described. What about 9.1?

$ CREATE TABLE test (
>> x varchar(16)
>> );
CREATE TABLE
 
$ insert into test select i::text from generate_series(1,1000000) i;
INSERT 0 1000000
 
$ select * from pg_class where relname = 'test';
-[ RECORD 1 ]--+-------
relname | test
relnamespace | 2200
reltype | 18663
reloftype | 0
relowner | 16387
relam | 0
relfilenode | 18661
reltablespace | 0
relpages | 0
reltuples | 0
reltoastrelid | 0
reltoastidxid | 0
relhasindex | f
relisshared | f
relpersistence | p
relkind | r
relnatts | 1
relchecks | 0
relhasoids | f
relhaspkey | f
relhasrules | f
relhastriggers | f
relhassubclass | f
relfrozenxid | 1090
relacl | [null]
reloptions | [null]

Test prepared, and now:

$ begin;
BEGIN
 
*$ alter table test alter column x set data type text;
ALTER TABLE
Time: 30.846 ms
 
Time: 0.391 ms
 
*$ select relation::regclass, mode from pg_locks where pid = pg_backend_pid() and relation is not null;
relation | mode
-------------------------------+---------------------
pg_toast.pg_toast_18661 | ShareLock
test | AccessExclusiveLock
pg_toast.pg_toast_18661_index | AccessExclusiveLock
pg_class | AccessShareLock
pg_class_oid_index | AccessShareLock
pg_locks | AccessShareLock
pg_class_relname_nsp_index | AccessShareLock
(7 rows)
 
*$ select * from pg_class where relname = 'test';
-[ RECORD 1 ]--+-------
relname | test
relnamespace | 2200
reltype | 18663
reloftype | 0
relowner | 16387
relam | 0
relfilenode | 18661
reltablespace | 0
relpages | 0
reltuples | 0
reltoastrelid | 18664
reltoastidxid | 0
relhasindex | f
relisshared | f
relpersistence | p
relkind | r
relnatts | 1
relchecks | 0
relhasoids | f
relhaspkey | f
relhasrules | f
relhastriggers | f
relhassubclass | f
relfrozenxid | 1090
relacl | [null]
reloptions | [null]

Here we can see three important informations:

  • relfilenode did not change – so it clearly proves that no rewrite happened
  • time spent by ALTER TABLE was 30ms, and not 1s
  • lock level didn't change – still AccessExclusiveLock on whole table

The last part could be troublesome, but given the very short time it means it's virtually irrelevant. Of course some caution is required, but the general disruption is much, much smaller.

One final note – while rewrite is avoided when changing from varchar() to text, it is not (as of now) avoided when changing varchar(n) to varchar(m)! Even for such a simple cases like increasing varchar(16) to varchar(32). So, if you have table with varchars you can easily change it to texts, and then add trigger/check to enforce length limit.

  1. 4 comments

  2. Jan 21, 2013

    What about int4 to int8?

    Did you do any research on that?

    Thanks.

  3. Jan 21, 2013

    Filip:
    int4 and int8 have different storage – basically it’s not “variable length”. So it will need to rewrite anyway.

  4. # ilmari
    Oct 16, 2013

    Note that as of PostgreSQL 9.2 lengthening varchar (and other similar fields) no longer requires a table rewrite.

    http://www.postgresql.org/docs/current/static/release-9-2.html#AEN116571

    «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.»

  5. Oct 16, 2013

    @Ilmari:
    I know: http://www.depesz.com/2012/02/14/waiting-for-9-2-more-rewrite-less-alter-table-alter-types/

Leave a comment