From: | Reid Thompson <reid(dot)thompson(at)ateb(dot)com> |
---|---|
To: | pgsql-general <pgsql-general(at)postgresql(dot)org> |
Subject: | Is this safe to perform on PostgreSQL 8.3.7 -> Resize a column in a PostgreSQL table without changing data |
Date: | 2011-11-22 14:50:05 |
Message-ID: | 4ECBB69D.7060805@ateb.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
reporting=# select version();
version
----------------------------------------------------------------------------------------------------------
PostgreSQL 8.3.7 on x86_64-redhat-linux-gnu, compiled by GCC gcc (GCC)
4.1.2 20071124 (Red Hat 4.1.2-42)
(1 row)
I've a parent table with several years of monthly partitioned children.
There has arisen a need to increase the max size of a couple of
varchar fields. Total size of these tables is approaching ~200 GB, with
the larger monthly tables approximately 7-10GB each.
Would it be safe to use the below process to accomplish this?
Whether I use the below method, or the standard
ALTER TABLE mytable ALTER COLUMN mycolumn TYPE varchar(new_size);
my assumption is that I should apply the change first to the child
tables, then to the parent???
From
http://sniptools.com/databases/resize-a-column-in-a-postgresql-table-without-changing-data
Resize a column in a PostgreSQL table without changing data
You use PostgreSQL. You find that a column you have in a table is of a
smaller length than you now wish. In my case, this was a varchar(20)
that I now wished to make varchar(35). Nothing else. I just want to
change the size, keeping the data intact.
The ALTER TABLE ...ALTER COLUMN...TYPE... command is useful only if
you want to alter the data somehow, or change the data type.
Otherwise, it'll be an aeon before this finishes even inside a
transaction on a database of any meaningful size.
Until now, I was not familiar with any sensible mechanism to
simply change the size in PG. But yesterday, Tom Lane himself
suggested something ubercool in the list.
Let's assume for the sake of simplicity that your table is called
"TABLE1" and your column is "COL1". You can find the size of your
"COL1" column by issuing the following query on the system tables:
SELECT atttypmod FROM pg_attribute
WHERE attrelid = 'TABLE1'::regclass
AND attname = 'COL1';
atttypmod
-----------
24
(1 ROW)
This means that the size is 20 (4 is added for legacy reasons, we're
told). You can now conveniently change this to a varchar(35) size by
issuing this command:
UPDATE pg_attribute SET atttypmod = 35+4
WHERE attrelid = 'TABLE1'::regclass
AND attname = 'COL1';
UPDATE 1
Note that I manually added the 4 to the desired size of 35..again, for
some legacy reasons inside PG. Done. That's it. Should we check?
d TABLE1
TABLE "public.TABLE1"
COLUMN | TYPE | Modifiers
--------+-----------------------+-----------
COL1 | CHARACTER VARYING(35) |
Such a simple yet effective trick. Of course it'd be nicer if this is
somehow included in a more proper way in the database, but this does
the job.
From | Date | Subject | |
---|---|---|---|
Next Message | Scott Marlowe | 2011-11-22 15:21:48 | Re: Is this safe to perform on PostgreSQL 8.3.7 -> Resize a column in a PostgreSQL table without changing data |
Previous Message | David Johnston | 2011-11-22 14:25:24 | Re: Why CASCADE constraint takes more time when table is loaded with huge records? |