• BEGIN TRANSACTION;
    
            DO $$ BEGIN
            IF (SELECT COUNT(*)=1 FROM information_schema.tables WHERE table_name IN ('history_text_y2018m01')) THEN
            ALTER TABLE history_text DETACH PARTITION history_text_y2018m01;
    
    CREATE FOREIGN TABLE IF NOT EXISTS archive_text_y2018m01
    PARTITION OF history_text FOR VALUES FROM (1514764800) TO (1517443200) SERVER archive;
            END IF; END $$;
    COMMIT;
    
            DO $$ BEGIN
            IF (SELECT COUNT(*)=2 FROM information_schema.tables WHERE table_name IN ('archive_text_y2018m01', 'history_text_y2018m01')) THEN
    
    WITH moved_rows AS (DELETE FROM history_text_y2018m01 a RETURNING a.*)
        INSERT INTO archive_text_y2018m01 SELECT * FROM moved_rows ORDER BY itemid, clock;
    DROP TABLE IF EXISTS history_text_y2018m01;
            END IF; END $$;
    Edited by Spindel 🕷 Ljungmark
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment