473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

select for update & lock contention


I think I'm seeing table-level lock contention in the following function
when I have many different concurrent callers, each with mutually distinct
values for $1. Is there a way to reimplement this function using
select-for-update (or equivalent) in order to get a row-level lock (and
thus less contention) while maintaining the function interface? The docs
seem to suggest so, but it's not clear how to return the SETOF queued_item
and also use select-for-update to get the row-level locks. TIA.

CREATE OR REPLACE FUNCTION getqueuedupdates (character)
RETURNS SETOF queued_item AS '
DECLARE
rows record;
BEGIN
FOR rows IN SELECT * FROM queued_item where subscriber=$1 LOOP
RETURN NEXT rows;
DELETE FROM queued_item WHERE key=rows.key;
END LOOP;
RETURN;
END;'
LANGUAGE plpgsql;

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #1
4 4055
This is on 7.3.4/7.3.6. Thx.

On Wednesday May 5 2004 10:42, Ed L. wrote:
I think I'm seeing table-level lock contention in the following function
when I have many different concurrent callers, each with mutually
distinct values for $1. Is there a way to reimplement this function
using select-for-update (or equivalent) in order to get a row-level lock
(and thus less contention) while maintaining the function interface? The
docs seem to suggest so, but it's not clear how to return the SETOF
queued_item and also use select-for-update to get the row-level locks.
TIA.

CREATE OR REPLACE FUNCTION getqueuedupdates (character)
RETURNS SETOF queued_item AS '
DECLARE
rows record;
BEGIN
FOR rows IN SELECT * FROM queued_item where subscriber=$1 LOOP
RETURN NEXT rows;
DELETE FROM queued_item WHERE key=rows.key;
END LOOP;
RETURN;
END;'
LANGUAGE plpgsql;

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #2
On Wednesday May 5 2004 10:42, Ed L. wrote:
I think I'm seeing table-level lock contention in the following function
when I have many different concurrent callers, each with mutually
distinct values for $1. Is there a way to reimplement this function
using select-for-update (or equivalent) in order to get a row-level lock
(and thus less contention) while maintaining the function interface? The
docs seem to suggest so, but it's not clear how to return the SETOF
queued_item and also use select-for-update to get the row-level locks.
TIA.

CREATE OR REPLACE FUNCTION getqueuedupdates (character)
RETURNS SETOF queued_item AS '
DECLARE
rows record;
BEGIN
FOR rows IN SELECT * FROM queued_item where subscriber=$1 LOOP
RETURN NEXT rows;
DELETE FROM queued_item WHERE key=rows.key;
END LOOP;
RETURN;
END;'
LANGUAGE plpgsql;


I should also mention what leads me to suspect lock contention. First, the
table is frequently vacuum analyzed, so I'm reasonably confident its not a
planner stats issue. Second, the table usually contains a small number of
rows (tens to a couple hundred), so I reason its unlikely that a planner
issue would slow it down much. Third, I have put in "RAISE NOTICE"
statements before and after each statement in the function, and can see the
stalls of several seconds in the server log within the deleting loop.

So, I tried to get a less conflicting lock by using SELECT FOR UPDATE as
follows with the "PERFORM" line (syntax corrections welcome; contextual
examples of how to do this were not plentiful on google or docs)...

CREATE OR REPLACE FUNCTION getqueuedupdates (character)
RETURNS SETOF queued_item AS '
DECLARE
rows record;
BEGIN
-- obtain row-level locks...
PERFORM * FROM queued_item WHERE subscriber = $1 FOR UPDATE OF
queued_item;
FOR rows IN SELECT * FROM queued_item where subscriber=$1 LOOP
RETURN NEXT rows;
DELETE FROM queued_item WHERE key=rows.key;
END LOOP;
RETURN;
END;'
LANGUAGE plpgsql;
Then I watched the locks with the following command, which I think basically
shows which backends are locking which tables in which modes from which SQL
statements:

while test 1; do psql -c "select now(), d.datname||':'||r.relname as table,
l.transaction as xact, l.pid, l.mode, l.granted,
pg_stat_get_backend_activity(S.backendid) AS sql from pg_locks l, pg_class
r, pg_database d, (SELECT pg_stat_get_backend_idset() AS backendid) AS S
where l.relation = r.oid and l.database = d.oid and d.datname = 'testdb'
and r.relname = 'queued_item' and pg_stat_get_backend_pid(S.backendid) =
l.pid and d.oid = pg_stat_get_backend_dbid(S.backendid)"; sleep 1; done
And with that command above, I notice several things leading to other
questions:

