Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Spring-JPA work with Postgres partitioning?

We have a Spring Boot project that uses Spring-JPA for data access. We have a couple of tables where we create/update rows once (or a few times, all within minutes). We don't update rows that are older than a day. These tables (like audit table) can get very large and we want to use Postgres' table partitioning features to help break up the data by month. So the main table always has this calendar month's data but if the query requires retrieval from previous months it would somehow read it from other partitions.

Two questions:

1) Is this a good idea for archiving older data but still leave it query-able? 2) Does Spring-JPA work with partitioned tables? Or do we have to figure out how to break up the query and do native queries and concatenate the restult set?

Thanks.

like image 617
pastafarian Avatar asked May 31 '16 18:05

pastafarian


People also ask

Does PostgreSQL support partitioning?

PostgreSQL supports basic table partitioning. This section describes why and how to implement partitioning as part of your database design.

What is the difference between spring JPA and Spring data JPA?

There is always confusion between JPA and Spring Data JPA.Spring Data JPA is an abstraction that makes it easier to work with a JPA provider like Hibernate which is used by default. Specifically, Spring Data JPA provides a set of interfaces for easily creating data access repositories.

How does Postgres partition work?

Partitioning helps to scale PostgreSQL by splitting large logical tables into smaller physical tables that can be stored on different storage media based on uses. Users can take better advantage of scaling by using declarative partitioning along with foreign tables using postgres_fdw.


1 Answers

I am working with postgres partitioning with Hibernate & Spring JPA for a period of time. So I think, I can try to answer your questions.

1) Is this a good idea for archiving older data but still leave it query-able?

If you are applying indexes and not re-indexing table frequently, then partitioning of data may result faster query results.

Also you can use clustered index feature in postgres as well to fetch the data faster.

Because table with older data will not going to be updated, so clustered index will improve the performance efficiently.

2) Does Spring-JPA work with partitioned tables? Or do we have to figure out how to break up the query and do native queries and concatenate the restult set?

Spring JPA will work out of the box with partitioned table. It will retrieve the data from master as well as child tables and returns the concatenated result set.

Note : Issue with partitioned table

The only issue you will face with partitioned table is insertion in partitioned table.

Let me explain, when you partition a table, you will create a trigger over master table, and that trigger will return null. This is the key behind insertion issue in partitioned table using Spring JPA / Hibernate.

When you try to insert a row using Spring JPA or Hibernate you will face below issue

Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

To overcome this issue you need to override implementation of Batching batcher.

In hibernate you can provide the custom implementation of batcher factory using below configuration

hibernate.jdbc.factory_class=path.to.my.batcher.factory.implementation

In spring JPA you can achieve the same by custom implementation of batch builder using below configuration

hibernate.jdbc.batch.builder=path.to.my.batch.builder.implementation

References :

  • Custom Batch Builder/Batch in Spring-JPA
  • Demo Application
like image 187
Anil Agrawal Avatar answered Sep 22 '22 09:09

Anil Agrawal