Unix Technical Forum

Value specific sequences?

This is a discussion on Value specific sequences? within the pgsql Sql forums, part of the PostgreSQL category; --> Hello everyone, I have a table of entities, each entity has a parent_id, I'd like to have an insert ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 11:52 AM
Dmitri Bichko
 
Posts: n/a
Default Value specific sequences?

Hello everyone,

I have a table of entities, each entity has a parent_id, I'd like to
have an insert trigger that assigns to that entity a sequential number
which gets incremented per parent_id.

i.e. doing:
INSERT INTO foo(id, parent_id) VALUES('a',1);
INSERT INTO foo(id, parent_id) VALUES('b',1);
INSERT INTO foo(id, parent_id) VALUES('c',2);

Should result in 'foo' containing:

id parent_id parent_index
'a' 1 0
'b' 1 1
'c' 2 0

The two ways that come to mind are quering 'foo' on every insert for the
largest index for that parent_id (which seems slow) and using a separate
table of counters (which seems breaky).

Performance wise: the number of rows in 'foo' is not limited (ie will
grow continuously over the life of the app), but each parent_id will
usually only have about 10 foo's associated with it. To make things
simple, nothing can be deleted from foo, and once inserted the relevant
values cannot change.

Is there an elegant way to do this?

Your help greatly appreciated,
Dmitri

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:13 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com