CREATE_RULE(7pg18)
๐ 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
INSTEADrule - must have an action that consists of a single
SELECTcommand
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
INSTEADrule 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 ofSELECT,INSERT,UPDATE, orDELETE. note that anINSERTcontaining anON CONFLICTclause cannot be used on tables that have eitherINSERTorUPDATErules. 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 exceptNEWandOLD, and cannot contain aggregate functions.INSTEADโINSTEADindicates that the commands should be executed instead of the original command.ALSOโALSOindicates 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 areSELECT,INSERT,UPDATE,DELETE, orNOTIFY.
within condition and command, the special table names NEW and OLD can be used to refer to values in the referenced table.
NEWis valid inON INSERTandON UPDATErules, to refer to the new row being inserted or updated. โจOLDis valid inON UPDATEandON DELETErules, 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 ๐
ALTER RULE(ALTER_RULE(7))DROP RULE(DROP_RULE(7))
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. ๐ธ
๐ฆ where this page comes from
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms