Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Zcwwg-0007BC-Jn for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Sep 2015 14:47:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1Zcwwg-0005MA-1O for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Sep 2015 14:47:26 +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 1Zcwwf-0005M4-EE for pgsql-hackers@postgresql.org; Fri, 18 Sep 2015 14:47:25 +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 1Zcwwc-0004TN-DR; Fri, 18 Sep 2015 14:47:24 +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 t8IElJG1016321; Fri, 18 Sep 2015 10:47:19 -0400 From: Tom Lane To: Jan Wieck cc: Kevin Grittner , pgsql-hackers@postgresql.org Subject: Re: [COMMITTERS] pgsql: Fix an O(N^2) problem in foreign key references. In-reply-to: <55FB7089.1030702@wi3ck.info> 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> Comments: In-reply-to Jan Wieck message dated "Thu, 17 Sep 2015 22:01:45 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <16308.1442587607.0@sss.pgh.pa.us> Date: Fri, 18 Sep 2015 10:47:19 -0400 Message-ID: <16320.1442587639@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: <16308.1442587607.1@sss.pgh.pa.us> Jan Wieck writes: > Attached is a complete rework of the fix from scratch, based on Tom's > suggestion. > The code now maintains a double linked list as suggested, but only uses > it to mark all currently valid entries as invalid when hashvalue == 0. > If a specific entry is invalidated it performs a hash lookup for maximum > efficiency in that case. That does not work; you're ignoring the possibility of hashvalue collisions, and for that matter not considering that the hash value is not equal to the hash key. I don't think your code is invalidating the right entry at all during a foreign key constraint update, and it certainly cannot do the right thing if there's a hash value collision. Attached is something closer to what I was envisioning; can you do performance testing on it? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fk-cache-performance-fix-v4.patch"; charset="us-ascii" Content-ID: <16308.1442587607.2@sss.pgh.pa.us> Content-Description: fk-cache-performance-fix-v4.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 61edde9..db1787a 100644 *** a/src/backend/utils/adt/ri_triggers.c --- b/src/backend/utils/adt/ri_triggers.c *************** *** 40,45 **** --- 40,46 ---- #include "commands/trigger.h" #include "executor/executor.h" #include "executor/spi.h" + #include "lib/ilist.h" #include "parser/parse_coerce.h" #include "parser/parse_relation.h" #include "miscadmin.h" *************** typedef struct RI_ConstraintInfo *** 125,130 **** --- 126,132 ---- * PK) */ Oid ff_eq_oprs[RI_MAX_NUMKEYS]; /* equality operators (FK =3D * FK) */ + dlist_node valid_link; /* Link in list of valid entries */ } RI_ConstraintInfo; =20=20 =20=20 *************** typedef struct RI_CompareHashEntry *** 185,190 **** --- 187,194 ---- static HTAB *ri_constraint_cache =3D NULL; static HTAB *ri_query_cache =3D NULL; static HTAB *ri_compare_cache =3D NULL; + static dlist_head ri_constraint_cache_valid_list; + static int ri_constraint_cache_valid_count =3D 0; =20=20 =20=20 /* ---------- *************** ri_LoadConstraintInfo(Oid constraintOid) *** 2924,2929 **** --- 2928,2940 ---- =20=20 ReleaseSysCache(tup); =20=20 + /* + * For efficient processing of invalidation messages below, we keep a + * doubly-linked list, and a count, of all currently valid entries. + */ + dlist_push_tail(&ri_constraint_cache_valid_list, &riinfo->valid_link); + ri_constraint_cache_valid_count++; +=20 riinfo->valid =3D true; =20=20 return riinfo; *************** ri_LoadConstraintInfo(Oid constraintOid) *** 2936,2956 **** * gets enough update traffic that it's probably worth being smarter. * Invalidate any ri_constraint_cache entry associated with the syscache * entry with the specified hash value, or all entries if hashvalue =3D= =3D 0. */ static void InvalidateConstraintCacheCallBack(Datum arg, int cacheid, uint32 hashvalu= e) { ! HASH_SEQ_STATUS status; ! RI_ConstraintInfo *hentry; =20=20 Assert(ri_constraint_cache !=3D NULL); =20=20 ! hash_seq_init(&status, ri_constraint_cache); ! while ((hentry =3D (RI_ConstraintInfo *) hash_seq_search(&status)) !=3D = NULL) { ! if (hentry->valid && ! (hashvalue =3D=3D 0 || hentry->oidHashValue =3D=3D hashvalue)) ! hentry->valid =3D false; } } =20=20 --- 2947,2987 ---- * gets enough update traffic that it's probably worth being smarter. * Invalidate any ri_constraint_cache entry associated with the syscache * entry with the specified hash value, or all entries if hashvalue =3D= =3D 0. + * + * 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 + * uses. (Any query using an entry should hold a lock sufficient to keep= that + * data from changing under it --- but we may get cache flushes anyway.) */ static void InvalidateConstraintCacheCallBack(Datum arg, int cacheid, uint32 hashvalu= e) { ! dlist_mutable_iter iter; =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 ! if (hashvalue =3D=3D 0 || riinfo->oidHashValue =3D=3D hashvalue) ! { ! riinfo->valid =3D false; ! /* Remove invalidated entries from the list, too */ ! dlist_delete(iter.cur); ! ri_constraint_cache_valid_count--; ! } } } =20=20 ------- =_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--