Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YmyvW-0007nn-3S for pgsql-bugs@arkaria.postgresql.org; Tue, 28 Apr 2015 06:23:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YmyvV-0006Xj-F3 for pgsql-bugs@arkaria.postgresql.org; Tue, 28 Apr 2015 06:23:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YmyvU-0006Xc-6L for pgsql-bugs@postgresql.org; Tue, 28 Apr 2015 06:23:24 +0000 Received: from smtprelay0181.b.hostedemail.com ([64.98.42.181] helo=smtprelay.b.hostedemail.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YmyvN-0001xt-RA for pgsql-bugs@postgresql.org; Tue, 28 Apr 2015 06:23:23 +0000 Received: from filter.hostedemail.com (10.5.19.248.rfc1918.com [10.5.19.248]) by smtprelay04.b.hostedemail.com (Postfix) with ESMTP id ADA417B868; Tue, 28 Apr 2015 06:23:16 +0000 (UTC) X-Session-Marker: 616C76686572726540616C76682E6E6F2D69702E6F7267 X-Spam-Summary: 40, 2.5, 0, , d41d8cd98f00b204, alvherre@alvh.no-ip.org, :::::::, RULES_HIT:1:41:355:379:599:871:967:968:973:988:989:1260:1263:1311:1312:1313:1314:1345:1359:1437:1515:1516:1518:1519:1541:1575:1594:1595:1596:1711:1730:1747:1764:1777:1792:2194:2199:2393:2525:2560:2563:2682:2685:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3353:3865:3866:3867:3868:3870:3871:3872:3873:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4250:4605:4659:5007:6119:6261:6748:7281:7398:7903:8660:8957:9025:9121:10011:10400:10450:10455:10471:10848:11232:11256:11257:11604:11658:11914:12438:12517:12519:13148:13163:13229:13230:13255:13869:13894:13895:19904:19999:21067:21080, 0, RBL:none, CacheIP:none, Bayesian:0.5, 0.5, 0.5, Netcheck:none, DomainCache:0, MSF:not bulk, SPF:fn, MSBL:0, DNSBL:none, Custom_rules:0:1:0 X-HE-Tag: army27_1c7382e29182a X-Filterd-Recvd-Size: 14752 Received: from alvin.alvh.no-ip.org (unknown [181.42.255.96]) (Authenticated sender: alvherre@alvh.no-ip.org) by omf10.b.hostedemail.com (Postfix) with ESMTPA; Tue, 28 Apr 2015 06:23:15 +0000 (UTC) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id 736ED3E8; Tue, 28 Apr 2015 03:23:52 -0300 (CLT) Date: Tue, 28 Apr 2015 03:23:52 -0300 From: Alvaro Herrera To: Thomas Munro Cc: Timothy Garnett , pgsql-bugs@postgresql.org, kgrittn@ymail.com Subject: Re: Re: BUG #12990: Missing pg_multixact/members files (appears to have wrapped, then truncated) Message-ID: <20150428062352.GW4368@alvh.no-ip.org> References: <20150406192130.2573.22545@wrigleys.postgresql.org> <20150413210801.GR4369@alvh.no-ip.org> <20150420190435.GV4369@alvh.no-ip.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Izn7cH1Com+I3R9J" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --Izn7cH1Com+I3R9J Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Thomas Munro wrote: > One thing I noticed about your patch is that it effectively halves the > amount of multixact members you can have on disk. Sure, I'd rather > hit an error at 2^31 members than a corrupt database at 2^32 members, > but I wondered if we should try to allow the full range to be used. Ah, yeah, we do want the full range; that's already built in the code elsewhere. In this version, I used your WouldWrap function, but there was a bug in your formulation of the call site: after the WARNING has been issued once, it is never issued again for that wraparound cycle, because the second time around the nextOffset has already crossed the boundary and your routine returns false. IMO this is wrong and the warning should be issued every time. To fix that problem I removed the offsetWarnLimit altogether, and instead do WouldWrap() of the value against offsetStopLimit minus the 20 segments. That way, the warning is issued continuously until the offsetStopLimit is reached (once there, obviously, only the error is thrown, not the warning, which is correct.) I also added a call to DetermineSafeOldestOffset() in TrimMultiXact: as far as I can tell, this is necessary for the time when a standby exits recovery, because when InRecovery we return early from DetermineSafeOldestOffset() so the safe point would never get set. -- Álvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --Izn7cH1Com+I3R9J Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="memberswrap-2.patch" diff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.c index 3dd9892..12da9f0 100644 --- a/src/backend/access/transam/multixact.c +++ b/src/backend/access/transam/multixact.c @@ -211,6 +211,9 @@ typedef struct MultiXactStateData MultiXactId multiStopLimit; MultiXactId multiWrapLimit; + /* support for members anti-wraparound measures */ + 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 +342,10 @@ static bool MultiXactOffsetPrecedes(MultiXactOffset offset1, MultiXactOffset offset2); static void ExtendMultiXactOffset(MultiXactId multi); static void ExtendMultiXactMember(MultiXactOffset offset, int nmembers); +static void DetermineSafeOldestOffset(MultiXactId oldestMXact); +static bool MultiXactOffsetWouldWrap(MultiXactOffset boundary, + MultiXactOffset start, uint32 distance); +static MultiXactOffset read_offset_for_multi(MultiXactId multi); static void WriteMZeroPageXlogRec(int pageno, uint8 info); @@ -972,7 +979,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) @@ -1048,6 +1055,46 @@ 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 offsetStopLimit, refuse to generate more multis. + * - if we're close to offsetStopLimit, emit a warning. + * + * Arbitrarily, we start emitting warnings when we're 20 segments or less + * from offsetStopLimit. + * + * 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 there is no point in forcing autovacuum runs here: the + * multixact freeze settings would have to be reduced for that to have any + * effect. + */ +#define OFFSET_WARN_SEGMENTS 20 + if (MultiXactOffsetWouldWrap(MultiXactState->offsetStopLimit, nextOffset, + nmembers)) + ereport(ERROR, + (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("multixact \"members\" limit exceeded"), + errdetail_plural("This command would create a multixact with %u members, which exceeds remaining space (%u member.)", + "This command would create a multixact with %u members, which exceeds remaining space (%u members.)", + MultiXactState->offsetStopLimit - nextOffset - 1, + nmembers, + MultiXactState->offsetStopLimit - nextOffset - 1), + errhint("Execute a database-wide VACUUM in database with OID %u, with reduced vacuum_multixact_freeze_min_age and vacuum_multixact_freeze_table_age settings.", + MultiXactState->oldestMultiXactDB))); + else if (MultiXactOffsetWouldWrap(MultiXactState->offsetStopLimit, + nextOffset, + nmembers + MULTIXACT_MEMBERS_PER_PAGE * SLRU_PAGES_PER_SEGMENT * OFFSET_WARN_SEGMENTS)) + ereport(WARNING, + (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("database with OID %u must be vacuumed before %d more multixact members are used", + 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); /* @@ -1918,6 +1965,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); } /* @@ -2011,6 +2064,8 @@ TrimMultiXact(void) } LWLockRelease(MultiXactMemberControlLock); + + DetermineSafeOldestOffset(MultiXactState->oldestMultiXactId); } /* @@ -2118,7 +2173,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) @@ -2161,6 +2216,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", @@ -2247,13 +2304,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); } /* @@ -2421,6 +2481,121 @@ GetOldestMultiXactId(void) } /* + * 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 a few complete + * segments before that. + */ + oldestOffset = read_offset_for_multi(oldestMXact); + /* move back to start of the corresponding 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); + LWLockRelease(MultiXactGenLock); +} + +/* + * Return whether adding "distance" to "start" would move past "boundary". + * + * We use this to determine whether the addition is "wrapping around" the + * boundary point, hence the name. The reason we don't want to use the regular + * 2^31-modulo arithmetic here is that we want to be able to use the whole of + * the 2^32-1 space here, allowing for more multixacts that would fit + * otherwise. See also SlruScanDirCbRemoveMembers. + */ +static bool +MultiXactOffsetWouldWrap(MultiXactOffset boundary, MultiXactOffset start, + uint32 distance) +{ + MultiXactOffset finish; + + Assert(distance >= 0); + + /* + * Note that offset number 0 is not used (see GetMultiXactIdMembers), so if + * the addition wraps around the UINT_MAX boundary, skip that value. + */ + finish = start + distance; + if (finish < start) + finish++; + + /*----------------------------------------------------------------------- + * When the boundary is numerically greater than the starting point, any + * value numerically between the two is not wrapped: + * + * <----S----B----> + * [---) = F wrapped past B (and UINT_MAX) + * [---) = F not wrapped + * [----] = F wrapped past B + * + * When the boundary is numerically less than the starting point (i.e. the + * UINT_MAX wraparound occurs somewhere in between) then all values in + * between are wrapped: + * + * <----B----S----> + * [---) = F not wrapped past B (but wrapped past UINT_MAX) + * [---) = F wrapped past B (and UINT_MAX) + * [----] = F not wrapped + *----------------------------------------------------------------------- + */ + if (start < boundary) + { + return finish >= boundary || finish < start; + } + else + { + return finish >= boundary && finish < start; + } +} + +/* + * 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; +} + +/* * SlruScanDirectory callback. * This callback deletes segments that are outside the range determined by * the given page numbers. @@ -2552,26 +2727,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.c index fc1b3c8..5c0fe9b 100644 --- a/src/backend/bootstrap/bootstrap.c +++ b/src/backend/bootstrap/bootstrap.c @@ -411,6 +411,12 @@ AuxiliaryProcessMain(int argc, char *argv[]) proc_exit(1); /* should never return */ case BootstrapProcess: + /* + * There was a brief instant during which mode was Normal; this is + * okay. We need to be in bootstrap mode during BootStrapXLOG for + * the sake of multixact initialization. + */ + SetProcessingMode(BootstrapProcessing); bootstrap_signals(); BootStrapXLOG(); BootstrapModeMain(); @@ -473,8 +479,7 @@ BootstrapModeMain(void) int i; Assert(!IsUnderPostmaster); - - SetProcessingMode(BootstrapProcessing); + Assert(IsBootstrapProcessingMode()); /* * Do backend-like initialization for bootstrap mode --Izn7cH1Com+I3R9J Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --Izn7cH1Com+I3R9J--