Re: md5 checksum of a given/previous row

From: Iaam Onkara <iamonkara(at)gmail(dot)com>
To: Niranjan <niranjan81(at)gmail(dot)com>
Cc: pgsql-in-general(at)postgresql(dot)org
Subject: Re: md5 checksum of a given/previous row
Date: 2017-11-12 22:45:28
Message-ID: CAMz9UCb8n9DCzf4P2ZMOziJzxrCqo3Hprdhgg+XKiRfpdwNmhw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-in-general

Here are the different options that I have tried using lag and md5 functions

http://www.sqlfiddle.com/#!17/69843/2

CREATE TABLE test
("id" uuid DEFAULT uuid_generate_v4() NOT NULL,
"value" decimal(5,2) NOT NULL,
"delta" decimal(5,2),
"created_at" timestamp default current_timestamp,
"words" text,
CONSTRAINT pid PRIMARY KEY (id)
)
;

INSERT INTO test
(value, words)
VALUES
(51.0, 'A'),
(52.0, 'B'),
(54.0, 'C'),
(57.0, 'D')
;

select
created_at, value,
value - lag(value, 1, 0.0) over(order by created_at) as delta,
md5(lag(words,1,words) over(order by created_at)) as the_word,
md5(textin(record_out(test))) as Hash
FROM test
ORDER BY created_at;

Still unable to figure out how to read the previous record as whole.

Thanks,
Onkara

On Sun, Nov 12, 2017 at 1:55 PM, Iaam Onkara <iamonkara(at)gmail(dot)com> wrote:

> I have tried the *lag* function but that works one only one column which
> means I can get md5 checksum for one column.
>
> But how do I use *lag* or something like lag to get the whole of the
> previous row?
>
> Thanks
> Onkara
>
> On Nov 12, 2017 1:53 PM, iamonkara(at)gmail(dot)com wrote:
>
>> I have tried the lag function but that works one only one column which
>> means I can get md5 checksum for one column.
>>
>> But how do I use lag or something like lag to get the whole of the
>> previous row.
>>
>> On Nov 12, 2017 1:22 PM, "Niranjan" <niranjan81(at)gmail(dot)com> wrote:
>>
>> What all combinations have you tried?
>>
>>
>> On 12-Nov-2017 23:56, "Iaam Onkara" <iamonkara(at)gmail(dot)com> wrote:
>>
>> Hi,
>>
>> I have a requirement to create an tamper proof chain of records for audit
>> purposes. The pseudo code is as follows
>>
>> before_insert:
>> 1. compute checksum of previous row (or conditionally selected row)
>> 2. insert the computed checksum in the current row
>>
>> For now lets assume we are using md5 checksum, I can easily get the md5
>> of current row using
>>
>> select md5(textin(record_out(test))) as md5_checksum FROM test;
>>
>> but how do I compute md5 of previous row or of a row selected by where
>> column=val;?
>>
>> Thanks.
>> Onkara
>>
>>
>>
>>

In response to

Responses

Browse pgsql-in-general by date

  From Date Subject
Next Message Niranjan 2017-11-12 22:53:47 Re: md5 checksum of a given/previous row
Previous Message Iaam Onkara 2017-11-12 19:55:22 Re: md5 checksum of a given/previous row