Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6695593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:14:25+00:00 2026-05-26T06:14:25+00:00

Hibernate generates UPDATE statements, which include all columns, regardless of whether I’m changing the

  • 0

Hibernate generates UPDATE statements, which include all columns, regardless of whether I’m changing the value in that columns, eg:

tx.begin();
Item i = em.find(Item.class, 12345);
i.setA("a-value");
tx.commit();

issues this UPDATE statement:

update Item set A = $1, B = $2, C = $3, D = $4 where id = $5

So columns B, C, D are updated, while I didn’t change them.

Say, Items are updated frequently and all columns are indexed.
Does it make sense to optimize the Hibernate part to something like this?

tx.begin();
em.createQuery("update Item i set i.a = :a where i.id = :id")
    .setParameter("a", "a-value")
    .setParameter("id", 12345)
    .executeUpdate();
tx.commit();

What confuses me most is that the EXPLAIN plans of the ‘unoptimized’ and the ‘optimized’ query version are identical!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:14:26+00:00Added an answer on May 26, 2026 at 6:14 am

    Due to PostgreSQL MVCC, an UPDATE is effectively much like a DELETE plus an INSERT. With the notable exception of toasted values – see:

    • Does Postgres rewrite entire row on update?

    (And minor differences for heap only tuples – DELETE + INSERT starts a new HOT chain – but that has no bearing on the case at hand.)

    To be precise, the “deleted” row is just invisible to any transaction starting after the delete has been committed, and vacuumed later. Therefore, on the database side, including index manipulation, there is in effect no difference between the two statements. (Exceptions apply, keep reading.) It increases network traffic a bit (depending on your data) and needs a bit of parsing.

    I studied HOT updates some more after @araqnid’s input and ran some tests. Updates on columns that don’t actually change the value make no difference whatsoever as far as HOT updates are concerned. My answer holds. See details below.

    This also applies to toasted attributes, since those are also not touched unless the values actually change.

    However, if you use per-column triggers (introduced with pg 9.0), this may have undesired side effects!

    I quote the manual on triggers:

    … a command such as UPDATE ... SET x = x ... will fire a trigger on
    column x, even though the column’s value did not change.

    Bold emphasis mine.

    Abstraction layers are for convenience. They are useful for SQL-illiterate developers or if the application needs to be portable between different RDBMS. On the downside, they can butcher performance and introduce additional points of failure. I avoid them wherever possible.

    HOT (Heap-only tuple) updates

    Heap-Only Tuples were introduced with Postgres 8.3, with important improvements in 8.3.4 and 8.4.9.
    The release notes for Postgres 8.3:

    UPDATEs and DELETEs leave dead tuples behind, as do failed INSERTs.
    Previously only VACUUM could reclaim space taken by dead tuples. With
    HOT dead tuple space can be automatically reclaimed at the time of
    INSERT or UPDATE if no changes are made to indexed columns. This
    allows for more consistent performance. Also, HOT avoids adding
    duplicate index entries.

    Emphasis mine. And “no changes” includes cases where columns are updated with the same value as they already hold. I actually tested, as I wasn’t sure.

    Ultimately, the extensive README.HOT in the source code confirms it.

    Toasted columns also don’t stand in the way of HOT updates. The HOT-updated tuple just links to the same, unchanged tuple(s) in the toast fork of the relation. HOT updates even work with toasted values in the target list (actually changed or not). If toasted values are changed, it entails writes to the toast relation fork, obviously. I tested all of that, too.

    Don’t take my word for it, see for yourself. Postgres provides a couple of functions to check statistics. Run your UPDATE with and without all columns and check if it makes any difference.

    -- Number of rows HOT-updated in table:
    SELECT pg_stat_get_tuples_hot_updated('table_name'::regclass::oid)
    
    -- Number of rows HOT-updated in table, in the current transaction:
    SELECT pg_stat_get_xact_tuples_hot_updated('table_name'::regclass::oid)
    

    Or use pgAdmin. Select your table and inspect the “Statistics” tab in the main window.

    Be aware that HOT updates are only possible when there is room for the new tuple version on the same page of the main relation fork. One simple way to force that condition is to test with a small table that holds only a few rows. Page size is typically 8k, so there must be free space on the page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one hibernate sequence, that generates all sequence-numbers in my app. When I
All what Hibernate reverse engineering generates is something like this @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name
Are there any tools that auto-generate the hibernate POJOs by gathering information from the
I wish to pass to Hibernate's SessionFactory hibernate.hbm2ddl.auto=update and see in log file generated
I have a database in which all of the tables have been generated by
In hibernate, we can update entities in DB by many different ways. If we
It's well known fact that hibernate work with indexes in a very strange way.
I'm tracking Hibernate's generated SQL statements and their bound parameters using log4j on a
When hibernate parses out this hql: UPDATE VERSIONED Person SET groupsCount = followingGroup.size WHERE
I have a custom UserType which stores a date/time value in a TIMESTAMP field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.