Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gtbwX-0003GO-DT for pgsql-hackers@arkaria.postgresql.org; Tue, 12 Feb 2019 17:34:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gtbwU-0000a7-Em for pgsql-hackers@arkaria.postgresql.org; Tue, 12 Feb 2019 17:33:58 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1gtbwU-0000Wu-0p for pgsql-hackers@lists.postgresql.org; Tue, 12 Feb 2019 17:33:58 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gtbwN-0000fT-02 for pgsql-hackers@lists.postgresql.org; Tue, 12 Feb 2019 17:33:57 +0000 Received: by mail-wm1-x344.google.com with SMTP id q21so3924722wmc.5 for ; Tue, 12 Feb 2019 09:33:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=Cn9GeKkrIWcQJwNtrt3SnQleO5/DtoXsSnStZVpys5k=; b=iBmppBCNthvCq5DC8UP5Un/riolz16SihKJb4c12b7Gx6I2TMNF0+Hhiy5AwWI5awk +4ExhSQCnXmOGrcwLaBSoE0oME63teT1kHtyMQKabsd70cpgIliAfZNMkWAXvhC9qyIH B+/vfvtSX68sxIH5tBZZpgKnHYbldbtVsSEB16fSnTBdWfg2AC2dUk03lajZXeESuB9q orVJJn9t9TWJ1UOCrya7LWIvlJAI/xjevi6Qcq0f1c1Fx9xE4ubJA6NNBhhzbd0JN5bf eWHDe3wPrBlpLGkOCB4fFkC6wZMrA4MBV+2EwNnbYL5943hAxSjZ9oVUgjrXaAFyZUVi ZIsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=Cn9GeKkrIWcQJwNtrt3SnQleO5/DtoXsSnStZVpys5k=; b=VJ62Or4I1555esi7xJhweMe0SOGUGklRl4x80Vrq5u3Ry3xd/Z2lJapCQjKMB4kb3g bDc6qZkeeAH3dWl2h02t9NyjaS4uA/DOrpJIXjMZzimD12h//9bR5Ch/A52U07lM5iU+ iu/B1oIlHdc9CIvGCDdS9lzrnXq5AJEuHCrQcAy2d00zwvpwxDqKywXOfqvXef7EmVNY +i/xMWr+kH9O762JKGAaJ7svTORPLLFDIVloorYgmvL8mBx/Vk3r+3Thlw3ycf7V5nF5 B5nAqkD9uPvOR7DV5IYsp69JeZMxGKSMuIggUZWpfTpOJkovIiETbmhhy+dR6tMZH4tw O0YA== X-Gm-Message-State: AHQUAubHuRE9z8pKHi/dVSFqSMXy+f64u/zzhyAVauE6uAS7obCkABuz kF7g1TUPAdCz9g3okgQK7kfIPw== X-Google-Smtp-Source: AHgI3Ib9kQEWi0AWExyPRgcX33DouX0ydph9BNN9gFB1B3/n5R/oMR4QLqHfDwZ3gOBfJZzsbZB+ug== X-Received: by 2002:a1c:b10a:: with SMTP id a10mr3708526wmf.148.1549992829668; Tue, 12 Feb 2019 09:33:49 -0800 (PST) Received: from [10.137.2.21] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id x18sm10402531wrw.61.2019.02.12.09.33.48 (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Tue, 12 Feb 2019 09:33:48 -0800 (PST) Subject: Re: Protect syscache from bloating with negative cache entries To: Kyotaro HORIGUCHI Cc: alvherre@2ndquadrant.com, bruce@momjian.us, andres@anarazel.de, robertmhaas@gmail.com, tgl@sss.pgh.pa.us, ideriha.takeshi@jp.fujitsu.com, pgsql-hackers@lists.postgresql.org, tsunakawa.takay@jp.fujitsu.com, michael.paquier@gmail.com, david@pgmasters.net, craig@2ndquadrant.com References: <20190207.152418.139132570.horiguchi.kyotaro@lab.ntt.co.jp> <20190207.211845.263152487.horiguchi.kyotaro@lab.ntt.co.jp> <74386116-0bc5-84f2-e614-0cff19aca2de@2ndquadrant.com> <20190212.203510.100151050.horiguchi.kyotaro@lab.ntt.co.jp> From: Tomas Vondra Message-ID: Date: Tue, 12 Feb 2019 18:33:46 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <20190212.203510.100151050.horiguchi.kyotaro@lab.ntt.co.jp> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2/12/19 12:35 PM, Kyotaro HORIGUCHI wrote: > Thank you for testing and the commits, Tomas. > > At Sat, 9 Feb 2019 19:09:59 +0100, Tomas Vondra wrote in <74386116-0bc5-84f2-e614-0cff19aca2de@2ndquadrant.com> >> On 2/7/19 1:18 PM, Kyotaro HORIGUCHI wrote: >>> At Thu, 07 Feb 2019 15:24:18 +0900 (Tokyo Standard Time), Kyotaro HORIGUCHI wrote in <20190207.152418.139132570.horiguchi.kyotaro@lab.ntt.co.jp> >> I've done a bunch of benchmarks on v13, and I don't see any serious >> regression either. Each test creates a number of tables (100, 1k, 10k, >> 100k and 1M) and then runs SELECT queries on them. The tables are >> accessed randomly - with either uniform or exponential distribution. For >> each combination there are 5 runs, 60 seconds each (see the attached >> shell scripts, it should be pretty obvious). >> >> I've done the tests on two different machines - small one (i5 with 8GB >> of RAM) and large one (e5-2620v4 with 64GB RAM), but the behavior is >> almost exactly the same (with the exception of 1M tables, which does not >> fit into RAM on the smaller one). >> >> On the xeon, the results (throughput compared to master) look like this: >> >> >> uniform 100 1000 10000 100000 1000000 >> ------------------------------------------------------------ >> v13 105.04% 100.28% 102.96% 102.11% 101.54% >> v13 (nodata) 97.05% 98.30% 97.42% 96.60% 107.55% >> >> >> exponential 100 1000 10000 100000 1000000 >> ------------------------------------------------------------ >> v13 100.04% 103.48% 101.70% 98.56% 103.20% >> v13 (nodata) 97.12% 98.43% 98.86% 98.48% 104.94% >> >> The "nodata" case means the tables were empty (so no files created), >> while in the other case each table contained 1 row. >> >> Per the results it's mostly break even, and in some cases there is >> actually a measurable improvement. > > Great! I guess it comes from reduced size of hash? > Not sure about that. I haven't actually verified that it reduces the cache size at all - I was measuring the overhead of the extra work. And I don't think the syscache actually shrunk significantly, because the throughput was quite high (~15-30k tps, IIRC) so pretty much everything was touched within the default 600 seconds. >> That being said, the question is whether the patch actually reduces >> memory usage in a useful way - that's not something this benchmark >> validates. I plan to modify the tests to make pgbench script >> time-dependent (i.e. to pick a subset of tables depending on time). > > Thank you. > >> A couple of things I've happened to notice during a quick review: >> >> 1) The sgml docs in 0002 talk about "syscache_memory_target" and >> "syscache_prune_min_age", but those options were renamed to just >> "cache_memory_target" and "cache_prune_min_age". > > I'm at a loss how call syscache for users. I think it is "catalog > cache". The most basic component is called catcache, which is > covered by the syscache layer, both of then are not revealed to > users, and it is shown to user as "catalog cache". > > "catalog_cache_prune_min_age", "catalog_cache_memory_target", (if > exists) "catalog_cache_entry_limit" and > "catalog_cache_prune_ratio" make sense? > I think "catalog_cache" sounds about right, although my point was simply that there's a discrepancy between sgml docs and code. >> 2) "cache_entry_limit" is not mentioned in sgml docs at all, and it's >> defined three times in guc.c for some reason. > > It is just PoC, added to show how it looks. (The multiple > instances must bex a result of a convulsion of my fingers..) I > think this is not useful unless it can be specfied per-relation > or per-cache basis. I'll remove the GUC and add reloptions for > the purpose. (But it won't work for pg_class and pg_attribute > for now). > OK, although I'd just keep it as simple as possible. TBH I can't really imagine users tuning limits for individual caches in any meaningful way. >> 3) I don't see why to define PRUNE_BY_AGE and PRUNE_BY_NUMBER, instead >> of just using two bool variables prune_by_age and prune_by_number doing >> the same thing. > > Agreed. It's a kind of memory-stingy, which is useless there. > >> 4) I'm not entirely sure about using stmtStartTimestamp. Doesn't that >> pretty much mean long-running statements will set the lastaccess to very >> old timestamp? Also, it means that long-running statements (like a PL >> function accessing a bunch of tables) won't do any eviction at all, no? >> AFAICS we'll set the timestamp only once, at the very beginning. >> >> I wonder whether using some other timestamp source (like a timestamp >> updated regularly from a timer, or something like that). > > I didin't consider planning that happen within a function. If > 5min is the default for catalog_cache_prune_min_age, 10% of it > (30s) seems enough and gettieofday() with such intervals wouldn't > affect forground jobs. I'd choose catalog_c_p_m_age/10 rather > than fixed value 30s and 1s as the minimal. > Actually, I see CatCacheCleanupOldEntries contains this comment: /* * Calculate the duration from the time of the last access to the * "current" time. Since catcacheclock is not advanced within a * transaction, the entries that are accessed within the current * transaction won't be pruned. */ which I think is pretty much what I've been saying ... But the question is whether we need to do something about it. > I obeserved significant degradation by setting up timer at every > statement start. The patch is doing the followings to get rid of > the degradation. > > (1) Every statement updates the catcache timestamp as currently > does. (SetCatCacheClock) > > (2) The timestamp is also updated periodically using timer > separately from (1). The timer starts if not yet at the time > of (1). (SetCatCacheClock, UpdateCatCacheClock) > > (3) Statement end and transaction end don't stop the timer, to > avoid overhead of setting up a timer. ( > > (4) But it stops by error. I choosed not to change the thing in > PostgresMain that it kills all timers on error. > > (5) Also changing the GUC catalog_cache_prune_min_age kills the > timer, in order to reflect the change quickly especially when > it is shortened. > Interesting. What was the frequency of the timer / how often was it executed? Can you share the code somehow? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services