How to add a column to a typed table without changing the type in PostgreSQL?

I am using PostgreSQL 9.5, I have a TYPE that describes a collection of columns:

CREATE TYPE datastore.record AS
   (recordid bigint,
    ...
    tags text[]);

I have created many tables related to this type:

CREATE TABLE datastore.events
OF datastore.record;

Now I would like to add a column to the table that relies on this TYPE without updating the TYPE. I think this is not possible, so I wonder if there is a way to disconnect my table from this TYPE without losing data or copying the table to a temporary table?

+4
source share
2 answers

There is a special option for this purpose not of. Per documentation :

NOT OF - This form separates the typed table from its type.

So:

alter table my_table not of;
alter table my_table add new_column integer;
+5
source

:

--drop table if exists t2;
--drop table if exists t1;
--drop type if exists tp_foo;

create type tp_foo as (i int, x int);

create table t1 of tp_foo;
create table t2 (y text) inherits(t1);

alter type tp_foo add attribute z date cascade;

select * from t2;
+1

Source: https://habr.com/ru/post/1664978/


All Articles