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 1pE9rj-0005JK-Jp for pgsql-hackers@arkaria.postgresql.org; Sat, 07 Jan 2023 14:08:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pE9rh-0006Fl-MK for pgsql-hackers@arkaria.postgresql.org; Sat, 07 Jan 2023 14:08:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pE9rg-0006Fc-SL for pgsql-hackers@lists.postgresql.org; Sat, 07 Jan 2023 14:08:05 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pE9rd-0001Em-8Q for pgsql-hackers@lists.postgresql.org; Sat, 07 Jan 2023 14:08:04 +0000 Received: by mail-ej1-x636.google.com with SMTP id ss4so2217601ejb.11 for ; Sat, 07 Jan 2023 06:08:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=content-transfer-encoding:in-reply-to:references:cc:to:from :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=bHsj/qbIScF2HhN/LGLvp8lr44o9VSCbIdHa3u1D4ds=; b=JzHzCLUdiSt1eD6GzT6j2cOwtppkZ/gtLSiabugsnrSq9NduZqt00JDctCNpMma2nz wh/iReP9/UvrPrkSI24g8HatnlZNcNZkvzNlot1YYJZIou2CHyA5NLBJmUkyYewlZIA2 StGNxMwKR4gNgvJ7QaLKx90axc9zxd8vPOAaUxdjiY8Ve1+MuaV8tLP1A/HofeH3UV3C 2W27XMBiU+9gE4dI43D+s6EjLLf8tO/eo0TpnCvFw9MfeZK8xELeDOIFNRG1bkWiPUZe /ZTI2BS3bWPmQ8X8odm8gRGJgBex6GSHFR0OTMbeFH5SW7ngw5RQDu3QykAua2I6bro6 8flA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:references:cc:to:from :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bHsj/qbIScF2HhN/LGLvp8lr44o9VSCbIdHa3u1D4ds=; b=uPHhTgJtuc5qhZjwr2RL9EGL9GOWvvwx+jMprS4h3SNvbNGsBwLV5CbqR8bHUP9AyA SaZbEXnHqKhfx5H7voy5BRT0u7B4KRscKW0AStPXubM1CYlRVbxQtA9jTyVAKV5k+hSW nHawI1SnASOXXarxD+dGH6Rit9RWgCy5aCRUjyH7YhMW3VaV62eC7indir5GpiYAec7x VMv36/Id3388COqxit/MLvmoq4bdPn3UQo+XmA70Su0OOeMqW8N7xzJKgIDOe7RPZVbL e7PSqV5CSXSjiKLkRn9c49ofgSxIN7jgU7kLak8/bCZ3OkjqWBaQ0C0CZ3anANnvoINk uwdw== X-Gm-Message-State: AFqh2kr5+ZzZ0LbqyuVmZZnV0TQ9Zb5pJdy61cw+wG1n7U+v668kIt2x /czeCFVodaGG/Fdi0RSxuLHFJf9nTQr70S/BfGxnhRDPYtOme6sbqq+0nROi3A/erYy+dmz8Lll WvRgS8p0sEwlxE6pXf9yuzTzCNgKw99fyZIOqxQGoQA4s2c2KnLVqoeP2NtqdrE74I/rOPk/PPG aW0H3rh/2J+Z1YUSlArJ7/dEvGHDBs7HvHz5DLjVrOg/qrG12B+P8AIWeSmx3B2xxmteKdZ0tNa OSv3Kf3nhJ9AvOfTUhuKp3wE8lnGc6XKwxu3AOQiRnJtFzoToI+d1uP7NTUgSdDsyPOo302Qg== X-Google-Smtp-Source: AMrXdXtx3fV0H1mb8AAKCjQwcK0ywyYV9OjPQrqqJIiStnEk+nXnRO07EX7+cnQju81voh+WIS09Yg== X-Received: by 2002:a17:906:2813:b0:829:59d5:e661 with SMTP id r19-20020a170906281300b0082959d5e661mr50226987ejc.29.1673100479877; Sat, 07 Jan 2023 06:07:59 -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 p4-20020a17090653c400b0084ca4bd745esm1474424ejo.35.2023.01.07.06.07.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 07 Jan 2023 06:07:59 -0800 (PST) Message-ID: <560c80fc-823e-418d-280e-e0c7c0dad59b@enterprisedb.com> Date: Sat, 7 Jan 2023 15:07:58 +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 From: Tomas Vondra To: Tom Lane Cc: James Finnerty , pgsql-hackers@lists.postgresql.org 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> <6da357b0-fdd3-095d-a06c-87666f4a5a8c@enterprisedb.com> <951485.1672461744@sss.pgh.pa.us> <233ee0b3-4da0-3370-cd0b-1b8528851ddd@enterprisedb.com> <1040922.1672507431@sss.pgh.pa.us> <3d719e62-64d2-5184-eebf-6128bcb398e2@enterprisedb.com> <2813656.1672952711@sss.pgh.pa.us> <3e8cca01-f102-d4b9-abe9-ed7b5e938323@enterprisedb.com> <2837327.1672955815@sss.pgh.pa.us> <4ee816f5-6146-7745-a6b4-f38a1b24105a@enterprisedb.com> <3013665.1673024332@sss.pgh.pa.us> <4daee204-341f-d413-fa93-29f603f61da8@enterprisedb.com> In-Reply-To: <4daee204-341f-d413-fa93-29f603f61da8@enterprisedb.com> 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 On 1/6/23 23:41, Tomas Vondra wrote: > On 1/6/23 17:58, Tom Lane wrote: >> Tomas Vondra writes: >>> The one difference is that I realized the relkind check does not >>> actually say we can't do sampling - it just means we can't use >>> TABLESAMPLE to do it. We could still use "random()" ... >> >>> Furthermore, I don't think we should silently disable sampling when the >>> user explicitly requests TABLESAMPLE by specifying bernoulli/system for >>> the table - IMHO it's less surprising to just fail in that case. >> >> Agreed on both points. This patch looks good to me. >> > > Good, I'll get this committed.The "ORDER BY random()" idea is a possible > improvement, can be discussed on it's own. > Pushed. >>> Of course, all relkinds that don't support TABLESAMPLE currently have >>> reltuples value that will disable sampling anyway (e.g. views have -1). >>> So we won't actually fallback to random() anyway, because we can't >>> calculate the sample fraction. >>> That's a bit annoying for foreign tables pointing at a view, which is a >>> more likely use case than table pointing at a sequence. >> >> Right, that's a case worth being concerned about. >> >>> But I realized we could actually still do "random()" sampling: >>> SELECT * FROM t ORDER BY random() LIMIT $X; >> >> Hmm, interesting idea, but it would totally bollix our correlation >> estimates. Not sure that those are worth anything for remote views, >> but still... > > But isn't that an issue that we already have? I mean, if we do ANALYZE > on a foreign table pointing to a view, we fetch all the results. But if > the view does not have a well-defined ORDER BY, a trivial plan change > may change the order of results and thus the correlation. > > Actually, how is a correlation even defined for a view? > > It's true this "ORDER BY random()" thing would make it less stable, as > it would change the correlation on every run. Although - the calculated > correlation is actually quite stable, because it's guaranteed to be > pretty close to 0 because we make the order random. > > Maybe that's actually better than the current state where it depends on > the plan? Why not to look at the relkind and just set correlation to 0.0 > in such cases? > > But if we want to restore that current behavior (where it depends on the > actual query plan), we could do something like this: > > SELECT * FROM the_remote_view ORDER BY row_number() over (); > > But yeah, this makes the remote sampling more expensive. Probably still > a win because of network costs, but not great. > I've been thinking about this a bit more, and I'll consider submitting a patch for the next CF. IMHO it's probably better to accept correlation being 0 in these cases - it's more representative of what we know about the view / plan output (or rather lack of knowledge). However, maybe views are not the best / most common example to think about. I'd imagine it's much more common to reference a regular table, but the table gets truncated / populated quickly, and/or the autovacuum workers are busy so it takes time to update reltuples. But in this case it's also quite simple to fix the correlation by just ordering by ctid (which I guess we might do based on the relkind). There's a minor issue with partitioned tables, with foreign partitions pointing to remote views. This is kinda broken, because the sample size for individual relations is determined based on relpages. But that's 0 for views, so these partitions get ignored when building the sample. But that's a pre-existing issue. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company