Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZfXPc-0005zW-Vu for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Sep 2015 18:08:01 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZfXPb-0002bo-NN for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Sep 2015 18:07:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZfXPb-0002ZJ-2W for pgsql-hackers@postgresql.org; Fri, 25 Sep 2015 18:07:59 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZfXPY-0004lT-8M; Fri, 25 Sep 2015 18:07:58 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id t8PI7oPW016224; Fri, 25 Sep 2015 14:07:50 -0400 From: Tom Lane To: Alvaro Herrera cc: Jan Wieck , Kevin Grittner , pgsql-hackers@postgresql.org Subject: Re: [COMMITTERS] pgsql: Fix an O(N^2) problem in foreign key references. In-reply-to: <14681.1443202990@sss.pgh.pa.us> References: <10758.1442239007@sss.pgh.pa.us> <55F81C48.4020009@wi3ck.info> <22834.1442325108@sss.pgh.pa.us> <1935.1442329129@sss.pgh.pa.us> <55F83ABF.2080609@wi3ck.info> <4716.1442332474@sss.pgh.pa.us> <55F84112.8020307@wi3ck.info> <55FB7089.1030702@wi3ck.info> <16320.1442587639@sss.pgh.pa.us> <20150925173254.GK295765@alvherre.pgsql> <14681.1443202990@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Fri, 25 Sep 2015 13:43:10 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <16195.1443204432.0@sss.pgh.pa.us> Date: Fri, 25 Sep 2015 14:07:50 -0400 Message-ID: <16223.1443204470@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <16195.1443204432.1@sss.pgh.pa.us> I wrote: > Alvaro Herrera writes: >> Would it make sense to remove the only the few oldest entries, instead >> of all of them? As is, I think this causes a storm of reloads every >> once in a while, if the number of FKs in the system is large enough. >> Maybe on a cache hit we could push the entry to the head of the list, >> and then remove N entries from the back of the list when the threshold >> is reached. > (FWIW, it might take less code to put the recently-used entries at the > back of the list. Then the loop in InvalidateConstraintCacheCallBack > could just invalidate/delete entries if either they're targets, or > the current list length exceeds the threshold.) Specifically, we could change it as per attached. But this adds some cycles to the mainline usage of fetching a valid cache entry, so I'd want to see some evidence that there are use-cases it helps before applying it. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="kill-only-oldest-FK-cache-entries.patch"; charset="us-ascii" Content-ID: <16195.1443204432.2@sss.pgh.pa.us> Content-Description: kill-only-oldest-FK-cache-entries.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/ri_triggers.c b/src/backend/utils/adt/ri= _triggers.c index 018cb99..54376ed 100644 *** a/src/backend/utils/adt/ri_triggers.c --- b/src/backend/utils/adt/ri_triggers.c *************** ri_LoadConstraintInfo(Oid constraintOid) *** 2814,2820 **** ri_InitHashTables(); =20=20 /* ! * Find or create a hash entry. If we find a valid one, just return it. */ riinfo =3D (RI_ConstraintInfo *) hash_search(ri_constraint_cache, (void *) &constraintOid, --- 2814,2821 ---- ri_InitHashTables(); =20=20 /* ! * Find or create a hash entry. If we find a valid one, just return it, ! * after pushing it to the end of the list to mark it recently used. */ riinfo =3D (RI_ConstraintInfo *) hash_search(ri_constraint_cache, (void *) &constraintOid, *************** ri_LoadConstraintInfo(Oid constraintOid) *** 2822,2828 **** --- 2823,2833 ---- if (!found) riinfo->valid =3D false; else if (riinfo->valid) + { + dlist_delete(&riinfo->valid_link); + dlist_push_tail(&ri_constraint_cache_valid_list, &riinfo->valid_link); return riinfo; + } =20=20 /* * Fetch the pg_constraint row so we can fill in the entry. *************** ri_LoadConstraintInfo(Oid constraintOid) *** 2931,2936 **** --- 2936,2942 ---- /* * For efficient processing of invalidation messages below, we keep a * doubly-linked list, and a count, of all currently valid entries. + * The most recently used entries are at the *end* of the list. */ dlist_push_tail(&ri_constraint_cache_valid_list, &riinfo->valid_link); ri_constraint_cache_valid_count++; *************** ri_LoadConstraintInfo(Oid constraintOid) *** 2948,2953 **** --- 2954,2965 ---- * Invalidate any ri_constraint_cache entry associated with the syscache * entry with the specified hash value, or all entries if hashvalue =3D= =3D 0. * + * We also invalidate entries if there are too many valid entries. This = rule + * avoids O(N^2) behavior in situations where a session touches many fore= ign + * keys and also does many ALTER TABLEs, such as a restore from pg_dump. = When + * we do kill entries for that reason, they'll be the least recently used + * ones, since they're at the front of the list. + * * Note: at the time a cache invalidation message is processed there may = be * active references to the cache. Because of this we never remove entri= es * from the cache, but only mark them invalid, which is harmless to active *************** InvalidateConstraintCacheCallBack(Datum=20 *** 2961,2981 **** =20=20 Assert(ri_constraint_cache !=3D NULL); =20=20 - /* - * If the list of currently valid entries gets excessively large, we mark - * them all invalid so we can empty the list. This arrangement avoids - * O(N^2) behavior in situations where a session touches many foreign ke= ys - * and also does many ALTER TABLEs, such as a restore from pg_dump. - */ - if (ri_constraint_cache_valid_count > 1000) - hashvalue =3D 0; /* pretend it's a cache reset */ -=20 dlist_foreach_modify(iter, &ri_constraint_cache_valid_list) { RI_ConstraintInfo *riinfo =3D dlist_container(RI_ConstraintInfo, valid_link, iter.cur); =20=20 ! if (hashvalue =3D=3D 0 || riinfo->oidHashValue =3D=3D hashvalue) { riinfo->valid =3D false; /* Remove invalidated entries from the list, too */ --- 2973,2985 ---- =20=20 Assert(ri_constraint_cache !=3D NULL); =20=20 dlist_foreach_modify(iter, &ri_constraint_cache_valid_list) { RI_ConstraintInfo *riinfo =3D dlist_container(RI_ConstraintInfo, valid_link, iter.cur); =20=20 ! if (hashvalue =3D=3D 0 || riinfo->oidHashValue =3D=3D hashvalue || ! ri_constraint_cache_valid_count > 1000) { riinfo->valid =3D false; /* Remove invalidated entries from the list, too */ ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers ------- =_aaaaaaaaaa0--