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?
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;
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With