From 9a94b3879a8c6ca34026ab09bd91c9a0095ab8b2 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Wed, 24 Jun 2015 11:53:07 +0200 Subject: [PATCH 1/4] 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`. --- scripts-available/CDB_QueryTables.sql | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/scripts-available/CDB_QueryTables.sql b/scripts-available/CDB_QueryTables.sql index cd8b51b..bd4d47f 100644 --- a/scripts-available/CDB_QueryTables.sql +++ b/scripts-available/CDB_QueryTables.sql @@ -2,12 +2,12 @@ -- -- Requires PostgreSQL 9.x+ -- -CREATE OR REPLACE FUNCTION CDB_QueryTables(query text) -RETURNS name[] +CREATE OR REPLACE FUNCTION CDB_QueryTablesRegclass(query text) +RETURNS regclass[] AS $$ DECLARE exp XML; - tables NAME[]; + tables regclass[]; rec RECORD; rec2 RECORD; 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/../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 -- RAISE DEBUG 'tab: %', rec2.p; -- 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; -- RAISE DEBUG 'Tables: %', tables; @@ -65,3 +65,14 @@ BEGIN return tables; END $$ 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; From ddb6b2c5b585df791a5d276b19ba5421077179b2 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Wed, 24 Jun 2015 14:13:58 +0200 Subject: [PATCH 2/4] Return text instead of regclass #86 This way the schema is always returned and backwards compatibility is kept, should it be needed. --- scripts-available/CDB_QueryTables.sql | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts-available/CDB_QueryTables.sql b/scripts-available/CDB_QueryTables.sql index bd4d47f..ac61281 100644 --- a/scripts-available/CDB_QueryTables.sql +++ b/scripts-available/CDB_QueryTables.sql @@ -2,12 +2,12 @@ -- -- Requires PostgreSQL 9.x+ -- -CREATE OR REPLACE FUNCTION CDB_QueryTablesRegclass(query text) -RETURNS regclass[] +CREATE OR REPLACE FUNCTION CDB_QueryTablesText(query text) +RETURNS text[] AS $$ DECLARE exp XML; - tables regclass[]; + tables text[]; rec RECORD; rec2 RECORD; BEGIN @@ -45,7 +45,7 @@ BEGIN LOOP -- RAISE DEBUG 'tab: %', rec2.p; -- RAISE DEBUG 'sc: %', rec2.sc; - tables := array_append(tables, (rec2.sc || '.' || rec2.p)::regclass); + tables := array_append(tables, (rec2.sc || '.' || rec2.p)); END LOOP; -- RAISE DEBUG 'Tables: %', tables; @@ -73,6 +73,6 @@ CREATE OR REPLACE FUNCTION CDB_QueryTables(query text) RETURNS name[] AS $$ BEGIN - RETURN CDB_QueryTablesRegclass(query)::name[]; + RETURN CDB_QueryTablesText(query)::name[]; END $$ LANGUAGE 'plpgsql' VOLATILE STRICT; From 22fc962d0983c97aa572f8d8bce5234e5ad9b5c9 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Wed, 24 Jun 2015 16:01:43 +0200 Subject: [PATCH 3/4] Change expectation #86 Just add CONTEXT lines since they are now added in case of WARNING/ERROR as a result of having CDB_QueryTables calling CDB_QueryTablesText. --- test/CDB_QueryTablesTest_expect | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/CDB_QueryTablesTest_expect b/test/CDB_QueryTablesTest_expect index bfe59df..d3a64d1 100644 --- a/test/CDB_QueryTablesTest_expect +++ b/test/CDB_QueryTablesTest_expect @@ -1,13 +1,18 @@ +\set ECHO none 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_catalog.pg_attribute,pg_catalog.pg_class} CREATE table "my'tab;le" as select 1|{} SELECT a.oid, b.oid FROM pg_class a, pg_class b|{pg_catalog.pg_class} SELECT 1 as col1; select 2 as col2|{} WARNING: CDB_QueryTables cannot explain query: select 1 from nonexistant (42P01: relation "nonexistant" does not exist) +CONTEXT: PL/pgSQL function cdb_querytables(text) line 3 at RETURN ERROR: relation "nonexistant" does not exist +CONTEXT: PL/pgSQL function cdb_querytables(text) line 3 at RETURN 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) +CONTEXT: PL/pgSQL function cdb_querytables(text) line 3 at RETURN ERROR: relation "test" does not exist +CONTEXT: PL/pgSQL function cdb_querytables(text) line 3 at RETURN WITH a AS (select * from pg_class) select * from a|{pg_catalog.pg_class} CREATE SCHEMA CREATE TABLE From c3eea08f66090f683ed96d187d8f40423734ab2c Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Wed, 24 Jun 2015 16:29:39 +0200 Subject: [PATCH 4/4] Remove ECHO from expectation #86 Remove the `\set ECHO none` from expectation that is automatically removed by the test harness but still appears in the output when a test fails. --- test/CDB_QueryTablesTest_expect | 1 - 1 file changed, 1 deletion(-) diff --git a/test/CDB_QueryTablesTest_expect b/test/CDB_QueryTablesTest_expect index d3a64d1..9959982 100644 --- a/test/CDB_QueryTablesTest_expect +++ b/test/CDB_QueryTablesTest_expect @@ -1,4 +1,3 @@ -\set ECHO none 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_catalog.pg_attribute,pg_catalog.pg_class} CREATE table "my'tab;le" as select 1|{}