Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pBOPJ-0006rB-45 for pgsql-hackers@arkaria.postgresql.org; Fri, 30 Dec 2022 23:03:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pBOPG-0004E8-Nz for pgsql-hackers@arkaria.postgresql.org; Fri, 30 Dec 2022 23:03:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pBOPF-0004Dw-Ny for pgsql-hackers@lists.postgresql.org; Fri, 30 Dec 2022 23:03:18 +0000 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pBOPA-0002Qr-SG for pgsql-hackers@lists.postgresql.org; Fri, 30 Dec 2022 23:03:14 +0000 Received: by mail-wr1-x42b.google.com with SMTP id n3so20261862wrc.5 for ; Fri, 30 Dec 2022 15:03:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=/ceu+Q+RYAfBBf6Nsea9SvzvqXWb/0tRQh/sNy3kD1U=; b=SfTMPqEhZI7VqLn0bHfd+txBqUR1o6Z5x8D4gGKkuwS7d4X3YaDZ4Tx5DueoQk6SkB Dn9+ah2FGRjwERWsuBysy8ltfYR+x/XqAS+zBymDyvh2Mj3RRstXhJrrRY4/4pTVKv8o Few/lMfwHXzjXVtN2RhIGK9mbPpnvZu85O+bCaMkwf9m3vCIfkJKQpmYk37aR5XM7DU9 Pwcwfepvj6lA3uWz06Ne22uBWuWthHZloRA7nqlTBpJsLU287dY/Ze9vsK5kTDUoApiM LRumC/CB/7ucK45EvNvRsk6GFuUtHwmRtP9E75hTNuwZT7+liMo34LKwqkGAZXCAqqbL ioJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/ceu+Q+RYAfBBf6Nsea9SvzvqXWb/0tRQh/sNy3kD1U=; b=6d7M2TDbE/YQfuEvEQhH25IavlQU0EtiGlOv/6S4GK9ErV8QDtGmxunnTg2QjKP4Uw yyO94r7SwRKaLIcDwC9tQ50KpDgQs0RyD7RzkdaJ2SQJc0DVhTyIfM2pCGJFkFg4WNJa qyGH9FRe+BmY6+873RhXUaqEDPpGanXekJ+Sp45HdQKTftveqQeC1X43bCUw4IFhvsWE gFR4JMtuLLD5Ml1biN2da2jgh3+61JQK7/C4Ahpk7QqONr5CoCvsrEMWkMZYkmSwNOF5 2OLQmlR10c45T4QsKMmVTMSadZUmjVEcH7pW+tqZiEy/FzokwKg4VhqdZY3zqQPkBgxW YLyQ== X-Gm-Message-State: AFqh2koVU522ghZo0KoQtQtQMMS9wvEPcLmmMwFvpfM/FuzlZgJMbTYO erlobcVJJWu/TETNMaqClBTKMSH4LHuUy2CuP6KWB2UHkEpP/bBu6IkZWa2At6gTUj71TE51LeK mIX19dcAIhP9K+u3UkE+4wM84nHHBosmNmNd2aTPhpsKNN47lnhWwjsXdA/tNV0bSdPk3qsqC1A ipa8UYohsVmRVjEBPePQr/X1Q5dmc3QEvvcfFuS8YIdDDlD1X1fwFPVI5znmyhxpoNcsnzcknqO M3B/W2sXbK2ujstKfXQfBkv6+mzBfjwXtEW0+5inlGx9zj6NblGAIH6Hhl7 X-Google-Smtp-Source: AMrXdXuv77E26eqIDIrlls+b6qLcNV0/bKHwtjVmBil6q+y1jNXN7deYoJggjjMrpUra2HS5MQ3zMw== X-Received: by 2002:a5d:5d8a:0:b0:274:fae4:a518 with SMTP id ci10-20020a5d5d8a000000b00274fae4a518mr17921403wrb.11.1672441390409; Fri, 30 Dec 2022 15:03:10 -0800 (PST) Received: from [10.137.0.17] (ip-86-49-228-162.bb.vodafone.cz. [86.49.228.162]) by smtp.gmail.com with ESMTPSA id h6-20020a056000000600b002423dc3b1a9sm20613460wrx.52.2022.12.30.15.03.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 30 Dec 2022 15:03:09 -0800 (PST) Message-ID: <6da357b0-fdd3-095d-a06c-87666f4a5a8c@enterprisedb.com> Date: Sat, 31 Dec 2022 00:03:08 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: Re: postgres_fdw: using TABLESAMPLE to collect remote sample Content-Language: en-US To: Tom Lane , James Finnerty Cc: pgsql-hackers@lists.postgresql.org, Tomas Vondra References: <151e835f-55d6-ddbc-b5b4-07ee606aba44@enterprisedb.com> <03182011-10a4-97b4-7c70-d99aa1c11eed@enterprisedb.com> <167112207543.1120.12195641719862911275.pgcf@coridan.postgresql.org> <3377008.1671122816@sss.pgh.pa.us> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Pushed. After thinking about it a bit more I decided to rip out the 10% sampling rate inflation. While stale reltuples may be an issue, but I couldn't convince myself this would be an improvement overall, or that this is the right place to deal with it. Firstly, if reltuples is too low (i.e. when the table grew), everything is "fine" - we sample more than targrows and then remove some of that locally. We end up with same sample size as without this patch - which is not necessarily the right sample size (more about that in a minute), but if it was good enough for now ... improving this bit was not the ambition of this patch. If reltuples is too high (e.g. right after bulk DELETE) we may end up sampling too few rows. If it's 1/2 what it should be, we'll end up sampling only 15k rows instead of 30k rows. But it's unclear how much should we adjust the value - 10% as was in the patch (kinda based on autovacuum_analyze_scale_factor being 10%)? I'd argue that's too low to make any difference - cases that are only ~10% off should work fine (we don't have perfect stats anyway). And for the really bad cases 10% is not going to make a difference. And if the reltuples is that off, it'll probably affect even queries planned on the remote node (directly or pushed down), so I guess the right fix should be making sure reltuples is not actually off - make autovacuum more aggressive or whatever. A thing that bothers me is that calculating sample size for partitioned tables is a bit cyclic - we look at number of blocks, and divide the whole sample based on that. And that's generally correlated with the reltuples, but OTOH it may also be wrong - and not necessarily in the same way. For example you might delete 90% of a table, which will make the table overrepresented in the sample. But then reltuples may be quite accurate thanks to recent vacuum - so fixing this by blindly adjusting the sampling rate seems misguided. If this turns out to be an issue in practice, we need to rethink acquire_inherited_sample_rows as a whole (not just for foreign tables). regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company