From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Tim Kane <tim.kane@gmail.com>
Cc: Ron <ronljohnsonjr@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Alter domain type / avoiding table rewrite
Date: Wed, 17 Apr 2019 09:15:25 -0700
Message-ID: <53d45372-e189-317e-b4c3-6317cb195b11@aklaver.com> (raw)
In-Reply-To: <CADVWZZKLGc5xovmeyKmXVA76vLNLw5quHy=yDy_Onxc-ahco7A@mail.gmail.com>
References: <CADVWZZKjhV9fLpewPdQMZx7V6kvGJViwMEDrPAv9m50rGeK9UA@mail.gmail.com>
<26216.1555423922@sss.pgh.pa.us>
<9dc52f4b-94b5-813c-52ba-be22dec1cb93@aklaver.com>
<27283.1555425748@sss.pgh.pa.us>
<c2a2f6f0-9e85-3c43-54d5-b68c592695ed@gmail.com>
<CADVWZZ+E2GUPnj8_JoPkh05d9t31HqBs4SfSVHY8YWPJfKZ=Ug@mail.gmail.com>
<2f57aafd-f034-0f82-5739-614607e4ba0e@aklaver.com>
<CADVWZZK0iyYMraqntj2+fYEaqb+dWU8p8suGU3248UEG7g3ffA@mail.gmail.com>
<8f3295e8-4be9-e40a-f16d-af33eab1176c@aklaver.com>
<CADVWZZKLGc5xovmeyKmXVA76vLNLw5quHy=yDy_Onxc-ahco7A@mail.gmail.com>
On 4/17/19 8:34 AM, Tim Kane wrote:
>
>
> On Wed, 17 Apr 2019 at 15:23, Adrian Klaver <adrian.klaver@aklaver.com
> <mailto:adrian.klaver@aklaver.com>> wrote:
>
>
>
> The table definition and the size of the data set would help with
> interpreting the below.
>
>
>
>
> The below example shows the issue on a table with just a single field. I
> can demonstrate the problem wether there are 100 records or a million
> records.
>
> In every case:
> Altering the type from a domain of varchar(9) to a raw varchar(9)
> results in a full table rewrite (as identified by relfilenode).
> Altering the type from a raw varchar(9) to a domain of varchar(9)
> occurs for free, with no change to relfilenode.
>
> The timing of each ALTER operation appears to back this up.
>
>
I stand corrected. The logs back it up also. See log entries inline below.
>
>
>
> postgres@[local]=# create domain old_type as varchar(9);
> CREATE DOMAIN
>
> postgres@[local]=# create table test (values old_type);
> CREATE TABLE
>
> postgres@[local]=# with data as (select generate_series(1,1000000),
> md5(random()::text))
> postgres@[local]-# insert into test select substring(md5, 1, 9) from data;
> INSERT 0 1000000 <tel:0%201000000>
> Time: 4097.162 ms
>
> postgres@[local]=# \d test
> Table "alpha_core.test"
> Column | Type | Modifiers
> --------+----------+-----------
> values | old_type |
>
> postgres@[local]=# \dD old_type
> List of domains
> Schema | Name | Type | Modifier | Check
> ------------+----------+----------------------+----------+-------
> alpha_core | old_type | character varying(9) | |
> (1 row)
>
> postgres@[local]=# select count(*) from test;
> count
> ---------
> 1000000 <tel:1000000>
> (1 row)
>
>
>
> postgres@[local]=# select relfilenode from pg_class where relname='test';
> relfilenode
> -------------
> 20689856 <tel:20689856>
> (1 row)
>
>
> postgres@[local]=# alter table test alter COLUMN values set data type
> varchar(9);
> ALTER TABLE
> Time: 993.271 ms
aklaver-2019-04-17 09:06:47.854 PDT-0LOG: statement: alter table test
alter COLUMN values set data type varchar(9);
aklaver-2019-04-17 09:06:47.884 PDT-38177DEBUG: rewriting table "test"
>
>
> postgres@[local]=# select relfilenode from pg_class where relname='test';
> relfilenode
> -------------
> 20691283 <tel:20691283>
> (1 row)
>
> postgres@[local]=# alter table test alter COLUMN values set data type
> old_type;
> ALTER TABLE
> Time: 21.569 ms
aklaver-2019-04-17 09:07:46.027 PDT-0LOG: statement: alter table test
alter COLUMN values set data type old_type;
aklaver-2019-04-17 09:07:46.027 PDT-38178DEBUG: building index
"pg_toast_668193_index" on table "pg_toast_668193" serially
>
>
> postgres@[local]=# select relfilenode from pg_class where relname='test';
> relfilenode
> -------------
> 20691283 <tel:20691283>
> (1 row)
>
> postgres@[local]=# drop table test;
> DROP TABLE
>
--
Adrian Klaver
adrian.klaver@aklaver.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, tim.kane@gmail.com, ronljohnsonjr@gmail.com
Subject: Re: Alter domain type / avoiding table rewrite
In-Reply-To: <53d45372-e189-317e-b4c3-6317cb195b11@aklaver.com>
References: <CADVWZZKjhV9fLpewPdQMZx7V6kvGJViwMEDrPAv9m50rGeK9UA@mail.gmail.com> <26216.1555423922@sss.pgh.pa.us> <9dc52f4b-94b5-813c-52ba-be22dec1cb93@aklaver.com> <27283.1555425748@sss.pgh.pa.us> <c2a2f6f0-9e85-3c43-54d5-b68c592695ed@gmail.com> <CADVWZZ+E2GUPnj8_JoPkh05d9t31HqBs4SfSVHY8YWPJfKZ=Ug@mail.gmail.com> <2f57aafd-f034-0f82-5739-614607e4ba0e@aklaver.com> <CADVWZZK0iyYMraqntj2+fYEaqb+dWU8p8suGU3248UEG7g3ffA@mail.gmail.com> <8f3295e8-4be9-e40a-f16d-af33eab1176c@aklaver.com> <CADVWZZKLGc5xovmeyKmXVA76vLNLw5quHy=yDy_Onxc-ahco7A@mail.gmail.com> <53d45372-e189-317e-b4c3-6317cb195b11@aklaver.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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