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 1kesXD-0006qH-SV for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Nov 2020 04:24:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kesXB-0002ic-76 for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Nov 2020 04:24:01 +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 1kesXA-0002hp-RU for pgsql-hackers@lists.postgresql.org; Tue, 17 Nov 2020 04:24:01 +0000 Received: from mail-ej1-x644.google.com ([2a00:1450:4864:20::644]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kesX1-0005uM-LQ for pgsql-hackers@postgresql.org; Tue, 17 Nov 2020 04:23:59 +0000 Received: by mail-ej1-x644.google.com with SMTP id y17so21898321ejh.11 for ; Mon, 16 Nov 2020 20:23:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=iCSadrV6GTIIqq3Je7tMV209UXy9vpWI9xkyjJ26tQI=; b=LvwVB6AcgbyWYTa1kfPBlL0shgVF7nlVb6gUqufQZmzSoXRvlXufYAVF58Z1KaF/tO k6YSJuqLK1wJM2or+LoSChh8LphabPW/D34ylXaK1VJozxqun8yjL3vGpyp1zWqF05Ta RtKxZrD8jkDj2RDkt4/pHS6BHM0+eYS/MDlRU1iXx10gFz9CoImd7tjqoI7FlgLh2o4m gh/5UKDHM8xTl4dHVOL53+zzLfMRTjG+gTFaUaW2ifSk7P5WMJ8A5fnOMmq+97RXi2q4 04n9KRlCVs9DXIUX18yOwqAh5u91zZrDQpbD7DPzy4gm9S4Hi12gNLZKSG7jsIIbJ+Jb 6IyQ== 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; bh=iCSadrV6GTIIqq3Je7tMV209UXy9vpWI9xkyjJ26tQI=; b=qf+wBOAps9IVqkwcX5TEbu4T8wFcp1fY7uKNBlOm/6l1gQuw/mIiTtXZ2TS8BeteKH D68mNNHsQEs4rfFVOvABKe5bOJwJkj7jElpXO32yxECV7Q7KgWIjQn1DqK6LBLi8AYDb U78vGO/NnAZMhLSK+uvtCHZIZh9HmYfkYnZCan542R0zeoLSjrwBzCneZVSQNsCHzHv2 sYatHXYJxLy415GTIUiYJQHRkzZgQS6rjpY4DrIOSmt0ZDzXtSGS9TWv8kxPAGBUK2uW ppHuYeYQ8d/LzOyLxZVHFpe9A7EigsY7DJ3fpEstS6C6ug0qyOY3JElTHdikU6CgSw3N TrFw== X-Gm-Message-State: AOAM533JH/8CdTw01ZJg/q64WmXGWlFs/TbM1RRRSIpur4nGFA+FcdVo W0mrCL7GB1eKdmaH0NAgtLM5hv3ISv7aEea5mpJknSk2icncz2exqrTbWCY5nryoMraYM778WT4 QccMhJ0kBNKTWy/xkVxSg3CqDRoFcDkM29UZdp2MXl8s0O00ma1/6qYKms1wRuanDiOzX96uzp4 TN1WPAoWRBv05KOw+bef2DcatGWXgrKJnqthPVJ0Lml1HB7O+L1t1h7iMUV3SVEgMO4ervqa5pb 73Q4o6YmTCWN2Y/Z9bqVxiXGkhUewWUioax8GDB X-Google-Smtp-Source: ABdhPJwHgB6ZMJYyyxIuX80UGimngMWdtgBF0HtVdCUn3AVKw0mh7xydR9sOgRUzUkcrHRNJS5Y+rA== X-Received: by 2002:a17:906:77c5:: with SMTP id m5mr17893452ejn.424.1605587029946; Mon, 16 Nov 2020 20:23:49 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-245-76.net.upcbroadband.cz. [86.49.245.76]) by smtp.gmail.com with ESMTPSA id i13sm11108703ejv.84.2020.11.16.20.23.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Nov 2020 20:23:49 -0800 (PST) Subject: Re: enable_incremental_sort changes query behavior To: luis.roberto@siscobra.com.br, Tomas Vondra Cc: James Coleman , Jaime Casanova , Pg Hackers References: <20201029220652.glf4xk2p7wkfisb6@development> <20201030210316.cbwv4arwp4lcfgx2@development> <20201103023743.sswkl6h74dkbcfmp@development> <20201103213942.sj3qohewdshrw4oz@development> <991884329.69403168.1605565987239.JavaMail.zimbra@siscobra.com.br> From: Tomas Vondra Message-ID: Date: Tue, 17 Nov 2020 05:23:46 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 In-Reply-To: <991884329.69403168.1605565987239.JavaMail.zimbra@siscobra.com.br> Content-Type: multipart/mixed; boundary="------------E3B5FF6AABA2B4FDE5D80D87" Content-Language: en-US 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: Precedence: bulk This is a multi-part message in MIME format. --------------E3B5FF6AABA2B4FDE5D80D87 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Hmm, I missed that other thread. That indeed seems like a bug in the same area already tweaked by ebb7ae839d033d0f2 for similar cases. The attached patch fixes this simply by adding is_parallel_safe to get_useful_pathkeys_for_relation - that does fix the reproducer, but I'm not entirely sure that's the right place. Maybe it should be done in find_em_expr_usable_for_sorting_rel (which might make a difference if an EC clause can contain a mix of parallel safe and unsafe expressions). Or maybe we should do it in the caller (which would allow using get_useful_pathkeys_for_relation in contexts not requiring parallel safety in the future). Anyway, while this is not an "incremental sort" bug, it seems like the root cause is the same as for ebb7ae839d033d0f2 - one of the incremental sort patches started considering sorting below gather nodes, not realizing these possible consequences. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------E3B5FF6AABA2B4FDE5D80D87 Content-Type: text/x-patch; charset=UTF-8; name="incremental-sort-fix-parallel-safe.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="incremental-sort-fix-parallel-safe.patch" diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 84a69b064a..93db261011 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -2826,6 +2826,7 @@ get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel) foreach(lc, root->query_pathkeys) { + Expr *expr; PathKey *pathkey = (PathKey *) lfirst(lc); EquivalenceClass *pathkey_ec = pathkey->pk_eclass; @@ -2840,7 +2841,14 @@ get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel) * enable not just an incremental sort on the entirety of * query_pathkeys but also incremental sort below a JOIN. */ - if (!find_em_expr_usable_for_sorting_rel(pathkey_ec, rel)) + if (!(expr = find_em_expr_usable_for_sorting_rel(pathkey_ec, rel))) + break; + + /* + * Also stop when the expression is not parallel-safe. We plan + * to add all of this under a Gather node. + */ + if (!is_parallel_safe(root, (Node *) expr)) break; npathkeys++; --------------E3B5FF6AABA2B4FDE5D80D87--