|
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>39.5. A Table Rewrite Event Trigger Example</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="event-trigger-example.html" title="39.4. A Complete Event Trigger Example" /><link rel="next" href="rules.html" title="Chapter 40. The Rule System" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">39.5. A Table Rewrite Event Trigger Example</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="event-trigger-example.html" title="39.4. A Complete Event Trigger Example">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="event-triggers.html" title="Chapter 39. Event Triggers">Up</a></td><th width="60%" align="center">Chapter 39. Event Triggers</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="rules.html" title="Chapter 40. The Rule System">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="EVENT-TRIGGER-TABLE-REWRITE-EXAMPLE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">39.5. A Table Rewrite Event Trigger Example</h2></div></div></div><p>
- Thanks to the <code class="literal">table_rewrite</code> event, it is possible to implement
- a table rewriting policy only allowing the rewrite in maintenance windows.
- </p><p>
- Here's an example implementing such a policy.
- </p><pre class="programlisting">
- CREATE OR REPLACE FUNCTION no_rewrite()
- RETURNS event_trigger
- LANGUAGE plpgsql AS
- $$
- ---
- --- Implement local Table Rewriting policy:
- --- public.foo is not allowed rewriting, ever
- --- other tables are only allowed rewriting between 1am and 6am
- --- unless they have more than 100 blocks
- ---
- DECLARE
- table_oid oid := pg_event_trigger_table_rewrite_oid();
- current_hour integer := extract('hour' from current_time);
- pages integer;
- max_pages integer := 100;
- BEGIN
- IF pg_event_trigger_table_rewrite_oid() = 'public.foo'::regclass
- THEN
- RAISE EXCEPTION 'you''re not allowed to rewrite the table %',
- table_oid::regclass;
- END IF;
-
- SELECT INTO pages relpages FROM pg_class WHERE oid = table_oid;
- IF pages > max_pages
- THEN
- RAISE EXCEPTION 'rewrites only allowed for table with less than % pages',
- max_pages;
- END IF;
-
- IF current_hour NOT BETWEEN 1 AND 6
- THEN
- RAISE EXCEPTION 'rewrites only allowed between 1am and 6am';
- END IF;
- END;
- $$;
-
- CREATE EVENT TRIGGER no_rewrite_allowed
- ON table_rewrite
- EXECUTE FUNCTION no_rewrite();
- </pre><p>
- </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="event-trigger-example.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="event-triggers.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="rules.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">39.4. A Complete Event Trigger Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 40. The Rule System</td></tr></table></div></body></html>
|