gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
3.7KB

  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>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>
  3. Thanks to the <code class="literal">table_rewrite</code> event, it is possible to implement
  4. 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">
  8. CREATE OR REPLACE FUNCTION no_rewrite()
  9. RETURNS event_trigger
  10. LANGUAGE plpgsql AS
  11. $$
  12. ---
  13. --- Implement local Table Rewriting policy:
  14. --- public.foo is not allowed rewriting, ever
  15. --- other tables are only allowed rewriting between 1am and 6am
  16. --- unless they have more than 100 blocks
  17. ---
  18. DECLARE
  19. 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;
  23. BEGIN
  24. IF pg_event_trigger_table_rewrite_oid() = 'public.foo'::regclass
  25. THEN
  26. RAISE EXCEPTION 'you''re not allowed to rewrite the table %',
  27. table_oid::regclass;
  28. END IF;
  29. SELECT INTO pages relpages FROM pg_class WHERE oid = table_oid;
  30. IF pages &gt; max_pages
  31. THEN
  32. RAISE EXCEPTION 'rewrites only allowed for table with less than % pages',
  33. max_pages;
  34. END IF;
  35. IF current_hour NOT BETWEEN 1 AND 6
  36. THEN
  37. RAISE EXCEPTION 'rewrites only allowed between 1am and 6am';
  38. END IF;
  39. END;
  40. $$;
  41. CREATE EVENT TRIGGER no_rewrite_allowed
  42. ON table_rewrite
  43. EXECUTE FUNCTION no_rewrite();
  44. </pre><p>
  45. </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>
上海开阖软件有限公司 沪ICP备12045867号-1