@@ -289,55 +289,75 @@ Partitioning strategies
289289***********************
290290
291291
292- Monthly partitioning
293- ~~~~~~~~~~~~~~~~~~~~
294-
295- Partitions will be named ``[table_name]_[3-letter month name] ``.
292+ Time-based partitioning
293+ ~~~~~~~~~~~~~~~~~~~~~~~
296294
297295.. code-block :: python
298296
297+ from dateutil.relativedelta import relativedelta
298+
299299 from psqlextra.partitioning import (
300- PostgresPartitioningManager,
301- partition_by_current_time,
300+ PostgresPartitioningManager,
301+ PostgresCurrentTimePartitioningStrategy,
302+ PostgresTimePartitionSize,
303+ partition_by_current_time,
302304 )
303305
304- # 3 partitions ahead, each partition is one month
305- manager = PostgresPartitioningManager(
306- model = MyPartitionedModel,
307- count = 3 ,
308- months = 1 ,
306+ manager = PostgresPartitioningManager([
307+ # 3 partitions ahead, each partition is one month
308+ # delete partitions older than 6 months
309+ # partitions will be named `[table_name]_[year]_[3-letter month name]`.
310+ PostgresPartitioningConfig(
311+ model = MyPartitionedModel,
312+ strategy = PostgresCurrentTimePartitioningStrategy(
313+ size = PostgresTimePartitionSize(months = 1 ),
314+ count = 3 ,
315+ max_age = relativedelta(months = 6 ),
316+ ),
317+ ),
318+ # 6 partitions ahead, each partition is two weeks
319+ # delete partitions older than 8 months
320+ # partitions will be named `[table_name]_[year]_week_[week number]`.
321+ PostgresPartitioningConfig(
322+ model = MyPartitionedModel,
323+ strategy = PostgresCurrentTimePartitioningStrategy(
324+ size = PostgresTimePartitionSize(weeks = 2 ),
325+ count = 6 ,
326+ max_age = relativedelta(weeks = 8 ),
327+ ),
328+ ),
329+ # 12 partitions ahead, each partition is 5 days
330+ # old partitions are never deleted, `max_age` is not set
331+ # partitions will be named `[table_name]_[year]_[month]_[month day number]`.
332+ PostgresPartitioningConfig(
333+ model = MyPartitionedModel,
334+ strategy = PostgresCurrentTimePartitioningStrategy(
335+ size = PostgresTimePartitionSize(wdyas = 5 ),
336+ count = 12 ,
337+ ),
338+ ),
339+ ])
340+
341+ # these are the default arguments
342+ partioning_plan = manager.plan(
343+ skip_create = False ,
344+ skip_delete = False ,
345+ using = ' default'
309346 )
310- manager.apply()
311347
348+ # prints a list of partitions to be created/deleted
349+ partitioning_plan.print()
312350
313- Weekly partitioning
314- ~~~~~~~~~~~~~~~~~~~
351+ # apply the plan
352+ partitioning_plan.apply( using = ' default ' ) ;
315353
316- Partitions will be named ``[table_name]_week_[week number] ``.
317354
318- .. code-block :: python
355+ Custom strategy
356+ ~~~~~~~~~~~~~~~
319357
320- from psqlextra.partitioning import (
321- PostgresPartitioningManager,
322- partition_by_current_time,
323- )
358+ You can create a custom partitioning strategy by implementing the :class: `psqlextra.partitioning.PostgresPartitioningStrategy ` interface.
324359
325- # 4 partitions ahead, each partition is 1 week
326- manager = PostgresPartitioningManager(
327- model = MyPartitionedModel,
328- count = 4 ,
329- weeks = 1 ,
330- )
331- manager.apply()
332-
333-
334- # 6 partitions ahead, each partition is 2 weeks
335- manager = PostgresPartitioningManager(
336- model = MyPartitionedModel,
337- count = 6 ,
338- weeks = 2 ,
339- )
340- manager.apply()
360+ You can look at :class: `psqlextra.partitioning.PostgresCurrentTimePartitioningStrategy ` as an example.
341361
342362
343363Switching partitioning strategies
0 commit comments