Skip to content

Instantly share code, notes, and snippets.

@ivan
Last active May 11, 2025 08:13
Show Gist options
  • Save ivan/d13fa94c94487335973a2b96795b9bcf to your computer and use it in GitHub Desktop.
Save ivan/d13fa94c94487335973a2b96795b9bcf to your computer and use it in GitHub Desktop.
Recreating all of your PostgreSQL periods history tables
-- for each table
psql --quiet --no-psqlrc --echo-errors -c "COPY rgp.accessions_history TO STDOUT" > accessions.tsv
SELECT periods.drop_system_versioning('rgp.accessions');
DROP TABLE rgp.accessions_history;
-- once
DROP EXTENSION periods CASCADE;
CREATE EXTENSION periods;
-- for each table
SELECT periods.add_system_time_period('rgp.accessions', 'row_start', 'row_end');
SELECT periods.add_system_versioning('rgp.accessions'); -- create the _history table...
SELECT periods.drop_system_versioning('rgp.accessions'); -- allow us to GRANT in the next line
GRANT INSERT ON TABLE rgp.accessions_history TO CURRENT_USER; -- allow \copy to work
\copy rgp.accessions_history FROM 'accessions.tsv';
SELECT periods.add_system_versioning('rgp.accessions');
@ivan
Copy link
Author

ivan commented May 2, 2025

YMMV, don't do anything from here without backups.

@ivan
Copy link
Author

ivan commented May 2, 2025

Linked from xocolatl/periods#22

@ivan
Copy link
Author

ivan commented May 11, 2025

Update: no need to dump and recreate the data in the _history tables; just periods.drop_system_versioning on all the versioned tables, drop the extension, create the extension, then periods.add_system_time_period and periods.add_system_versioning on the tables which should be versioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment