Skip to content

Commit 5a82de8

Browse files
committed
schema cache automatically reloaded when collections are created, altered or dropped. Also, removed a trigger that did essentially nothing.
1 parent 6ab79b7 commit 5a82de8

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

xcube_geodb/sql/geodb.sql

+57-14
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,6 @@ CREATE TABLE IF NOT EXISTS public.geodb_user_databases
129129
GRANT SELECT, INSERT, UPDATE ON geodb_user_databases TO PUBLIC;
130130
GRANT SELECT, UPDATE, USAGE ON geodb_user_databases_seq TO PUBLIC;
131131

132-
CREATE OR REPLACE FUNCTION public.notify_ddl_postgrest()
133-
RETURNS event_trigger
134-
LANGUAGE plpgsql
135-
AS
136-
$$
137-
BEGIN
138-
NOTIFY ddl_command_end;
139-
END;
140-
$$;
141-
142-
DROP EVENT TRIGGER IF EXISTS ddl_postgrest;
143-
CREATE EVENT TRIGGER ddl_postgrest ON ddl_command_end
144-
EXECUTE PROCEDURE public.notify_ddl_postgrest();
145-
146132
-- ensures that database of collection belongs to user or group
147133
CREATE
148134
OR REPLACE FUNCTION public.geodb_user_allowed(
@@ -1685,3 +1671,60 @@ BEGIN
16851671
) as res', collection);
16861672
END
16871673
$BODY$;
1674+
1675+
-- Below: watching PostGREST schema cache changes to the database, and trigger a
1676+
-- reload.
1677+
-- Code copied from https://postgrest.org/en/stable/references/schema_cache.html.
1678+
-- BEGIN of copied code
1679+
-- watch CREATE and ALTER
1680+
CREATE OR REPLACE FUNCTION pgrst_ddl_watch() RETURNS event_trigger AS
1681+
$$
1682+
DECLARE
1683+
cmd record;
1684+
BEGIN
1685+
FOR cmd IN SELECT * FROM pg_event_trigger_ddl_commands()
1686+
LOOP
1687+
IF cmd.command_tag IN (
1688+
'CREATE SCHEMA', 'ALTER SCHEMA', 'CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO',
1689+
'ALTER TABLE', 'CREATE FOREIGN TABLE', 'ALTER FOREIGN TABLE', 'CREATE VIEW',
1690+
'ALTER VIEW', 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW',
1691+
'CREATE FUNCTION', 'ALTER FUNCTION', 'CREATE TRIGGER', 'CREATE TYPE', 'ALTER TYPE',
1692+
'CREATE RULE', 'COMMENT'
1693+
)
1694+
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
1695+
AND cmd.schema_name is distinct from 'pg_temp'
1696+
THEN
1697+
NOTIFY pgrst, 'reload schema';
1698+
END IF;
1699+
END LOOP;
1700+
END;
1701+
$$ LANGUAGE plpgsql;
1702+
1703+
-- watch DROP
1704+
CREATE OR REPLACE FUNCTION pgrst_drop_watch() RETURNS event_trigger AS
1705+
$$
1706+
DECLARE
1707+
obj record;
1708+
BEGIN
1709+
FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
1710+
LOOP
1711+
IF obj.object_type IN (
1712+
'schema', 'table', 'foreign table', 'view', 'materialized view', 'function',
1713+
'trigger', 'type', 'rule'
1714+
)
1715+
AND obj.is_temporary IS false -- no pg_temp objects
1716+
THEN
1717+
NOTIFY pgrst, 'reload schema';
1718+
END IF;
1719+
END LOOP;
1720+
END;
1721+
$$ LANGUAGE plpgsql;
1722+
1723+
CREATE EVENT TRIGGER pgrst_ddl_watch
1724+
ON ddl_command_end
1725+
EXECUTE PROCEDURE pgrst_ddl_watch();
1726+
1727+
CREATE EVENT TRIGGER pgrst_drop_watch
1728+
ON sql_drop
1729+
EXECUTE PROCEDURE pgrst_drop_watch();
1730+
-- END of copied code

0 commit comments

Comments
 (0)