pg_partman icon indicating copy to clipboard operation
pg_partman copied to clipboard

how to do IF NOT EXISTS ( SELECT partman.create_parent )

Open Shanky-21 opened this issue 3 years ago • 1 comments

I'm adding SELECT partman.create_parent() function into a evolution file, which will be used to create tables. but if the partitions already exists it is throwing error, in production environment we can't use it like this. we want to make this function run only if there is no previous entry. Some thing like -> CREATE TABLE IF NOT EXISTS .....

Shanky-21 avatar Aug 04 '22 15:08 Shanky-21

Are you saying the partition configuration already exists in pg_partman? If that's the case, you'll probably need something that first checks the part_config table to see if it has an entry there before calling create_parent(). Could write your own custom function to do that.

keithf4 avatar Aug 12 '22 15:08 keithf4

Hey Thank You @keithf4 it worked for me

Shanky-21 avatar Aug 26 '22 08:08 Shanky-21

On PostgreSQL, I found the best way to do this is:

select partman.create_parent(
               p_parent_table => 'public.my_table',
               p_control => 'inserted_at',
               p_type => 'native', -- I'm using pg_partman 4.6.2
               p_interval=> '1 day'
       )
 where not exists(
     select parent_table
       from partman.part_config
     where parent_table = 'public.my_table'
                 )

rlemaitre-ledger avatar Dec 12 '23 13:12 rlemaitre-ledger