md5 hash on table row

From: Jon Lapham <lapham(at)jandr(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: md5 hash on table row
Date: 2005-11-02 13:38:46
Message-ID: 4368C166.6080004@jandr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello all,

I would like a generic way to generate an md5 hash for each row of a
table. Currently I do it thusly:

select id, md5(col_a || col_b || col_c || col_d) from mytable;
id | md5
------+----------------------------------
1 | 75acee3133f19d1a81ab2e7c1c32eb29
2 | 496f5e8bc945a922fcdd487e1ddde5c5
3 | ace10f4b1408d179da2e93267b300108
4 | bd029a826a98c21ec4c3661cc34657f8
5 | 4bacd2b0f34213a32f911ed5c1240c09

As you can see, I place each field inside the md5() function call.
This, however, requires that I know what the table fields are, and it is
not very robust (ie: boolean and any other column type that can't use
"||" concatenation fails). Is there a better, robust, or more generic
way to do this? I have tables with columns of type bytea, boolean,
binary, etc... all the difficult column types.

I would love something like this:
select id, md5(*) from mytable;

...which of course does not work.

Thanks for any ideas!
-Jon

--
-**-*-*---*-*---*-*---*-----*-*-----*---*-*---*-----*-----*-*-----*---
Jon Lapham <lapham(at)jandr(dot)org> Rio de Janeiro, Brasil
Personal: http://www.jandr.org/
***-*--*----*-------*------------*--------------------*---------------

Responses

Browse pgsql-general by date

  From Date Subject
Next Message YL 2005-11-02 13:57:39 looking for multi-language app example using postgresql
Previous Message MaXX 2005-11-02 13:04:31 Clustered indexes - When to use them?