CDB_QueryTables return tables with schema

This commit is contained in:
javi 2014-06-25 19:38:29 +02:00
parent 1815096a37
commit da9b9a640f
3 changed files with 26 additions and 11 deletions

View File

@ -22,7 +22,7 @@ BEGIN
END IF; END IF;
BEGIN BEGIN
EXECUTE 'EXPLAIN (FORMAT XML) ' || rec.q INTO STRICT exp; EXECUTE 'EXPLAIN (FORMAT XML, VERBOSE) ' || rec.q INTO STRICT exp;
EXCEPTION WHEN others THEN EXCEPTION WHEN others THEN
-- TODO: if error is 'relation "xxxxxx" does not exist', take xxxxxx as -- TODO: if error is 'relation "xxxxxx" does not exist', take xxxxxx as
-- the affected table ? -- the affected table ?
@ -33,14 +33,19 @@ BEGIN
-- Now need to extract all values of <Relation-Name> -- Now need to extract all values of <Relation-Name>
--RAISE DEBUG 'Explain: %', exp; -- RAISE DEBUG 'Explain: %', exp;
FOR rec2 IN WITH FOR rec2 IN WITH
inp AS ( SELECT xpath('//x:Relation-Name/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as x ) inp AS (
SELECT unnest(x)::name as p from inp SELECT
xpath('//x:Relation-Name/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as x,
xpath('//x:Schema/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as s
)
SELECT unnest(x)::name as p, unnest(s)::name as sc from inp
LOOP LOOP
--RAISE DEBUG 'tab: %', rec2.p; -- RAISE DEBUG 'tab: %', rec2.p;
tables := array_append(tables, rec2.p); -- RAISE DEBUG 'sc: %', rec2.sc;
tables := array_append(tables, (rec2.sc || '.' || rec2.p)::name);
END LOOP; END LOOP;
-- RAISE DEBUG 'Tables: %', tables; -- RAISE DEBUG 'Tables: %', tables;

View File

@ -25,3 +25,9 @@ WITH inp AS ( select 'create table test (a int); insert into test values (1); se
WITH inp AS ( select 'WITH a AS (select * from pg_class) select * from a'::text as q ) WITH inp AS ( select 'WITH a AS (select * from pg_class) select * from a'::text as q )
SELECT q, CDB_QueryTables(q) from inp; SELECT q, CDB_QueryTables(q) from inp;
CREATE SCHEMA sc;
create table sc.test (a int);
insert into sc.test values (1);
WITH inp AS ( select 'select * from sc.test'::text as q )
SELECT q, CDB_QueryTables(q) from inp;

View File

@ -1,11 +1,15 @@
SELECT * FROM geometry_columns|{pg_attribute,pg_class,pg_namespace,pg_type} SELECT * FROM geometry_columns|{pg_catalog.pg_attribute,pg_catalog.pg_class,pg_catalog.pg_namespace,pg_catalog.pg_type}
SELECT a.attname FROM pg_class c JOIN pg_attribute a on (a.attrelid = c.oid)|{pg_attribute,pg_class} SELECT a.attname FROM pg_class c JOIN pg_attribute a on (a.attrelid = c.oid)|{pg_catalog.pg_attribute,pg_catalog.pg_class}
CREATE table "my'tab;le" as select 1|{} CREATE table "my'tab;le" as select 1|{}
SELECT a.oid, b.oid FROM pg_class a, pg_class b|{pg_class} SELECT a.oid, b.oid FROM pg_class a, pg_class b|{pg_catalog.pg_class}
SELECT 1 as col1; select 2 as col2|{} SELECT 1 as col1; select 2 as col2|{}
WARNING: CDB_QueryTables cannot explain query: select 1 from nonexistant (42P01: relation "nonexistant" does not exist) WARNING: CDB_QueryTables cannot explain query: select 1 from nonexistant (42P01: relation "nonexistant" does not exist)
ERROR: relation "nonexistant" does not exist ERROR: relation "nonexistant" does not exist
begin; select * from pg_class; commit;|{pg_class} begin; select * from pg_class; commit;|{pg_catalog.pg_class}
WARNING: CDB_QueryTables cannot explain query: select * from test (42P01: relation "test" does not exist) WARNING: CDB_QueryTables cannot explain query: select * from test (42P01: relation "test" does not exist)
ERROR: relation "test" does not exist ERROR: relation "test" does not exist
WITH a AS (select * from pg_class) select * from a|{pg_class} WITH a AS (select * from pg_class) select * from a|{pg_catalog.pg_class}
CREATE SCHEMA
CREATE TABLE
INSERT 0 1
select * from sc.test|{sc.test}