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 1jROL3-0005PG-0U for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Apr 2020 22:59:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jROL1-0006WB-RB for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Apr 2020 22:59:27 +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 1jROL1-0006Vz-KR for pgsql-hackers@lists.postgresql.org; Wed, 22 Apr 2020 22:59:27 +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 1jROKz-0004Ji-5X for pgsql-hackers@postgresql.org; Wed, 22 Apr 2020 22:59:26 +0000 Received: by mail-wr1-x442.google.com with SMTP id d17so4573700wrg.11 for ; Wed, 22 Apr 2020 15:59:25 -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=e8hEuEv2tB7u2QOz+/CRGnsVHdXPMAcrV5dr7CFsnko=; b=yzsFQL9xaO37XkziXcJrz8AeUAIdrziEuD12nlSIM2hxfgyc2ff9NoAcmIknPXwgyU YtOgUy6osk3XMj3WF1vDYpR6SzSqNOqotumOuAZswFxT9+rsU0mOYuwXjNLYVNo/k436 6ws+g3Rt9D8iMSF1Qj2miFrV2rW1gTxK/TdOL0fkBa8mLA418C1H1UMzBiDA6NVkIf62 Cya7zC2TFyWpZdaqCQwDz5z4797kG6GjthEr98eeEy/1mYkcrdZ9ucvj2l30Pi4+Pnpy djXV+jKjSor7jS43CLbsM102hkr7tnkgbDCv9gmsM+pm9L4bUTyFIdR6rrWnYLeTLt0+ a3vg== 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=e8hEuEv2tB7u2QOz+/CRGnsVHdXPMAcrV5dr7CFsnko=; b=CLsxYYKvaVPWmYwcl6CTy9EYlerah5T60GfbD9y04ZAeHFrhobPJ6yeB/Wx3izdi6A bwK+owBw+ZizcPx/JJ0bwmC8uOhQr3ho6fOaelXyGNPFF96GByRDXx8NGTJltnYbjWj1 akWQjLHyZXyriRcBYDx/yFKtMZdq+itkH9JXTYBaRPahzoTLybGRucdGCVphv1A4YGtn myYXQ6eFsUBdqiM1BwREzxWw55arVcOUcs3V5W1BF3yzkrpXXMMc7uRRUSN8JmWYr3rw YL3WU6jYMstCodCjf3t6FIMqfoe5jgkRJ3NLGI/RtcE8E5S3wEsrpI73NhPI/bP0lWPc Fsjw== X-Gm-Message-State: AGi0PuZtukvdDMM28cCokERuDT8vnxj/TakpwGXOSKGljyn4TEAABspw UokZxb163OS9n/U/BqFnWkYJgQ== X-Google-Smtp-Source: APiQypIKU+mT6ITa68VZTfQyHiR4gXuiyLytYty5Z4UFVvA4DJ4bqAzEZ0BhP9sETqW17/tSNpMHPQ== X-Received: by 2002:adf:a15d:: with SMTP id r29mr1387199wrr.134.1587596362999; Wed, 22 Apr 2020 15:59:22 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id v19sm978425wra.57.2020.04.22.15.59.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Apr 2020 15:59:22 -0700 (PDT) Date: Thu, 23 Apr 2020 00:59:19 +0200 From: Tomas Vondra To: James Coleman Cc: Tom Lane , Justin Pryzby , pgsql-hackers Subject: Re: sqlsmith crash incremental sort Message-ID: <20200422225919.ucs5xnlgnr5ekhod@development> References: <20200411214639.GK2228@telsasoft.com> <20200411224445.x4whiaeo67glpqen@development> <20200413000943.bejni6axj7v4q5rn@development> <20200415144712.wyvpgvsujzzcf2qg@development> <20200417005420.ye342t2nljg4nuhk@development> <20403.1587086800@sss.pgh.pa.us> <20200419234729.tglhf2iqlpkkpk3m@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20200419234729.tglhf2iqlpkkpk3m@development> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Apr 20, 2020 at 01:47:29AM +0200, Tomas Vondra wrote: >On Sat, Apr 18, 2020 at 02:23:25PM -0400, James Coleman wrote: >>On Thu, Apr 16, 2020 at 9:26 PM Tom Lane wrote: >>> >>>Tomas Vondra writes: >>>> I think we have essentially three options: >>>> 1) assuming there's just a single group >>>> 2) assuming each row is a separate group >>>> 3) something in between >>>> If (1) and (2) are worst/best-case scenarios, maybe we should pick >>>> something in between. We have DEFAULT_NUM_DISTINCT (200) which >>>> essentially says "we don't know what the number of groups is" so maybe >>>> we should use that. >>> >>>I wouldn't recommend picking either the best or worst cases. >>> >>>Possibly DEFAULT_NUM_DISTINCT is a sane choice, though it's fair to >>>wonder if it's quite applicable to the case where we already know >>>we've grouped by some columns. >> >>Do you think defining a new default, say, >>DEFAULT_NUM_DISTINCT_PRESORTED is preferred then? And choose some >>value like "1/2 of the normal DEFAULT_NUM_DISTINCT groups" or some >>such? >> > >If we had a better intuition what a better value is, maybe. But I don't >think we have that at all, so I'd just use the existing one. > I've pushed fix with the DEFAULT_NUM_DISTINCT. The input comes from a set operation (which is where we call generate_append_tlist), so it's probably fairly unique, so maybe we should use input_tuples. But it's not guaranteed, so DEFAULT_NUM_DISTINCT seems reasonably defensive. One detail I've changed is that instead of matching the expression directly to a Var, it now calls pull_varnos() to also detect Vars somewhere deeper. Lookig at examine_variable() it calls find_base_rel for such case too, but I haven't tried constructing a query triggering the issue. One improvement I can think of is handling lists with only some expressions containing varno 0. We could still call estimate_num_groups for expressions with varno != 0, and multiply that by the estimate for the other part (be it DEFAULT_NUM_DISTINCT). This might produce a higher estimate than just using DEFAULT_NUM_DISTINCT directly, resulting in a lower incremenal sort cost. But it's not clear to me if this can even happen - AFAICS either all Vars have varno 0 or none, so I haven't done this. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services