Skip to content

Instantly share code, notes, and snippets.

@yyscamper
Created April 28, 2018 09:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save yyscamper/da366585ee53b818822e21d5dac1c62a to your computer and use it in GitHub Desktop.
Postgresql function to create partition table by city_id
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