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 1jNOrt-0006Fo-Gw for pgsql-hackers@arkaria.postgresql.org; Sat, 11 Apr 2020 22:44:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jNOrs-0004dp-49 for pgsql-hackers@arkaria.postgresql.org; Sat, 11 Apr 2020 22:44:52 +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 1jNOrr-0004di-TY for pgsql-hackers@lists.postgresql.org; Sat, 11 Apr 2020 22:44:51 +0000 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jNOrp-0003NY-3f for pgsql-hackers@postgresql.org; Sat, 11 Apr 2020 22:44:51 +0000 Received: by mail-wr1-x435.google.com with SMTP id i10so6226905wrv.10 for ; Sat, 11 Apr 2020 15:44:48 -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=dLuKfLaXPX5PDgibzo8LIKzSoc5VCVR0aiuBmOqqP/E=; b=iCN8es09IF4CHBnyZ4Vp0rYz2VtBH/ItkaMz8Dgi4bzfWS7viQdEmNnP6L8y3gH0+n d5jLd3w3h5T5zD4SJdGBeoEzaaReBhnM3qVNBxDYpE9LMKLNGuhAgx+6go2rxk/KfPdW pc1TLuYb1BafZSuDZD0rhdnHZRWSM4MPhcX5xqK1gdwWQG5kpzCW1OLqFoER3i8LLUF4 AlWa0l6rPG+I4J0ElFQPDF9IW4jxCOrX1n7r3IFgVcBNMnyi5oP+7l3Fftg9XVlzQb3U +MI3XqMGHVfVWA11GZZXE9rYZHTBwr8WmwOdDb+B8a/HR0S4qjHDKGMISZSlrbLxLBiZ s3SQ== 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=dLuKfLaXPX5PDgibzo8LIKzSoc5VCVR0aiuBmOqqP/E=; b=h1wQCJ/5zy/ugLhTA8FntbMO66HFuN0WlN+ZXVofCLrtKWdcSd//6FF8Xj5ocZd7Nx hT0sQNP1WRvxHdbvFXOVePHdUhZykcJxMolkZVrUzWxO+8ZuBkX6gbCLltu0AS4CjSsX uczeWhjo6FrLxnpKvJHQQnOevkJiCAplg3OFFZUWDrkaBgGhc3eAEXSRuWG9ee0d5asI 1tYzgl3ot/mQULj55WfY3LXiPA/ejoSO23yi9AO0hmaM/EKvmWmYc5WIgKmbKLz3+SAE EqNehTT9qwraDjoQTIwvAmsbgh8ei6lsiA/fU7cAh3P97/GOOqV7nLEmertVEma7cyM7 pAXw== X-Gm-Message-State: AGi0PuaKFijp4NuwBnoJAkMlkwttRyYWqX7URrbHCf5s+2WpU4kuivPJ kgZXH8rVYWry3HbLLZFeCqO8Dw== X-Google-Smtp-Source: APiQypK0afd+VCSLND2xEyR08KNW5vJE2TgcBtLPaW1USViqdfeWkd9rSFV4qU++etzXorpOVfCq9A== X-Received: by 2002:a5d:43c6:: with SMTP id v6mr11484244wrr.395.1586645088098; Sat, 11 Apr 2020 15:44:48 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id p10sm8395036wrm.6.2020.04.11.15.44.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Apr 2020 15:44:47 -0700 (PDT) Date: Sun, 12 Apr 2020 00:44:45 +0200 From: Tomas Vondra To: Justin Pryzby Cc: James Coleman , pgsql-hackers@postgresql.org Subject: Re: sqlsmith crash incremental sort Message-ID: <20200411224445.x4whiaeo67glpqen@development> References: <20200411191952.GA30754@telsasoft.com> <20200411202639.2o7quia37zd6ypw7@development> <20200411214639.GK2228@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20200411214639.GK2228@telsasoft.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, I've looked into this a bit, and at first I thought that maybe the issue is in how cost_incremental_sort picks the EC members. It simply does this: EquivalenceMember *member = (EquivalenceMember *) linitial(key->pk_eclass->ec_members); so I was speculating that maybe there are multiple EC members and the one we need is not the first one. That would have been easy to fix. But that doesn't seem to be the case - in this example the EC ony has a single EC member anyway. (gdb) p key->pk_eclass->ec_members $14 = (List *) 0x12eb958 (gdb) p *key->pk_eclass->ec_members $15 = {type = T_List, length = 1, max_length = 5, elements = 0x12eb970, initial_elements = 0x12eb970} and the member is a Var with varno=0 (with a RelabelType on top, but that's irrelevant). (gdb) p *(Var*)((RelabelType*)member->em_expr)->arg $12 = {xpr = {type = T_Var}, varno = 0, varattno = 1, vartype = 12445, vartypmod = -1, varcollid = 950, varlevelsup = 0, varnosyn = 0, varattnosyn = 1, location = -1} which then triggers the assert in find_base_rel. When looking for other places calling estimate_num_groups I found this in prepunion.c: * XXX you don't really want to know about this: we do the estimation * using the subquery's original targetlist expressions, not the * subroot->processed_tlist which might seem more appropriate. The * reason is that if the subquery is itself a setop, it may return a * processed_tlist containing "varno 0" Vars generated by * generate_append_tlist, and those would confuse estimate_num_groups * mightily. We ought to get rid of the "varno 0" hack, but that * requires a redesign of the parsetree representation of setops, so * that there can be an RTE corresponding to each setop's output. which seems pretty similar to the issue at hand, because the subpath is T_UpperUniquePath (not sure if that passes as setop, but the symptoms match nicely). Not sure what to do about it in cost_incremental_sort, though :-( regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services