Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZbrhA-00028O-09 for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Sep 2015 14:58:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1Zbrh9-0003CH-Ht for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Sep 2015 14:58:55 +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 1Zbrh9-0003CB-2B for pgsql-hackers@postgresql.org; Tue, 15 Sep 2015 14:58:55 +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 1Zbrh5-0003xR-Gx; Tue, 15 Sep 2015 14:58:54 +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 t8FEwnBW001936; Tue, 15 Sep 2015 10:58:49 -0400 From: Tom Lane To: Jan Wieck , Kevin Grittner cc: pgsql-hackers@postgresql.org Subject: Re: [COMMITTERS] pgsql: Fix an O(N^2) problem in foreign key references. In-reply-to: <22834.1442325108@sss.pgh.pa.us> References: <10758.1442239007@sss.pgh.pa.us> <55F81C48.4020009@wi3ck.info> <22834.1442325108@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Tue, 15 Sep 2015 09:51:48 -0400" Date: Tue, 15 Sep 2015 10:58:49 -0400 Message-ID: <1935.1442329129@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 I wrote: > Jan Wieck writes: >> I'm able to reproduce that failure with CLOBBER_CACHE_ALWAYS and it >> definitely is caused by this commit. Do you want to back it out for the >> time being? Kevin is on vacation right now. > I'll take a look today, and either fix it if I can find the problem > quickly or back it out. The answer is "back it out", because this patch is fundamentally and irretrievably broken. You can't just clobber the hashtable like that, because there are very possibly active uses of hashtable entries in outer function call levels. In the particular case exhibited in http://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=friarbird&dt=2015-09-15%2004%3A20%3A00 namely TRAP: FailedAssertion("!(riinfo->constraint_id == constraintOid)", File: "/pgbuild/root/HEAD/pgsql.build/../pgsql/src/backend/utils/adt/ri_triggers.c", Line: 2838) what's evidently happened is that a cache flush occurred during the syscache fetch at line 2828, and the patch chose that moment to destroy the hashtable, in particular the entry already created at line 2817. Hashtable resets occurring later in a particular RI trigger's execution would cause other symptoms. The pre-existing coding is safe, despite the lack of any reference counting on hashtable entries, because the cache flush callback never actually destroys any entries. It just cautiously marks them invalid, which won't actually have any effect until the next ri_LoadConstraintInfo() call on that constraint OID. AFAICS, there's no way to make this work reliably without changing the cache API somehow. Reference counts would make it possible to tell whether it's safe to delete a particular entry, but they would be fairly complicated to maintain (because of the need to make sure they get decremented after an elog(ERROR)). Given that the entries are just flat chunks of memory, another answer is to have cache lookups copy the entry data into caller-provided storage rather than rely on the cache to stay valid. Not sure offhand whether that would be unreasonably expensive; sizeof(RI_ConstraintInfo) is about 600 bytes on my machine, which seems like kind of a lot, but I guess it's not enormous. For that matter, if we're gonna do it like that, it's not very clear that we need the ri_constraint_cache at all, rather than just having ri_LoadConstraintInfo fill the data from the pg_constraint syscache row every time. In any case, this patch seems like a pretty ugly and brute-force way to limit the cache size, since it destroys not only old and uninteresting entries but also valid, up-to-date, recently used entries. So I'm not very excited about thinking of some band-aid way to let this work; I don't like the approach of destroying the entire hash table in the first place. So I'm going to revert it and send it back for rework. 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