Finding the hash value of a row in postgresql

23,858

Solution 1

Cast the row to text and use md5 to make a hash:

SELECT
    md5(CAST((f.*)AS text))
FROM
    foo f;

Solution 2

An alternative approach would be to set an ON INSERT OR UPDATE trigger which would insert the current timestamp into a last_modified column, and then simply querying based on this column when your import process runs.

Share:
23,858
Arun P Johny
Author by

Arun P Johny

LinkedIn

Updated on October 07, 2020

Comments

  • Arun P Johny
    Arun P Johny over 2 years

    Is there a way to get the hash code of a row in postgresql?

    I need to export some data only if there is some changes in the data after the last export, the last exported data rows can be stored in a table, the when the again I need to export the data I can get the hash values of all the data and export only those rows who has a different hash value than the last export.

    Is it possible to achieve using postgresql?

    Thank you

  • Arun P Johny
    Arun P Johny over 12 years
    The problem I'm facing is I'm working on a legacy system, where data can come from a import or through add/edit pages and last modified column on all the data is not dependable. That is the reason I'm checking for some kind of hash technique to check for any changes.
  • matt b
    matt b over 12 years
    With this approach, the database trigger would be determining and writing the last_modified time - not the clients putting data in this table. The clients won't know about this column, and shouldn't - your database would manage it itself.
  • Jelen
    Jelen about 6 years
    OR, shorter SELECT md5(f::text) FROM f
  • john mich
    john mich almost 2 years
    @frank-heikens is there a way to get selective columns and cast them to text? Example- md5(CAST(col_1,col_2,col_3)AS text)
  • datico
    datico over 1 year
    @johnmich well, md5( (col_1 || col_2 || col_3)::text ) works for me under postgres 12.
  • john mich
    john mich over 1 year
    Thanks, datico.
  • cstork
    cstork over 1 year
    @johnmich @datico Careful! This way different columns can produce the same string, e.g. '' || '' || 'a' = 'a' || '' || ''. Better to use md5(row(col_1,col_2,col_2)::text).