snippetsqlMinor
How to validate column while creating Table?
Viewed 0 times
validatewhilecolumncreatinghowtable
Problem
Need to validate the column while creating the tables in postgres , An example before developer create table We are in need to validate the Audit columns,
Example :
Should not create the tables without the audit columns , Is it possible by trigger or any other concept.
Example :
create table tab_1 (
a integer,
b integer,
Audit1 integer,
audit2 integer)Should not create the tables without the audit columns , Is it possible by trigger or any other concept.
Solution
PostgreSQL does not support triggers on system tables, so there's no way you can write a normal trigger that fires when table definitions are changed.
PostgreSQL 9.4 introduces a new feature called event triggers. (More options have been added for event triggers in the coming 9.5 release too). You could use an event trigger to validate that table definitions follow rules you set out by writing suitable validation code in PL/PgSQL. You can query
That said, this seems like a complicated technical solution to a simple people problem. I wouldn't advise going down this route when your code review process can be fixed to catch these sorts of problems, your app can be modified to query the schema and emit warnings on tables that aren't defined how you require, etc.
PostgreSQL 9.4 introduces a new feature called event triggers. (More options have been added for event triggers in the coming 9.5 release too). You could use an event trigger to validate that table definitions follow rules you set out by writing suitable validation code in PL/PgSQL. You can query
information_schema and/or pg_catalog to find details of the tables, and you can abort the transaction with an ERROR if you don't like what you see.That said, this seems like a complicated technical solution to a simple people problem. I wouldn't advise going down this route when your code review process can be fixed to catch these sorts of problems, your app can be modified to query the schema and emit warnings on tables that aren't defined how you require, etc.
Context
StackExchange Database Administrators Q#107441, answer score: 2
Revisions (0)
No revisions yet.