pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Jan Wieck <jan@wi3ck.info>
Cc: Kevin Grittner <kgrittn@postgresql.org>
Cc: pgsql-hackers@postgresql.org
Subject: Re: [COMMITTERS] pgsql: Fix an O(N^2) problem in foreign key references.
Date: Fri, 18 Sep 2015 10:47:19 -0400
Message-ID: <16320.1442587639@sss.pgh.pa.us> (raw)
In-Reply-To: <55FB7089.1030702@wi3ck.info>
References: <E1ZaSzE-0007q3-2V@gemulon.postgresql.org>
	<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>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

Jan Wieck <jan@wi3ck.info> 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

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Attachments:

fk-cache-performance-fix-v4.patch (text/x-diff, 3.8K) download 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 = * FK) */ + dlist_node valid_link; /* Link in list of valid entries */ } RI_ConstraintInfo; *************** typedef struct RI_CompareHashEntry *** 185,190 **** --- 187,194 ---- static HTAB *ri_constraint_cache = NULL; static HTAB *ri_query_cache = NULL; static HTAB *ri_compare_cache = NULL; + static dlist_head ri_constraint_cache_valid_list; + static int ri_constraint_cache_valid_count = 0; /* ---------- *************** ri_LoadConstraintInfo(Oid constraintOid) *** 2924,2929 **** --- 2928,2940 ---- ReleaseSysCache(tup); + /* + * 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++; + riinfo->valid = true; 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 == 0. */ static void InvalidateConstraintCacheCallBack(Datum arg, int cacheid, uint32 hashvalue) { ! HASH_SEQ_STATUS status; ! RI_ConstraintInfo *hentry; Assert(ri_constraint_cache != NULL); ! hash_seq_init(&status, ri_constraint_cache); ! while ((hentry = (RI_ConstraintInfo *) hash_seq_search(&status)) != NULL) { ! if (hentry->valid && ! (hashvalue == 0 || hentry->oidHashValue == hashvalue)) ! hentry->valid = false; } } --- 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 == 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 entries + * 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 hashvalue) { ! dlist_mutable_iter iter; Assert(ri_constraint_cache != NULL); ! /* ! * 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 keys ! * and also does many ALTER TABLEs, such as a restore from pg_dump. ! */ ! if (ri_constraint_cache_valid_count > 1000) ! hashvalue = 0; /* pretend it's a cache reset */ ! ! dlist_foreach_modify(iter, &ri_constraint_cache_valid_list) { ! RI_ConstraintInfo *riinfo = dlist_container(RI_ConstraintInfo, ! valid_link, iter.cur); ! ! if (hashvalue == 0 || riinfo->oidHashValue == hashvalue) ! { ! riinfo->valid = false; ! /* Remove invalidated entries from the list, too */ ! dlist_delete(iter.cur); ! ri_constraint_cache_valid_count--; ! } } }

view thread (19+ messages)  latest in thread

Message-ID: <16320.1442587639@sss.pgh.pa.us>
Permalink:  ../16320.1442587639@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/16320.1442587639@sss.pgh.pa.us

 ·  · 

reply

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-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, jan@wi3ck.info, kgrittn@postgresql.org, pgsql-hackers@postgresql.org
  Subject: Re: [COMMITTERS] pgsql: Fix an O(N^2) problem in foreign key references.
  In-Reply-To: <16320.1442587639@sss.pgh.pa.us>
  References: <E1ZaSzE-0007q3-2V@gemulon.postgresql.org> <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>

* 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