Skip site navigation (1) Skip section navigation (2)

Re: History Tables Vs History Field

From: Paul BREEN <pbreen(at)computerpark(dot)co(dot)uk>
To: "Bart McFarling" <pbm1(at)midsouth(dot)rr(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: History Tables Vs History Field
Date: 2004-03-11 16:11:28
Message-ID: 44777. (view raw, whole thread or download thread mbox)
Lists: pgsql-admin
> I have a table that will recieve about 2000 inserts per day,  Although
> it will technically never be dead data, about 99.999% of it will be
> uninteresing after 30-40 days,  My problem is that I dont know if I
> should create a new table that is a history table or add a indexed field
> and ignore the data in queries unless someone asks for it. The latter is
> my prefered way of dealing with it, Is there something Im missing? Is
> there another way to do this?
> Any Suggestions are appreciated.

Hello Bart,

We normally do this sort of thing by using a history table.  There's no
strong reason why we do it this way as such, except that it just seems a
logical separation.

Advantages are that the data (in the history table) is easier to
manipulate (e.g., archive) without disturbing users who are looking at the
current 'live' data.  Also, it's simple to query both live data & old
history data by using a union should we need to.

Normally, we setup a db rule that on deletion of a record from the live
table, copies it into the history table.  In this way, we don't have to
manage it at the application level.  In the app., we simply delete the
record when we're done with it & the db takes care of copying it into the
history table for us.  We use a rule like this:

-- This is an SQL script to define a rule for deletions on buffer_pallets.
-- It automatically copies the records in the buffer_pallets table into the
-- pallet_history table

\connect - postgres

drop rule del_buffer_pallets;

create rule del_buffer_pallets
as on delete to buffer_pallets
do insert into pallet_history
select * from buffer_pallets
where pallet_urn = OLD.pallet_urn;

It's simple & clean.  You can setup a cron job to clear out data older
than n days etc.

Hope this helps.

Deep Joy - Paul

Paul M. Breen, Software Engineer - Computer Park Ltd.

Tel:   (01536) 417155
Email: pbreen(at)computerpark(dot)co(dot)uk


This private and confidential e-mail has been sent to you 
by Computer Park Ltd. 

If you are not the intended recipient of this e-mail and 
have received it in error, please notify us via the email 
address or telephone number below, and then delete it from 
your mailbox.

Email: mailbox(at)computerpark(dot)co(dot)uk

Tel: +44 (0) 1536 417155
Fax: +44 (0) 1536 417566

Head Office:
Computer Park Ltd, Broughton Grange, Headlands, Kettering
Northamptonshire NN15 6XA

Registered in England: 3022961. 

Registered Office:
6 North Street, Oundle, Peterborough PE8 4AL 


In response to

pgsql-admin by date

Next:From: Nick Fankhauser - DoxpopDate: 2004-03-11 20:53:23
Subject: using disable-triggers in pg_dump
Previous:From: Tom LaneDate: 2004-03-11 15:54:41
Subject: Re: Multiple postmasters for one data directory...

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group