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 1jNmfj-0004bt-WD for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Apr 2020 00:09:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jNmfi-00087Y-Ru for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Apr 2020 00:09:54 +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 1jNmfi-00087R-CF for pgsql-hackers@lists.postgresql.org; Mon, 13 Apr 2020 00:09:54 +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 1jNmfd-00078p-Bu for pgsql-hackers@postgresql.org; Mon, 13 Apr 2020 00:09:52 +0000 Received: by mail-wm1-x341.google.com with SMTP id c195so9081610wme.1 for ; Sun, 12 Apr 2020 17:09:49 -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=ktwlS5KR34gy8rm2fHTAtbwNyaM44TxB2zW82etA1L8=; b=MrdESd/c+YCIlOUA3Fvb/z3w85amk7++MlFVdhWWBa7Dd4CvF3VlQPiZwTU5sQzrSg IPxwBpcNtsYTQKeGzjtTJQWhz45PKkdmPrfkc3GgS86cMdNWAfS1Rw0nPYTaX3yBDvH9 dj1i3vXCZ26y7OGQsbyicPDCrf/EWsA7nb86OABqsr1rFzCtN7WAD2FTyMQxU5FGyXcc VxBQzTdi+rEUaMzrX9i81TChyDmXJeYsyRjv5VM3QShGvvhSVOTt8He+zYMUtB0BaMLF 8BXqGb3TV74sKMb9+cVDuPuQ5pRgwqBCViWItknenacRdRkG8nakiud2eJe+56Jgjznp e+bQ== 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=ktwlS5KR34gy8rm2fHTAtbwNyaM44TxB2zW82etA1L8=; b=BxpxkIyUme2t7YPeDJxSehh1uPbgkbZPMg/vVyM+tx2OTj7z8ni7Mb8NvcMla8Fbub GBgUyFecNGdZgxUWS1OO+WmS5c75+TkewclruLUdQUGWXMR/BZ/zkCA2fNGlOTuc5Pq6 iH89m6GUVA4enBcLK6MKZVBs6okL4uEgd9eCk+T9rZ4mOUjx221wrlyQ2Q49Z3J7hDFx ohKAWaAUFrQGNfFOR1+GGMmyDYfl3BHXfF5ZD8U9soFL1JLefEknDE5ACxA3dE8s2q9W 9MORUawRHijjHjNyt/tcT1qPGhFlMHmaWRvDIBON475zL8b3UYnuh6IP87jteye0FTp8 ICdA== X-Gm-Message-State: AGi0PuY4TavNeDNYymVA3UP4ya6OD+Oi08LB1TI78hBlMJse/OIjS0dr QpnW6LEB7H7P/sjTWPtQ7HDXZQ== X-Google-Smtp-Source: APiQypIfC2k5Hj/TfcsDqR4ncuyKw29JwmykbEKZ7rnnisepO2UOjsYDLexEbH0Wp5w9nShGyxTD3w== X-Received: by 2002:a05:600c:2c47:: with SMTP id r7mr15796312wmg.50.1586736586447; Sun, 12 Apr 2020 17:09:46 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id x18sm12126874wmi.29.2020.04.12.17.09.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Apr 2020 17:09:45 -0700 (PDT) Date: Mon, 13 Apr 2020 02:09:43 +0200 From: Tomas Vondra To: Justin Pryzby Cc: James Coleman , pgsql-hackers@postgresql.org Subject: Re: sqlsmith crash incremental sort Message-ID: <20200413000943.bejni6axj7v4q5rn@development> References: <20200411191952.GA30754@telsasoft.com> <20200411202639.2o7quia37zd6ypw7@development> <20200411214639.GK2228@telsasoft.com> <20200411224445.x4whiaeo67glpqen@development> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="3rs6tudy6n2ttmzo" Content-Disposition: inline In-Reply-To: <20200411224445.x4whiaeo67glpqen@development> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --3rs6tudy6n2ttmzo Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Sun, Apr 12, 2020 at 12:44:45AM +0200, Tomas Vondra wrote: >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 :-( > 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. But I'm not entirely sure what to do about it in cost_incremental_sort. The comment (introduced by 89deca582a in 2017) suggests a proper fix would require redesigning the parsetree representation of setops, and it's a bit too late for that. So I wonder what a possible solution might look like. I was hoping we might grab the original target list and use that, similarly to recurse_set_operations, but I'm not sure how/where to get it. Another option is to use something as simple as checking for Vars with varno==0 in cost_incremental_sort() and ignoring them somehow. We could simply use some arbitrary estimate - by assuming the rows are unique or something like that. Yes, I agree it's pretty ugly and I'd much rather find a way to generate something sensible, but I'm not even sure we can generate good estimate when doing UNION of data from different relations and so on. The attached (ugly) patch does this. Justin, can you try if this resolves the crashes or if there's something else going on? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --3rs6tudy6n2ttmzo Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="incremental-sort-groups-fix.patch" diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index 0eef5d7707..34b5ecfd07 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -1804,6 +1804,7 @@ cost_incremental_sort(Path *path, List *presortedExprs = NIL; ListCell *l; int i = 0; + bool found_zero_varno = false; Assert(presorted_keys != 0); @@ -1817,10 +1818,22 @@ cost_incremental_sort(Path *path, /* Extract presorted keys as list of expressions */ foreach(l, pathkeys) { + Node *expr; PathKey *key = (PathKey *) lfirst(l); EquivalenceMember *member = (EquivalenceMember *) linitial(key->pk_eclass->ec_members); + expr = (Node *) member->em_expr; + + if (IsA(expr, RelabelType)) + expr = (Node *) ((RelabelType *) expr)->arg; + + if (IsA(expr, Var) && ((Var *) expr)->varno == 0) + { + found_zero_varno = true; + break; + } + presortedExprs = lappend(presortedExprs, member->em_expr); i++; @@ -1829,7 +1842,11 @@ cost_incremental_sort(Path *path, } /* Estimate number of groups with equal presorted keys */ - input_groups = estimate_num_groups(root, presortedExprs, input_tuples, NULL); + if (!found_zero_varno) + input_groups = estimate_num_groups(root, presortedExprs, input_tuples, NULL); + else + input_groups = input_tuples; + group_tuples = input_tuples / input_groups; group_input_run_cost = input_run_cost / input_groups; --3rs6tudy6n2ttmzo--