Break routine into two halves

master
Paul Ramsey 10 years ago
parent c1bfef25e0
commit 0899c64d0b

@ -1064,7 +1064,7 @@ $$ LANGUAGE 'plpgsql';
-- a "good" one, and the same for the geometry columns. If all the required -- a "good" one, and the same for the geometry columns. If all the required
-- columns are in place already, it no-ops and just renames the table to -- columns are in place already, it no-ops and just renames the table to
-- the destination if necessary. -- the destination if necessary.
CREATE OR REPLACE FUNCTION _CDB_Rewrite_Table(reloid REGCLASS, destschema TEXT, has_usable_primary_key BOOLEAN, has_usable_geoms BOOLEAN) CREATE OR REPLACE FUNCTION _CDB_Rewrite_Table(reloid REGCLASS, destschema TEXT DEFAULT NULL)
RETURNS BOOLEAN RETURNS BOOLEAN
AS $$ AS $$
DECLARE DECLARE
@ -1090,6 +1090,9 @@ DECLARE
str TEXT; str TEXT;
table_srid INTEGER; table_srid INTEGER;
has_usable_primary_key BOOLEAN;
has_usable_geoms BOOLEAN;
BEGIN BEGIN
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): %', 'entered function'; RAISE DEBUG 'CDB(_CDB_Rewrite_Table): %', 'entered function';
@ -1097,17 +1100,56 @@ BEGIN
-- Read CartoDB standard column names in -- Read CartoDB standard column names in
const := _CDB_Columns(); const := _CDB_Columns();
-- No-op if there is no rewrite to be done
IF has_usable_primary_key AND has_usable_geoms THEN
RETURN true;
END IF;
-- Save the raw schema/table names for later -- Save the raw schema/table names for later
SELECT n.nspname, c.relname, c.relname SELECT n.nspname, c.relname, c.relname
INTO STRICT relschema, relname, destname INTO STRICT relschema, relname, destname
FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE c.oid = reloid; WHERE c.oid = reloid;
-- Default the destination to current schema if unspecified
IF destschema IS NULL THEN
destschema := relschema;
END IF;
-- See if there is a primary key column we need to carry along to the
-- new table. If this is true, it implies there is an indexed
-- primary key of integer type named (by default) cartodb_id
SELECT _CDB_Has_Usable_Primary_ID(reloid) AS has_usable_primary_key
INTO STRICT has_usable_primary_key;
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): has_usable_primary_key %', has_usable_primary_key;
-- See if the geometry columns we need are already available
-- on the table. If they are, we don't need to do any bulk
-- transformation of the table, we can just ensure proper
-- indexes are in place and apply a rename
SELECT _CDB_Has_Usable_Geom(reloid) AS has_usable_geoms
INTO STRICT has_usable_geoms;
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): has_usable_geoms %', has_usable_geoms;
-- We can only avoid a rewrite if both the key and
-- geometry are usable
-- No table re-write is required, BUT a rename is required to
-- a destination schema, so do that now
IF has_usable_primary_key AND has_usable_geoms AND destschema != relschema THEN
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): perfect table needs to be moved to schema (%)', destschema;
PERFORM _CDB_SQL(Format('ALTER TABLE %s SET SCHEMA %s', reloid::text, destschema), '_CDB_Rewrite_Table');
RETURN true;
-- Don't move anything, just make sure our destination information is set right
ELSIF has_usable_primary_key AND has_usable_geoms AND destschema = relschema THEN
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): perfect table in the perfect place';
RETURN true;
END IF;
-- We must rewrite, so here we go...
-- Put the primary key sequence in the right schema -- Put the primary key sequence in the right schema
-- If the new table is not moving, better ensure the sequence name -- If the new table is not moving, better ensure the sequence name
-- is unique -- is unique
@ -1396,11 +1438,6 @@ DECLARE
destoid REGCLASS; destoid REGCLASS;
destname TEXT; destname TEXT;
has_usable_primary_key BOOLEAN;
has_usable_geoms BOOLEAN;
rewrite_success BOOLEAN;
rewrite BOOLEAN;
index_success BOOLEAN;
rec RECORD; rec RECORD;
BEGIN BEGIN
@ -1414,6 +1451,7 @@ BEGIN
-- Check destination schema exists -- Check destination schema exists
-- Throws an exception of there is no matching schema -- Throws an exception of there is no matching schema
IF destschema IS NOT NULL THEN IF destschema IS NOT NULL THEN
SELECT n.nspname SELECT n.nspname
INTO rec FROM pg_namespace n WHERE n.nspname = destschema; INTO rec FROM pg_namespace n WHERE n.nspname = destschema;
IF NOT FOUND THEN IF NOT FOUND THEN
@ -1427,66 +1465,18 @@ BEGIN
-- Drop triggers first -- Drop triggers first
PERFORM _CDB_drop_triggers(reloid); PERFORM _CDB_drop_triggers(reloid);
-- See if there is a primary key column we need to carry along to the -- Rewrite (or rename) the table to the new location
-- new table. If this is true, it implies there is an indexed PERFORM _CDB_Rewrite_Table(reloid, destschema);
-- primary key of integer type named (by default) cartodb_id
SELECT _CDB_Has_Usable_Primary_ID(reloid) AS has_usable_primary_key
INTO STRICT has_usable_primary_key;
RAISE DEBUG 'CDB(CDB_CartodbfyTable2): has_usable_primary_key %', has_usable_primary_key;
-- See if the geometry columns we need are already available
-- on the table. If they are, we don't need to do any bulk
-- transformation of the table, we can just ensure proper
-- indexes are in place and apply a rename
SELECT _CDB_Has_Usable_Geom(reloid) AS has_usable_geoms
INTO STRICT has_usable_geoms;
RAISE DEBUG 'CDB(CDB_CartodbfyTable2): has_usable_geoms %', has_usable_geoms;
-- We can only avoid a rewrite if both the key and
-- geometry are usable
rewrite := NOT (has_usable_primary_key AND has_usable_geoms);
-- No table re-write is required, BUT a rename is required to
-- a destination schema, so do that now
IF NOT rewrite AND destschema != relschema THEN
RAISE DEBUG 'CDB(CDB_CartodbfyTable2): perfect table needs to be moved to schema (%)', destschema;
EXECUTE Format('ALTER TABLE %s SET SCHEMA %s', reloid::text, destschema);
-- Don't move anything, just make sure our destination information is set right
ELSIF NOT rewrite AND destschema = relschema THEN
RAISE DEBUG 'CDB(CDB_CartodbfyTable2): perfect table in the perfect place';
-- We must rewrite, so here we go...
ELSIF rewrite THEN
SELECT _CDB_Rewrite_Table(reloid, destschema, has_usable_primary_key, has_usable_geoms)
INTO STRICT rewrite_success;
IF NOT rewrite_success THEN
PERFORM _CDB_Error('rewriting table', 'CDB_CartodbfyTable2');
END IF;
END IF;
-- The old regclass might not be valid anymore if we re-wrote the table... -- The old regclass might not be valid anymore if we re-wrote the table...
destoid := (destschema || '.' || destname)::regclass; destoid := (destschema || '.' || destname)::regclass;
-- Add indexes to the destination table, as necessary -- Add indexes to the destination table, as necessary
SELECT _CDB_Add_Indexes(destoid) PERFORM _CDB_Add_Indexes(destoid);
INTO STRICT index_success;
IF NOT index_success THEN
PERFORM _CDB_Error('indexing table', 'CDB_CartodbfyTable2');
END IF;
-- Add triggers to the destination table, as necessary -- Add triggers to the destination table, as necessary
-- PERFORM _CDB_create_triggers(destschema, reloid); -- PERFORM _CDB_create_triggers(destschema, reloid);
END; END;
$$ LANGUAGE 'plpgsql'; $$ LANGUAGE 'plpgsql';

Loading…
Cancel
Save