Skip to content

Instantly share code, notes, and snippets.

@PandaWhoCodes
Last active April 14, 2022 20:50
Show Gist options
  • Save PandaWhoCodes/ea6693333c7c565714324ec1c186f962 to your computer and use it in GitHub Desktop.
Save PandaWhoCodes/ea6693333c7c565714324ec1c186f962 to your computer and use it in GitHub Desktop.
lock tables from being altered in postgres
CREATE OR REPLACE FUNCTION schema.ddl_issue()
RETURNS event_trigger
LANGUAGE plpgsql
AS $function$
DECLARE r RECORD;
BEGIN
FOR r IN SELECT * FROM pg_event_trigger_ddl_commands() LOOP
IF ( r.objid::regclass::text = 'schema.tablename' )
THEN
RAISE EXCEPTION 'You are not allowed to change %', r.object_identity;
END IF;
END LOOP;
END;
$function$
;
-- event trigger
CREATE EVENT TRIGGER no_ddl_allowed
ON ddl_command_end WHEN TAG IN ('ALTER TABLE')
EXECUTE PROCEDURE schema.ddl_issue();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment