pg_partman icon indicating copy to clipboard operation
pg_partman copied to clipboard

Partman is creating empty partitions till the max value

Open gouthasnm opened this issue 4 years ago • 7 comments

Following is my DDL.

create table if not exists schema_a.data_project ( project varchar(30) not null, startdate timestamp not null, value double precision, lastupdatedtime timestamp not null ) partition by LIST (project);

create table schema_a.data_project_c partition of schema_a.data_project FOR VALUES IN ('Capacity') partition by range (startdate);

select partman.create_parent( p_parent_table => 'schema_a.data_project_c',p_control => 'startdate',p_type => 'native',p_interval=> 'daily',p_premake => 600, p_start_partition := '2022-04-01');

If start date has a row with value, let's say 2100-01-01, partman seems to be creating partitions till 2100-01-01. All of my data has 2022, but only one row has 2100.

When the maintainace proc runs, it runs for hours trying to do the following (for each day till 2100):

ALTER TABLE schema_a.data_project_c ATTACH PARTITION schema_a.data_project_c_p2066_02_19 FOR VALUES FROM ('2066-02-19 00:00:00+00') TO ('2066-02-20 00:00:00+00'

The documentation did not have information on how to prevent this from happening. The issue happens irrespective of p_start_partition being used or not.

Is there a way to exclude 2100-01-01 while creating partitions through the scheduled maintenance? Thank You!

gouthasnm avatar May 26 '22 13:05 gouthasnm

The row with value 2100-01-01 is going into the default and I do have maintenance checking for data in the default partition when making future partitions. I did this because often people would say they have gaps in their data insertion and data would end up in the default since maintenance wasn't keeping up. The infinite_time_partitions option can get around this, but people don't always want partitions to be created even if no data is being inserted.

However, I can see about making an option to ignore the data in the default during maintenance.

I think your situation is having a bigger issue with this because you have the premake set to 600 so the next time maintenance runs, it's trying to create 600 child tables. First, I would ask if this is really the expected time window of data you ingest? If it is, then that's fine. But if not, I would suggest lowering the premake a bit to be closer to the expected time window of data insertion. As long as you're running maintenance daily, premake will always keep that many child tables created at minimum.

keithf4 avatar May 26 '22 13:05 keithf4

In the mean time, I would recommend setting up a monitor that checks the result of the check_default() function and act on that as soon as possible before the next maintenance runs.

Or, if you do not want to accept any data into the partition set that does not fit into existing children, you can remove the default child table after creating the partition set. In that case, an error will be thrown if such data is inserted.

keithf4 avatar May 27 '22 13:05 keithf4

@keithf4 Thank you for responding to my issue. I believe you are taking the max value from default partition and creating partitions sequentially all the way up to the max value. Instead, what do you think of an option to create partitions for just the distinct values in default partitions. This way if we have sporadic values, we don't end up with many empty partitions. Also, your proposal to have an option to ignore default partition while running maintenance would also be helpful.

My table is having plan values, hence we would have values going a year or 2 into the future. So, I would like to premake 600. And this isn't really causing any problem. The utility is premaking 600 on create and on a daily basis just creating one new partition. Both these operations complete in less than a minute. So I am good with that. And I can't drop the default partition because users sometime put dates way into future.

Thanks for any option that that can be added.

gouthasnm avatar May 27 '22 16:05 gouthasnm

Ok, I'll have to review that code again.

The partition_data_() functions/procedures can do what you're looking for to only make the specific child tables for the data in the default. The data has to be moved out of the default before that child table can be made and that handles it. I don't want this to be a part of automatic maintenance for the same reasons you're seeing here... it can make a ton of unnecessary child partitions during an accidental insertion of bad data to the default.

keithf4 avatar May 27 '22 16:05 keithf4

Thank you @keithf4

gouthasnm avatar May 28 '22 02:05 gouthasnm

Will have an option to ignore the default data in the next release. After revisiting this code, may see about making this the default in the next major release as well

keithf4 avatar Jun 25 '22 21:06 keithf4

Version 4.7.0 has been released with an option to ignore data in the default partition during maintenance (ignore_default_data in the part_config table. As I said, I'll likely make this the default behavior during the 5.x refactor design. Thank you for pointing out the issue!

keithf4 avatar Aug 12 '22 15:08 keithf4