Re: Is it safe to increase pg_attribute.atttypmod ?

Lists: pgsql-admin
From: "Nick Fankhauser" <nickf(at)ontko(dot)com>
To: "pgsql-admin" <pgsql-admin(at)postgresql(dot)org>
Cc: "Ray Ontko" <rayo(at)ontko(dot)com>
Subject: Is it safe to increase pg_attribute.atttypmod ?
Date: 2002-05-20 20:56:42
Message-ID: NEBBLAAHGLEEPCGOBHDGEEPDEPAA.nickf@ontko.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin

Hi-

We need to increase the length of a varchar field, and couldn't find a way
to do it using "alter table". Since the alternative is copying & re-creating
a very large table, we starting poking around under the hood & found that
pg_attribute.atttypmod seems to be always equal to the length plus 4 in the
case of varchar fields.

We created a test table and altered the varchar length on a field by
increasing pg_attribute.atttypmod, and the length seemed to change and act
as expected.

The update to change a varchar(10) field to varchar(40) looked like this:

update pg_attribute set atttypmod = 44
where attrelid = ( select oid from pg_class where relname = 'test' )
and attname = 'oldtest' ;

Is this a smart thing to do?

Are there other hidden related bits of data that will come back to haunt us
later?

-Nick

--------------------------------------------------------------------------
Nick Fankhauser nickf(at)ontko(dot)com Phone 1.765.935.4283 Fax 1.765.962.9788
Ray Ontko & Co. Software Consulting Services http://www.ontko.com/


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: nickf(at)ontko(dot)com
Cc: "pgsql-admin" <pgsql-admin(at)postgresql(dot)org>, "Ray Ontko" <rayo(at)ontko(dot)com>
Subject: Re: Is it safe to increase pg_attribute.atttypmod ?
Date: 2002-05-21 00:17:15
Message-ID: 16447.1021940235@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin

"Nick Fankhauser" <nickf(at)ontko(dot)com> writes:
> The update to change a varchar(10) field to varchar(40) looked like this:

> update pg_attribute set atttypmod = 44
> where attrelid = ( select oid from pg_class where relname = 'test' )
> and attname = 'oldtest' ;

> Is this a smart thing to do?

Yup, that's the standard hack.

regards, tom lane


From: Joe Conway <mail(at)joeconway(dot)com>
To: nickf(at)ontko(dot)com
Cc: pgsql-admin <pgsql-admin(at)postgresql(dot)org>, Ray Ontko <rayo(at)ontko(dot)com>
Subject: Re: Is it safe to increase pg_attribute.atttypmod ?
Date: 2002-05-21 00:49:03
Message-ID: 3CE9997F.4030307@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin

Nick Fankhauser wrote:
> Hi-
>
> We need to increase the length of a varchar field, and couldn't find
> a way to do it using "alter table". Since the alternative is copying
> & re-creating a very large table, we starting poking around under
> the hood & found that pg_attribute.atttypmod seems to be always
> equal to the length plus 4 in the case of varchar fields.

Yes, the first 4 bytes are for an integer which represents length, and
the rest is the data itself.

>
> We created a test table and altered the varchar length on a field by
> increasing pg_attribute.atttypmod, and the length seemed to change
> and act as expected.
>
> The update to change a varchar(10) field to varchar(40) looked like
> this:
>
> update pg_attribute set atttypmod = 44 where attrelid = ( select oid
> from pg_class where relname = 'test' ) and attname = 'oldtest' ;
>
> Is this a smart thing to do?
>
> Are there other hidden related bits of data that will come back to
> haunt us later?
>

IIRC, this has been discussed a number of times before on one or another
of the PostgreSQL lists. The short answer, I think, is that the
procedure you describe should work just fine (but I do recommend you
take a look at the past threads on the subject). You may need to be a
bit more careful if you were going the other direction, i.e. varchar(40)
to varchar(10).

There was some talk of embedding this knowledge in an ALTER TABLE ALTER
COLUMN command for 7.3, but I haven't seen anything on that recently.

HTH,

Joe


From: "Nick Fankhauser" <nickf(at)ontko(dot)com>
To: <mail(at)joeconway(dot)com>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "pgsql-admin" <pgsql-admin(at)postgresql(dot)org>, "Ray Ontko" <rayo(at)ontko(dot)com>
Subject: Re: Is it safe to increase pg_attribute.atttypmod ?
Date: 2002-05-21 13:33:00
Message-ID: NEBBLAAHGLEEPCGOBHDGKEABFAAA.nickf@ontko.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin

Tom, Joe:

> Yup, that's the standard hack.

Thanks very much! This saved us hours.

-Nick