Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are PostgreSQL RULEs good for?

Question

I often see it stated that rules should be avoided and triggers used instead. I can see the danger in the rule system, but certainly there are valid uses for rules, right? What are they?

I'm asking this out of general interest; I'm not very seasoned with databases.

Example of what might be a valid use

For instance, in the past I've needed to lock down certain data, so I've done something like this:

CREATE OR REPLACE RULE protect_data AS   ON UPDATE TO exampletable             -- another similar rule for DELETE   WHERE OLD.type = 'protected'   DO INSTEAD NOTHING; 

Then if I want to edit the protected data:

START TRANSACTION;   ALTER TABLE exampletable DISABLE RULE protect_data;   -- edit data as I like   ALTER TABLE exampletable ENABLE RULE protect_data; COMMIT; 

I agree this is hacky, but I couldn't change the application(s) accessing the database in this case (or even throw errors at it). So bonus points for finding a reason why this is a dangerous/invalid use of the rule system, but not for why this is bad design.

like image 734
StackExchange saddens dancek Avatar asked May 05 '11 07:05

StackExchange saddens dancek


People also ask

What is the advantage of using PostgreSQL?

PostgreSQL is the most professional of the relational Open Source databases and was awarded “Database System Of The Year” several times. It is a highly reliable, stable, scalable and secure system, and has been around for more than two decades now.

What is true for triggers and rules in PostgreSQL?

A trigger is fired for any row affected once. A rule manipulates the parsetree or generates an additional one. So if many rows are affected in one statement, a rule issuing one extra query would usually do a better job than a trigger that is called for any single row and must execute his operations this many times.

Why does everyone use Postgres?

Postgres allows you to store large and sophisticated data safely. It helps developers to build the most complex applications, run administrative tasks and create integral environments. Since 1986, when PostgreSQL was created, it has had both a lot of supporters and critics.


1 Answers

One of the use cases for RULES are updateable views (although that changes in 9.1 as that version introduces INSTEAD OF triggers for views)

Another good explanation can be found in the manual:

For the things that can be implemented by both, which is best depends on the usage of the database. A trigger is fired for any affected row once. A rule manipulates the query or generates an additional query. So if many rows are affected in one statement, a rule issuing one extra command is likely to be faster than a trigger that is called for every single row and must execute its operations many times. However, the trigger approach is conceptually far simpler than the rule approach, and is easier for novices to get right.

(Taken from: http://www.postgresql.org/docs/current/static/rules-triggers.html)

like image 150
a_horse_with_no_name Avatar answered Sep 21 '22 04:09

a_horse_with_no_name