@DbPartition

This is currently only applicable to Postgres 10.

We can specify common partitioning ranges such as Day, Week, Month and Year. This will then result in appropriate DDL generation and some supporting features to make maintaining partitions easier.

@DbPartition(mode = PartitionMode.MONTH, property = "whenOccurred")
@Entity
public class Event extends Model {
  ...

Postgres DDL

  • The table DDL uses partition by range
  • Ebean supplies helper functions to create partitions
  • When CREATE ALL DLL is executed (ie. testing) Ebean will check for partitions and create some if none exist
create table event (
  id                            bigserial not null,
  name                          varchar(255) not null,
  when_occurred                 timestamptz not null
  ...
) partition by range (when_occurred);

Creating Partitions

Ebean by default supplies helper functions partition and partition_init to make it easier to create partitions going forward. The below statement creates Week partitions on the event table. It will create a unique index on the id column and an index on the when_occurred column.

select partition('week','event','id','when_occurred',3)

The above ensures the current partition and the 3 next partitions are created.

Initial partitions

Generally we will specify explicitly some DDL to create the initial partitions in extra-ddl.xml. The below example uses partition_init to create partitions that have a large initial partition.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<extra-ddl xmlns="http://ebean-orm.github.io/xml/ns/extraddl">

  <ddl-script name="1 initial partitions" >
select partition_init(date '2015-01-01', 'week', 'event', 'id', 'when_occurred', 3);
select partition_init(date '2015-01-01', 'month', 'event_log', 'id', '', 2);
  </ddl-script>

</extra-ddl>

When we don't specify any initial partitions Ebean will detect that there are no partitions and create some for us when it is executing the create all ddl. In the logs we will see messages like the ones below when this occurs.

12:18:47.711 [main] INFO  i.e.dbmigration.DdlGenerator - No table partitions for table event
12:18:47.711 [main] INFO  io.ebean.DDL - Executing initial table partitions - 1 statements
12:18:47.711 [main] DEBUG io.ebean.DDL - executing 1 of 1 select partition('week','event','id','when_occurred',1)

Once we explicitly specify how to create initial partitions in extra-ddl then Ebean will no longer create these partitions automatically.