Re: serialization errors - Mailing list pgsql-general

From Tom Lane
Subject Re: serialization errors
Date
Msg-id 19954.1043819950@sss.pgh.pa.us
Whole thread Raw
In response to Re: serialization errors  (Ryan VanderBijl <rvbijl@vanderbijlfamily.com>)
Responses Re: serialization errors
Re: serialization errors
List pgsql-general
Ryan VanderBijl <rvbijl@vanderbijlfamily.com> writes:
> For adding a new node, I basically do this:
>    insert into tree_node(parent_id, node_order)
>    values(1, (select max(node_order)+1 from tree_node where parent_id = 1) );

That "select max()+1" makes me itch ;-) ... that's as perfect a recipe
for concurrency problems as I can imagine.

At first glance it seems that all this is doing is assigning sequential
node_order values to the children of any particular parent.  Is it
really necessary that those node_order values be consecutive?  If they
only need to be strictly increasing, you could use a sequence to
generate them.  (Actually, I think you could dispense with node_order
entirely, and use the node_id field instead.)

In any case, I'd suggest some careful thought about what your data
structure really is, and how you could avoid creating serialization
bottlenecks like this one.

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: What happens when you run out of transaction ID's ???
Next
From: Dennis Gearon
Date:
Subject: crypt