2019-03-11 21:02:45 +08:00
|
|
|
-- Enqueues a job to run Ghost tables linking process for the provided username
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_LinkGhostTables(username text, db_name text, event_name text)
|
2019-03-07 01:19:38 +08:00
|
|
|
RETURNS void
|
|
|
|
AS $$
|
2019-03-08 15:28:06 +08:00
|
|
|
if not username:
|
2019-03-07 01:19:38 +08:00
|
|
|
return
|
|
|
|
|
2019-03-08 19:03:55 +08:00
|
|
|
if 'json' not in GD:
|
|
|
|
import json
|
|
|
|
GD['json'] = json
|
|
|
|
else:
|
|
|
|
json = GD['json']
|
2019-03-07 01:19:38 +08:00
|
|
|
|
2019-05-31 21:29:28 +08:00
|
|
|
tis_config = plpy.execute("select @extschema@.CDB_Conf_GetConf('invalidation_service');")[0]['cdb_conf_getconf']
|
2019-03-13 23:34:12 +08:00
|
|
|
if not tis_config:
|
2019-03-14 18:39:10 +08:00
|
|
|
plpy.warning('Invalidation service configuration not found. Skipping Ghost Tables linking.')
|
2019-03-13 23:34:12 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
tis_config_dict = json.loads(tis_config)
|
|
|
|
tis_host = tis_config_dict.get('host')
|
|
|
|
tis_port = tis_config_dict.get('port')
|
2019-03-08 19:03:55 +08:00
|
|
|
tis_timeout = tis_config_dict.get('timeout', 5)
|
|
|
|
tis_retry = tis_config_dict.get('retry', 5)
|
|
|
|
|
|
|
|
client = GD.get('invalidation', None)
|
2019-03-07 01:19:38 +08:00
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
if not client:
|
|
|
|
try:
|
|
|
|
import redis
|
2019-03-11 16:20:05 +08:00
|
|
|
client = redis.Redis(host=tis_host, port=tis_port, socket_timeout=tis_timeout)
|
|
|
|
GD['invalidation'] = client
|
2019-03-07 01:19:38 +08:00
|
|
|
except Exception as err:
|
|
|
|
error = "client_error - %s" % str(err)
|
|
|
|
# NOTE: no retries on connection error
|
2019-03-14 01:16:08 +08:00
|
|
|
plpy.warning('Error trying to connect to Invalidation Service to link Ghost Tables: ' + str(err))
|
2019-03-07 01:19:38 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
try:
|
2019-03-11 21:02:45 +08:00
|
|
|
client.execute_command('DBSCH', db_name, username, event_name)
|
2019-03-07 01:19:38 +08:00
|
|
|
break
|
|
|
|
except Exception as err:
|
|
|
|
error = "request_error - %s" % str(err)
|
2019-03-08 19:03:55 +08:00
|
|
|
client = GD['invalidation'] = None # force reconnect
|
|
|
|
if not tis_retry:
|
2019-03-14 01:16:08 +08:00
|
|
|
plpy.warning('Error calling Invalidation Service to link Ghost Tables: ' + str(err))
|
2019-03-07 01:19:38 +08:00
|
|
|
break
|
2019-03-08 19:03:55 +08:00
|
|
|
tis_retry -= 1 # try reconnecting
|
2019-03-07 01:19:38 +08:00
|
|
|
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE;
|
|
|
|
|
|
|
|
-- Enqueues a job to run Ghost tables linking process for the current user
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_LinkGhostTables(event_name text DEFAULT 'USER')
|
2019-03-07 01:19:38 +08:00
|
|
|
RETURNS void
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
2019-03-08 15:28:06 +08:00
|
|
|
username TEXT;
|
|
|
|
db_name TEXT;
|
2019-03-07 01:19:38 +08:00
|
|
|
BEGIN
|
2019-05-31 21:29:28 +08:00
|
|
|
EXECUTE 'SELECT @extschema@.CDB_Username();' INTO username;
|
2019-03-08 15:28:06 +08:00
|
|
|
EXECUTE 'SELECT current_database();' INTO db_name;
|
2019-03-11 21:02:45 +08:00
|
|
|
|
2019-05-31 21:29:28 +08:00
|
|
|
PERFORM @extschema@._CDB_LinkGhostTables(username, db_name, event_name);
|
2019-03-11 21:02:45 +08:00
|
|
|
RAISE NOTICE '_CDB_LinkGhostTables() called with username=%, event_name=%', username, event_name;
|
2019-03-07 01:19:38 +08:00
|
|
|
END;
|
2019-10-03 19:59:51 +08:00
|
|
|
$$ LANGUAGE plpgsql
|
|
|
|
VOLATILE
|
|
|
|
PARALLEL UNSAFE
|
|
|
|
SECURITY DEFINER
|
|
|
|
SET search_path = @extschema@, pg_temp;
|
2019-03-07 01:19:38 +08:00
|
|
|
|
|
|
|
-- Trigger function to call CDB_LinkGhostTables()
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE FUNCTION @extschema@._CDB_LinkGhostTablesTrigger()
|
2019-03-07 01:19:38 +08:00
|
|
|
RETURNS trigger
|
|
|
|
AS $$
|
2019-03-11 21:02:45 +08:00
|
|
|
DECLARE
|
|
|
|
ddl_tag TEXT;
|
2019-03-07 01:19:38 +08:00
|
|
|
BEGIN
|
2019-05-31 21:29:28 +08:00
|
|
|
EXECUTE 'DELETE FROM @extschema@.cdb_ddl_execution WHERE txid = txid_current() RETURNING tag;' INTO ddl_tag;
|
|
|
|
PERFORM @extschema@.CDB_LinkGhostTables(ddl_tag);
|
2019-03-07 01:19:38 +08:00
|
|
|
RETURN NULL;
|
|
|
|
END;
|
2019-10-03 19:59:51 +08:00
|
|
|
$$ LANGUAGE plpgsql
|
|
|
|
VOLATILE
|
|
|
|
PARALLEL UNSAFE
|
|
|
|
SECURITY DEFINER
|
|
|
|
SET search_path = @extschema@, pg_temp;
|
2019-03-07 01:19:38 +08:00
|
|
|
|
2019-05-31 21:29:28 +08:00
|
|
|
-- Event trigger to save the current transaction in @extschema@.cdb_ddl_execution
|
|
|
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_SaveDDLTransaction()
|
2019-03-07 01:19:38 +08:00
|
|
|
RETURNS event_trigger
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
2019-06-19 21:27:28 +08:00
|
|
|
INSERT INTO @extschema@.cdb_ddl_execution VALUES (txid_current(), tg_tag) ON CONFLICT ON CONSTRAINT cdb_ddl_execution_pkey DO NOTHING;
|
2019-03-07 01:19:38 +08:00
|
|
|
END;
|
2019-10-03 19:59:51 +08:00
|
|
|
$$ LANGUAGE plpgsql
|
|
|
|
VOLATILE
|
|
|
|
PARALLEL UNSAFE
|
|
|
|
SECURITY DEFINER
|
|
|
|
SET search_path = @extschema@, pg_temp;
|
2019-03-07 01:19:38 +08:00
|
|
|
|
|
|
|
-- Creates the trigger on DDL events to link ghost tables
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_EnableGhostTablesTrigger()
|
2019-03-07 01:19:38 +08:00
|
|
|
RETURNS void
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
2019-03-08 15:43:22 +08:00
|
|
|
DROP EVENT TRIGGER IF EXISTS link_ghost_tables;
|
2019-05-31 21:29:28 +08:00
|
|
|
DROP TRIGGER IF EXISTS check_ddl_update ON @extschema@.cdb_ddl_execution;
|
2019-03-11 18:28:45 +08:00
|
|
|
|
2019-03-11 21:02:45 +08:00
|
|
|
-- Table to store the transaction id from DDL events to avoid multiple executions
|
2019-06-20 17:20:03 +08:00
|
|
|
CREATE TABLE IF NOT EXISTS @extschema@.cdb_ddl_execution(txid bigint PRIMARY KEY, tag text);
|
2019-03-11 21:02:45 +08:00
|
|
|
|
2019-03-11 18:28:45 +08:00
|
|
|
CREATE CONSTRAINT TRIGGER check_ddl_update
|
2019-05-31 21:29:28 +08:00
|
|
|
AFTER INSERT ON @extschema@.cdb_ddl_execution
|
2019-03-11 18:28:45 +08:00
|
|
|
INITIALLY DEFERRED
|
|
|
|
FOR EACH ROW
|
2019-05-31 21:29:28 +08:00
|
|
|
EXECUTE PROCEDURE @extschema@._CDB_LinkGhostTablesTrigger();
|
2019-03-11 18:28:45 +08:00
|
|
|
|
2019-03-07 01:19:38 +08:00
|
|
|
CREATE EVENT TRIGGER link_ghost_tables
|
|
|
|
ON ddl_command_end
|
2019-10-01 16:46:37 +08:00
|
|
|
WHEN TAG IN ('CREATE TABLE',
|
|
|
|
'SELECT INTO',
|
|
|
|
'DROP TABLE',
|
|
|
|
'ALTER TABLE',
|
|
|
|
|
|
|
|
'CREATE TRIGGER',
|
|
|
|
'DROP TRIGGER',
|
|
|
|
'ALTER TRIGGER',
|
|
|
|
|
|
|
|
'CREATE VIEW',
|
|
|
|
'DROP VIEW',
|
|
|
|
'ALTER VIEW',
|
|
|
|
|
|
|
|
'CREATE FOREIGN TABLE',
|
|
|
|
'ALTER FOREIGN TABLE',
|
|
|
|
'DROP FOREIGN TABLE',
|
|
|
|
|
|
|
|
'ALTER MATERIALIZED VIEW',
|
|
|
|
'CREATE MATERIALIZED VIEW',
|
|
|
|
'DROP MATERIALIZED VIEW',
|
|
|
|
|
|
|
|
'IMPORT FOREIGN SCHEMA',
|
|
|
|
|
|
|
|
'DROP EXTENSION',
|
|
|
|
'DROP SCHEMA',
|
|
|
|
'DROP SERVER',
|
|
|
|
'DROP TYPE')
|
2019-05-31 21:29:28 +08:00
|
|
|
EXECUTE PROCEDURE @extschema@.CDB_SaveDDLTransaction();
|
2019-03-07 01:19:38 +08:00
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|
|
|
|
|
|
|
|
-- Drops the trigger on DDL events to link ghost tables
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_DisableGhostTablesTrigger()
|
2019-03-07 01:19:38 +08:00
|
|
|
RETURNS void
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
2019-03-08 15:43:22 +08:00
|
|
|
DROP EVENT TRIGGER IF EXISTS link_ghost_tables;
|
2019-05-31 21:29:28 +08:00
|
|
|
DROP TRIGGER IF EXISTS check_ddl_update ON @extschema@.cdb_ddl_execution;
|
|
|
|
DROP TABLE IF EXISTS @extschema@.cdb_ddl_execution;
|
2019-03-07 01:19:38 +08:00
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
|