Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UjvZB-0003ir-SU for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Jun 2013 18:02:42 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UjvZB-0004om-Br for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Jun 2013 18:02:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UjvZA-0004oh-J6 for pgsql-bugs@postgresql.org; Tue, 04 Jun 2013 18:02:40 +0000 Received: from cl-262.qas-01.us.sixxs.net ([2001:4830:1600:105::2] helo=tamriel.snowman.net) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UjvZ2-0006q8-2D for pgsql-bugs@postgresql.org; Tue, 04 Jun 2013 18:02:40 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id AC2CB222E7; Tue, 4 Jun 2013 14:02:29 -0400 (EDT) Date: Tue, 4 Jun 2013 14:02:29 -0400 From: Stephen Frost To: Andres Freund Cc: Heikki Linnakangas , Naoya Anzai , pgsql-bugs@postgresql.org, Akio Iwaasa Subject: Re: Memory-leak in BackgroundWriter(and Checkpointer) Message-ID: <20130604180229.GJ5871@tamriel.snowman.net> References: <20130604122736.GH5871@tamriel.snowman.net> <51ADEA34.4080704@vmware.com> <20130604132758.GC2854@alap2.anarazel.de> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="OX2aLCKeO1apYW07" Content-Disposition: inline In-Reply-To: <20130604132758.GC2854@alap2.anarazel.de> X-Editor: Vim http://www.vim.org/ X-Info: http://www.snowman.net X-Operating-System: Linux/3.2.0-4-amd64 (x86_64) X-Uptime: 13:43:58 up 1 day, 17:22, 4 users, load average: 0.16, 0.11, 0.13 User-Agent: Mutt/1.5.21 (2010-09-15) 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 --OX2aLCKeO1apYW07 Content-Type: multipart/mixed; boundary="ed/6oDxOLijJh8b0" Content-Disposition: inline --ed/6oDxOLijJh8b0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline * Andres Freund (andres@2ndquadrant.com) wrote: > Seems more consistent with the rest of the code too. But anyway, I am > fine with fixing it either way. Patch attached which mirrors what GetRunningTransactionData() (the other function called from LogStandbySnapshot) does, more-or-less- uses a static variable to keep track of memory allocated for the data being returned to the caller. Looks like this will need to be back-patched to 9.0. Barring objections or better ideas, I'll do that in a few hours. Thanks, Stephen --ed/6oDxOLijJh8b0 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="hs_memleak.patch" Content-Transfer-Encoding: quoted-printable colordiff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmg= r/lock.c new file mode 100644 index 8cd871f..1fb7884 *** a/src/backend/storage/lmgr/lock.c --- b/src/backend/storage/lmgr/lock.c *************** GetLockStatusData(void) *** 3401,3415 **** * Returns a list of currently held AccessExclusiveLocks, for use * by GetRunningTransactionData(). */ xl_standby_lock * GetRunningTransactionLocks(int *nlocks) { PROCLOCK *proclock; HASH_SEQ_STATUS seqstat; int i; int index; ! int els; ! xl_standby_lock *accessExclusiveLocks; =20 /* * Acquire lock on the entire shared lock data structure. --- 3401,3445 ---- * Returns a list of currently held AccessExclusiveLocks, for use * by GetRunningTransactionData(). */ + #define INIT_STANDBY_LOCK_ARR_SIZE 32 /* Initial return workspace size */ xl_standby_lock * GetRunningTransactionLocks(int *nlocks) { + /* result workspace */ + static xl_standby_locks *accessExclusiveLocks; +=20 PROCLOCK *proclock; HASH_SEQ_STATUS seqstat; int i; int index; !=20 ! /* ! * Allocating space for all locks is overkill. We only need space for ! * access exclusive locks, but we can't count the number of locks without ! * locking all of the lock partitions. We don't really want to do ! * allocations while holding all those locks, so instead do a bit of ! * allocation up front, to hopefully avoid having to do them later. ! */ ! if (accessExclusiveLocks =3D=3D NULL) ! { ! /* ! * First call ! * ! * Note: We are tracking the allocated space for locks in ->nlocks, ! * not the actual number of locks found, that's handled with index ! * below. Also, allocating the structure will start us off with space ! * for one, so discount that from the malloc request. ! */ ! accessExclusiveLocks =3D (xl_standby_locks *) ! malloc(sizeof(xl_standby_locks) + ! ((INIT_STANDBY_LOCK_ARR_SIZE - 1) * sizeof(xl_standby_lock))); ! if (accessExclusiveLocks =3D=3D NULL) ! ereport(ERROR, ! (errcode(ERRCODE_OUT_OF_MEMORY), ! errmsg("out of memory"))); !=20 ! accessExclusiveLocks->nlocks =3D INIT_STANDBY_LOCK_ARR_SIZE; ! } =20 /* * Acquire lock on the entire shared lock data structure. *************** GetRunningTransactionLocks(int *nlocks) *** 3419,3433 **** for (i =3D 0; i < NUM_LOCK_PARTITIONS; i++) LWLockAcquire(FirstLockMgrLock + i, LW_SHARED); =20 - /* Now we can safely count the number of proclocks */ - els =3D hash_get_num_entries(LockMethodProcLockHash); -=20 - /* - * Allocating enough space for all locks in the lock table is overkill, - * but it's more convenient and faster than having to enlarge the array. - */ - accessExclusiveLocks =3D palloc(els * sizeof(xl_standby_lock)); -=20 /* Now scan the tables to copy the data */ hash_seq_init(&seqstat, LockMethodProcLockHash); =20 --- 3449,3454 ---- *************** GetRunningTransactionLocks(int *nlocks) *** 3459,3467 **** if (!TransactionIdIsValid(xid)) continue; =20 ! accessExclusiveLocks[index].xid =3D xid; ! accessExclusiveLocks[index].dbOid =3D lock->tag.locktag_field1; ! accessExclusiveLocks[index].relOid =3D lock->tag.locktag_field2; =20 index++; } --- 3480,3508 ---- if (!TransactionIdIsValid(xid)) continue; =20 ! /* ! * Check if we need to allocate more space in our workspace due to ! * the number of access exclusive locks actually found. If we do ! * run out, double the size of our return area to hopefully avoid ! * having to do this again any time soon. ! */ ! if (index >=3D accessExclusiveLocks->nlocks) ! { ! accessExclusiveLocks->nlocks *=3D 2; !=20 ! accessExclusiveLocks =3D (xl_standby_locks *) ! realloc(accessExclusiveLocks, ! sizeof(xl_standby_locks) + ! ((accessExclusiveLocks->nlocks - 1) * ! sizeof(xl_standby_lock))); ! if (accessExclusiveLocks =3D=3D NULL) ! ereport(ERROR, ! (errcode(ERRCODE_OUT_OF_MEMORY), ! errmsg("out of memory"))); ! } ! accessExclusiveLocks->locks[index].xid =3D xid; ! accessExclusiveLocks->locks[index].dbOid =3D lock->tag.locktag_field1; ! accessExclusiveLocks->locks[index].relOid =3D lock->tag.locktag_field2; =20 index++; } *************** GetRunningTransactionLocks(int *nlocks) *** 3477,3484 **** for (i =3D NUM_LOCK_PARTITIONS; --i >=3D 0;) LWLockRelease(FirstLockMgrLock + i); =20 *nlocks =3D index; ! return accessExclusiveLocks; } =20 /* Provide the textual name of any lock mode */ --- 3518,3526 ---- for (i =3D NUM_LOCK_PARTITIONS; --i >=3D 0;) LWLockRelease(FirstLockMgrLock + i); =20 + /* Pass back out the number of locks actually found. */ *nlocks =3D index; ! return accessExclusiveLocks->locks; } =20 /* Provide the textual name of any lock mode */ --ed/6oDxOLijJh8b0-- --OX2aLCKeO1apYW07 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iEYEARECAAYFAlGuK7UACgkQrzgMPqB3kih3ewCfS3IjLWOSVhW03jdTjRHP1R7B wdIAn2WsRY5WZS9sHiNyquJcemhIc5un =mJtw -----END PGP SIGNATURE----- --OX2aLCKeO1apYW07--