PHPackages                             brokenice/laravel-pgsql-partition - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. brokenice/laravel-pgsql-partition

ActiveLibrary[Database &amp; ORM](/categories/database)

brokenice/laravel-pgsql-partition
=================================

PostgreSQL Partition extension for Laravel.

v1.0.1(1mo ago)07MITPHPPHP &gt;=8.1

Since Feb 4Pushed 1mo agoCompare

[ Source](https://github.com/lucabecchetti/laravel-postgresql-partition)[ Packagist](https://packagist.org/packages/brokenice/laravel-pgsql-partition)[ RSS](/packages/brokenice-laravel-pgsql-partition/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (6)Versions (6)Used By (0)

Laravel PostgreSQL Partition
============================

[](#laravel-postgresql-partition)

**Laravel-pgsql-partition** is a useful Laravel package to easily work with [PostgreSQL Table Partitioning](https://www.postgresql.org/docs/current/ddl-partitioning.html). Partitioning requires PostgreSQL version &gt;= 10.0.

Installation
------------

[](#installation)

Add the package using composer:

```
$ composer require brokenice/laravel-pgsql-partition
```

For Laravel versions before 5.5 or if not using auto-discovery, register the service provider in `config/app.php`:

```
'providers' => [
  /*
   * Package Service Providers...
   */
  Brokenice\LaravelPgsqlPartition\PartitionServiceProvider::class,
],
```

Quickstart
----------

[](#quickstart)

### Create a partitioned table migration

[](#create-a-partitioned-table-migration)

From the command line:

```
php artisan make:migration create_partitioned_orders_table
```

Then edit the migration you just created:

```
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Brokenice\LaravelPgsqlPartition\Models\Partition;
use Brokenice\LaravelPgsqlPartition\Schema\Schema;

class CreatePartitionedOrdersTable extends Migration
{
    public function up()
    {
        // Create the partitioned table
        DB::statement('
            CREATE TABLE orders (
                id BIGSERIAL,
                customer_id BIGINT NOT NULL,
                order_date DATE NOT NULL,
                total DECIMAL(10,2),
                created_at TIMESTAMP,
                updated_at TIMESTAMP,
                PRIMARY KEY (id, order_date)
            ) PARTITION BY RANGE (order_date)
        ');

        // Add partitions for years
        Schema::partitionByYears('orders', 'order_date', 2022, 2025);
    }

    public function down()
    {
        Schema::drop('orders');
    }
}
```

Run the migration:

```
php artisan migrate
```

Partition Types Support
-----------------------

[](#partition-types-support)

PostgreSQL supports these partitioning methods:

TypeDescriptionRANGEPartition based on a range of valuesLISTPartition based on a list of discrete valuesHASHPartition based on hash of the partition keyUsage Examples
--------------

[](#usage-examples)

### Partition by RANGE

[](#partition-by-range)

This type of partitioning assigns rows to partitions based on column values falling within a given range.

```
use Brokenice\LaravelPgsqlPartition\Models\Partition;
use Brokenice\LaravelPgsqlPartition\Schema\Schema;

// Add individual range partitions
Schema::addRangePartition('orders', 'orders_2024', '2024-01-01', '2025-01-01');
Schema::addRangePartition('orders', 'orders_2025', '2025-01-01', '2026-01-01');

// Or create multiple partitions at once using Partition objects
$partitions = [
    Partition::range('orders_2024', '2024-01-01', '2025-01-01'),
    Partition::range('orders_2025', '2025-01-01', '2026-01-01'),
];
Schema::partitionByRange('orders', 'order_date', $partitions);

// Add a default partition for future values
Schema::addDefaultPartition('orders', 'orders_default');
```

### Partition by LIST

[](#partition-by-list)

Similar to partitioning by RANGE, except that the partition is selected based on columns matching one of a set of discrete values.

```
// Create LIST partitions
Schema::addListPartition('users', 'users_europe', ['IT', 'FR', 'DE', 'ES']);
Schema::addListPartition('users', 'users_america', ['US', 'CA', 'MX', 'BR']);

// Or using Partition objects
$partitions = [
    Partition::list('server_east', [1, 43, 65, 12, 56, 73]),
    Partition::list('server_west', [534, 6422, 196, 956, 22]),
];
Schema::partitionByList('servers', 'region_id', $partitions);
```

### Partition by HASH

[](#partition-by-hash)

With this type of partitioning, a partition is selected based on the hash of the partition key.

```
// Create 4 hash partitions
Schema::partitionByHash('logs', 'user_id', 4);

// This creates:
// logs_p0 - FOR VALUES WITH (MODULUS 4, REMAINDER 0)
// logs_p1 - FOR VALUES WITH (MODULUS 4, REMAINDER 1)
// logs_p2 - FOR VALUES WITH (MODULUS 4, REMAINDER 2)
// logs_p3 - FOR VALUES WITH (MODULUS 4, REMAINDER 3)
```

### Partition by YEARS

[](#partition-by-years)

Convenience method to partition a table by year ranges:

```
// Create yearly partitions from 2020 to 2025
Schema::partitionByYears('events', 'event_date', 2020, 2025);

// Omit end year to use current year
Schema::partitionByYears('events', 'event_date', 2020);
```

### Partition by YEARS AND MONTHS

[](#partition-by-years-and-months)

Create partitions for each month within a year range:

```
// Create monthly partitions for 2024
Schema::partitionByYearsAndMonths('logs', 'created_at', 2024);

// Create monthly partitions from 2023 to 2024
Schema::partitionByYearsAndMonths('logs', 'created_at', 2023, 2024);
```

Partition Maintenance
---------------------

[](#partition-maintenance)

### Detach a Partition

[](#detach-a-partition)

Detaching a partition keeps the data but removes it from the partitioned table:

```
Schema::detachPartition('orders', 'orders_2022');
// orders_2022 is now a standalone table
```

### Attach a Partition

[](#attach-a-partition)

Attach an existing table as a partition:

```
$partitionDef = Partition::range('orders_2022', '2022-01-01', '2023-01-01');
Schema::attachPartition('orders', 'orders_2022', $partitionDef);
```

### Drop a Partition

[](#drop-a-partition)

Permanently delete a partition and its data:

```
Schema::dropPartition('orders_2022');
```

### Truncate a Partition

[](#truncate-a-partition)

Remove all data from a partition:

```
Schema::truncatePartition('orders_2022');

// Or multiple partitions
Schema::truncatePartitions(['orders_2022', 'orders_2023']);
```

### Vacuum (Optimize)

[](#vacuum-optimize)

Reclaim storage and update statistics:

```
Schema::vacuumPartition('orders_2024');
Schema::vacuumPartition('orders_2024', true); // VACUUM FULL
```

### Analyze

[](#analyze)

Update query planner statistics:

```
Schema::analyzePartition('orders_2024');
Schema::analyzePartitions(['orders_2024', 'orders_2025']);
```

### Reindex

[](#reindex)

Rebuild indexes on a partition:

```
Schema::reindexPartition('orders_2024');
```

Querying Partitions with Eloquent
---------------------------------

[](#querying-partitions-with-eloquent)

### Create a Model

[](#create-a-model)

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $table = 'orders';
}
```

### Query a Specific Partition

[](#query-a-specific-partition)

In PostgreSQL, partitions are separate tables, so you can query them directly:

```
// Query the orders_2024 partition directly
Order::partition('orders_2024')->where('total', '>', 100)->get();
```

### Query Multiple Partitions

[](#query-multiple-partitions)

```
// Query multiple partitions
Order::partitions(['orders_2024', 'orders_2025'])->get();
```

### Using MultipleSchemaModel

[](#using-multipleschemamodel)

For multi-schema support:

```
namespace App\Models;

use Brokenice\LaravelPgsqlPartition\Models\MultipleSchemaModel;

class Order extends MultipleSchemaModel
{
    protected $table = 'orders';
}

// Query with schema
$order = new Order();
$order->setSchema('sales');
$order->save();

// Or save directly
$order->saveOnSchema('sales');
```

Artisan Commands
----------------

[](#artisan-commands)

This package provides a set of artisan commands for partition management:

```
php artisan laravel-pgsql-partition {action} [options]
```

### Available Actions

[](#available-actions)

ActionDescription`list`List all partitions for a table`create`Create partitions on an existing partitioned table`detach`Detach a partition (keeps data)`attach`Attach a table as a partition`drop`Drop a partition (deletes data)`truncate`Truncate partition data`vacuum`Run VACUUM on partitions`analyze`Run ANALYZE on partitions`reindex`Run REINDEX on partitions### Examples

[](#examples)

```
# List partitions
php artisan laravel-pgsql-partition list --table=orders

# Create partitions by year
php artisan laravel-pgsql-partition create --table=orders --column=order_date --method=YEAR

# Create hash partitions
php artisan laravel-pgsql-partition create --table=logs --method=HASH --number=8

# Detach a partition
php artisan laravel-pgsql-partition detach --table=orders --partitions=orders_2022

# Truncate partitions
php artisan laravel-pgsql-partition truncate --partitions=orders_2022,orders_2023

# Vacuum with FULL option
php artisan laravel-pgsql-partition vacuum --partitions=orders_2024 --full

# Analyze partitions
php artisan laravel-pgsql-partition analyze --partitions=orders_2024,orders_2025

# Reindex partitions
php artisan laravel-pgsql-partition reindex --partitions=orders_2024
```

### Options

[](#options)

OptionDescription`--schema`PostgreSQL schema (default: public)`--table`Parent table name`--method`Partition method: RANGE, LIST, HASH, YEAR, MONTH, YEAR\_MONTH`--column`Column to partition by`--number`Number of partitions (for HASH)`--partitions`Partition names (comma-separated)`--excludeDefault`Don't create a default partition`--from`Start value for RANGE partition`--to`End value for RANGE partition`--full`Use VACUUM FULLPostgreSQL vs MySQL Partitioning
--------------------------------

[](#postgresql-vs-mysql-partitioning)

Key differences from the MySQL version of this package:

FeatureMySQLPostgreSQLPartition creationALTER TABLE on existingAt CREATE TABLE or separate CREATE TABLE ... PARTITION OFFuture valuesMAXVALUEDEFAULT partitionSubpartitionsNative supportPartition of partitionKEY partitioningSupportedUse HASH insteadMaintenanceOPTIMIZE, REPAIR, REBUILDVACUUM, ANALYZE, REINDEXImportant Notes
---------------

[](#important-notes)

1. **PostgreSQL 10+ Required**: Native declarative partitioning requires PostgreSQL 10 or higher.
2. **Primary Keys**: In PostgreSQL, the partition key must be included in any primary key or unique constraint.
3. **Auto-increment**: Use `BIGSERIAL` for auto-increment columns, but include the partition key in the primary key:

    ```
    PRIMARY KEY (id, order_date)
    ```
4. **Indexes**: Each partition maintains its own indexes. Create indexes on the parent table and they'll be automatically created on partitions.
5. **Default Partition**: Always consider adding a default partition to catch rows that don't match any other partition.

Tests
-----

[](#tests)

```
$ composer test
# or
$ composer test:unit
```

For integration tests (requires PostgreSQL):

```
$ composer test:integration
```

Contributing
------------

[](#contributing)

Recommendations and pull requests are most welcome! Pull requests with tests are the best!

Credits &amp; License
---------------------

[](#credits--license)

laravel-pgsql-partition is owned and maintained by [Luca Becchetti](http://www.lucabecchetti.com)

As open source creation any help is welcome!

The code of this library is licensed under MIT License; you can use it in commercial products without any limitation.

The only requirement is to add a line in your Credits/About section with the text below:

```
Partition by laravel-pgsql-partition - http://www.lucabecchetti.com
Created by Becchetti Luca and licensed under MIT License.

```

###  Health Score

40

—

FairBetter than 87% of packages

Maintenance96

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

4

Last Release

51d ago

Major Versions

v0.1.0 → v1.0.02026-03-18

0.1.1 → v1.0.12026-03-19

### Community

Maintainers

![](https://www.gravatar.com/avatar/1c2e1529784c4c9651d08562582a2b702816caa9d6c942d457f52a30f958067b?d=identicon)[lucabecchetti](/maintainers/lucabecchetti)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/brokenice-laravel-pgsql-partition/health.svg)

```
[![Health](https://phpackages.com/badges/brokenice-laravel-pgsql-partition/health.svg)](https://phpackages.com/packages/brokenice-laravel-pgsql-partition)
```

###  Alternatives

[owen-it/laravel-auditing

Audit changes of your Eloquent models in Laravel

3.4k33.0M95](/packages/owen-it-laravel-auditing)[staudenmeir/eloquent-json-relations

Laravel Eloquent relationships with JSON keys

1.1k5.8M24](/packages/staudenmeir-eloquent-json-relations)[bavix/laravel-wallet

It's easy to work with a virtual wallet.

1.3k1.1M11](/packages/bavix-laravel-wallet)[dragon-code/migrate-db

Easy data transfer from one database to another

15717.4k](/packages/dragon-code-migrate-db)[gearbox-solutions/eloquent-filemaker

A package for getting FileMaker records as Eloquent models in Laravel

6454.8k2](/packages/gearbox-solutions-eloquent-filemaker)[cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

9126.6k3](/packages/cybercog-laravel-ownership)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
