Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a hash of an entire table in postgresql?

I would like a fairly efficient way to condense an entire table to a hash value.

I have some tools that generate entire data tables, which can then be used to generate further tables, and so on. I'm trying to implement a simplistic build system to coordinate build runs and avoid repeating work. I want to be able to record hashes of the input tables so that I can later check whether they have changed. Building a table takes minutes or hours, so spending several seconds building hashes is acceptable.

A hack I have used is to just pipe the output of pg_dump to md5sum, but that requires transferring the entire table dump over the network to hash it on the local box. Ideally I'd like to produce the hash on the database server.

Finding the hash value of a row in postgresql gives me a way to calculate a hash for a row at a time, which could then be combined somehow.

Any tips would be greatly appreciated.

Edit to post what I ended up with: tinychen's answer didn't work for me directly, because I couldn't use 'plpgsql' apparently. When I implemented the function in SQL instead, it worked, but was very inefficient for large tables. So instead of concatenating all the row hashes and then hashing that, I switched to using a "rolling hash", where the previous hash is concatenated with the text representation of a row and then that is hashed to produce the next hash. This was much better; apparently running md5 on short strings millions of extra times is better than concatenating short strings millions of times.

create function zz_concat(text, text) returns text as      'select md5($1 || $2);' language 'sql';  create aggregate zz_hashagg(text) (     sfunc = zz_concat,     stype = text,     initcond = ''); 
like image 501
Ben Avatar asked Oct 26 '10 01:10

Ben


People also ask

What is hash table in PostgreSQL?

Hash Index. Just like the name suggests, Hash indexes in PostgreSQL use a form of the hash table data structure. Hash table is a common data structure in many programming languages. For example, a Dict in Python, a HashMap in Java or the new Map type in JavaScript.

How do I hash a column in PostgreSQL?

Use a trigger to set the hash column on insert and update. For SHA-256, use the pgcrypto extension module's digest function. Since you haven't specified your PostgreSQL version I'll assume you're using the current 9.2 in the following examples. Note that the CREATE EXTENSION function must be run as a superuser.

How do I fetch a table in PostgreSQL?

If you want to select data from all the columns of the table, you can use an asterisk ( * ) shorthand instead of specifying all the column names. The select list may also contain expressions or literal values. Second, specify the name of the table from which you want to query data after the FROM keyword.


1 Answers

I know this is old question, however this is my solution:

SELECT             md5(CAST((array_agg(f.* order by id))AS text)) /* id is a primary key of table (to avoid random sorting) */ FROM     foo f;  
like image 70
Tomas Greif Avatar answered Sep 29 '22 23:09

Tomas Greif