From ecbdb4a430343099482b6fee0543ea6cb6f6ccaf Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Tue, 9 Feb 2016 13:40:18 +0100 Subject: [PATCH] Move fdw-aware functions to CDB_ForeignTable.sql --- scripts-available/CDB_ForeignTable.sql | 66 ++++++++++++++++++++++++++ scripts-available/CDB_QueryTables.sql | 65 ------------------------- 2 files changed, 66 insertions(+), 65 deletions(-) diff --git a/scripts-available/CDB_ForeignTable.sql b/scripts-available/CDB_ForeignTable.sql index 85042db..6367512 100644 --- a/scripts-available/CDB_ForeignTable.sql +++ b/scripts-available/CDB_ForeignTable.sql @@ -130,3 +130,69 @@ BEGIN END $$ LANGUAGE plpgsql; + + +-- Return a set of (dbname, schema_name, table_name, updated_at) +-- It is aware of foreign tables +-- It assumes the local (schema_name, table_name) map to the remote ones with the same name +CREATE OR REPLACE FUNCTION cartodb.CDB_QueryTables_Updated_At(query text) +RETURNS TABLE(db_name text, schema_name text, table_name text, updated_at timestamptz) +AS $$ + WITH query_tables AS ( + SELECT unnest(CDB_QueryTablesText(query)) schema_table_name + ), query_tables_oid AS ( + SELECT schema_table_name, schema_table_name::regclass::oid AS reloid + FROM query_tables + ), + fqtn AS ( + SELECT + (CASE WHEN c.relkind = 'f' THEN _cdb_dbname_of_foreign_table(query_tables_oid.reloid) + ELSE current_database() + END)::text AS dbname, + n.nspname::text schema_name, + c.relname::text table_name, + c.relkind, + query_tables_oid.reloid + FROM query_tables_oid, pg_catalog.pg_class c + LEFT JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid + WHERE c.oid = query_tables_oid.reloid + ) + SELECT fqtn.dbname, fqtn.schema_name, fqtn.table_name, + (CASE WHEN relkind = 'f' THEN CDB_Get_Foreign_Updated_At(reloid) + ELSE (SELECT md.updated_at FROM cartodb.CDB_TableMetadata md WHERE md.tabname = reloid) + END) AS updated_at + FROM fqtn; +$$ LANGUAGE SQL; + + +CREATE OR REPLACE FUNCTION cartodb._cdb_dbname_of_foreign_table(reloid oid) +RETURNS TEXT AS $$ + SELECT option_value FROM pg_options_to_table(( + + SELECT fs.srvoptions + FROM pg_foreign_table ft + LEFT JOIN pg_foreign_server fs ON ft.ftserver = fs.oid + WHERE ft.ftrelid = reloid + + )) WHERE option_name='dbname'; +$$ LANGUAGE SQL; + + +-- Return the last updated time of a set of tables +-- It is aware of foreign tables +-- It assumes the local (schema_name, table_name) map to the remote ones with the same name +CREATE OR REPLACE FUNCTION cartodb.CDB_Last_Updated_Time(tables text[]) +RETURNS timestamptz AS $$ + WITH t AS ( + SELECT unnest(tables) AS schema_table_name + ), t_oid AS ( + SELECT (t.schema_table_name)::regclass::oid as reloid FROM t + ), t_updated_at AS ( + SELECT + (CASE WHEN relkind = 'f' THEN CDB_Get_Foreign_Updated_At(reloid) + ELSE (SELECT md.updated_at FROM cartodb.CDB_TableMetadata md WHERE md.tabname = reloid) + END) AS updated_at + FROM t_oid + LEFT JOIN pg_catalog.pg_class c ON c.oid = reloid + ) SELECT max(updated_at) FROM t_updated_at; +$$ LANGUAGE SQL; diff --git a/scripts-available/CDB_QueryTables.sql b/scripts-available/CDB_QueryTables.sql index 80ef620..c7cfa64 100644 --- a/scripts-available/CDB_QueryTables.sql +++ b/scripts-available/CDB_QueryTables.sql @@ -76,68 +76,3 @@ BEGIN RETURN CDB_QueryTablesText(query)::name[]; END $$ LANGUAGE 'plpgsql' VOLATILE STRICT; - --------------------------------------------------------------------------------- - --- Return a set of (dbname, schema_name, table_name, updated_at) --- This assumes the local (schema_name, table_name) maps to the remote one with the same name -CREATE OR REPLACE FUNCTION CDB_QueryTables_Updated_At(query text) -RETURNS TABLE(db_name text, schema_name text, table_name text, updated_at timestamptz) -AS $$ - WITH query_tables AS ( - SELECT unnest(CDB_QueryTablesText(query)) schema_table_name - ), query_tables_oid AS ( - SELECT schema_table_name, schema_table_name::regclass::oid AS reloid - FROM query_tables - ), - fqtn AS ( - SELECT - (CASE WHEN c.relkind = 'f' THEN _cdb_dbname_of_foreign_table(query_tables_oid.reloid) - ELSE current_database() - END)::text AS dbname, - n.nspname::text schema_name, - c.relname::text table_name, - c.relkind, - query_tables_oid.reloid - FROM query_tables_oid, pg_catalog.pg_class c - LEFT JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid - WHERE c.oid = query_tables_oid.reloid - ) - SELECT fqtn.dbname, fqtn.schema_name, fqtn.table_name, - (CASE WHEN relkind = 'f' THEN CDB_Get_Foreign_Updated_At(reloid) - ELSE (SELECT md.updated_at FROM CDB_TableMetadata md WHERE md.tabname = reloid) - END) AS updated_at - FROM fqtn; -$$ LANGUAGE SQL; - - -CREATE OR REPLACE FUNCTION _cdb_dbname_of_foreign_table(reloid oid) -RETURNS TEXT AS $$ - SELECT option_value FROM pg_options_to_table(( - - SELECT fs.srvoptions - FROM pg_foreign_table ft - LEFT JOIN pg_foreign_server fs ON ft.ftserver = fs.oid - WHERE ft.ftrelid = reloid - - )) WHERE option_name='dbname'; -$$ LANGUAGE SQL; - - --- TODO: move to the right place --- Return the last updated time of a set of tables -CREATE OR REPLACE FUNCTION CDB_Last_Updated_Time(tables text[]) -RETURNS timestamptz AS $$ - WITH t AS ( - SELECT unnest(tables) AS schema_table_name - ), t_oid AS ( - SELECT (t.schema_table_name)::regclass::oid as reloid FROM t - ), t_updated_at AS ( - SELECT - (CASE WHEN relkind = 'f' THEN CDB_Get_Foreign_Updated_At(reloid) - ELSE (SELECT md.updated_at FROM CDB_TableMetadata md WHERE md.tabname = reloid) - END) AS updated_at - FROM t_oid - LEFT JOIN pg_catalog.pg_class c ON c.oid = reloid - ) SELECT max(updated_at) FROM t_updated_at; -$$ LANGUAGE SQL;