Re: 'COPY ... FROM' inserts to btree, blocks on buffer writeout

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Wildpaner <mike(at)rainbow(dot)studorg(dot)tuwien(dot)ac(dot)at>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: 'COPY ... FROM' inserts to btree, blocks on buffer writeout
Date: 2005-01-01 00:42:20
Message-ID: 14208.1104540140@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Wildpaner 2005-01-01 00:48:53 Re: 'COPY ... FROM' inserts to btree, blocks on buffer
Previous Message Andrew Dunstan 2004-12-31 23:02:36 Re: exception handling in plpgsql