hstore and populate_record

From: Jan Eskilsson <janeskil1525(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: hstore and populate_record
Date: 2012-02-17 12:12:41
Message-ID: CAHot5JEqswuXrt__AhKZUbdnk=mNDSzDCcnyEKbOEz=sFgYizA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi All

Background, I have a table that i need to audit changes for
tracability etc. I found an example on one of the postgres lists to
use hstore:s to store the audit data. I find this to be a really good
idea since its really simple to implement and it works really well.

But now comes next step, how to get the data back from the hstore into
a record in the original format.

My two tables looks like this, first the table to be audited,
CREATE TABLE commissionbase
(
commissionbase_pkey integer NOT NULL,
salesorder character varying(20) NOT NULL,
salesmanid character varying(50) NOT NULL,
stockitem character varying(50) NOT NULL,
quantity double precision,
price numeric(15,2),
commission character varying(10),
commissionsum numeric(15,2),
CONSTRAINT commissionbase_pkey PRIMARY KEY (commissionbase_pkey ),
)

And then the audit table,
CREATE TABLE audit.audit_log
(
log_operation text,
log_old_values hstore,
log_new_values hstore,
log_table text,
CONSTRAINT audit_log_pkey PRIMARY KEY (log_id )
)

My problem,
SELECT * FROM public.populate_record(null::commissionbase,
(select log_new_values from
audit.audit_log where log_table = 'commissionbase' LIMIT 1));

This select works really well as long as i limit it to one record,
when i remove the limit restriction it dont work because the subquery
returns more then one record. Is it possible to rewrite to handle
multiple records, my sql knowledge is a bit to limited to figure out
how?

All input is greatly apreciated !

Thank you in advance !

Best Regards
Jan Eskilsson

--
No trees were killed in the creation of this message.
However, many electrons were terribly inconvenienced.

Please consider the environment before you print this email.

(1) The contents of this transmission are privileged and confidential
and intended solely for the use of the addressee. Any disclosure,
distribution or copying of the contents, other than by the addressee,
is strictly prohibited. If you receive this transmission in error,
please notify us immediately and destroy the material received.
(2) All incoming and outgoing emails and any attachments are subjected
to a virus scanner and are believed to be free of any virus, or any
other defect which might affect any computer or IT system into which
they are received and opened. Therefore, it is the responsibility of
the recipient to ensure that they are virus free and no responsibility
is accepted by Jan Eskilsson  for any loss or damage arising in any
way from receipt or use thereof.

Browse pgsql-sql by date

  From Date Subject
Next Message vpapavas 2012-02-18 02:52:42 Re: Window function frame clause
Previous Message John Tuliao 2012-02-17 07:52:13 How can I use this subselect properly in an update?