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 1m5yr4-00078k-S9 for pgsql-hackers@arkaria.postgresql.org; Tue, 20 Jul 2021 23:08:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m5yr3-0007X9-S8 for pgsql-hackers@arkaria.postgresql.org; Tue, 20 Jul 2021 23:08:49 +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 1m5yr3-0007X0-69 for pgsql-hackers@lists.postgresql.org; Tue, 20 Jul 2021 23:08:49 +0000 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m5yr0-0000f9-EC for pgsql-hackers@lists.postgresql.org; Tue, 20 Jul 2021 23:08:48 +0000 Received: by mail-ed1-x52b.google.com with SMTP id h8so54382eds.4 for ; Tue, 20 Jul 2021 16:08:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=6Tpeh5vyjQj/KwLupyBuJnHQEw8wbs1AAY82O7wN8sY=; b=jPxHFyeYKkZdz/r6fRX59cRCPJ+ON9x16Mm4Ni5Ay6JmI92Pudm0+1m0lsXBw2+71j Z6NB6c82q27Aauul9qip8yFd3jRrM0ZeREcbfQ76UoLcMkE/oiD8ts6bJoyrDVf+mccK nqYagCW62gXjfNr1IYzV1FSInK47MGu/RQndBsSrfR1KuDtPHl6gGNo1IkYSQCFetuOE oq+Vjvj9BnsJJf1UHxejjJb8qU8LKr7AREmI8VLU3VrBPCF3sqn8Yt4bRh8YTOB8exk+ rYdSBvyw+GhX/oHmyTP3jGMFYt2Auf4weFQB95JBOyRyAcfaApHt442q1/I/6+LdUvqA yySg== 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=6Tpeh5vyjQj/KwLupyBuJnHQEw8wbs1AAY82O7wN8sY=; b=QxceZzq/8GgDfoWlBv/bVDpVzVMAHnIBLkUFPgyFYXkfe2uSiza1hm8nX7uxIoS48X BJq35zeokYfpzWTiBNRyjnMiCo9TOusMIGv1N3XlZGpHIflSuD1y/+ZkSX6flUbrK01t Qdf98L+EcW7yn9pfBQQy8DNAeUcPkHrZ4s8msFtqhbSzckW+Ml+xTol1nH1nnIYpG8TN 1Lv5xPIWAav5d3BknDTli8SeZxThGZ32a/PNTcxphaAyGzDmEL9PTImoAxjYuqaLh/uN vLDehIoPff7rckVQLtQvHE1S2sdX5rposTib4nUF6Nk4fG6a4gfgM7zIKn5sVAS2L0jK Uj5w== X-Gm-Message-State: AOAM533uND0tcWCyUZRO+p4GOX6wIX8c0NS9exiQq/n+tJUgPA9iaRaI Rt8I7U+bXsuOiN2kbnRB6poe9nZioDmvYja9nLOmFUTXUAeKqkFrxdXy/FyxqZhP1MJExgcm+B3 IXSe/jwMVdOOzzzuv7POswVU6SnGuKbGuLpPAZ4xLGsIOpb96s2ED+6lScVrykNf9PvY4AHf4Yg p+yE6/arlEG3RB78aPg+t1uEYwXAgP35T8Tcmt6k1aApUNfSYSPzbYKBc0+wCbrCCcMj33n5Gxu T6hR+4zm+Q6QmLXanmjw67+9dmdt5OMVSyVoqBHKS3oN32uFdHKRA== X-Google-Smtp-Source: ABdhPJwimKXR7gJrvRZvGHs0oxstugNORu7yqy081Ah6FEc7KvH6UgtG1uW6PXnxFbf0y88CildP7A== X-Received: by 2002:a05:6402:30af:: with SMTP id df15mr44974448edb.19.1626822523825; Tue, 20 Jul 2021 16:08:43 -0700 (PDT) Received: from [10.137.0.19] (ip-86-49-254-183.net.upcbroadband.cz. [86.49.254.183]) by smtp.gmail.com with ESMTPSA id q8sm7539276edd.43.2021.07.20.16.08.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 20 Jul 2021 16:08:43 -0700 (PDT) Subject: Re: POC: GROUP BY optimization To: Ibrar Ahmed , Pavel Borisov , Teodor Sigaev Cc: Tomas Vondra , Dmitry Dolgov <9erthalion6@gmail.com>, Gavin Flower , Andres Freund , Michael Paquier , PostgreSQL Developers References: <20190503215510.bcr5ycszntqg65tw@development> <20190524225725.embuha33qvc5avz3@development> <20200514235220.xewrrwjvatxzn3g6@development> <20201026085721.g6h5xljxvodnmk34@localhost> <20201026104040.6bigvej6f55vjvgp@localhost> <20201027201509.jfv6sfhrmxhdwlvk@development> <20201029145025.tn3tekboot37nkbf@localhost> <22c44f98-bfa8-8630-62b5-5155e11eb284@enterprisedb.com> From: Tomas Vondra Message-ID: Date: Wed, 21 Jul 2021 01:08:40 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US 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 7/20/21 3:12 PM, Tomas Vondra wrote: > ... > > The other comments from the review still apply - I'm particularly > concerned about the (1) point, i.e. plan changes in postgres_fdw. Those > seem to be rather strange (LIMIT not being pushed down in queries > without any grouping). I'd bet this is due to changes in sort costing > and does not seem very desirable. > I did look at this a bit closer, and yes - this very much seems like a costing issue in the patch. The first query in postgres_fdw that changes switches from a query with LIMIT pushed to remote QUERY PLAN ------------------------------------------------------------------------ Foreign Scan (cost=196.29..196.52 rows=10 width=14) Output: t1.c1, t2.c1, t1.c3 Relations: (public.ft1 t1) INNER JOIN (public.ft2 t2) Remote SQL: SELECT r1."C 1", r2."C 1", r1.c3 FROM ("S 1"."T 1" r1 INNER JOIN "S 1"."T 1" r2 ON ... LIMIT ... OFFSET ... (4 rows) to the LIMIT executed locally QUERY PLAN ------------------------------------------------------------------------- Limit (cost=241.72..241.94 rows=10 width=14) Output: t1.c1, t2.c1, t1.c3 -> Foreign Scan (cost=239.47..261.97 rows=1000 width=14) Output: t1.c1, t2.c1, t1.c3 Relations: (public.ft1 t1) INNER JOIN (public.ft2 t2) Remote SQL: SELECT r1."C 1", r2."C 1", r1.c3 FROM ("S 1"."T 1" r1 INNER JOIN ... (6 rows) The FDW code runs explain on both queries - with and without LIMIT pushed to remote side, to get estimates, and without the patch it gets this: QUERY PLAN ------------------------------------------------------------------------------ Sort (cost=106.97..109.47 rows=1000 width=14) Sort Key: r1.c3, r1."C 1" -> Hash Join (cost=33.50..57.14 rows=1000 width=14) Hash Cond: (r1."C 1" = r2."C 1") -> Seq Scan on "T 1" r1 (cost=0.00..21.00 rows=1000 width=10) -> Hash (cost=21.00..21.00 rows=1000 width=4) -> Seq Scan on "T 1" r2 (cost=0.00..21.00 rows=1000 width=4) (7 rows) QUERY PLAN ------------------------------------------------------------------------------------ Limit (cost=96.29..96.32 rows=10 width=14) -> Sort (cost=96.04..98.54 rows=1000 width=14) Sort Key: r1.c3, r1."C 1" -> Hash Join (cost=33.50..57.14 rows=1000 width=14) Hash Cond: (r1."C 1" = r2."C 1") -> Seq Scan on "T 1" r1 (cost=0.00..21.00 rows=1000 width=10) -> Hash (cost=21.00..21.00 rows=1000 width=4) -> Seq Scan on "T 1" r2 (cost=0.00..21.00 rows=1000 width=4) (8 rows) while with the patch it gets this: QUERY PLAN ------------------------------------------------------------------------------ Sort (cost=139.47..141.97 rows=1000 width=14) Sort Key: r1.c3, r1."C 1" -> Hash Join (cost=33.50..57.14 rows=1000 width=14) Hash Cond: (r1."C 1" = r2."C 1") -> Seq Scan on "T 1" r1 (cost=0.00..21.00 rows=1000 width=10) -> Hash (cost=21.00..21.00 rows=1000 width=4) -> Seq Scan on "T 1" r2 (cost=0.00..21.00 rows=1000 width=4) (7 rows) QUERY PLAN ------------------------------------------------------------------------------------ Limit (cost=145.75..145.77 rows=10 width=14) -> Sort (cost=145.50..148.00 rows=1000 width=14) Sort Key: r1.c3, r1."C 1" -> Hash Join (cost=33.50..57.14 rows=1000 width=14) Hash Cond: (r1."C 1" = r2."C 1") -> Seq Scan on "T 1" r1 (cost=0.00..21.00 rows=1000 width=10) -> Hash (cost=21.00..21.00 rows=1000 width=4) -> Seq Scan on "T 1" r2 (cost=0.00..21.00 rows=1000 width=4) (8 rows) Notice that the costs get "inverted" master patched ---------------------------------------------- no limit 106.97..109.47 139.47..141.97 limit 96.29.. 96.32 145.75..145.77 so the limit looks a bit more expensive - just enough so that it seems cheaper to transfer more rows and execute the limit locally. IMO this looks rather suspicious (or wrong), and compute_cpu_sort_cost may need some fixes. I wonder why differs from the old code so much ... regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company