Add a new function CDB_QueryTablesRegclass #86

The return values of it can be safely used when len(schema.table_name)
exceeds the 63 char limit of the postgres type `name`.
This commit is contained in:
Rafa de la Torre
2015-06-24 11:53:09 +02:00
parent 189309e1a5
commit 9a94b3879a
+16 -5
View File
@@ -2,12 +2,12 @@
-- --
-- Requires PostgreSQL 9.x+ -- Requires PostgreSQL 9.x+
-- --
CREATE OR REPLACE FUNCTION CDB_QueryTables(query text) CREATE OR REPLACE FUNCTION CDB_QueryTablesRegclass(query text)
RETURNS name[] RETURNS regclass[]
AS $$ AS $$
DECLARE DECLARE
exp XML; exp XML;
tables NAME[]; tables regclass[];
rec RECORD; rec RECORD;
rec2 RECORD; rec2 RECORD;
BEGIN BEGIN
@@ -41,11 +41,11 @@ BEGIN
xpath('//x:Relation-Name/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as x, xpath('//x:Relation-Name/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as x,
xpath('//x:Relation-Name/../x:Schema/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as s xpath('//x:Relation-Name/../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 SELECT unnest(x) as p, unnest(s) as sc from inp
LOOP LOOP
-- RAISE DEBUG 'tab: %', rec2.p; -- RAISE DEBUG 'tab: %', rec2.p;
-- RAISE DEBUG 'sc: %', rec2.sc; -- RAISE DEBUG 'sc: %', rec2.sc;
tables := array_append(tables, (rec2.sc || '.' || rec2.p)::name); tables := array_append(tables, (rec2.sc || '.' || rec2.p)::regclass);
END LOOP; END LOOP;
-- RAISE DEBUG 'Tables: %', tables; -- RAISE DEBUG 'Tables: %', tables;
@@ -65,3 +65,14 @@ BEGIN
return tables; return tables;
END END
$$ LANGUAGE 'plpgsql' VOLATILE STRICT; $$ LANGUAGE 'plpgsql' VOLATILE STRICT;
-- Keep CDB_QueryTables with same signature for backwards compatibility.
-- It should probably be removed in the future.
CREATE OR REPLACE FUNCTION CDB_QueryTables(query text)
RETURNS name[]
AS $$
BEGIN
RETURN CDB_QueryTablesRegclass(query)::name[];
END
$$ LANGUAGE 'plpgsql' VOLATILE STRICT;