Skip to content

Instantly share code, notes, and snippets.

@zacharyarnaise
Created November 28, 2023 16:58
Show Gist options
  • Select an option

  • Save zacharyarnaise/05cb8ba79fb28ab5e950e12fb9393f4c to your computer and use it in GitHub Desktop.

Select an option

Save zacharyarnaise/05cb8ba79fb28ab5e950e12fb9393f4c to your computer and use it in GitHub Desktop.
PostgreSQL: duplicate a row multiple times
CREATE TEMPORARY TABLE temporary_table AS SELECT * FROM <my_table> WHERE <condition>;
-- Remove any columns that will cause a constraint error when re-inserting the row
ALTER TABLE temporary_table DROP COLUMN id;
-- Re-insert 100 copies of the row with a new UUID
INSERT INTO <my_table> SELECT uuid_generate_v4() AS id, temporary_table.* FROM temporary_table CROSS JOIN GENERATE_SERIES(1,100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment