Thread: Search for sample pg stored procedure

Search for sample pg stored procedure

From
"Dwayne Miller"
Date:
I'm looking for a stored procedure example to do the following:

Given a table something like

create table audit (
  aid serial,
  amount float,
  balance float,
  parent aid int)

the sp would be used to find the last entry in the table, take the
balance from that entry and add it to the current amount and do an
insert back into the same table. This would of course grab a lock on the
table so that two pending inserts would occur sequentially.  The order
in that case would not be significant as long as the second insert used
the results of the first insert.

I'm really not sure how to do the locking in such an example.

Thanks in advance for any help,
Dwayne