2 38.4. A Table Rewrite Event Trigger Example #
4 Thanks to the table_rewrite event, it is possible to implement a table
5 rewriting policy only allowing the rewrite in maintenance windows.
7 Here's an example implementing such a policy.
8 CREATE OR REPLACE FUNCTION no_rewrite()
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
19 table_oid oid := pg_event_trigger_table_rewrite_oid();
20 current_hour integer := extract('hour' from current_time);
22 max_pages integer := 100;
24 IF pg_event_trigger_table_rewrite_oid() = 'public.foo'::regclass
26 RAISE EXCEPTION 'you''re not allowed to rewrite the table %',
30 SELECT INTO pages relpages FROM pg_class WHERE oid = table_oid;
33 RAISE EXCEPTION 'rewrites only allowed for table with less than % pages'
38 IF current_hour NOT BETWEEN 1 AND 6
40 RAISE EXCEPTION 'rewrites only allowed between 1am and 6am';
45 CREATE EVENT TRIGGER no_rewrite_allowed
47 EXECUTE FUNCTION no_rewrite();