CREATE_RULE(7)

๐Ÿ’– CREATE_RULE ๐Ÿ’–

okay bestie, welcome to the PostgreSQL 18.6 docs, and today we're learning about CREATE_RULE โ€” as in, define a new rewrite rule. this is the part of PostgreSQL where you get to be sneaky in the most powerful way possible, and honestly? it's giving main character energy. ๐Ÿฆ‹


โœจ SYNOPSIS โœจ

CREATE [ OR REPLACE ] RULE name AS ON event
    TO table_name [ WHERE condition ]
    DO [ ALSO | INSTEAD ] { NOTHING | command | ( command ; command ... ) }

and event can be literally one of these:

SELECT | INSERT | UPDATE | DELETE

๐ŸŒธ DESCRIPTION ๐ŸŒธ

so CREATE RULE defines a brand-new rule that applies to a table or a view you pick. and if you say CREATE OR REPLACE RULE? it'll either make a whole new rule or replace an existing rule with the same name on the same table. two birds, one statement, slay. ๐Ÿ’…

the PostgreSQL rule system lets you define an alternative action to be performed on insertions, updates, or deletions in database tables. roughly speaking โ€” and this is the vibe โ€” a rule makes extra commands run when a given command on a given table runs. OR, an INSTEAD rule can swap a given command out for another one entirely, or make a command just... not happen at all. iconic behavior. rules are also how SQL views get implemented, iykyk.

it's really important to clock this: a rule is genuinely a command transformation mechanism, aka a command macro. the transformation happens before the execution of the command even starts.

so if what you actually want is an operation that fires independently for each physical row, you probably want a trigger, not a rule. no cap. more info about the rules system lives in Chapter 39.


๐ŸŽ€ the _RETURN situation

right now, ON SELECT rules can only be attached to views. and such a rule:

  • must be named "_RETURN"
  • must be an unconditional INSTEAD rule
  • must have an action that consists of a single SELECT command

that command defines the visible contents of the view. (the view itself is basically a dummy table with no storage, kinda fake ngl.) it's best to regard such a rule as an implementation detail. and yeah, a view can technically be redefined via CREATE OR REPLACE RULE "_RETURN" AS ..., but it's better style to just use CREATE OR REPLACE VIEW. periodt.


๐Ÿฆ‹ making an updatable view (the illusion)

you can create the illusion of an updatable view by defining ON INSERT, ON UPDATE, and ON DELETE rules โ€” or any subset of those that's sufficient for your purposes โ€” to replace update actions on the view with appropriate updates on other tables. and if you want to support INSERT RETURNING and so on, be sure to put a suitable RETURNING clause into each of these rules. don't forget it bestie, it matters. ๐Ÿ™ˆ


๐Ÿ’€ the catch (conditional rules + complex view updates)

okay so there IS a catch if you try to use conditional rules for complex view updates:

  • there must be an unconditional INSTEAD rule for each action you wish to allow on the view
  • if the rule is conditional, or is not INSTEAD, then the system will still reject attempts to perform the update action โ€” because it thinks it might end up trying to perform the action on the dummy table of the view in some cases

if you want to handle all the useful cases in conditional rules, add an unconditional DO INSTEAD NOTHING rule to ensure the system understands it will never be called on to update the dummy table. then make the conditional rules non-INSTEAD; in the cases where they are applied, they add to the default INSTEAD NOTHING action.

(this method does not currently work to support RETURNING queries, however. ๐Ÿ˜ญ just so you know.)

๐Ÿ‘‘ Note

a view that is simple enough to be automatically updatable (see CREATE VIEW (CREATE_VIEW(7))) does not require a user-created rule in order to be updatable. while you can create an explicit rule anyway, the automatic update transformation will generally outperform an explicit rule.

another alternative worth considering is to use INSTEAD OF triggers (see CREATE TRIGGER (CREATE_TRIGGER(7))) in place of rules. options! we love options. ๐ŸŒˆ