1) I can now see the lock modes for the function's delete statements have
changed from RowExclusiveLock (a table-level lock?) to RowShareLock (a
row-level lock, a good thing).

2) The contention appears to continue. I am now wondering if my assumption
that RowShareLock would reduce contention over RowExclusiveLock is sound in
such a case where you have multiple writers with each deleting a distinct
set of rows (one set per subscriber)...? What else could explain the
delays I see in the delete loop?

3) This table is populated by INSERT triggers on other tables; each of
those inserts results in N triggered INSERTs into queued_item, one insert
for each of N subscribers. Given I also see the RowExclusiveLock mode from
those inserts, I'm also wondering if those inserts aren't momentarily
blocking the delete statements in the function above? The 7.3.4 docs
(http://www.postgresql.org/docs/7.3/s...LOCKING-TABLES)
seem to me to suggest RowExclusiveLock will not interfere with
RowShareLock, but the modes seen above and the modes in the docs leave room
for doubt.

One last thought: This table does at times have a fairly high volume of
rows being inserted and then quickly deleted (as much as 100
inserts/deletes per second). So the volume of change is large, but the
number of rows present remains in flux within a range of maybe 0-1000.
Vaccuum/analyze is done via autovacuum maybe every 5-20 minutes, so I guess
the planner could be out of touch with actual index distributions.

Idears?

TIA.
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #3
"Ed L." <pg***@bluepolka.net> writes:
I think I'm seeing table-level lock contention in the following function


I think you're barking up the wrong tree entirely. There's nothing in
that function that would acquire a conflicting table lock.

I'm wondering about foreign key lock contention, myself. Look to what
the DELETE must do.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #4
On Thursday May 6 2004 6:11, Tom Lane wrote:
"Ed L." <pg***@bluepolka.net> writes:
I think I'm seeing table-level lock contention in the following
function


I think you're barking up the wrong tree entirely. There's nothing in
that function that would acquire a conflicting table lock.

I'm wondering about foreign key lock contention, myself. Look to what
the DELETE must do.


We've dropped all foreign key constraints on the queued_item table and moved
the delete out of the loop as follows...
DECLARE
rows record;
BEGIN
PERFORM * FROM queued_item WHERE subscriber = $1 FOR UPDATE OF
queued_item;
RAISE NOTICE 'getupdates(%):going to call select', $1;
FOR rows IN SELECT * FROM queued_item where subscriber=$1 LOOP
RAISE NOTICE 'getupdates(%): in select loop, returning %', $1,
rows.key;
RETURN NEXT rows;
END LOOP;
RAISE NOTICE 'getupdates(%):going to call delete', $1;
DELETE FROM queued_item WHERE subscriber = $1;
RAISE NOTICE 'getupdates(%):done calling delete', $1;
RETURN;
END;
So the delete seems a non-factor. The delay is now occurring inside the
loop, sometimes for 4-8 seconds. During this delay, it is possible that
other triggers are inserting into the queued_item table. Other ideas as to
what is going on?

TIA.
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jayson_13 | last post by:
Hi, I need to implement a counter and i face problem of locking so hope that u guys can help me. I try to do test like this : 1st connection SELECT * FROM nextkey WHERE tblname = 'PLCN'...
7
by: Paul Serby | last post by:
Why does '*select count(id) from "tblContacts"'* do a sequential scan when the field '*id*' is indexed using a btree? MySql simply looks at the index which is keeping a handy record of the...
18
by: Frank Rizzo | last post by:
Hello, I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done...
5
by: Bob Bins | last post by:
Is there a way to create a shared lock using the Monitor class or any other synchronization class in .NET? So I have one resource that may have multiple threads using it at once but a second...
4
by: pike | last post by:
DB2 UDB 8.1 FP7 We are getting intermittent deadlocks (911 RC 2) even when U row-lock has been secured. The transaction is as follows: 1) Select current application number value from table....
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
17
by: djc | last post by:
I got great info on related question previously. This link <http://www.yoda.arachsys.com/csharp/threads/volatility.shtml> from Brian Gideon was especially informative. 1) the lock statement does...
0
by: PeterC | last post by:
We're getting numerous deadlocks in a multi-user system where users are coming in and updating their own data. In our troubleshooting/traces, the deadlocks seem to be coming from lock contention...
5
by: sticky | last post by:
Hi I need to be able to lock a table against INSERT and UPDATE, but not SELECT, for the duration of a transaction. The transaction will be defined at the application level in c#, and then use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.