Thread: Resize varchar column

Resize varchar column

From
"John Sidney-Woollett"
Date:
Is it possible to alter a table to resize a varchar column? The PG docs
indicate lots of uses for "alter table T alter column C..." but not one
that allows the changing of the type (or same type, new size).

This is possible in Oracle (provided you are increasing the column size).

Is there a way to resize the column without having to drop or recreate the
table or column?

Thanks

John Sidney-Woollett

Re: Resize varchar column

From
Bruno Wolff III
Date:
On Wed, Feb 04, 2004 at 14:45:37 -0000,
  John Sidney-Woollett <johnsw@wardbrook.com> wrote:
> Is it possible to alter a table to resize a varchar column? The PG docs
> indicate lots of uses for "alter table T alter column C..." but not one
> that allows the changing of the type (or same type, new size).
>
> This is possible in Oracle (provided you are increasing the column size).
>
> Is there a way to resize the column without having to drop or recreate the
> table or column?

It is possible to change the size of varchar data by updating the system
catalogs. I don't know the exact details of what to do, but the instructions
should be in the archives (multiple times).

In general (in 7.4.x) you can change data types using add, drop, rename and
a query to copy/translate the data. This will have the side effect of making
the updated column the last column when using * to refer to the table's columns.

Re: Resize varchar column

From
"John Sidney-Woollett"
Date:
Bruno Wolff III said:
> In general (in 7.4.x) you can change data types using add, drop, rename
> and a query to copy/translate the data. This will have the side effect of
> making the updated column the last column when using * to refer to the
> table's columns.

I'll take a look at the system catalogs - thanks for the tip.

> It is possible to change the size of varchar data by updating the system
> catalogs. I don't know the exact details of what to do, but the
> instructions should be in the archives (multiple times).

I was hoping to avoid a column drop and recreate.

Thanks for the info.

John Sidney-Woollett



Re: Resize varchar column

From
Dirk Försterling
Date:
Bruno Wolff III wrote:
> catalogs. I don't know the exact details of what to do, but the instructions
> should be in the archives (multiple times).

Maybe. But how to find this? Any query on archives.postgresql.org
returns just

"Sorry, but search returned no results.

Try to produce less restrictive search query"

for me (with varying search time).

   -dirk

--
                    D i r k   F "o r s t e r l i n g
                    r@zorbla.de  http://r@zorbla.de/
                             -------------
  "Diese Rekursionen sind nicht mit den Haaren herbeigezaubert" - R.K.


Re: Resize varchar column

From
"scott.marlowe"
Date:
On Wed, 4 Feb 2004, John Sidney-Woollett wrote:

> Bruno Wolff III said:
> > In general (in 7.4.x) you can change data types using add, drop, rename
> > and a query to copy/translate the data. This will have the side effect of
> > making the updated column the last column when using * to refer to the
> > table's columns.
>
> I'll take a look at the system catalogs - thanks for the tip.
>
> > It is possible to change the size of varchar data by updating the system
> > catalogs. I don't know the exact details of what to do, but the
> > instructions should be in the archives (multiple times).
>
> I was hoping to avoid a column drop and recreate.
>
> Thanks for the info.

There is a way to do it.  I don't recall it off my head, it has to do with
tweaking the system catalogs directly.  That said, I would practice on a
test database, backup the live database, and cross my fingers and use a
chicken foot to make sure you don't scram your database, as playing with
system catalogs by hand is not something that's guaranteed to be a nice
happy safe thing.  But as messing with the system catalogs go, this is one
of the easier things to do.

It should be in the list archives somewhere, and should probably be an
admin faq by now.


Re: Resize varchar column

From
Richard Huxton
Date:
On Wednesday 04 February 2004 16:00, John Sidney-Woollett wrote:
> Bruno Wolff III said:
> > In general (in 7.4.x) you can change data types using add, drop, rename
> > and a query to copy/translate the data. This will have the side effect of
> > making the updated column the last column when using * to refer to the
> > table's columns.
>
> I'll take a look at the system catalogs - thanks for the tip.
>
> > It is possible to change the size of varchar data by updating the system
> > catalogs. I don't know the exact details of what to do, but the
> > instructions should be in the archives (multiple times).
>
> I was hoping to avoid a column drop and recreate.

The message (one of them anyway) you want is:

http://archives.postgresql.org/pgsql-general/2001-10/msg01208.php

Try and find something more recent though, in case anything important has
changed. Oh, and do take Tom's advice about the backup.

--
  Richard Huxton
  Archonet Ltd

Re: Resize varchar column

From
"John Sidney-Woollett"
Date:
Richard Huxton said:
> The message (one of them anyway) you want is:
>
> http://archives.postgresql.org/pgsql-general/2001-10/msg01208.php
>
> Try and find something more recent though, in case anything important has
> changed. Oh, and do take Tom's advice about the backup.

Thanks, that's great.

It would be nice if there was a command available that would do simple
column resizes (upwards), or even data type changes (assuming no implied
data truncation), ie

alter table T alter column X varchar(x);

where x is the new size, and x > old size;

In fact, looking at your URL above (and using the technique explained), it
may be possible to create a function that does this.

I wonder if it works, though, if there is an index on the column...

Anyway, this time, I dropped and recreated the table as this is a test
system. But reinstating all the objects that referenced that table was a
pain!

Thanks again.

John Sidney-Woollett