pgsql: Don't open formally non-existent segments in _mdfd_getseg().

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Don't open formally non-existent segments in _mdfd_getseg().
Date: 2016-04-27 03:35:56
Message-ID: E1avGGa-0007I0-Ua@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Don't open formally non-existent segments in _mdfd_getseg().

Before this commit _mdfd_getseg(), in contrast to mdnblocks(), did not
verify whether all segments leading up to the to-be-opened one, were
RELSEG_SIZE sized. That is e.g. not the case after truncating a
relation, because later segments just get truncated to zero length, not
removed.

Once a "non-existent" segment has been opened in a session, mdnblocks()
will return wrong results, causing errors like "could not read block %u
in file" when accessing blocks. Closing the session, or the later
arrival of relevant invalidation messages, would "fix" the problem.

That, so far, was mostly harmless, because most segment accesses are
only done after an mdnblocks() call. But since 428b1d6b29ca we try to
open segments that might have been deleted, to trigger kernel writeback
from a backend's queue of recent writes.

To fix check segment sizes in _mdfd_getseg() when opening previously
unopened segments. In practice this shouldn't imply a lot of additional
lseek() calls, because mdnblocks() will most of the time already have
opened all relevant segments.

This commit also fixes a second problem, namely that _mdfd_getseg(
EXTENSION_RETURN_NULL) extends files during recovery, which is not
desirable for the mdwriteback() case. Add EXTENSION_REALLY_RETURN_NULL,
which does not behave that way, and use it.

Reported-By: Thom Brown
Author: Andres Freund, Abhijit Menon-Sen
Reviewd-By: Robert Haas, Fabien Coehlo
Discussion: CAA-aLv6Dp_ZsV-44QA-2zgkqWKQq=GedBX2dRSrWpxqovXK=Pg(at)mail(dot)gmail(dot)com
Fixes: 428b1d6b29ca599c5700d4bc4f4ce4c5880369bf

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/72a98a639574d2e25ed94652848555900c81a799

Modified Files
--------------
src/backend/storage/smgr/md.c | 96 +++++++++++++++++++++++++++++++------------
1 file changed, 69 insertions(+), 27 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Andres Freund 2016-04-27 03:42:25 Re: pgsql: Emit invalidations to standby for transactions without xid.
Previous Message Andres Freund 2016-04-27 03:04:11 Re: Re: pgsql: Convert contrib/seg's bool-returning SQL functions to V1 call co