Re: alter column to varchar without view drop/re-creation - Mailing list pgsql-general

From Adrian Klaver
Subject Re: alter column to varchar without view drop/re-creation
Date
Msg-id 5400D1A8.4050100@aklaver.com
Whole thread Raw
In response to Re: alter column to varchar without view drop/re-creation  (Emi Lu <emilu@encs.concordia.ca>)
Responses Re: alter column to varchar without view drop/re-creation  (Emi Lu <emilu@encs.concordia.ca>)
List pgsql-general
On 08/29/2014 12:09 PM, Emi Lu wrote:
>
>> Hello list,
>>
>> May I know is there a way to "alter column type to varchar" (previous
>> is varchar(***)) without view drop/re-creation?
>>
>> Basically, looking for a way to change column without have to
>> drop/re-create dependent views.
>>
>> varchar(***) to varchar and no date/numeric changes.
>>
> I saw docs mention about: update pg_attribute. May I know:
>
> . will dependent views updated automatically or there might be potential
> problems?
> . If it's fine, will the following SQL enough to change column from
> varchar(***) to varchar?
>
>    update pg_attribute set atttypmod =-1
>    where  attrelid = 'oid' ;

Here is what I did. I would definitely test first and run in a transaction:

test=# SELECT version();
                                                          version


--------------------------------------------------------------------------------------------------------------------------
  PostgreSQL 8.3.23 on i686-pc-linux-gnu, compiled by GCC gcc (SUSE
Linux) 4.7.2 20130108 [gcc-4_7-branch revision 195012]


test=# create TABLE base_tbl (id integer, vc_fld varchar(10));
CREATE TABLE
test=# CREATE view v_test as SELECT * from base_tbl ;
CREATE VIEW
test=# insert INTO base_tbl VALUES(1, 'one');
INSERT 0 1
test=# insert INTO base_tbl VALUES(2, 'two');
INSERT 0 1
test=# \d base_tbl
           Table "public.base_tbl"
  Column |         Type          | Modifiers
--------+-----------------------+-----------
  id     | integer               |
  vc_fld | character varying(10) |

test=# \d v_test
             View "public.v_test"
  Column |         Type          | Modifiers
--------+-----------------------+-----------
  id     | integer               |
  vc_fld | character varying(10) |
View definition:
  SELECT base_tbl.id, base_tbl.vc_fld
    FROM base_tbl;

test=# UPDATE pg_attribute SET atttypmod = -1 WHERE attrelid =
'base_tbl'::regclass AND attname = 'vc_fld';
UPDATE 1
test=# UPDATE pg_attribute SET atttypmod = -1 WHERE attrelid =
'v_test'::regclass AND attname = 'vc_fld';
UPDATE 1
test=# \d base_tbl
         Table "public.base_tbl"
  Column |       Type        | Modifiers
--------+-------------------+-----------
  id     | integer           |
  vc_fld | character varying |

test=# \d v_test
           View "public.v_test"
  Column |       Type        | Modifiers
--------+-------------------+-----------
  id     | integer           |
  vc_fld | character varying |
View definition:
  SELECT base_tbl.id, base_tbl.vc_fld
    FROM base_tbl;

test=# insert INTO base_tbl VALUES(3, '123456789012345678901234567890');
INSERT 0 1
test=# SELECT * from base_tbl ;
  id |             vc_fld
----+--------------------------------
   1 | one
   2 | two
   3 | 123456789012345678901234567890
(3 rows)

test=# SELECT * from v_test ;
  id |             vc_fld
----+--------------------------------
   1 | one
   2 | two
   3 | 123456789012345678901234567890
(3 rows)


>
> Thanks a lot!
>
>> ---
>> *PostgreSQL 8.3.18 on x86_64*
>
>


--
Adrian Klaver
adrian.klaver@aklaver.com


pgsql-general by date:

Previous
From: Emi Lu
Date:
Subject: Re: alter column to varchar without view drop/re-creation
Next
From: Adrian Klaver
Date:
Subject: Re: Single Table Report With Calculated Column