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 1jP3z6-0005aN-Gw for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Apr 2020 12:51:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jP3z5-0007hO-BY for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Apr 2020 12:51:11 +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 1jP3z5-0007hH-4f for pgsql-hackers@lists.postgresql.org; Thu, 16 Apr 2020 12:51:11 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jP3z2-00064D-D8 for pgsql-hackers@postgresql.org; Thu, 16 Apr 2020 12:51:09 +0000 Received: by mail-wm1-x341.google.com with SMTP id v8so2463042wma.0 for ; Thu, 16 Apr 2020 05:51:08 -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=gfIDdCJs2dOxBIdg9zQpPP713o1USuUkCrEIc13Ie28=; b=N2ss9r7KBoZui1QNideWZwhsf5VERYlvUpMnvKYYdhRk/zTiAkHTxbfDOkGeC9gjvG 9aO6mfdR5gnPgNdh4zqDRxRbMUpWvDemxU0M5tP0GT3sWB9E/1JfDt4lkir+XS2I7nEj acRCFwgAbgaHzmtLapiU+DVb8GYyBjLcUxfdOVYq+J6sUSgCxps3IMvl2+6ZjdjLGk3p cqm3x+EQSYYpBbItgCfX4PMOUCfxr/rJZvQyd9sZPMhYFP7J/jwBuyq+LBGZpG9pi6k8 v+XkDLd0DcSYTl4wfIcfrdBFhF9sk6LjNl76Bg3nercfQVCYhp1vrmLrsgNXEdNLkhCl VFUw== 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=gfIDdCJs2dOxBIdg9zQpPP713o1USuUkCrEIc13Ie28=; b=MYCfldQUmTz0prpmvJB4T8s1aSJ5dPFhcSZBy3MY0Q+1pXxi580Pn60O1/5HguRkcA 0njxPXVx83f4pf2sy9assydhSWRXV/KvtYlTgfdOR/QUmvQntDDuBB7ocaw4KkeU75Tc JCuWFSmgTz1JRXJlvkIBQYlq1Q4fJBviaRdJWlKdIrx3et+uYoYF8Q7t4w3ht/hx0NGf p1tn7bJNEiQKMx/YBY97Lv3+Fc9j7dc6yPWkdmFcrubRkUQVXi4R9OUnsxNOmS/3LnTn vGcjmYCiSTTBj0updw5wa/m9Z8QpZEDJq4Tgv2XXS5gz3PkAHs04xyQ0fNR/7JPm9Ydm 0wIw== X-Gm-Message-State: AGi0PuZKQ1b/qqKPirEErlPTxDmjgGUhJIuDfxxEzHxFDWdPYTjUySjX q2kw+z4b/oty7n2r3s02vV6rVg== X-Google-Smtp-Source: APiQypJH5qiRWvgC8xCs4Qre3PbUYw8TjPOSDorT319ai6n145B/pTELxxHgaz8rQzM8WzymZx1kSg== X-Received: by 2002:a1c:4c10:: with SMTP id z16mr4809722wmf.77.1587041465122; Thu, 16 Apr 2020 05:51:05 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id w18sm27014510wrn.55.2020.04.16.05.51.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Apr 2020 05:51:04 -0700 (PDT) Date: Thu, 16 Apr 2020 14:51:01 +0200 From: Tomas Vondra To: Richard Guo Cc: Justin Pryzby , James Coleman , PostgreSQL-development Subject: Re: sqlsmith crash incremental sort Message-ID: <20200416125101.bkvsy5yhrjcb2ydz@development> References: <20200411191952.GA30754@telsasoft.com> <20200411202639.2o7quia37zd6ypw7@development> <20200411214639.GK2228@telsasoft.com> <20200411224445.x4whiaeo67glpqen@development> <20200413000943.bejni6axj7v4q5rn@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 04:44:10PM +0800, Richard Guo wrote: >On Mon, Apr 13, 2020 at 8:09 AM Tomas Vondra >wrote: > >> >> I've been messing with this the whole day, without much progress :-( >> >> I'm 99.9999% sure it's the same issue described by the quoted comment, >> because the plan looks like this: >> >> Nested Loop Left Join >> -> Sample Scan on pg_namespace >> Sampling: system ('7.2'::real) >> -> Incremental Sort >> Sort Key: ... >> Presorted Key: ... >> -> Unique >> -> Sort >> Sort Key: ... >> -> Append >> -> Nested Loop >> ... >> -> Nested Loop >> ... >> >> so yeah, the plan does have set operations, and generate_append_tlist >> does generate Vars with varno == 0, causing this issue. >> > >After some digging I believe here is what happened. > >1. For the UNION query, we build an upper rel of UPPERREL_SETOP and >generate Append path for it. Since Append doesn't actually evaluate its >targetlist, we generate 'varno 0' Vars for its targetlist. (setrefs.c >would just replace them with OUTER_VAR when adjusting the final plan so >this usually does not cause problems.) > >2. To remove duplicates for UNION, we use hash/sort to unique-ify the >result. If sort is chosen, we add Sort path and then Unique path above >Append path, with pathkeys made from Append's targetlist. > >3. Also the Append's targetlist would be built into >root->processed_tlist and with that we calculate root->sort_pathkeys. > >4. When handling ORDER BY clause, we figure out the pathkeys of >Unique->Sort->Append path share some same prefix with >root->sort_pathkeys and thus incremental sort would be considered. > >5. When calculating cost for incremental sort, estimate_num_groups does >not cope with 'varno 0' Vars extracted from root->sort_pathkeys. > Right. > >With this scenario, here is a simple recipe: > >create table foo(a int, b int, c int); >set enable_hashagg to off; >explain select * from foo union select * from foo order by 1,3; > Yep, that's a much simpler query / plan. Thanks. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services