Resize a column in a PostgreSQL table without changing data

Written by Shanx January 13th, 2009

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 [...]

Continue reading →
Close

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.

Miscellaneous

I use the Nokia e61i as my mobile. Instead of my telco’s data plan (which offers me a meagre 1GB per month) I simply prefer to use my home wireless [...]

Continue reading →

View all

Web Tools

If you use Firefox (and if not, what are you waiting for?) you are familiar with useful extensions such as Video Downloader, which allow you to save local copies of [...]

Continue reading →

View all

Databases

This regexp worked for me. SELECT * FROM table WHERE NOT column ~ ( ‘^(‘|| $$[\09\0A\0D\x20-\x7E]|$$|| — ASCII $$[\xC2-\xDF][\x80-\xBF]|$$|| — non-overlong 2-byte $$\xE0[\xA0-\xBF][\x80-\xBF]|$$|| — excluding overlongs $$[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|$$|| — straight 3-byte [...]

Continue reading →

View all

Windows

So you’ve been visited by the much dreaded CRC — Cyclical Redundancy Check error, most likely encountered while copying files between hard disks. On Mac OSX, this will usually appear [...]

Continue reading →

View all

Mac OSX

A simple app ought to do it. Download iRinger. It’s a Windows app. If you’re on Mac, you’ll want to use it within a virtual machine, like Parallels or VMWare [...]

Continue reading →

View all

System Maintenance

I use the Nokia e61i as my mobile. Instead of my telco’s data plan (which offers me a meagre 1GB per month) I simply prefer to use my home wireless [...]

Continue reading →

View all

Wordpress

Among many new exciting features, WordPress 2.6 released the ability to store each and every revision of your posts, like an elaborate update history. Now this can be a pretty [...]

Continue reading →

View all

Audio/Video

Panic, the makers of some fantastic software such as Transmit or Panic, also have the most light-weight audio converter for the Mac OSX platform. It’s called Audion: get it here. [...]

Continue reading →

View all

iPhone

A simple app ought to do it. Download iRinger. It’s a Windows app. If you’re on Mac, you’ll want to use it within a virtual machine, like Parallels or VMWare [...]

Continue reading →

View all