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 1khc7v-0000Qo-E8 for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Nov 2020 17:29:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khc7t-000884-AR for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Nov 2020 17:29:13 +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 1khc7t-00087x-3a for pgsql-hackers@lists.postgresql.org; Tue, 24 Nov 2020 17:29:13 +0000 Received: from mail-ej1-x644.google.com ([2a00:1450:4864:20::644]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1khc7p-0005PY-Da for pgsql-hackers@postgresql.org; Tue, 24 Nov 2020 17:29:12 +0000 Received: by mail-ej1-x644.google.com with SMTP id gj5so29653161ejb.8 for ; Tue, 24 Nov 2020 09:29:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=CSecoxVbgZEHI2RzeHGfT+7dyMkNJtDxYcylugo0ppA=; b=U0X1yuNl1yCBNUMPm355gh8zbeTm6r2oUEJauqYORT9DeW6O9O/HZBpo17MVQeM090 k4MzRL3YQnf7heZP8T1+HLRxzvvZhuWTv7FOlMn+Sn/Oj1C9Z3qOe7FKL4YPkBCRIUrE WqBTjGR0SFbiNqNCu2inUHroEkKqRgMIdHLKsZ6c9tGHzZlXTbewdEls299choP31EwI HzFgNGzWwiu+Oo9epzzSf9z3BurdvnjlZVDMFQNekNwAMDZspf7/D1vu6MsbCV3bmbey 0LClMx2Lj8CPwSbrXYD6JTJ/NQE4VEcc5tpUAykHap5/ipODeSUuSLkvE1fjO5kQQXh5 42Kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=CSecoxVbgZEHI2RzeHGfT+7dyMkNJtDxYcylugo0ppA=; b=j7gawOTrofNIKvn6O0kSyegQa0ihA38NE1Ny0IsrP/N/VRavKGY6II3xGv028js5Cm omwu5DSbiWFn7EoHA+UHi8J1kMz9spt0BciflieLpZsdpM34CYidFTypZ1FJJIaUfLdB KExFVg6RBGLr6mwdI/rihFJ77EbZk1TmA+zKeDJ+9/Jm0mHCTC+l6JKm2ZvBQ1pgqtpF 7P85An2Usfxy95oUfB2MEKnp25UwDYJu5Her9uMX/i8hVKZq+AC210IouWImzVyDiovm 2v6gg5Xwaw5UF8UJ2UVxy+kdPrys2v/5SHPiEOVGM+Wo5Vi5sgJiuP/X0asCHMqW0e66 hOag== X-Gm-Message-State: AOAM532bbGMjvqc3T87ms7Ie5ihVvFtU9xPs4q83Xn7hUxsHEBnXYRgW 4ahl525++lth+8Sin0jQTpj2Nn53yKPcjwNvdm7LUCpzafURW/BJYWQDLnWr42iMIPC1usKpwku m1GalxRloC9eocrcGcSzgtvjRCUcU/2Hzfqk09BfGFl7dohYR52WOPuseagcB+ovRDQuwqVrYhk N90we7ANjRCOEohfaYxOmQ6ZwZMMmjE9z4qvQ/zn0VEaM15NGqXqcRui/cQQBSD53UDXm5mtRHq og8LM7gvsTsXtuXdnqrdGcs8VWPU01onrT2OQkw X-Google-Smtp-Source: ABdhPJyIHJuajkSmOItFjUfSmLaX3IcQ2DVDkUIlHUQfPf2JmUOHrf6CQR863fAp8r99f6wna0Jmxg== X-Received: by 2002:a17:906:4ed7:: with SMTP id i23mr5093461ejv.172.1606238947199; Tue, 24 Nov 2020 09:29:07 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-245-76.net.upcbroadband.cz. [86.49.245.76]) by smtp.gmail.com with ESMTPSA id gf6sm6663846ejb.80.2020.11.24.09.29.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 24 Nov 2020 09:29:06 -0800 (PST) Subject: Re: PoC/WIP: Extended statistics on expressions To: Justin Pryzby Cc: pgsql-hackers References: <3fbbd68b-0b52-cb5a-d468-7e7280c33e6c@enterprisedb.com> <4654037b-8df8-77fb-2981-67be93aebdf1@enterprisedb.com> <20201123022604.GD24052@telsasoft.com> <20201124162320.GR24052@telsasoft.com> From: Tomas Vondra Message-ID: <28bfe53b-0e75-79c1-3403-ebf10714a94e@enterprisedb.com> Date: Tue, 24 Nov 2020 18:29:05 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 In-Reply-To: <20201124162320.GR24052@telsasoft.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: Precedence: bulk On 11/24/20 5:23 PM, Justin Pryzby wrote: > On Mon, Nov 23, 2020 at 04:30:26AM +0100, Tomas Vondra wrote: >> 0004 - Seems fine. IMHO not really "silly errors" but OK. > > This is one of the same issues you pointed out - shadowing a variable. > Could be backpatched. > > On Mon, Nov 23, 2020 at 04:30:26AM +0100, Tomas Vondra wrote: >>> + errmsg("statistics expressions and predicates can refer only to the table being indexed"))); >>> + * partial-index predicates. Create it in the per-index context to be >>> >>> I think these are copied and shouldn't mention "indexes" or "predicates". Or >>> should statistics support predicates, too ? >>> >> >> Right. Stupid copy-pasto. > > Right, but then I was wondering if CREATE STATS should actually support > predicates, since one use case is to do what indexes do without their overhead. > I haven't thought about it enough yet. > Well, it's not supported now, so the message is bogus. I'm not against supporting "partial statistics" with predicates in the future, but it's going to be non-trivial project on it's own. It's not something I can bolt onto the current patch easily. >> 0006 - Not sure. I think CreateStatistics can be fixed with less code, >> keeping it more like PG13 (good for backpatching). Not sure why rename >> extended statistics to multi-variate statistics - we use "extended" >> everywhere. > > - if (build_expressions && (list_length(stxexprs) == 0)) > + if (!build_expressions_only && (list_length(stmt->exprs) < 2)) > ereport(ERROR, > (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), > - errmsg("extended expression statistics require at least one expression"))); > + errmsg("multi-variate statistics require at least two columns"))); > > I think all of "CREATE STATISTICS" has been known as "extended stats", so I > think it may be confusing to say that it requires two columns for the general > facility. > >> Not sure what's the point of serialize_expr_stats changes, >> that's code is mostly copy-paste from update_attstats. > > Right. I think "i" is poor variable name when it isn't a loop variable and not > of limited scope. > OK, I understand. I'll consider tweaking that. >> 0007 - I suspect this makes the pg_stats_ext too complex to work with, >> IMHO we should move this to a separate view. > > Right - then unnest() the whole thing and return one row per expression rather > than array, as you've done. Maybe the docs should say that this returns one > row per expression. > > Looking quickly at your new patch: I guess you know there's a bunch of > lingering references to "indexes" and "predicates": > > I don't know if you want to go to the effort to prohibit this. > postgres=# CREATE STATISTICS asf ON (tableoid::int+1) FROM t; > CREATE STATISTICS > Hmm, we're already rejecting system attributes, I suppose we should do the same thing for expressions on system attributes. > I think a lot of people will find this confusing: > > postgres=# CREATE STATISTICS asf ON i FROM t; > ERROR: extended statistics require at least 2 columns > postgres=# CREATE STATISTICS asf ON (i) FROM t; > CREATE STATISTICS > > postgres=# CREATE STATISTICS asf (expressions) ON i FROM t; > ERROR: extended expression statistics require at least one expression > postgres=# CREATE STATISTICS asf (expressions) ON (i) FROM t; > CREATE STATISTICS > > I haven't looked, but is it possible to make it work without parens ? > Hmm, you're right that may be surprising. I suppose we could walk the expressions while creating the statistics, and replace such trivial expressions with the nested variable, but I haven't tried. I wonder what the CREATE INDEX behavior would be in these cases. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company