473,811 Members | 2,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deadlocks -- what can I do about them?


Hello,

I'm using PostgreSQL 7.4.3 on a RedHat 9 Linux server
(a P4 HyperThreaded, using the SMP kernel, in case this
makes a difference).

I'm not 100% sure I understand exactly why I am causing
them, but let's say that several inserts inside an SQL
transaction (i.e., a BEGIN / COMMIT-ROLLBACK block),
where each insert references two different foreign-keys
kind of sets the alarm.

The thing is, what can I do? Is it ok if I check the
error and whenever a deadlock is detected, a execute
a delay of some random number of milliseconds and
then try again?

After some preliminary analysis, I'm not sure there is
anything (sensible) that I can do about those SQL insert
statements or the fact that they're enclosed in an SQL
transaction. I can't seem to see why those would be
fundamentally wrong. That's why I'm looking for a
solution that may involve recovering from the error.
The transaction has been rolled back, so I'm guessing
I could try again a little bit later, no?

Is there some standard practice to deal with these
deadlocks, or to avoid them in a situation like I
described (I know, I didn't post any details, but
details would only bore you at this point, I guess)

Thanks for any advice/comments,

Carlos
--

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

http://archives.postgresql.org

Nov 23 '05 #1
4 1491
mo****@mochima. com (Carlos Moreno) wrote in message news:<41******* *******@mochima .com>...
Hello,

I'm using PostgreSQL 7.4.3 on a RedHat 9 Linux server
(a P4 HyperThreaded, using the SMP kernel, in case this
makes a difference).

I'm not 100% sure I understand exactly why I am causing
them, but let's say that several inserts inside an SQL
transaction (i.e., a BEGIN / COMMIT-ROLLBACK block),
where each insert references two different foreign-keys
kind of sets the alarm.

The thing is, what can I do? Is it ok if I check the
error and whenever a deadlock is detected, a execute
a delay of some random number of milliseconds and
then try again?

After some preliminary analysis, I'm not sure there is
anything (sensible) that I can do about those SQL insert
statements or the fact that they're enclosed in an SQL
transaction. I can't seem to see why those would be
fundamentally wrong. That's why I'm looking for a
solution that may involve recovering from the error.
The transaction has been rolled back, so I'm guessing
I could try again a little bit later, no?

Is there some standard practice to deal with these
deadlocks, or to avoid them in a situation like I
described (I know, I didn't post any details, but
details would only bore you at this point, I guess)

Thanks for any advice/comments,

Carlos


Best thing to do is know your locking. IF you are using Pg you have
several different levels avail. You know what causes deadlocks, the
key is to use as little locking as is necesary so that under no
circumstances /can/ you get a deadlock, while at the same time
ensuring consistency (ACID). Pg even supports row-level locks I think
so you have a lot of options.

Sometimes complex trx can cause challenges but there is always a way.

gl
Nov 23 '05 #2

Ok, now I'm really intrigued by what looks to me
(possibly from a naive point of view) like a bug,
or rather, a limitation on the implementation.

I can't find a reasonable justification why the
following would cause a deadlock:

I run two instances of psql using the same DB on
the same machine. On one of them, I run:

create table A (id int primary key);
create table B (id int primary key);
create table AB
(
A_id int references A(id),
B_id int references B(id)
);

Then I add a few records (all this from the same
instance of psql):

insert into A values (1);
insert into A values (2);
insert into B values (10);
insert into B values (11);

Ok, now, I try two concurrent transactions, by
executing commands alternating from one psql
instance to the other one:

I'll prefix each line with 1: or 2: indicating
which console I execute it on -- the commands were
executing in the time sequence corresponding to the
lines below:
1: begin;
2: begin;

1: insert into AB values (1,10);
2: insert into AB values (2,10);
<AT THIS POINT, CONSOLE 2 BLOCKS>

1: insert into AB values (2,11);

At this point, console 1 blocks for a second or
two, and then I get an error, reporting that a
deadlock was detected; then, console 2 unblocks.

I can't see how it is justified that the above
causes a deadlock.

I do understand how the deadlock is happening:
trans. 1 puts a lock on rows 1 of A and row 10
of B -- meaning, "nobody touches these rows until
I'm finished"; then trans 2. locks row 2 of A,
but is put on hold waiting to lock row 10 of B,
since there is already a lock on it. When trans.
A now tries to put a lock on row 2 of A, the
deadlock happens.

The thing is, why? Why is this a deadlock? When
we look at the low-level details, sure; but when
you look at the nature of what's happening at a
conceptual level, a deadlock is not justified,
IMHO:

Trans. 1 doesn't really need to put a mutex type
of lock around row 1 of A -- it simply needs to
atomically flag the order: "nobody delete or
modify this row of table A"... Another trans.
that attempts to place the same order should
not block -- it should succeed and return
immediately and continue with the transaction;
there is no conflict in the above example -- the
first transaction does not want to allow anyone
to mess with row 1 of A; the other transaction
wants exactly the same, so it seems to me that
the lock is more restrictive than it needs to be.

