5

I am using PostgreSQL 9.5, I have a TYPE which discribes a collection of columns:

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

I have created many tables reliying on this TYPE:

CREATE TABLE datastore.events
OF datastore.record;

Now I would like to add a column to a table which rely on this TYPE without updating the TYPE. I think it is impossible as this, thus I am wondering if there is a way to unbind my table from this TYPE without losing any data or copying the table into a temporary table?

jlandercy
  • 7,183
  • 1
  • 39
  • 57

2 Answers2

6

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

NOT OF - This form dissociates a typed table from its type.

So:

alter table my_table not of;
alter table my_table add new_column integer;
klin
  • 112,967
  • 15
  • 204
  • 232
2

If you don't want to break relations:

--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;
Abelisto
  • 14,826
  • 2
  • 33
  • 41