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 1lPHHM-0000VU-OO for pgsql-hackers@arkaria.postgresql.org; Thu, 25 Mar 2021 04:07:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lPHHL-0008JH-MP for pgsql-hackers@arkaria.postgresql.org; Thu, 25 Mar 2021 04:07:27 +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 1lPHHL-0008JA-Ae for pgsql-hackers@lists.postgresql.org; Thu, 25 Mar 2021 04:07:27 +0000 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lPHHH-0000E1-JZ for pgsql-hackers@postgresql.org; Thu, 25 Mar 2021 04:07:26 +0000 Received: by mail-ed1-x52b.google.com with SMTP id e7so835426edu.10 for ; Wed, 24 Mar 2021 21:07:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:from:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=mLw3x2mTGjJt3XDAlouEQY7MBFKYLgyhITZFguhQt0o=; b=dM/2mhUV5KB2ysTT6dRR3gLu996wK1cVoTxkm2dlceVULpxstvUrDRbYBoGhtXtxxC Nv6XbVlEZKESAyzOWX+md20k49SZf25OWBn2BWvCs4dK0Zj+sTSZzMJdchK9BXHHB2Ex Diqb6LJmMODU2Sfq7SQjik10iMyz0ywLZBh83pwBkyffJErX7ccA/Vslj/PgqEOYJIIj LRAj+8IvBBl+H22hiXsXiYA7giAbGE2GfXFRwDfCOka3VFmhnmDO+8AKCX33l5SexAQI 72nB/0rEd/lhycoqW291aCqvtePvYfW/UUpwtT+Q+7KviILkGGSDaYIMAKauZXtWw6R2 ek9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=mLw3x2mTGjJt3XDAlouEQY7MBFKYLgyhITZFguhQt0o=; b=iJagceEtpq6J5SDUUZk66dQK9aXFUsSCLkuSX2kOeAotesWHuQ8JpNIaGHtnpJtDLF o5hashMlbO4P/WWWZUGfI+N2TqpMrSfS5cgbQBZCnc3vl62+EBdkk0PVYZQnVY76qGyb u59I3/vTgYVhCGSiwn5xH7EPEbmKTU/IuAibAxsX64YRgOr6YpYqB2H3kTjOM7Hbcxnq FCEDJIQ21pr0jODsI7lJ6R+n8+HAJY+N7Jo3YxypDMCa08uyECHNo0egHRlxUyfmTKBM BtDO372t2Rbn/2It11F51Zc5TRDl2VOfSgua21wVJrW+UF/fXwYzpfC8UhoBW14UXV5w GPZg== X-Gm-Message-State: AOAM531RlesTOwlN9aq9wCO/c57XPaqtWuJDxYRmxqJ9Waj1SKRprsSD j54/m4PAMCLaXNmAM7LrBhAUBJqiv80cNTcVcn+R5ypZe6hNx6Sf334b2MEU2sb173meK1rw+JV Bei0324ghVByn5gBx936X7OYxDPVEERPuxew9HLAv1nSXDV+TIKSdsoMb431XKATHOJLdc7DkOL K0qelzd+l0fRmZivmhi60t8ZgRWWSUkprADgJe2Pby9Y+ynZv+j63IxCFUEVYSeXbI6Ee7OEpfY Ru+bHdaHlS0naTvFKG62Wf3IeMY2r/Czqd3kg== X-Google-Smtp-Source: ABdhPJwO7OsFt0Id2is/qgKvBWXwDvszXHrE5VD3iTn+2tsvMzFfZ7atiTocsrKHhMTKSI8UaF+g4A== X-Received: by 2002:a50:ef0a:: with SMTP id m10mr6805245eds.261.1616645242166; Wed, 24 Mar 2021 21:07:22 -0700 (PDT) Received: from [10.137.0.20] (ip-86-49-253-127.net.upcbroadband.cz. [86.49.253.127]) by smtp.gmail.com with ESMTPSA id t12sm2093694edy.56.2021.03.24.21.07.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 Mar 2021 21:07:20 -0700 (PDT) Subject: Re: PoC/WIP: Extended statistics on expressions From: Tomas Vondra To: Dean Rasheed Cc: Justin Pryzby , PostgreSQL Hackers References: <9f6654b1-ca31-f979-0fe8-137683818273@enterprisedb.com> <20210324081402.GB28335@telsasoft.com> <20210324091708.GC28335@telsasoft.com> <1ff3fc47-8f6e-7839-c3ff-6b694956f92e@enterprisedb.com> <1ed534fb-b42b-f008-35ee-4eb930b0088f@enterprisedb.com> Message-ID: Date: Thu, 25 Mar 2021 05:07:17 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <1ed534fb-b42b-f008-35ee-4eb930b0088f@enterprisedb.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/25/21 1:05 AM, Tomas Vondra wrote: > ... > > 0002 is an attempt to fix an issue I noticed today - we need to handle > type changes. Until now we did not have problems with that, because we > only had attnums - so we just reset the statistics (with the exception > of functional dependencies, on the assumption that those remain valid). > > With expressions it's a bit more complicated, though. > > 1) we need to transform the expressions so that the Vars contain the > right type info etc. Otherwise an analyze with the old pg_node_tree crashes > > 2) we need to reset the pg_statistic[] data too, which however makes > keeping the functional dependencies a bit less useful, because those > rely on the expression stats :-( > > So I'm wondering what to do about this. I looked into how ALTER TABLE > handles indexes, and 0003 is a PoC to do the same thing for statistics. > Of couse, this is a bit unfortunate because it recreates the statistics > (so we don't keep anything, not even functional dependencies). > > I think we have two options: > > a) Make UpdateStatisticsForTypeChange smarter to also transform and > update the expression string, and reset pg_statistics[] data. > > b) Just recreate the statistics, just like we do for indexes. Currently > this does not force analyze, so it just resets all the stats. Maybe it > should do analyze, though. > > Any opinions? I need to think about this a bit more, but maybe (b) with > the analyze is the right thing to do. Keeping just some of the stats > always seemed a bit weird. (This is why the 0002 patch breaks one of the > regression tests.) > After thinking about this a bit more I think (b) is the right choice, and the analyze is not necessary. The reason is fairly simple - we drop the per-column statistics, because ATExecAlterColumnType does RemoveStatistics(RelationGetRelid(rel), attnum); so the user has to run analyze anyway, to get any reasonable estimates (we keep the functional dependencies, but those still rely on per-column statistics quite a bit). And we'll have to do the same thing with per-expression stats too. It was a nice idea to keep at least the stats that are not outright broken, but unfortunately it's not a very useful optimization. It increases the instability of the system, because now we have estimates with all statistics, no statistics, and something in between after the partial reset. Not nice. So my plan is to get rid of UpdateStatisticsForTypeChange, and just do mostly what we do for indexes. It's not perfect (as demonstrated in last message), but that'd apply even to option (a). Any better ideas? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company