I don't know about the internals of how transactions
and locks and FK constraints are handled, but I'm
analyzing it and describing what seems to be
happening internally, based on the behaviour I
observe.

Any comments?

Carlos
--
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #3

On Tue, 24 Aug 2004, Carlos Moreno wrote:
Ok, now I'm really intrigued by what looks to me
(possibly from a naive point of view) like a bug,
or rather, a limitation on the implementation.


Yep. See recent (and historical) discussions on
needing a weaker lock than FOR UPDATE for handling
foreign keys.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #4
>>>>> "CM" == Carlos Moreno <mo****@mochima .com> writes:

CM> Ok, now I'm really intrigued by what looks to me
CM> (possibly from a naive point of view) like a bug,
CM> or rather, a limitation on the implementation.
[[ ... ]]
CM> I don't know about the internals of how transactions
CM> and locks and FK constraints are handled, but I'm
CM> analyzing it and describing what seems to be
CM> happening internally, based on the behaviour I
CM> observe.

FWIW I get bit by this quite a bit. Unfortunately all the deadlock
avoidance theory doesn't help you since you're not explicitly getting
the locks, and as you see, ordering the insert/update operations such
as to avoid conflicting locks is hard to do.

If I could designate the transaction I prefer to be killed, it would
save me a lot: often my short easy to repeat transaction wins out over
some large multi-thousand row select/insertion operation.

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Vivek Khera, Ph.D. Khera Communications, Inc.
Internet: kh***@kciLink.c om Rockville, MD +1-301-869-4449 x806
AIM: vivekkhera Y!: vivek_khera http://www.khera.org/~vivek/

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #5

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

Similar topics

14
3050
by: Eloff | last post by:
This is not really Python specific, but I know Python programmers are among the best in the world. I have a fair understanding of the concepts involved, enough to realize that I would benefit from the experience of others :) I have a shared series of objects in memory that may be > 100MB. Often to perform a task for a client several of these objects must be used. Since many clients can be making requests at once (>100 per second during...
1
19127
by: AKS | last post by:
I am getting lot of deadlocks in my application. As it is very complex ti avoid deadlocks at this stage of application we have done few steps to lessen the impact. We have added retries after deadlock is capturted. We have added select * from TABLE with (nolock) wherever possible. But interestingly second step is not working. I have few simple select statements where i am using nolock criteria still I am getting deadlock victim error....
1
2227
by: Matt White | last post by:
We've found deadlocks in the trace file that were not captured by our Powerbuilder application. Some deadlocks are trapped or, at least, reported to the user as a db error, and others are completely silent. We've also seen evidence of strange data that would be explained by unprocessed deadlocks, although we've not yet proven that the unreported deadlocks are killing updates to the db. Putting a raiserror into various parts of the same...
4
6720
by: Leaf | last post by:
Greetings, I've been reading with interest the threads here on deadlocking, as I'm finding my formerly happy app in a production environment suddenly deadlocking left and right. It started around the time I decided to wrap a series of UPDATE commands with BEGIN/END. The gist of it is I have a .NET app that can do some heavy reading (no writing) from tblWOS. It can take a minute or so to read all the data into the app, along with data...
7
2214
by: Marcus | last post by:
Hello all, I am trying to figure out when it is appropriate to use shared and exclusive locks with InnoDB. Using shared locks (lock in share mode), I can easily create a scenario with 2 clients that deadlocks - start 2 transactions, open 2 shared locks, and both try to insert a new row before either commits. Using exclusive locks (for update) I cannot come up with a scenario that results in a deadlock, since the very nature of the...
9
2450
by: Mike Carr | last post by:
I am running into an issue. Recently I installed IBuySpy Portal and then converted the data source to odp.net. When debugging the app my machine would freeze or become really slow. I can reproduce it by: Setting a breakpoint somewhere in the code, hitting F5, once the application has completely loaded hit the stop button, make a quick change and then select F5 again. It appears that the debugger attaches to the previous aspnet_wp.exe...
16
2891
by: Ben | last post by:
I'm doing a bunch of data mining against a postgres database and have run into an interesting problem with deadlocks. The problem is, postgres is detecting them and then wacking the offending process, and I can't figure out what's causing them. I have a ton of select queries (but none for update), and then a single query to insert into a table. Nothing selects from that table. So where could the deadlock be? pg_stat_activity has a column...
6
2524
by: Greg Stark | last post by:
There's another poster complaining about referential integrity checks causing deadlocks. Unfortunately I've deleted the message so this response (and the archives aren't responding) isn't going to show up on the right thread. The reason the deadlock is happening is because of a known deficiency in Postgres that postgres has to take an exclusive lock on the records to ensure they aren't deleted before your insert/update commits....
4
4228
by: John Rivers | last post by:
There are many references to deadlock handlers that retry the transaction automatically. But IMO a deadlock is the result of a design flaw that should be fixed. My applications treat deadlocks like any other runtime error - they are logged and fixed. There seems to be a an opinion that deadlocks are inevitable !
0
9734
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9607
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10395
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10408
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10137
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7673
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3874
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3026
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.