Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Zcl1D-0000Nu-Kw for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Sep 2015 02:03:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1Zcl1C-0003DN-Qq for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Sep 2015 02:03:18 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1Zckzu-0001Jh-3r for pgsql-hackers@postgresql.org; Fri, 18 Sep 2015 02:01:58 +0000 Received: from mail-qg0-f44.google.com ([209.85.192.44]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1Zckzm-0007T2-9y for pgsql-hackers@postgresql.org; Fri, 18 Sep 2015 02:01:56 +0000 Received: by qgev79 with SMTP id v79so28454598qge.0 for ; Thu, 17 Sep 2015 19:01:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-type; bh=f6Oesz6X6IccWKC4j7GOLYcAQvmtd/fsPk6DY7A5uig=; b=GVX/R335w9bWTvEUI3E87Y+ZPFXzi4iH6OutsOLrI74Jp0QgE5ZSnA0YU61bxky9r7 vtEHAbfJ+blaneOHMFyv6Cw2nuG3a7OgvfTsPG9wU5DytiWfDBrYCoCADDOWuLTvIYRn tI1Urn8e/TEgevI1vUR60jBoUX4hQjIpP7YoYtXya25HwLtndafpGe7WIHcQoyCAWzGJ kc459ABqWTF9WUTbCYfUcS3CZCsX6XmhpQ828abiD8tGAfbvRNWsVVU91WYPfoMksqgT 9PUnO7bEbzc3GUiBSLoikoLzqzlSZpZTSrq86aW92b4KEYHDftGp4i5NAUk5CBplqVvm 1QYQ== X-Gm-Message-State: ALoCoQk6dHCcAzvjZBx6BMH+OZVPp/1ZzBRZRQUuz8icaRR2LUfW4rVAgasRui1Y38mpPWmbbb9z X-Received: by 10.140.128.81 with SMTP id 78mr4005278qha.62.1442541706728; Thu, 17 Sep 2015 19:01:46 -0700 (PDT) Received: from jupiter.onmars.janwieck.no-ip.info (pool-173-49-130-135.phlapa.fios.verizon.net. [173.49.130.135]) by smtp.googlemail.com with ESMTPSA id 38sm2583948qgh.11.2015.09.17.19.01.45 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Sep 2015 19:01:46 -0700 (PDT) Message-ID: <55FB7089.1030702@wi3ck.info> Date: Thu, 17 Sep 2015 22:01:45 -0400 From: Jan Wieck User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.8.0 MIME-Version: 1.0 To: Tom Lane CC: Kevin Grittner , pgsql-hackers@postgresql.org Subject: Re: [COMMITTERS] pgsql: Fix an O(N^2) problem in foreign key references. 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> In-Reply-To: <55F84112.8020307@wi3ck.info> Content-Type: multipart/mixed; boundary="------------010703010507070903090603" X-Pg-Spam-Score: -2.6 (--) 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 This is a multi-part message in MIME format. --------------010703010507070903090603 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit On 09/15/2015 12:02 PM, Jan Wieck wrote: > On 09/15/2015 11:54 AM, Tom Lane wrote: >> Jan Wieck writes: >>> Would it be appropriate to use (Un)RegisterXactCallback() in case of >>> detecting excessive sequential scanning of that cache and remove all >>> invalid entries from it at that time? > >> Another idea is that it's not the size of the cache that's the problem, >> it's the cost of finding the entries that need to be invalidated. >> So we could also consider adding list links that chain only the entries >> that are currently marked valid. If the list gets too long, we could mark >> them all invalid and thereby reset the list to nil. This doesn't do >> anything for the cache's space consumption, but that wasn't what you were >> worried about anyway was it? > > That sounds like a workable solution to this edge case. I'll see how > that works. 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. This code does pass the regression tests with CLOBBER_CACHE_ALWAYS, does have the desired performance impact on restore of hundreds of thousands of foreign key constraints and does not show any negative impact on bulk loading of data with foreign keys. Regards, Jan -- Jan Wieck Senior Software Engineer http://slony.info --------------010703010507070903090603 Content-Type: text/x-patch; name="pg96_ri_constraint_cache_flush_v3.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="pg96_ri_constraint_cache_flush_v3.patch" diff --git a/src/backend/utils/adt/ri_triggers.c b/src/backend/utils/adt/ri_triggers.c index 61edde9..eaec737 100644 --- a/src/backend/utils/adt/ri_triggers.c +++ b/src/backend/utils/adt/ri_triggers.c @@ -125,6 +125,8 @@ typedef struct RI_ConstraintInfo * PK) */ Oid ff_eq_oprs[RI_MAX_NUMKEYS]; /* equality operators (FK = * FK) */ + struct RI_ConstraintInfo *prev_valid; /* Previous valid entry */ + struct RI_ConstraintInfo *next_valid; /* Next valid entry */ } RI_ConstraintInfo; @@ -185,6 +187,7 @@ typedef struct RI_CompareHashEntry static HTAB *ri_constraint_cache = NULL; static HTAB *ri_query_cache = NULL; static HTAB *ri_compare_cache = NULL; +static RI_ConstraintInfo *ri_constraint_cache_valid_list = NULL; /* ---------- @@ -2924,6 +2927,19 @@ ri_LoadConstraintInfo(Oid constraintOid) ReleaseSysCache(tup); + /* + * 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. For efficient processing + * of invalidation messages below we keep a double linked list of + * currently valid entries. + */ + if (ri_constraint_cache_valid_list != NULL) + ri_constraint_cache_valid_list->prev_valid = riinfo; + riinfo->prev_valid = NULL; + riinfo->next_valid = ri_constraint_cache_valid_list; + ri_constraint_cache_valid_list = riinfo; + riinfo->valid = true; return riinfo; @@ -2940,17 +2956,55 @@ ri_LoadConstraintInfo(Oid constraintOid) static void InvalidateConstraintCacheCallBack(Datum arg, int cacheid, uint32 hashvalue) { - HASH_SEQ_STATUS status; - RI_ConstraintInfo *hentry; + RI_ConstraintInfo *hentry; + RI_ConstraintInfo *hnext; + bool found; Assert(ri_constraint_cache != NULL); - hash_seq_init(&status, ri_constraint_cache); - while ((hentry = (RI_ConstraintInfo *) hash_seq_search(&status)) != NULL) + if (hashvalue == 0) { - if (hentry->valid && - (hashvalue == 0 || hentry->oidHashValue == hashvalue)) + /* + * Set all valid entries in the cache to invalid and clear the + * list of valid entries. + */ + hnext = ri_constraint_cache_valid_list; + while (hnext) + { + hentry = hnext; + hnext = hnext->next_valid; + + Assert(hentry->valid); + + hentry->prev_valid = NULL; + hentry->next_valid = NULL; hentry->valid = false; + } + ri_constraint_cache_valid_list = NULL; + } + else + { + /* + * Search for the specified entry and set that one invalid + * and remove it from the list of valid entries. + */ + hentry = (RI_ConstraintInfo *) hash_search(ri_constraint_cache, + (void *) &hashvalue, + HASH_FIND, &found); + if (found && hentry->valid) + { + Assert(hentry->oidHashValue == hashvalue); + + if (hentry->prev_valid != NULL) + hentry->prev_valid->next_valid = hentry->next_valid; + else + ri_constraint_cache_valid_list = hentry->next_valid; + if (hentry->next_valid != NULL) + hentry->next_valid->prev_valid = hentry->prev_valid; + hentry->prev_valid = NULL; + hentry->next_valid = NULL; + hentry->valid = false; + } } } --------------010703010507070903090603 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 --------------010703010507070903090603--