Created
January 27, 2022 18:33
-
-
Save Xof/ac765d38196a79ea1366a122db5cdcd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION rebuild_view() RETURNS event_trigger AS | |
$rebuild_view$ | |
DECLARE | |
table_name text; | |
view_name text; | |
BEGIN | |
SELECT object_identity INTO table_name FROM pg_event_trigger_ddl_commands() LIMIT 1; | |
SELECT split_part(table_name, '.', 1) || '.v_' || split_part(table_name, '.', 2) | |
INTO view_name; | |
EXECUTE 'DROP VIEW IF EXISTS ' || view_name; | |
EXECUTE 'CREATE VIEW ' || view_name || ' AS SELECT * FROM ' || table_name; | |
END; | |
$rebuild_view$ | |
LANGUAGE plpgsql; | |
CREATE EVENT TRIGGER rebuild_view ON ddl_command_end | |
WHEN TAG IN ('ALTER TABLE') | |
EXECUTE FUNCTION rebuild_view(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment