pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Strange locking behaviour
2+ messages / 2 participants
[nested] [flat]

* Strange locking behaviour
@ 2002-02-22 19:10  Brian McCane <bmccane@mccons.net>
  0 siblings, 1 reply; 2+ messages in thread

From: Brian McCane @ 2002-02-22 19:10 UTC (permalink / raw)
  To: pgsql-admin

I am running 7.2 on FreeBSD-current.  I have an web page that I have been
playing with for tracking stocks online.  It has a table which contains
all of my calculations.  Once a day I pull down a report from MarketWatch
of all stocks that meet a criterion I have defined.  Once every 5 minutes
I download new stock data, and compute buy/sell recommendations. Anyway,
that is all working.  Now for my problem:

I download my list from MarketWatch and then go and pull historical
data for all the new stocks in the list.  The program that gets the
historical data does:

BEGIN WORK ;
SELECT stockid FROM stocks WHERE ticker = 'M$FT' FOR UPDATE ;
UPDATE stocks SET nextupdate = NOW + INTERVAL '1 DAY' ;
....Insert 7 years of stock data....
UPDATE stocks SET nextupdate = NOW() ;
COMMIT WORK ;

The program that gets the 5 minute updates does:

SELECT stockid FROM stocks WHERE nextupdate <= NOW() ;
-- and for each stockid it finds above
BEGIN WORK ;
....Insert new data...
COMMIT WORK ;

Looks simple enough, right?

Anyway, while the historical data INSERTs are occurring, no other
application can insert data into any of my tables, so my 5 minute update
sits there and diddles around forever, and I don't get recommendations.
My understanding from reading the docs is that 'FOR UPDATE' gets a 'ROW
SHARE' lock, and updates and inserts get a 'ROW EXCLUSIVE' lock.  I cannot
understand why all my apps, except the historical data app, are showing
'waiting' in a 'ps' command.  Is the problem that after I do the fist
"UPDATE" command it promotes the lock to 'ROW EXCLUSIVE', and then the 5
minute update cannot even look at the value of 'nextupdate' until after
the inserts have completed?  I set 'nextupdate' to tomorrow specifically
to prevent the 5 minute update from doing anything until the historical
data is available.

Any ideas?

- brian


Wm. Brian McCane                    | Life is full of doors that won't open
Search http://recall.maxbaud.net/   | when you knock, equally spaced amid those
Usenet http://freenews.maxbaud.net/ | that open when you don't want them to.
Auction http://www.sellit-here.com/ | - Roger Zelazny "Blood of Amber"




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Strange locking behaviour
@ 2002-02-23 00:51  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Brian McCane <bmccane@mccons.net>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2002-02-23 00:51 UTC (permalink / raw)
  To: Brian McCane <bmccane@mccons.net>; +Cc: pgsql-admin

Brian McCane <bmccane@mccons.net> writes:
> BEGIN WORK ;
> SELECT stockid FROM stocks WHERE ticker = 'M$FT' FOR UPDATE ;
> UPDATE stocks SET nextupdate = NOW + INTERVAL '1 DAY' ;
> ....Insert 7 years of stock data....
> UPDATE stocks SET nextupdate = NOW() ;
> COMMIT WORK ;

What exactly does the "Insert" step do?  Why are you bothering to UPDATE
stocks.nextupdate twice in this transaction?  It's not like any other
transaction will be able to see the now + 1 day setting.  Do you really
mean to update *all* rows of stocks here?

> The program that gets the 5 minute updates does:

> SELECT stockid FROM stocks WHERE nextupdate <= NOW() ;
> -- and for each stockid it finds above
> BEGIN WORK ;
> ....Insert new data...
> COMMIT WORK ;

Again, what exactly does the "Insert" step do?  Does it change
stocks.nextupdate?  (One would hope so, else there's a tight loop here.)
If it does, won't it block at that point waiting for the other
transaction's updates to be committed?

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2002-02-23 00:51 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-02-22 19:10 Strange locking behaviour Brian McCane <bmccane@mccons.net>
2002-02-23 00:51 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox