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.
Comments
-
Arun P Johny over 2 yearsIs 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 over 12 yearsThe 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 over 12 yearsWith this approach, the database trigger would be determining and writing thelast_modifiedtime - 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 about 6 yearsOR, shorterSELECT md5(f::text) FROM f -
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 over 1 year@johnmich well, md5( (col_1 || col_2 || col_3)::text ) works for me under postgres 12. -
john mich over 1 yearThanks, datico. -
cstork over 1 year@johnmich @datico Careful! This way different columns can produce the same string, e.g.'' || '' || 'a' = 'a' || '' || ''. Better to usemd5(row(col_1,col_2,col_2)::text).