Created
April 28, 2018 09:58
-
-
Save yyscamper/da366585ee53b818822e21d5dac1c62a to your computer and use it in GitHub Desktop.
Postgresql function to create partition table by city_id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION create_city_partition(schema_name text, parent_table_name text, city_id int) | |
RETURNS text AS | |
$func$ | |
DECLARE | |
partition_table_name text; | |
BEGIN | |
partition_table_name := parent_table_name || '_' || city_id::text; | |
EXECUTE format(' | |
CREATE TABLE %I.%I PARTITION OF %I FOR VALUES IN (%L) | |
', schema_name, partition_table_name, parent_table_name, city_id); | |
RETURN schema_name || '.' || partition_table_name; | |
END | |
$func$ LANGUAGE plpgsql; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment