
Thanks to the `table_rewrite` event, it is possible to implement a table rewriting policy only allowing the rewrite in maintenance windows.

Here's an example implementing such a policy.

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();
