# Add partition key to updates

**URL:** <https://discourse.hibernate.org/t/add-partition-key-to-updates/6710>\
**Category:** Hibernate ORM\
**Created:** [September 29, 2022, 1:05pm UTC](https://discourse.hibernate.org/t/add-partition-key-to-updates/6710 "2022-09-29T13:05:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Heitor\_Neves](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/heitor_neves/32/2034_2.png) [@Heitor\_Neves](https://discourse.hibernate.org/u/Heitor_Neves)\
**Post date:** [September 29, 2022, 1:05pm UTC](https://discourse.hibernate.org/t/add-partition-key-to-updates/6710/1 "2022-09-29T13:05:23Z")

</div>

Hello,

I have a large table on PostgreSQL which is partitioned by a column that is not the primary key.  
That’s ok for SELECTs as we use native queries, and for INSERTs which include the partition key.

However, UPDATEs and DELETEs are managed and issued by Hibernate, which only adds primary key to the WHERE clause. While this is proper SQL and works, query planning and execution time are huge if we don’t add the partition key for pruning.

Is there anyway to solve this in Hibernate?

I was thinking if there’s a way to customize UPDATEs and DELETEs for partitioned entities or, even better, annotate the partition key column so that Hibernate is aware of that.

Thank you very much.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [September 29, 2022, 3:25pm UTC](https://discourse.hibernate.org/t/add-partition-key-to-updates/6710/2 "2022-09-29T15:25:38Z")

</div>

There is currently no way to do this, but I can see how this can help with query planning performance. I created [Allow marking partition columns · Discussion #5353 · hibernate/hibernate-orm · GitHub](https://github.com/hibernate/hibernate-orm/discussions/5353) where you can chime in.

---

<div class="post-metadata">

**Author:** ![Heitor\_Neves](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/heitor_neves/32/2034_2.png) [@Heitor\_Neves](https://discourse.hibernate.org/u/Heitor_Neves)\
**Post date:** [September 29, 2022, 5:02pm UTC](https://discourse.hibernate.org/t/add-partition-key-to-updates/6710/3 "2022-09-29T17:02:30Z")

</div>

Hey @beikov thanks for the fast reply.

I just upvoted there. Do you think I should move discussion there and close this topic?

Anyway, would it be too much of a hack to intercept queries with `org.hibernate.Interceptor` and try to rewrite the queries, adding the extra condition on the partition key? Is there any safe way to do this? Have you every seen it?

Thanks.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [September 30, 2022, 9:19am UTC](https://discourse.hibernate.org/t/add-partition-key-to-updates/6710/4 "2022-09-30T09:19:02Z")

</div>

If you want to discuss the design of the annotations i.e. add your use cases, you can comment on the GitHub discussion, but for other things we can keep the conversation here.

As for a workaround, you could implement a custom `EntityPersister` by i.e. extending `SingleTableEntityPersister` and specifying the class on your entity with `@Persister(impl = MyPersister.class)`. There you can build your custom update/delete statements by overriding `org.hibernate.persister.entity.AbstractEntityPersister#generateUpdateString(boolean[], int, java.lang.Object[], boolean)` and `org.hibernate.persister.entity.AbstractEntityPersister#generateDeleteString`, as well as override `org.hibernate.persister.entity.AbstractEntityPersister#update(java.lang.Object, java.lang.Object[], java.lang.Object[], java.lang.Object, boolean[], int, java.lang.Object, java.lang.Object, java.lang.String, org.hibernate.engine.spi.SharedSessionContractImplementor)` and `org.hibernate.persister.entity.AbstractEntityPersister#delete(java.lang.Object, java.lang.Object, int, java.lang.Object, java.lang.String, org.hibernate.engine.spi.SharedSessionContractImplementor, java.lang.Object[])` to bind the additional parameters.

---

<div class="post-metadata">

**Author:** ![Heitor\_Neves](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/heitor_neves/32/2034_2.png) [@Heitor\_Neves](https://discourse.hibernate.org/u/Heitor_Neves)\
**Post date:** [September 30, 2022, 12:58pm UTC](https://discourse.hibernate.org/t/add-partition-key-to-updates/6710/5 "2022-09-30T12:58:52Z")

</div>

That’s awesome. It’s a pretty straightforward way and I will surely use it. Thanks a lot.
