Search code examples
postgresqlhashchecksum

How to generate a hash of the result set in Postgress?


I have two databases for logging stuff, which I want to check for synchronization.

The solution approved is to send periodically (lets say hourly) a select to both, generate a hash of the result set and compare them. If they match then great, otherwise generate some alarms.

Currently I'm doing it by (bash script):

 log_table="SELECT column1, column2, column3 FROM log_table where to_char(timestamp, '$ts_format') = '$tx_moment'";
PSQL="psql -t -q -h $_gp_host -U $_gp_user -d log_schema -c ";
echo "`${PSQL} $tx_fix${log_table} | sort | cksum`";

I would like to do the cksum/hash on the postgres side. Because currently it downloads the whole result set (which can have even 25MB or more) and generates the checksum on the server side.

Google didn't help.

Any suggestions?

Thanks.


Solution

  • You could use md5:

     log_table="
    SELECT 
      md5(column1 || column2 || column3) AS hash,
      column1, column2, column3
    FROM log_table where to_char(timestamp, '$ts_format') = '$tx_moment'";