pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedLength of Varchar
6+ messages / 5 participants
[nested] [flat]
* Length of Varchar
@ 2003-01-24 06:37 Andre Schubert <andre@km3.de>
0 siblings, 1 reply; 6+ messages in thread
From: Andre Schubert @ 2003-01-24 06:37 UTC (permalink / raw)
To: pgsql-admin
Hi all,
i have a little question on changing the length of a varchar field.
Is there another way than dump and reload if i want to change the length
of a varchar field ?
I have search the idocs and found some docs about the system-table pg_attribute,
where the length of a varchar-field is stored in atttypmod.
Is it possible to change the value of atttypmod and is it safe to change this
value?
Thanks in advance
as
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Length of Varchar
@ 2003-01-24 08:26 Ian Barwick <barwick@gmx.net>
parent: Andre Schubert <andre@km3.de>
0 siblings, 1 reply; 6+ messages in thread
From: Ian Barwick @ 2003-01-24 08:26 UTC (permalink / raw)
To: Andre Schubert <andre@km3.de>; pgsql-admin
On Friday 24 January 2003 07:37, Andre Schubert wrote:
> i have a little question on changing the length of a varchar field.
> Is there another way than dump and reload if i want to change the length
> of a varchar field ?
in 7.3:
BEGIN;
ALTER TABLE foo RENAME your_field TO your_field_old;
ALTER TABLE foo ADD COLUMN your_field VARCHAR([new length]);
UPDATE foo SET your_field=your_field_old;
ALTER TABLE foo DROP COLUMN your_field_old;
COMMIT;
In 7.3, if the new column is shorter you may need to truncate the values
being inserted.
In versions < 7.3 you will not be able to drop the old column; there you
may want to recreate the table, there's a techdoc article here:
http://techdocs.postgresql.org/techdocs/updatingcolumns.php
> I have search the idocs and found some docs about the system-table
> pg_attribute, where the length of a varchar-field is stored in atttypmod.
> Is it possible to change the value of atttypmod and is it safe to change
> this value?
Possible but probably not safe. No doubt someone will be along shortly
with a more accurate opinion ;-).
Ian Barwick
barwick@gmx.net
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Length of Varchar
@ 2003-01-24 10:33 Ian Barwick <barwick@gmx.net>
parent: Ian Barwick <barwick@gmx.net>
0 siblings, 1 reply; 6+ messages in thread
From: Ian Barwick @ 2003-01-24 10:33 UTC (permalink / raw)
To: Andre Schubert <andre@km3.de>; pgsql-admin
On Friday 24 January 2003 09:26, Ian Barwick wrote:
> On Friday 24 January 2003 07:37, Andre Schubert wrote:
> > i have a little question on changing the length of a varchar field.
> > Is there another way than dump and reload if i want to change the length
> > of a varchar field ?
>
> in 7.3:
>
> BEGIN;
> ALTER TABLE foo RENAME your_field TO your_field_old;
> ALTER TABLE foo ADD COLUMN your_field VARCHAR([new length]);
> UPDATE foo SET your_field=your_field_old;
> ALTER TABLE foo DROP COLUMN your_field_old;
> COMMIT;
>
> In 7.3, if the new column is shorter you may need to truncate the values
> being inserted.
>
> In versions < 7.3 you will not be able to drop the old column; there you
> may want to recreate the table, there's a techdoc article here:
> http://techdocs.postgresql.org/techdocs/updatingcolumns.php
Addendum:
Someone has written me a private email pointing out that if you do
recreate a table like this, dependent views etc. will no longer work,
and asks if there is any way around this apart from upgrading
to 7.3. I think the short answer is "no". (As in there's "no such thing
as a free lunch" ;-)
Ian Barwick
barwick@gmx.net
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Length of Varchar
@ 2003-01-24 14:30 Robert Treat <xzilla@users.sourceforge.net>
parent: Ian Barwick <barwick@gmx.net>
0 siblings, 1 reply; 6+ messages in thread
From: Robert Treat @ 2003-01-24 14:30 UTC (permalink / raw)
To: Ian Barwick <barwick@gmx.net>; +Cc: Andre Schubert <andre@km3.de>; pgsql-admin
On Fri, 2003-01-24 at 05:33, Ian Barwick wrote:
> On Friday 24 January 2003 09:26, Ian Barwick wrote:
> > On Friday 24 January 2003 07:37, Andre Schubert wrote:
> > > i have a little question on changing the length of a varchar field.
> > > Is there another way than dump and reload if i want to change the length
> > > of a varchar field ?
> >
> > in 7.3:
> >
> > BEGIN;
> > ALTER TABLE foo RENAME your_field TO your_field_old;
> > ALTER TABLE foo ADD COLUMN your_field VARCHAR([new length]);
> > UPDATE foo SET your_field=your_field_old;
> > ALTER TABLE foo DROP COLUMN your_field_old;
> > COMMIT;
> >
> > In 7.3, if the new column is shorter you may need to truncate the values
> > being inserted.
> >
> > In versions < 7.3 you will not be able to drop the old column; there you
> > may want to recreate the table, there's a techdoc article here:
> > http://techdocs.postgresql.org/techdocs/updatingcolumns.php
>
> Addendum:
> Someone has written me a private email pointing out that if you do
> recreate a table like this, dependent views etc. will no longer work,
> and asks if there is any way around this apart from upgrading
> to 7.3. I think the short answer is "no". (As in there's "no such thing
> as a free lunch" ;-)
>
There is a "gamblers lunch", for those willing to risk total system
failure and the lives of there first born child. You can issue the
following query to find out the attribute information for the field you
want to modify
select a.* from pg_attribute a, pg_class c where c.relname='tablename'
and a.attnum > 0 and a.attrelid = c.oid;
if you then do:
update pg_attribute set atttypmod=N where attrelid=(select oid from
pg_class where relname='tablename') and attname='fieldname';
where N = length of desired field + 4.
you can then run the first query to verify the changes.
Robert Treat
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Length of Varchar
@ 2003-01-24 15:45 Tom Lane <tgl@sss.pgh.pa.us>
parent: Robert Treat <xzilla@users.sourceforge.net>
0 siblings, 1 reply; 6+ messages in thread
From: Tom Lane @ 2003-01-24 15:45 UTC (permalink / raw)
To: Robert Treat <xzilla@users.sourceforge.net>; +Cc: Ian Barwick <barwick@gmx.net>; Andre Schubert <andre@km3.de>; pgsql-admin
Robert Treat <xzilla@users.sourceforge.net> writes:
> There is a "gamblers lunch", for those willing to risk total system
> failure and the lives of there first born child.
Aw, it's not that dangerous ;-). You can improve your odds considerably
if you issue BEGIN; before you start dorking with the system tables,
and COMMIT only after you're satisfied you have things right. If you
realize you blew it, ROLLBACK.
Still, I'd recommend practicing on a scratch database before you do it
for real.
regards, tom lane
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Length of Varchar
@ 2003-01-27 19:01 Bruce Momjian <pgman@candle.pha.pa.us>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 6+ messages in thread
From: Bruce Momjian @ 2003-01-27 19:01 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Robert Treat <xzilla@users.sourceforge.net>; Ian Barwick <barwick@gmx.net>; Andre Schubert <andre@km3.de>; pgsql-admin
Also, it only really works well for lengthening columns. If you shorten
them, the old values will remain their original longer length.
---------------------------------------------------------------------------
Tom Lane wrote:
> Robert Treat <xzilla@users.sourceforge.net> writes:
> > There is a "gamblers lunch", for those willing to risk total system
> > failure and the lives of there first born child.
>
> Aw, it's not that dangerous ;-). You can improve your odds considerably
> if you issue BEGIN; before you start dorking with the system tables,
> and COMMIT only after you're satisfied you have things right. If you
> realize you blew it, ROLLBACK.
>
> Still, I'd recommend practicing on a scratch database before you do it
> for real.
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2003-01-27 19:01 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-01-24 06:37 Length of Varchar Andre Schubert <andre@km3.de>
2003-01-24 08:26 ` Ian Barwick <barwick@gmx.net>
2003-01-24 10:33 ` Ian Barwick <barwick@gmx.net>
2003-01-24 14:30 ` Robert Treat <xzilla@users.sourceforge.net>
2003-01-24 15:45 ` Tom Lane <tgl@sss.pgh.pa.us>
2003-01-27 19:01 ` Bruce Momjian <pgman@candle.pha.pa.us>
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