Skip to content

Option to specify monthly partition suffix #197

Closed

Description

@mneirynck

Right now the monthly partitions are named:
partitions will be named [table_name]_[year]_[3-letter month name].

Which doesn't make sense for use, we would prefer to have the month number instead.
Maybe a toggle to specify which format you want to use might make sense?

Activity

  1. ckrybus commented on Jan 27, 2023

    @ckrybus
    Contributor

    I'm also interested in this. Right now I see no "easy" way to customize the naming.

    I needed to change the daily partitioning naming from "%Y_%b_%d" to "%Y_%m_%d".

    Currently I'm using this as a "workaround" (basically just copy and paste and make the necessary change):

    settings.py

    PSQLEXTRA_PARTITIONING_MANAGER = 'myapp.core.partitioning.manager'

    myapp/core/partitioning.py

    from psqlextra.partitioning import PostgresPartitioningManager, PostgresTimePartitionSize
    from psqlextra.partitioning.config import PostgresPartitioningConfig
    
    from myapp.core.models import MyModel
    from myapp.core.partitioning_custom_current_time_strategy import \
        CustomPostgresCurrentTimePartitioningStrategy
    
    manager = PostgresPartitioningManager([
        PostgresPartitioningConfig(
            model=MyModel,
            strategy=CustomPostgresCurrentTimePartitioningStrategy(
                size=PostgresTimePartitionSize(days=1),
                count=7,
            ),
        ),
    ])

    myapp/core/partitioning_custom_current_time_strategy.py

    from typing import Generator
    
    from psqlextra.partitioning.current_time_strategy import PostgresCurrentTimePartitioningStrategy
    from psqlextra.partitioning.time_partition import PostgresTimePartition
    from psqlextra.partitioning.time_partition_size import PostgresTimePartitionUnit
    
    
    class CustomPostgresTimePartition(PostgresTimePartition):
        _unit_name_format = {
            PostgresTimePartitionUnit.YEARS: "%Y",
            PostgresTimePartitionUnit.MONTHS: "%Y_%b",
            PostgresTimePartitionUnit.WEEKS: "%Y_week_%W",
            PostgresTimePartitionUnit.DAYS: "%Y_%m_%d",  # --> HERE IS THE CUSTOM CHANGE
        }
    
    
    class CustomPostgresCurrentTimePartitioningStrategy(
        PostgresCurrentTimePartitioningStrategy
    ):
        def to_create(self) -> Generator[CustomPostgresTimePartition, None, None]:
            current_datetime = self.size.start(self.get_start_datetime())
    
            for _ in range(self.count):
                yield CustomPostgresTimePartition(
                    start_datetime=current_datetime, size=self.size
                )
    
                current_datetime += self.size.as_delta()
    
        def to_delete(self) -> Generator[CustomPostgresTimePartition, None, None]:
            if not self.max_age:
                return
    
            current_datetime = self.size.start(
                self.get_start_datetime() - self.max_age
            )
    
            while True:
                yield CustomPostgresTimePartition(
                    start_datetime=current_datetime, size=self.size
                )
    
                current_datetime -= self.size.as_delta()
  2. added
    table-partitioning
    on Feb 3, 2023
  3. Photonios commented on Feb 3, 2023

    @Photonios
    Contributor

    I am happy to accept a PR that adds parameters to PostgresCurrentTimePartitioningStrategy and PostgresTimePartition to customize the format.

  4. Photonios commented on Mar 25, 2023

    @Photonios
    Contributor

    Closed by #200. Thanks @ckrybus!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementtable-partitioning

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions