2014-05-05 23:13:06 +08:00
|
|
|
-- Return an array of table names scanned by a given query
|
|
|
|
--
|
|
|
|
-- Requires PostgreSQL 9.x+
|
|
|
|
--
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_QueryTablesText(query text)
|
2015-06-24 20:13:58 +08:00
|
|
|
RETURNS text[]
|
2014-05-05 23:13:06 +08:00
|
|
|
AS $$
|
|
|
|
DECLARE
|
|
|
|
exp XML;
|
2015-06-24 20:13:58 +08:00
|
|
|
tables text[];
|
2014-05-05 23:13:06 +08:00
|
|
|
rec RECORD;
|
|
|
|
rec2 RECORD;
|
|
|
|
BEGIN
|
2018-07-20 19:01:51 +08:00
|
|
|
|
2014-05-05 23:13:06 +08:00
|
|
|
tables := '{}';
|
|
|
|
|
2019-05-31 21:29:28 +08:00
|
|
|
FOR rec IN SELECT @extschema@.CDB_QueryStatements(query) q LOOP
|
2014-05-05 23:13:06 +08:00
|
|
|
BEGIN
|
2014-06-26 01:38:29 +08:00
|
|
|
EXECUTE 'EXPLAIN (FORMAT XML, VERBOSE) ' || rec.q INTO STRICT exp;
|
2018-07-20 19:01:51 +08:00
|
|
|
EXCEPTION WHEN syntax_error THEN
|
|
|
|
-- We can get a syntax error if the user tries to EXPLAIN a DDL
|
|
|
|
CONTINUE;
|
|
|
|
WHEN others THEN
|
2014-05-05 23:13:06 +08:00
|
|
|
-- TODO: if error is 'relation "xxxxxx" does not exist', take xxxxxx as
|
|
|
|
-- the affected table ?
|
|
|
|
RAISE WARNING 'CDB_QueryTables cannot explain query: % (%: %)', rec.q, SQLSTATE, SQLERRM;
|
|
|
|
RAISE EXCEPTION '%', SQLERRM;
|
|
|
|
CONTINUE;
|
|
|
|
END;
|
|
|
|
|
|
|
|
-- Now need to extract all values of <Relation-Name>
|
|
|
|
|
2014-06-26 01:38:29 +08:00
|
|
|
-- RAISE DEBUG 'Explain: %', exp;
|
2014-05-05 23:13:06 +08:00
|
|
|
|
|
|
|
FOR rec2 IN WITH
|
2014-06-26 01:38:29 +08:00
|
|
|
inp AS (
|
|
|
|
SELECT
|
|
|
|
xpath('//x:Relation-Name/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as x,
|
2014-08-02 00:06:06 +08:00
|
|
|
xpath('//x:Relation-Name/../x:Schema/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as s
|
2014-06-26 01:38:29 +08:00
|
|
|
)
|
2015-09-03 19:12:29 +08:00
|
|
|
SELECT unnest(x)::text as p, unnest(s)::text as sc from inp
|
2014-05-05 23:13:06 +08:00
|
|
|
LOOP
|
2014-06-26 01:38:29 +08:00
|
|
|
-- RAISE DEBUG 'tab: %', rec2.p;
|
|
|
|
-- RAISE DEBUG 'sc: %', rec2.sc;
|
2015-09-03 19:12:29 +08:00
|
|
|
tables := array_append(tables, format('%s.%s', quote_ident(rec2.sc), quote_ident(rec2.p)));
|
2014-05-05 23:13:06 +08:00
|
|
|
END LOOP;
|
|
|
|
|
|
|
|
-- RAISE DEBUG 'Tables: %', tables;
|
|
|
|
|
|
|
|
END LOOP;
|
|
|
|
|
|
|
|
-- RAISE DEBUG 'Tables: %', tables;
|
|
|
|
|
|
|
|
-- Remove duplicates and sort by name
|
|
|
|
IF array_upper(tables, 1) > 0 THEN
|
|
|
|
WITH dist as ( SELECT DISTINCT unnest(tables)::text as p ORDER BY p )
|
|
|
|
SELECT array_agg(p) from dist into tables;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
--RAISE DEBUG 'Tables: %', tables;
|
|
|
|
|
|
|
|
return tables;
|
|
|
|
END
|
2017-10-24 20:16:56 +08:00
|
|
|
$$ LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE;
|
2015-06-24 17:53:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
-- Keep CDB_QueryTables with same signature for backwards compatibility.
|
|
|
|
-- It should probably be removed in the future.
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_QueryTables(query text)
|
2015-06-24 17:53:07 +08:00
|
|
|
RETURNS name[]
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
2019-05-31 21:29:28 +08:00
|
|
|
RETURN @extschema@.CDB_QueryTablesText(query)::name[];
|
2015-06-24 17:53:07 +08:00
|
|
|
END
|
2017-10-24 20:16:56 +08:00
|
|
|
$$ LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE;
|