Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
event-trigger-table-rewrite-example.html48 linesDownload Raw Back to html
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!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>40.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 Vsnapshot" /><link rel="prev" href="event-trigger-example.html" title="40.4. A Complete Event Trigger Example" /><link rel="next" href="rules.html" title="Chapter 41. The Rule System" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">40.5. A Table Rewrite Event Trigger Example</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="event-trigger-example.html" title="40.4. A Complete Event Trigger Example">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="event-triggers.html" title="Chapter 40. Event Triggers">Up</a></td><th width="60%" align="center">Chapter 40. Event Triggers</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="rules.html" title="Chapter 41. The Rule System">Next</a></td></tr></table><hr /></div><div class="sect1" id="EVENT-TRIGGER-TABLE-REWRITE-EXAMPLE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">40.5. A Table Rewrite Event Trigger Example <a href="#EVENT-TRIGGER-TABLE-REWRITE-EXAMPLE" class="id_link">#</a></h2></div></div></div><p>3    Thanks to the <code class="literal">table_rewrite</code> event, it is possible to implement4    a table rewriting policy only allowing the rewrite in maintenance windows.5   </p><p>6    Here's an example implementing such a policy.7</p><pre class="programlisting">8CREATE OR REPLACE FUNCTION no_rewrite()9 RETURNS event_trigger10 LANGUAGE plpgsql AS11$$12---13--- Implement local Table Rewriting policy:14---   public.foo is not allowed rewriting, ever15---   other tables are only allowed rewriting between 1am and 6am16---   unless they have more than 100 blocks17---18DECLARE19  table_oid oid := pg_event_trigger_table_rewrite_oid();20  current_hour integer := extract('hour' from current_time);21  pages integer;22  max_pages integer := 100;23BEGIN24  IF pg_event_trigger_table_rewrite_oid() = 'public.foo'::regclass25  THEN26        RAISE EXCEPTION 'you''re not allowed to rewrite the table %',27                        table_oid::regclass;28  END IF;29 30  SELECT INTO pages relpages FROM pg_class WHERE oid = table_oid;31  IF pages &gt; max_pages32  THEN33        RAISE EXCEPTION 'rewrites only allowed for table with less than % pages',34                        max_pages;35  END IF;36 37  IF current_hour NOT BETWEEN 1 AND 638  THEN39        RAISE EXCEPTION 'rewrites only allowed between 1am and 6am';40  END IF;41END;42$$;43 44CREATE EVENT TRIGGER no_rewrite_allowed45                  ON table_rewrite46   EXECUTE FUNCTION no_rewrite();47</pre><p>48   </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" title="40.4. A Complete Event Trigger Example">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="event-triggers.html" title="Chapter 40. Event Triggers">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="rules.html" title="Chapter 41. The Rule System">Next</a></td></tr><tr><td width="40%" align="left" valign="top">40.4. A Complete Event Trigger Example </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> Chapter 41. The Rule System</td></tr></table></div></body></html>
codekingpro/portable-devtools · Team Ai