Monday, February 2, 2009

Creating an unmodifiable table in Postgres

Here's a simple and effective way of avoiding any accidental INSERT/UPDATE/DELETE operation against any table:

(developed this as part of Postgres porting of Spacewalk )


CREATE TABLE dual ( dummy char );
INSERT INTO dual values ( 'X' );
CREATE OR REPLACE RULE insert_dual AS ON INSERT TO dual DO INSTEAD NOTHING;
CREATE OR REPLACE RULE update_dual AS ON UPDATE TO dual DO INSTEAD NOTHING;
CREATE OR REPLACE RULE delete_dual AS ON DELETE TO dual DO INSTEAD NOTHING;

No comments:

Post a Comment