is there a way to create a trigger on adding columns?

1.9k Views Asked by At

I'm using Postgres. I'm in the middle of the development. I need to create a replica of table ABC. I will name it as XYZ. I tried create table XYZ () inherits(ABC). It works ok but when I insert values on XYZ, table ABC is also populated (virtually). I am getting duplicate values when I run query something like this select * from ABC where id = 1. I get the correct record when I run query like this select * from only ABC where id = 1.

The this is, I don't want to use 'only'. So, I don't think inheritance is what I need. I just want to ask if there is a way to add column automatically on XYZ when I alter(add column) ABC?

I don't want to create two queries of alter for ABC and XYZ. I need the two tables to have the same columns always. Any idea?

1

There are 1 best solutions below

1
Mike Sherrill 'Cat Recall' On

The kind of trigger you're talking about is generally known as a DDL trigger. In PostgreSQL, they're known as event triggers. I'm not sure whether you can execute an alter table statement in a PostgreSQL event trigger, though.

I'm not confident that this is really the solution you're looking for, though. I think you might be in the middle of an X-Y problem.