I am new to postgresql and I need help on strategy on when a large processing is being executed.
I have table which holds invoices, The records in this invoice table need to be 'posted' to multiple tables say sales table and also income table. This invoice table will be access by multiple users at the same time and when a user 'post' a particular record in the invoice table I want to prevent other users from making changes and 'posting' until the 'posting' is finished by the first user. How should i do it properly? Should I wrap the 'posting' in a transaction?
Thanks
Thanks Stepel and Landa for replying and sorry for not being clear.
Here's the coding that I am currently using in Foxpro.
Select InvHeader
If !Rlock()
Return
EndIf
Select InvDetail
Scan
do processing and verification
..
Insert Into tAr ... &&& temporary AR table
Insert Into tGl ... &&& temporary GL table
EndScan
*** the reason I am using temporary table is that at this stage, the use may
print out the detail of invoices that are to be posted and can then decide
whether to proceed to commit the transaction.
Select InvHeader
Replace InvHeader.Posted With .T.
Select tAr
Scan
do processing ...
Insert Into Ar (....
EndScan
Select tGl
Scan
do processing ...
Insert Into GL ( ...
EndScan
Begin Transaction
Select Ar
If !TableUpdate()
Rollback
Return
EndIf
Select Gl
If !TableUpdate()
Rollback
Return
EndIf
Select InvHeader
If !TableUpdate()
Rollback
Return
EndIf
** everything is ok the commit
End Transaction
Return
Should I do this posting procedure in the server using plpgsql? Or a combination of Foxpro code and SqlExec() statements? Are there any better ways to accomplish this?
Thanks.
Yes, you should use transactions. In this case the most sensitive thing is id of invoice header. If you use
INSERT INTO invoice_header_table VALUES (..) RETURNING id;
you have unique id of invoice header.
I am not sure if it was your point actually but I hope this information will be usefull.