From: Alvaro Herrera <alvherre@2ndquadrant.com>
To: tgarnett@panjiva.com
Cc: pgsql-bugs@postgresql.org
Cc: kgrittn@ymail.com
Subject: Re: BUG #12990: Missing pg_multixact/members files (appears to have wrapped, then truncated)
Date: Mon, 20 Apr 2015 16:04:35 -0300
Message-ID: <20150420190435.GV4369@alvh.no-ip.org> (raw)
In-Reply-To: <20150413210801.GR4369@alvh.no-ip.org>
References: <20150406192130.2573.22545@wrigleys.postgresql.org>
<20150413210801.GR4369@alvh.no-ip.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Alvaro Herrera wrote:
> The fix is to raise an ERROR when generating a new multixact, if we
> detect that doing so would get close to the oldest multixact that the
> system knows about. If that happens, the solution is to vacuum so that
> the "oldest" point is advanced a bit more and you have room to generate
> more multixacts. In production, you would typically adjust the
> multixact freeze parameters so that "oldest multixact" is advanced more
> aggressively and you don't hit the ERROR.
Here's a patch. I have tested locally and it closes the issue for me.
If those affected can confirm that it stops the file removal from
happening, I'd appreciate it.
It would be much better to avoid that additional file reading, but it
seems difficult (read: I don't see how) without changing pg_control.
Note: in bootstrap.c, I had to move setting the bootstrap a bit earlier.
Otherwise, the is-in-bootstrap mode test returned false while
bootstrapping multixact, so initdb would fail on the initial phase
because of trying to read pg_multixact/offset/0000 which hasn't been
created at that point. The amount of code that runs in the bootstrap
mode after this change that wasn't running in bootstrap mode previously
is pretty tiny and shouldn't cause any problem -- it's essentially
the whole of BootStrapXLOG().
--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:memberswrap.patch (text/x-diff, 8.7K) downloaddiff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.cindex d13d2e7..488a49a 100644--- a/src/backend/access/transam/multixact.c+++ b/src/backend/access/transam/multixact.c@@ -211,6 +211,10 @@ typedef struct MultiXactStateData
MultiXactId multiStopLimit;
MultiXactId multiWrapLimit;
+ /* support for members anti-wraparound measures */+ MultiXactOffset offsetWarnLimit;+ MultiXactOffset offsetStopLimit;+
/*
* Per-backend data starts here. We have two arrays stored in the area
* immediately following the MultiXactStateData struct. Each is indexed by
@@ -339,6 +343,8 @@ static bool MultiXactOffsetPrecedes(MultiXactOffset offset1,
MultiXactOffset offset2);
static void ExtendMultiXactOffset(MultiXactId multi);
static void ExtendMultiXactMember(MultiXactOffset offset, int nmembers);
+static MultiXactOffset read_offset_for_multi(MultiXactId multi);+static void DetermineSafeOldestOffset(MultiXactId oldestMXact);
static void WriteMZeroPageXlogRec(int pageno, uint8 info);
@@ -975,7 +981,7 @@ GetNewMultiXactId(int nmembers, MultiXactOffset *offset)
/*
* To avoid swamping the postmaster with signals, we issue the autovac
- * request only once per 64K transaction starts. This still gives+ * request only once per 64K multis generated. This still gives
* plenty of chances before we get into real trouble.
*/
if (IsUnderPostmaster && (result % 65536) == 0)
@@ -1051,6 +1057,36 @@ GetNewMultiXactId(int nmembers, MultiXactOffset *offset)
else
*offset = nextOffset;
+ /*+ * Protect against overrun of the members space as well, with the following+ * rules:+ *+ * - if we're past offsetWarnLimit, emit a warning.+ * - if we're past offsetStopLimit, refuse to generate more multis.+ *+ * Note we haven't updated the shared state yet, so if we fail at this+ * point, the multixact ID we grabbed can still be used by the next guy.+ *+ * Note that thre is no point in forcing autovacuum runs here; the+ * multixact freeze settings would have to be reduced for that to have any+ * effect, and we can't do that from here.+ */+ if (MultiXactOffsetPrecedes(MultiXactState->offsetStopLimit, nextOffset + nmembers))+ ereport(ERROR,+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),+ errmsg("database is not accepting commands that generate new MultiXactIds to avoid \"members\" wraparound data loss in database with OID %u",+ MultiXactState->oldestMultiXactDB),+ errhint("Execute a database-wide VACUUM in that database, with reduced vacuum_multixact_freeze_min_age and vacuum_multixact_freeze_table_age settings.")));+ else if (MultiXactOffsetPrecedes(MultiXactState->offsetWarnLimit, nextOffset + nmembers))+ ereport(WARNING,+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),+ errmsg_plural("database with OID %u must be vacuumed before %d more multixact member is used",+ "database with OID %u must be vacuumed before %d more multixact members are used",+ MultiXactState->offsetStopLimit - nextOffset + nmembers,+ MultiXactState->oldestMultiXactDB,+ MultiXactState->offsetStopLimit - nextOffset + nmembers),+ errhint("Execute a database-wide VACUUM in that database, with reduced vacuum_multixact_freeze_min_age and vacuum_multixact_freeze_table_age settings.")));+
ExtendMultiXactMember(nextOffset, nmembers);
/*
@@ -1921,6 +1957,12 @@ StartupMultiXact(void)
*/
pageno = MXOffsetToMemberPage(offset);
MultiXactMemberCtl->shared->latest_page_number = pageno;
++ /*+ * compute the oldest member we need to keep around to avoid old member+ * data overrun.+ */+ DetermineSafeOldestOffset(MultiXactState->oldestMultiXactId);
}
/*
@@ -2121,7 +2163,7 @@ SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid)
*
* Note: This differs from the magic number used in
* SetTransactionIdLimit() since vacuum itself will never generate new
- * multis.+ * multis. XXX actually it does, if it needs to freeze old multis.
*/
multiStopLimit = multiWrapLimit - 100;
if (multiStopLimit < FirstMultiXactId)
@@ -2164,6 +2206,8 @@ SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid)
curMulti = MultiXactState->nextMXact;
LWLockRelease(MultiXactGenLock);
+ DetermineSafeOldestOffset(oldest_datminmxid);+
/* Log the info */
ereport(DEBUG1,
(errmsg("MultiXactId wrap limit is %u, limited by database with OID %u",
@@ -2250,13 +2294,16 @@ MultiXactAdvanceNextMXact(MultiXactId minMulti,
/*
* Update our oldestMultiXactId value, but only if it's more recent than
- * what we had.+ * what we had. However, even if not, always update the oldest multixact+ * offset limit.
*/
void
MultiXactAdvanceOldest(MultiXactId oldestMulti, Oid oldestMultiDB)
{
if (MultiXactIdPrecedes(MultiXactState->oldestMultiXactId, oldestMulti))
SetMultiXactIdLimit(oldestMulti, oldestMultiDB);
+ else+ DetermineSafeOldestOffset(oldestMulti);
}
/*
@@ -2424,6 +2471,70 @@ GetOldestMultiXactId(void)
}
/*
+ * Read the offset of the first member of the given multixact.+ */+static MultiXactOffset+read_offset_for_multi(MultiXactId multi)+{+ MultiXactOffset offset;+ int pageno;+ int entryno;+ int slotno;+ MultiXactOffset *offptr;++ pageno = MultiXactIdToOffsetPage(multi);+ entryno = MultiXactIdToOffsetEntry(multi);++ /* lock is acquired by SimpleLruReadPage_ReadOnly */+ slotno = SimpleLruReadPage_ReadOnly(MultiXactOffsetCtl, pageno, multi);+ offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];+ offptr += entryno;+ offset = *offptr;+ LWLockRelease(MultiXactOffsetControlLock);++ return offset;+}++/*+ * Based on the given oldest MultiXactId, determine what's the oldest member+ * offset and install the limit info in MultiXactState, where it can be used to+ * prevent overrun of old data in the members SLRU area.+ */+static void+DetermineSafeOldestOffset(MultiXactId oldestMXact)+{+ MultiXactOffset oldestOffset;++ /*+ * Can't do this while initdb'ing or in the startup process while replaying+ * WAL: the segment file to read might have not yet been created, or+ * already been removed.+ */+ if (IsBootstrapProcessingMode() || InRecovery)+ return;++ /*+ * We determine the safe upper bound for offsets of new xacts by reading+ * the offset of the oldest multixact, and going back one segment. This+ * way, the sequence of multixact member segments will always have a+ * one-segment hole at a minimum. We start spewing warnings 19 segments+ * before that.+ */+ oldestOffset = read_offset_for_multi(oldestMXact);+ /* move back to start of this segment */+ oldestOffset -= oldestOffset / MULTIXACT_MEMBERS_PER_PAGE * SLRU_PAGES_PER_SEGMENT;++ LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);+ /* always leave one segment before the wraparound point */+ MultiXactState->offsetStopLimit = oldestOffset -+ (MULTIXACT_MEMBERS_PER_PAGE * SLRU_PAGES_PER_SEGMENT);+ /* start complaining 20 segments before the wraparound point */+ MultiXactState->offsetWarnLimit = oldestOffset -+ (MULTIXACT_MEMBERS_PER_PAGE * SLRU_PAGES_PER_SEGMENT * 20);+ LWLockRelease(MultiXactGenLock);+}++/*
* SlruScanDirectory callback.
* This callback deletes segments that are outside the range determined by
* the given page numbers.
@@ -2555,26 +2666,7 @@ TruncateMultiXact(void)
* First, compute the safe truncation point for MultiXactMember. This is
* the starting offset of the oldest multixact.
*/
- {- int pageno;- int slotno;- int entryno;- MultiXactOffset *offptr;-- /* lock is acquired by SimpleLruReadPage_ReadOnly */-- pageno = MultiXactIdToOffsetPage(oldestMXact);- entryno = MultiXactIdToOffsetEntry(oldestMXact);-- slotno = SimpleLruReadPage_ReadOnly(MultiXactOffsetCtl, pageno,- oldestMXact);- offptr = (MultiXactOffset *)- MultiXactOffsetCtl->shared->page_buffer[slotno];- offptr += entryno;- oldestOffset = *offptr;-- LWLockRelease(MultiXactOffsetControlLock);- }+ oldestOffset = read_offset_for_multi(oldestMXact);
/*
* To truncate MultiXactMembers, we need to figure out the active page
diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.cindex fc1b3c8..43a50e4 100644--- a/src/backend/bootstrap/bootstrap.c+++ b/src/backend/bootstrap/bootstrap.c@@ -411,6 +411,7 @@ AuxiliaryProcessMain(int argc, char *argv[])
proc_exit(1); /* should never return */
case BootstrapProcess:
+ SetProcessingMode(BootstrapProcessing);
bootstrap_signals();
BootStrapXLOG();
BootstrapModeMain();
@@ -473,8 +474,7 @@ BootstrapModeMain(void)
int i;
Assert(!IsUnderPostmaster);
-- SetProcessingMode(BootstrapProcessing);+ Assert(IsBootstrapProcessingMode());
/*
* Do backend-like initialization for bootstrap mode
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: alvherre@2ndquadrant.com, tgarnett@panjiva.com, kgrittn@ymail.com
Subject: Re: BUG #12990: Missing pg_multixact/members files (appears to have wrapped, then truncated)
In-Reply-To: <20150420190435.GV4369@alvh.no-ip.org>
References: <20150406192130.2573.22545@wrigleys.postgresql.org> <20150413210801.GR4369@alvh.no-ip.org> <20150420190435.GV4369@alvh.no-ip.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox