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 1jP9Ux-0002Iq-3V for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Apr 2020 18:44:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jP9Uv-0002AV-Tx for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Apr 2020 18:44:25 +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 1jP9Uv-0002AO-Iv for pgsql-hackers@lists.postgresql.org; Thu, 16 Apr 2020 18:44:25 +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 1jP9Us-0000jS-Lr for pgsql-hackers@postgresql.org; Thu, 16 Apr 2020 18:44:25 +0000 Received: by mail-wr1-x443.google.com with SMTP id b11so6057356wrs.6 for ; Thu, 16 Apr 2020 11:44:22 -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=PGH4lccM/j5FvFeGdBfWpcQ4PSdf5YqCuP66hR8jzxM=; b=SkpDMiNKHiZ4kvEtWG39aKakXAn6OgLkX7AswrvCfsPJoHsxPnqNdQ2ClMkiZr1q5a BVFBBVx7C5p3OrkjVHr4ycm29+oYWcrktvK4FpEPisCUwf5Dinxq7KMAEME4vYWIWhcm MaBPph8QcRtASdd8t6qVWKOKwODtxpvMWeNgtDcgzTmu3RutpjFUip/d05uyBEVCbOda j/jN8CyF0Sh/dM1HBn5/R08QCNlLzqouI9vy3QWmqet6I7JlHvhiL1xz8y/8WINqN/ub zSSmYIOGB8p/8O9LhLdsUnrKZUlWP2TLKENxKVUH13bYEfohjR5U8jdM2GA5l0ItNVST pMkw== 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=PGH4lccM/j5FvFeGdBfWpcQ4PSdf5YqCuP66hR8jzxM=; b=Egbgkd3WDfPBKejucHauwgE9NQBTPe/RzLkM+JoJGRYv6v/PY7UsUGRspJlq2SwQGd 4Pjd/j71RS0h8Y6uezPkEpJYfn2fjIByy//MyrC7hEwRehOgawmQ88z5lA6hQ47j/M7F J5VX1ojgcUZ+W+iy+f6tea3UvGsBWcQoxWmlHUifEMeVwi/PySQwYe0A6Ar46wrgFXkQ bQHTbHHkezQrAhx+oraBOoGiyGIrWtcbFihjasNdmAswFMTfSp450VOEaAFPR599ZwHd ywFFMN5y9g8Q+vQR7dVQw0VXPZ2ajLMjP+N58nFA4IM7c07JMuNVy2sGISoCKmt37YMz HP2g== X-Gm-Message-State: AGi0PuZC+0iKZv1OVXOFsQga4nmTmJU6ZBTID5BvghNbsTTQrdgqRRKh fgvIEHD8u79F1i02wiGKs5exPA== X-Google-Smtp-Source: APiQypLlNGNC4xV0bCJaNQSbhtkQ80UFP5XtLcugPCPbn23YSfcE5O+cCHHK1CPfuWWKYWzm1dnAEA== X-Received: by 2002:adf:f146:: with SMTP id y6mr24999821wro.132.1587062660665; Thu, 16 Apr 2020 11:44:20 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id y9sm4486619wmm.26.2020.04.16.11.44.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Apr 2020 11:44:19 -0700 (PDT) Date: Thu, 16 Apr 2020 20:44:16 +0200 From: Tomas Vondra To: James Coleman Cc: Richard Guo , Justin Pryzby , pgsql-hackers Subject: Re: sqlsmith crash incremental sort Message-ID: <20200416184416.vcmmy22zfmov54kl@development> References: <20200411191952.GA30754@telsasoft.com> <20200411202639.2o7quia37zd6ypw7@development> <20200411214639.GK2228@telsasoft.com> <20200411224445.x4whiaeo67glpqen@development> <20200413000943.bejni6axj7v4q5rn@development> <20200415144712.wyvpgvsujzzcf2qg@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services