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 1jPDh1-0003iU-8O for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Apr 2020 23:13:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jPDgz-0000cN-1E for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Apr 2020 23:13:09 +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 1jPDgy-0000cF-QL for pgsql-hackers@lists.postgresql.org; Thu, 16 Apr 2020 23:13:08 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jPDgv-0002PQ-Sw for pgsql-hackers@postgresql.org; Thu, 16 Apr 2020 23:13:07 +0000 Received: by mail-wr1-x442.google.com with SMTP id k11so734663wrp.5 for ; Thu, 16 Apr 2020 16:13:05 -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=Xs979Npl2kdX6OYiTFtI/ij61Jp0bbS+9ac9X+P96wQ=; b=FA/aU3h/hcwrm4vKm/MwTqH9SMdL8uOcDJqs1UVLfgStmyVG8jeec/x1MSDBzSWtUt wMPIVNYIGkAMtuCJJ6CVBxpnsFgy4s8S95NAWh/Q3Y888v1f2tlRoxJG6GhdIHIZGTUT QScA7DC9fzx8T8yt8yX+9mwfN5NfLtXoZWU8IKezTGg0aSmrTdwetBGJWdOIONg8P6Rs IBe4CEb7XbJZT4NdfnNzS7AAJ4ErlvdVHHlogG1dSVenogg4dC6cqtGdr0A/KB9Cg0TV CKtptYDEMsQC2Dl6alrSVzn2sKVTyCb7U8tdYjwtGRMlQtp9mK+DitsBJZmdJJnghPZB eXKQ== 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=Xs979Npl2kdX6OYiTFtI/ij61Jp0bbS+9ac9X+P96wQ=; b=k8LdRqLb6cFCFEacaLVYwLwphf97cNb4UpSGs8EBJbUdj+UT73kKzH/WetQwhnuHHp J1lhasRUpHmoKrifGyElX705TAgDJY+fX7J9sMzVm5xs9jFc/PyxPvOTnfN7Z/rDN3Vb gIwmn6YY50ITqaYO7ct0z6umQcNOkvC+s4ydv45OsK9xxSxG54bn0lpzry06Bb372upI D8OqfMoei+eGMYf2kMlihIcPk8T65JV1uC5Kosxnu/bdC+5RruWTNqHcQxBuO5Nf8KFn cJm5Rzs+S94GpgCfAYwYqkwIa9ijJQKq4+z0/rqYW0PFYGEwnOXUa1/ncHgO/YtMtoI6 HFCA== X-Gm-Message-State: AGi0PubkJtHJs9LMGWYHjoFikJYuZeZIRAO2eulwdHynjLDAdMmQiAMc ijNfmzpP9dXMow8zIG+SSdKb/g== X-Google-Smtp-Source: APiQypIzObmvjRjSveP9e4S9FhxdhlJGQOYgp/rTJ7fr7b+GdNM408/1WO99GAPhihn7AR+CaUhS8g== X-Received: by 2002:adf:df8e:: with SMTP id z14mr677914wrl.296.1587078784039; Thu, 16 Apr 2020 16:13:04 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id 145sm5591630wma.1.2020.04.16.16.13.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Apr 2020 16:13:02 -0700 (PDT) Date: Fri, 17 Apr 2020 01:13:00 +0200 From: Tomas Vondra To: James Coleman Cc: Richard Guo , Justin Pryzby , pgsql-hackers Subject: Re: sqlsmith crash incremental sort Message-ID: <20200416231300.5mkhjzs6ki4yntto@development> References: <20200411202639.2o7quia37zd6ypw7@development> <20200411214639.GK2228@telsasoft.com> <20200411224445.x4whiaeo67glpqen@development> <20200413000943.bejni6axj7v4q5rn@development> <20200415144712.wyvpgvsujzzcf2qg@development> <20200416184416.vcmmy22zfmov54kl@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20200416184416.vcmmy22zfmov54kl@development> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Apr 16, 2020 at 08:44:16PM +0200, Tomas Vondra wrote: >On Thu, Apr 16, 2020 at 12:04:03PM -0400, James Coleman wrote: >>On Thu, Apr 16, 2020 at 8:22 AM Richard Guo wrote: >>> >>> >>>On Thu, Apr 16, 2020 at 6:35 PM Richard Guo wrote: >>>> >>>> >>>>On Wed, Apr 15, 2020 at 10:47 PM Tomas Vondra wrote: >>>>> >>>>> >>>>>Well, yeah. The problem is the Unique simply compares the columns in the >>>>>order it sees them, and it does not match the column order desired by >>>>>incremental sort. But we don't push down this information at all :-( >>>> >>>> >>>>This is a nice optimization better to have. Since the 'Sort and Unique' >>>>would unique-ify the result of a UNION by sorting on all columns, why >>>>not we adjust the sort order trying to match parse->sortClause so that >>>>we can avoid the final sort node? >>>> >>>>Doing that we can transform plan from: >>>> >>>># explain (costs off) select * from foo union select * from foo order by 1,3; >>>> QUERY PLAN >>>>----------------------------------------------- >>>> Incremental Sort >>>> Sort Key: foo.a, foo.c >>>> Presorted Key: foo.a >>>> -> Unique >>>> -> Sort >>>> Sort Key: foo.a, foo.b, foo.c >>>> -> Append >>>> -> Seq Scan on foo >>>> -> Seq Scan on foo foo_1 >>>>(9 rows) >>>> >>>>To: >>>> >>>># explain (costs off) select * from foo union select * from foo order by 1,3; >>>> QUERY PLAN >>>>----------------------------------------- >>>> Unique >>>> -> Sort >>>> Sort Key: foo.a, foo.c, foo.b >>>> -> Append >>>> -> Seq Scan on foo >>>> -> Seq Scan on foo foo_1 >>>>(6 rows) >>>> >>> >>>Attached is what I'm thinking about this optimization. Does it make any >>>sense? >> >>Shouldn't this go one either a new thread or on the thread for the >>patch Tomas was referencing (by Teodor I believe)? >> > >FWIW the optimization I had in mind is this: > > https://commitfest.postgresql.org/21/1651/ > >I now realize that was about GROUP BY, but it's not all that different >and the concerns will / should be fairly similar, I think. > >IMO simply tweaking the sort keys to match the upper parts of the plan >is probably way too simplistic, I'm afraid. For example, if the Unique >significantly reduces cardinality, then the cost of the additional sort >is much less important. It may be much better to optimize the "large" >sort of the whole data set, either by reordering the columns as proposed >by Teodor in his patch (by number of distinct values and/or cost of the >comparison function function). > >Furthermore, this is one of the places that is not using incremental >sort yet - I can easily imagine doing something like this: > > > Sort > -> Unique > -> Incremenal Sort > -> ... > >could be a massive win. So I think we can't just rejigger the sort keys >abitrarily, we should / need to consider those alternatives. > >>Or are you saying you believe this patch guarantees we never see this >>problem in incremental sort costing? >> > >Yeah, that's not entirely close to me. But maybe it shows us where we to >get the unprocessed target list? > I think at the very least this needs to apply the same change also to generate_nonunion_paths, because otherwise this fails because of the same issue: set enable_hashagg = off; explain select * from foo except select * from foo order by 1, 3; I'm still of the opinion that this is really an optimization and behavior change, and I feel rather uneasy about pushing it post feature freeze without appropriate review. I also think we really ought to consider how would this work with the other optimizations I outlined elsewhere in this thread (comparison costs, ...). regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services