๐Ÿ’… PARAMETERS ๐Ÿ’…

  • name โ€” the name of a rule to create. this must be distinct from the name of any other rule for the same table. and heads up: multiple rules on the same table and same event type are applied in alphabetical name order. so name wisely, bestie.
  • event โ€” the event is one of SELECT, INSERT, UPDATE, or DELETE. note that an INSERT containing an ON CONFLICT clause cannot be used on tables that have either INSERT or UPDATE rules. consider using an updatable view instead.
  • table_name โ€” the name (optionally schema-qualified) of the table or view the rule applies to.
  • condition โ€” any SQL conditional expression (returning boolean). the condition expression cannot refer to any tables except NEW and OLD, and cannot contain aggregate functions.
  • INSTEAD โ€” INSTEAD indicates that the commands should be executed instead of the original command.
  • ALSO โ€” ALSO indicates that the commands should be executed in addition to the original command.

if neither ALSO nor INSTEAD is specified, ALSO is the default. (yeah, ALSO is the lowkey default. ๐Ÿ’œ)

  • command โ€” the command or commands that make up the rule action. valid commands are SELECT, INSERT, UPDATE, DELETE, or NOTIFY.

within condition and command, the special table names NEW and OLD can be used to refer to values in the referenced table.

  • NEW is valid in ON INSERT and ON UPDATE rules, to refer to the new row being inserted or updated. โœจ
  • OLD is valid in ON UPDATE and ON DELETE rules, to refer to the existing row being updated or deleted. ๐Ÿซถ

๐Ÿ“ NOTES ๐Ÿ“

you must be the owner of a table to create or change rules for it. no ownership, no rules, that's the rule. ๐Ÿ‘‘


๐Ÿ’œ RETURNING, but make it rules

in a rule for INSERT, UPDATE, or DELETE on a view, you can add a RETURNING clause that emits the view's columns. this clause will be used to compute the outputs if the rule is triggered by an INSERT RETURNING, UPDATE RETURNING, or DELETE RETURNING command respectively. when the rule is triggered by a command without RETURNING, the rule's RETURNING clause will be ignored.

the current implementation allows only unconditional INSTEAD rules to contain RETURNING; furthermore there can be at most one RETURNING clause among all the rules for the same event. (this ensures that there is only one candidate RETURNING clause to be used to compute the results.) and RETURNING queries on the view will be rejected if there is no RETURNING clause in any available rule.


๐Ÿ”ฅ circular rules: do NOT do this

it is very important to take care to avoid circular rules. for example, though each of the following two rule definitions are accepted by PostgreSQL, the SELECT command would cause PostgreSQL to report an error because of recursive expansion of a rule:

CREATE RULE "_RETURN" AS
    ON SELECT TO t1
    DO INSTEAD
        SELECT * FROM t2;

CREATE RULE "_RETURN" AS
    ON SELECT TO t2
    DO INSTEAD
        SELECT * FROM t1;

SELECT * FROM t1;
like. it accepted them. it did NOT enjoy them. ๐Ÿ˜ญ

๐Ÿงš NOTIFY and the unconditional situation

presently, if a rule action contains a NOTIFY command, the NOTIFY command will be executed unconditionally โ€” that is, the NOTIFY will be issued even if there are not any rows that the rule should apply to. for example, in:

CREATE RULE notify_me AS ON UPDATE TO mytable DO ALSO NOTIFY mytable;

UPDATE mytable SET name = 'foo' WHERE id = 42;

one NOTIFY event will be sent during the UPDATE, whether or not there are any rows that match the condition id = 42. this is an implementation restriction that might be fixed in future releases.


โญ COMPATIBILITY โญ

CREATE RULE is a PostgreSQL language extension, as is the entire query rewrite system. not standard SQL, just us. ๐Ÿ’–


๐ŸŒˆ SEE ALSO ๐ŸŒˆ


and that's the whole page, bestie!! ๐ŸŽ€ you now officially know more about rewrite rules than like 99% of people at any given party. go forth and be the command macro icon you were always meant to be. ๐Ÿ‘‘

PostgreSQL 18.6 โ€” 2026. mwah. ๐Ÿ’‹ stay slaying, and remember: rules transform before execution, triggers fire per row, and you are doing amazing sweetie. ๐ŸŒธ