2014-05-05 23:13:06 +08:00
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS
|
2019-05-31 21:29:28 +08:00
|
|
|
@extschema@.CDB_TableMetadata (
|
2014-05-05 23:13:06 +08:00
|
|
|
tabname regclass not null primary key,
|
|
|
|
updated_at timestamp with time zone not null default now()
|
|
|
|
);
|
|
|
|
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE VIEW @extschema@.CDB_TableMetadata_Text AS
|
2016-02-12 02:16:00 +08:00
|
|
|
SELECT FORMAT('%I.%I', n.nspname::text, c.relname::text) tabname, updated_at
|
2019-05-31 21:29:28 +08:00
|
|
|
FROM @extschema@.CDB_TableMetadata m JOIN pg_catalog.pg_class c ON m.tabname::oid = c.oid
|
2016-02-12 02:16:00 +08:00
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid;
|
|
|
|
|
2015-02-10 22:27:42 +08:00
|
|
|
-- No one can see this
|
|
|
|
-- Updates are only possible trough the security definer trigger
|
2019-05-31 21:29:28 +08:00
|
|
|
-- GRANT SELECT ON @extschema@.CDB_TableMetadata TO public;
|
2014-05-05 23:13:06 +08:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Trigger logging updated_at in the CDB_TableMetadata
|
|
|
|
-- and notifying cdb_tabledata_update with table name as payload.
|
|
|
|
--
|
|
|
|
-- Attach to tables like this:
|
|
|
|
--
|
|
|
|
-- CREATE trigger track_updates
|
|
|
|
-- AFTER INSERT OR UPDATE OR TRUNCATE OR DELETE ON <tablename>
|
|
|
|
-- FOR EACH STATEMENT
|
|
|
|
-- EXECUTE PROCEDURE cdb_tablemetadata_trigger();
|
|
|
|
--
|
|
|
|
-- NOTE: _never_ attach to CDB_TableMetadata ...
|
|
|
|
--
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_TableMetadata_Trigger()
|
2014-05-05 23:13:06 +08:00
|
|
|
RETURNS trigger AS
|
|
|
|
$$
|
|
|
|
BEGIN
|
|
|
|
-- Guard against infinite loop
|
2019-05-31 21:29:28 +08:00
|
|
|
IF TG_RELID = '@extschema@.CDB_TableMetadata'::regclass::oid THEN
|
2014-05-05 23:13:06 +08:00
|
|
|
RETURN NULL;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
-- Cleanup stale entries
|
2019-05-31 21:29:28 +08:00
|
|
|
DELETE FROM @extschema@.CDB_TableMetadata
|
2014-05-05 23:13:06 +08:00
|
|
|
WHERE NOT EXISTS (
|
|
|
|
SELECT oid FROM pg_class WHERE oid = tabname
|
|
|
|
);
|
|
|
|
|
|
|
|
WITH nv as (
|
2019-10-03 23:27:40 +08:00
|
|
|
SELECT TG_RELID as tabname, now() as t
|
2014-05-05 23:13:06 +08:00
|
|
|
), updated as (
|
2019-05-31 21:29:28 +08:00
|
|
|
UPDATE @extschema@.CDB_TableMetadata x SET updated_at = nv.t
|
2014-05-05 23:13:06 +08:00
|
|
|
FROM nv WHERE x.tabname = nv.tabname
|
|
|
|
RETURNING x.tabname
|
|
|
|
)
|
2019-05-31 21:29:28 +08:00
|
|
|
INSERT INTO @extschema@.CDB_TableMetadata SELECT nv.*
|
2014-05-05 23:13:06 +08:00
|
|
|
FROM nv LEFT JOIN updated USING(tabname)
|
|
|
|
WHERE updated.tabname IS NULL;
|
|
|
|
|
|
|
|
RETURN NULL;
|
|
|
|
END;
|
2019-10-03 23:27:40 +08:00
|
|
|
$$ LANGUAGE plpgsql
|
|
|
|
VOLATILE
|
|
|
|
PARALLEL UNSAFE
|
|
|
|
SECURITY DEFINER
|
2019-10-03 23:29:35 +08:00
|
|
|
SET search_path = pg_temp;
|
2014-05-05 23:13:06 +08:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Trigger invalidating varnish whenever CDB_TableMetadata
|
|
|
|
-- record change.
|
|
|
|
--
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_TableMetadata_Updated()
|
2014-05-05 23:13:06 +08:00
|
|
|
RETURNS trigger AS
|
|
|
|
$$
|
|
|
|
DECLARE
|
2016-03-02 23:22:22 +08:00
|
|
|
tabname regclass;
|
2014-05-15 01:02:46 +08:00
|
|
|
rec RECORD;
|
|
|
|
found BOOL;
|
2016-03-02 23:22:22 +08:00
|
|
|
schema_name TEXT;
|
|
|
|
table_name TEXT;
|
2014-05-05 23:13:06 +08:00
|
|
|
BEGIN
|
|
|
|
|
|
|
|
IF TG_OP = 'UPDATE' or TG_OP = 'INSERT' THEN
|
|
|
|
tabname = NEW.tabname;
|
|
|
|
ELSE
|
|
|
|
tabname = OLD.tabname;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
-- Notify table data update
|
|
|
|
-- This needs a little bit more of research regarding security issues
|
|
|
|
-- see https://github.com/CartoDB/cartodb/pull/241
|
|
|
|
-- PERFORM pg_notify('cdb_tabledata_update', tabname);
|
|
|
|
|
|
|
|
--RAISE NOTICE 'Table % was updated', tabname;
|
|
|
|
|
|
|
|
-- This will be needed until we'll have someone listening
|
|
|
|
-- on the event we just broadcasted:
|
|
|
|
--
|
|
|
|
-- LISTEN cdb_tabledata_update;
|
|
|
|
--
|
2014-05-15 01:02:46 +08:00
|
|
|
|
|
|
|
-- Call the first varnish invalidation function owned
|
2019-05-31 21:29:28 +08:00
|
|
|
-- by a superuser found in @extschema@ or public schema
|
2014-05-15 01:02:46 +08:00
|
|
|
-- (in that order)
|
|
|
|
found := false;
|
|
|
|
FOR rec IN SELECT u.usesuper, u.usename, n.nspname, p.proname
|
|
|
|
FROM pg_proc p, pg_namespace n, pg_user u
|
|
|
|
WHERE p.proname = 'cdb_invalidate_varnish'
|
|
|
|
AND p.pronamespace = n.oid
|
2019-05-31 21:29:28 +08:00
|
|
|
AND n.nspname IN ('public', '@extschema@')
|
2014-05-15 01:02:46 +08:00
|
|
|
AND u.usesysid = p.proowner
|
|
|
|
AND u.usesuper
|
|
|
|
ORDER BY n.nspname
|
|
|
|
LOOP
|
2016-03-02 23:22:22 +08:00
|
|
|
SELECT n.nspname, c.relname FROM pg_class c, pg_namespace n WHERE c.oid=tabname AND c.relnamespace = n.oid INTO schema_name, table_name;
|
2014-05-15 01:02:46 +08:00
|
|
|
EXECUTE 'SELECT ' || quote_ident(rec.nspname) || '.'
|
|
|
|
|| quote_ident(rec.proname)
|
2016-03-02 23:22:22 +08:00
|
|
|
|| '(' || quote_literal(quote_ident(schema_name) || '.' || quote_ident(table_name)) || ')';
|
2014-05-15 01:02:46 +08:00
|
|
|
found := true;
|
|
|
|
EXIT;
|
|
|
|
END LOOP;
|
|
|
|
IF NOT found THEN RAISE WARNING 'Missing cdb_invalidate_varnish()'; END IF;
|
2015-11-17 20:10:19 +08:00
|
|
|
|
2014-05-05 23:13:06 +08:00
|
|
|
RETURN NULL;
|
|
|
|
END;
|
2019-10-03 23:27:40 +08:00
|
|
|
$$ LANGUAGE plpgsql
|
|
|
|
VOLATILE
|
|
|
|
PARALLEL UNSAFE
|
|
|
|
SECURITY DEFINER
|
2019-10-03 23:29:35 +08:00
|
|
|
SET search_path = pg_temp;
|
2014-05-05 23:13:06 +08:00
|
|
|
|
2019-05-31 21:29:28 +08:00
|
|
|
DROP TRIGGER IF EXISTS table_modified ON @extschema@.CDB_TableMetadata;
|
2014-05-09 23:34:18 +08:00
|
|
|
-- NOTE: on DELETE we would be unable to convert the table
|
|
|
|
-- oid (regclass) to its name
|
|
|
|
CREATE TRIGGER table_modified AFTER INSERT OR UPDATE
|
2019-05-31 21:29:28 +08:00
|
|
|
ON @extschema@.CDB_TableMetadata FOR EACH ROW EXECUTE PROCEDURE
|
2019-06-03 21:22:52 +08:00
|
|
|
@extschema@._CDB_TableMetadata_Updated();
|
2014-05-05 23:13:06 +08:00
|
|
|
|
2015-02-19 00:08:46 +08:00
|
|
|
|
|
|
|
-- similar to TOUCH(1) in unix filesystems but for table in cdb_tablemetadata
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_TableMetadataTouch(tablename regclass)
|
2015-02-19 00:08:46 +08:00
|
|
|
RETURNS void AS
|
|
|
|
$$
|
|
|
|
BEGIN
|
|
|
|
WITH upsert AS (
|
2019-05-31 21:29:28 +08:00
|
|
|
UPDATE @extschema@.cdb_tablemetadata
|
2015-02-19 00:08:46 +08:00
|
|
|
SET updated_at = NOW()
|
|
|
|
WHERE tabname = tablename
|
|
|
|
RETURNING *
|
|
|
|
)
|
2019-05-31 21:29:28 +08:00
|
|
|
INSERT INTO @extschema@.cdb_tablemetadata (tabname, updated_at)
|
2015-02-19 00:08:46 +08:00
|
|
|
SELECT tablename, NOW()
|
|
|
|
WHERE NOT EXISTS (SELECT * FROM upsert);
|
|
|
|
END;
|
|
|
|
$$
|
2017-10-24 20:16:56 +08:00
|
|
|
LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE;
|