Re: 'COPY ... FROM' inserts to btree, blocks on buffer writeout - Mailing list pgsql-hackers

From Tom Lane
Subject Re: 'COPY ... FROM' inserts to btree, blocks on buffer writeout
Date
Msg-id 14208.1104540140@sss.pgh.pa.us
Whole thread Raw
In response to Re: 'COPY ... FROM' inserts to btree, blocks on buffer writeout  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: 'COPY ... FROM' inserts to btree, blocks on buffer
Re: 'COPY ... FROM' inserts to btree, blocks on buffer writeout
List pgsql-hackers
I wrote:
> A possible fix for this is to reorder the operations in FlushBuffer
> so that we share-lock the buffer before releasing BufMgrLock ... but
> I'm not sure that doesn't introduce other deadlock risks.

That's too simplistic, since at least one caller of FlushBuffer is
trying to write pages that may be in active use.  If the target page
is already exclusive-locked by another process then a deadlock between
the per-buffer lock and BufMgrLock is entirely possible.

I think that it would work for BufferAlloc to share-lock the victim
buffer before calling FlushBuffer; we'd have to add a bool parameter to
FlushBuffer telling it the lock was already acquired.  This is safe in
this particular path because a buffer that was just taken from the
freelist should not be exclusive-locked by anyone.  (It could possibly
be share-locked by the bgwriter, but that won't cause a deadlock.)
A process that wanted exclusive lock would first have to pin the buffer,
which can't happen before we release the BufMgrLock.  By adding the
parameter we'd avoid changing the behavior for other callers.

Comments, better ideas?

BTW, it looks to me like this deadlock potential has existed at least
since 7.0.  I seem to recall one or two reports of unexplainable
apparent deadlocks, which perhaps are now explained.
        regards, tom lane


pgsql-hackers by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: exception handling in plpgsql
Next
From: Michael Wildpaner
Date:
Subject: Re: 'COPY ... FROM' inserts to btree, blocks on buffer