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 1kOM6d-0001e5-76 for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Oct 2020 14:32:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kOM6Z-0002w8-K8 for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Oct 2020 14:32:15 +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 1kOM6Z-0002w1-81 for pgsql-hackers@lists.postgresql.org; Fri, 02 Oct 2020 14:32:15 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kOM6W-00050i-5T for pgsql-hackers@postgresql.org; Fri, 02 Oct 2020 14:32:14 +0000 Received: by mail-wr1-x443.google.com with SMTP id g4so2074065wrs.5 for ; Fri, 02 Oct 2020 07:32:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=b9Ecv7P0L3VWm+IQnII4dcStE5lqYSzvOZQ8tx2vo/I=; b=h8AC4uPt7U5N9zXIwEoltODmPx6N8PUQokSDGU70KFz9ugVZX18s2XnNZisfSaS85I wdB396ld8BDwcuprNdNHDjXoQpH0784+y6mjQ60qKZl5FP/qxm+HDOyikG5jsjjFtiB8 1gmKbAKhrToXcC7yZFOJpwPga4/vf2wBKiH9oCQv1bL+bl+gg7Pz8/M9BIHVN1N/AsHg Pkno+Lm5O4ekrZ9lTXXwmihB0lVQuKCXsvfQwbwBqacZ3/jnMyR5H2k81rmGs0WEf2mS TSq77qxHqMVx14bzLCoKpvXsmbsLWwloMcsSvCWfoZnOvG6X7F2iA+SnKVasGI4bGhK6 PFmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=b9Ecv7P0L3VWm+IQnII4dcStE5lqYSzvOZQ8tx2vo/I=; b=LUHXoZpnXsgmi8A/SyeXidygnUxhfG6ePYXtlw6G9TaSMTUoy+mB+y27OM0vTr8JEv RfgOFDvHouDzHAJtbaxa3LmAjNIbjo5Nx2TZQhb+ziERxg4wTRR8LSzcXRo97Fdp0yj1 0DJWacrWtQZEOeFXkHU2vZlpH8tOn7dMLuhwnuLzPTiDNINCzIPR1m2Ru2m5m8OmV231 68big6eH1w9DDYy/f0o3QIVVA7H+oM15VzilZL1d6icprdiBOAauQz+xDhUX5ew0HS1y l5aLp45hBb4caSVrbtf/T14pXZr1+SYlOmXQmx4aRN92BPT3w8Mg10MR7cttumPM+DJr YHyw== X-Gm-Message-State: AOAM530YMAHLAbG2Adqs+rfJIuHsBYw/NhfCpain+T9ai8t5HdObqDiv 7aIAIM0SDoA8QA1U4l5nRbOTYA== X-Google-Smtp-Source: ABdhPJw3ClxTXx94XmoAodj3lQuc93RL4EdipHJoFibCR/+kB88TO8cnNIar42dSx98J9EIlLfhMXA== X-Received: by 2002:a5d:4645:: with SMTP id j5mr3393465wrs.230.1601649130380; Fri, 02 Oct 2020 07:32:10 -0700 (PDT) Received: from localhost (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id l5sm1919105wrv.24.2020.10.02.07.32.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Oct 2020 07:32:09 -0700 (PDT) Date: Fri, 2 Oct 2020 16:32:07 +0200 From: Tomas Vondra To: James Coleman Cc: Jaime Casanova , Pg Hackers Subject: Re: enable_incremental_sort changes query behavior Message-ID: <20201002143207.qt7re34mhsfmfi6f@development> References: <20201001220822.rwwxajwzobljgqwz@development> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="mznfkguwnrughcc2" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --mznfkguwnrughcc2 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Fri, Oct 02, 2020 at 09:19:44AM -0400, James Coleman wrote: > > ... > >I've been able to confirm that the problem goes away if we stop adding >the gather merge paths in generate_useful_gather_paths(). > >I'm not sure yet what conclusion that leads us to. It seems to be that >the biggest clue remains that all of this works correctly unless one >of the selected columns (which happens to be a pathkey at this point >because it's a DISTINCT query) contains a volatile expression. > Yeah. It seems to me this is a bug in get_useful_pathkeys_for_relation, which is calling find_em_expr_for_rel and is happy with anything it returns. But this was copied from postgres_fdw, which however does a bit more here: if (pathkey_ec->ec_has_volatile || !(em_expr = find_em_expr_for_rel(pathkey_ec, rel)) || !is_foreign_expr(root, rel, em_expr)) So not only does it explicitly check volatility of the pathkey, it also calls is_foreign_expr which checks the expression for mutable functions. The attached patch seems to fix this, but it only adds the check for mutable functions. Maybe it should check ec_has_volatile too ... regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --mznfkguwnrughcc2 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="incremental-sort-fix.patch" diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index b399592ff8..33df9b0783 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -2771,6 +2771,7 @@ get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel) { PathKey *pathkey = (PathKey *) lfirst(lc); EquivalenceClass *pathkey_ec = pathkey->pk_eclass; + Expr *em_expr; /* * We can only build an Incremental Sort for pathkeys which @@ -2783,7 +2784,8 @@ 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_for_rel(pathkey_ec, rel)) + if (!(em_expr = find_em_expr_for_rel(pathkey_ec, rel)) || + contain_mutable_functions((Node *) em_expr)) break; npathkeys++; --mznfkguwnrughcc2--