I would like to create a sql query (or plpgsql) that will md5() all given rows regardless of type. However, below, if one is null then the hash is null:
UPDATE t
There is much more elegant solution for this.
In Postgres, using table name in SELECT
is permitted and it has type ROW
. If you cast this to type TEXT
, it gives all columns concatenated together in string that is actually JSON.
Having this, you can get md5
of all columns as follows:
SELECT md5(mytable::TEXT)
FROM mytable
If you want to only use some columns, use ROW
constructor and cast it to TEXT
:
SELECT md5(ROW(col1, col2, col3)::TEXT)
FROM mytable
Another nice property about this solution is that md5
will be different for NULL
vs. empty string.
Obligatory SQLFiddle.