Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support partitions #55

Open
vensko opened this issue Apr 17, 2023 · 3 comments
Open

Support partitions #55

vensko opened this issue Apr 17, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@vensko
Copy link

vensko commented Apr 17, 2023

I recently found a great use for Postgres partitions beyond huge action logs. They appear to be a great way to store soft deleted records using the LIST partition method. I mark soft deleted records either with booleans, or enum statuses, and Postgres does scan only the main table, skipping the table with deleted records when it's not needed.

As a result, I'm making raw schemas in Laravel migrations. It would be great to have partition support in your library.

@tpetry
Copy link
Owner

tpetry commented Apr 17, 2023

Can you share an example of your raw schema commands?

@vensko
Copy link
Author

vensko commented Apr 17, 2023

Simplified example from my current project:

        DB::unprepared('
            CREATE TABLE files (
                id bigserial NOT NULL,
                user_id int8 NOT NULL,
                file_status int2 NOT NULL DEFAULT \'2\'::smallint,
                file_name varchar(255) NOT NULL
            )
            PARTITION BY LIST (file_status);
        ');

        DB::unprepared('CREATE TABLE files_published PARTITION OF files DEFAULT WITH (
            fillfactor=90,
            autovacuum_analyze_scale_factor=0.02
        )');

        DB::unprepared('CREATE TABLE files_deleted PARTITION OF files FOR VALUES IN (0)');
enum FileStatus: int
{
    case DELETED = 0;
    case QUARANTINED = 1;
    case PUBLISHED = 2;
}

All soft-deleted files get moved to files_deleted automatically, and files_deleted is not scanned in most scenarios at all, Postgres skips it just fine when I query records with file_status > 0.

PS Most people would use partition by range, since it's a better documented use case.

@vensko
Copy link
Author

vensko commented Apr 17, 2023

Another use case - model versioning. Old versions can be automatically stored in a separate partition.

@tpetry tpetry added the enhancement New feature or request label Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants