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 1mLTMp-0007GS-C4 for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Sep 2021 16:45:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mLTMo-0002Bq-BR for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Sep 2021 16:45:38 +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 1mLTMn-00028W-LW for pgsql-hackers@lists.postgresql.org; Wed, 01 Sep 2021 16:45:38 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mLTMk-0003Qp-0O for pgsql-hackers@postgresql.org; Wed, 01 Sep 2021 16:45:37 +0000 Received: by mail-wm1-x332.google.com with SMTP id g135so225516wme.5 for ; Wed, 01 Sep 2021 09:45:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=FHIsZVVKHh3q2XJIVU40cnk6amzoos+zJ28JhGKmmpY=; b=BTz9qCbDFE4Npp9JkOat/xnzEIVYKv9pF/EcI6lAb7fRp8JCVB5aVLn1ZtAiiI4SjE OXbq+t6XjO+nJzEQdSEOSI9MvlnWRM5TS2TFc88+8u69ZeeJkvy5A31MJ7CSNbQ+9kPk 6h0RgPDxMc1CrvugkUAQBqTobZ5LlA0gIJEHZg/G61ENxSCP0kqR6MlYAXawAv2bSuov uu7023TKBHGg4fqMImRxecCECV6Jr+7cefIzus/p9O0R5m5mrc8f+axKzTHEiKUDdbBF 8XkabDqErbyoVvwBiRTuETJVuFUopssZWdHfhoJ+MPbNBeWvuN6j4fvyiUCvz23Rjvp4 YV/Q== 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; bh=FHIsZVVKHh3q2XJIVU40cnk6amzoos+zJ28JhGKmmpY=; b=HuwQ91aTmkTvt+SAdZzhJ5KuMLcYYIK7SzwjAEXJgB8R27U8GY3SfuHlqiQjFrsxI1 SrLXSBOeTlHa1qlXC9Ic1BcTjOanh9oRH+gMS7wtk/zRMuFLyvHtB3a82MHJTPD578Ha IH8yht6FGrmmIMso+RcUXYESDwDmCxxul5NxhRE2JyDdzjspBQ+F/y2KKMfHfGpLTub6 DCmyggv0DzDhM31wfEcey1Iq81NO4lE3ICltHv0Ko83rNZRkAzsdrmybp3qvlP07bNu3 j2JXMOah8+wd+rUuoK2wgzd7+EV1yqWv49xeXTcSDWoyGsV/YFuAiMJpnJTctwt1OdV3 XZtg== X-Gm-Message-State: AOAM5306PDoCYGIwP0Uc1WIxFMphPwD2ieOxAbu4YbqT4q3FXcbS+bfG nAJQsmtTpKjxPI9q+ymJ34BzpvNlVjL5XQ9M0x5IlaaKnupU6ppZcAzHK8Xjosrfy7C0oKJlFc/ LqfDHouhlN/krB72m/YBT4ZJT8M/O+yF3w4d8SFNgdfj3eOrUSL2Sc49BRBGmtxbKDTw7PFUa9l z8Kwahh4oE+6O7ztuMUsR3KDuJguXxIG0Nc09KpnDJ7/KDF2ed8mvmk6XXgE4QUj98zVXCMUxqj 8FwD0CE60HlR4GkwqFsKIbNFYxL//LqYP78iA== X-Google-Smtp-Source: ABdhPJwYHAh4TtMdqR2RCRlt+CZ+xsDxfZ/Wm6lKMplw8grPegPAatfzFLaBZlz/Frwia1k8tktWWA== X-Received: by 2002:a05:600c:b4e:: with SMTP id k14mr426874wmr.139.1630514731594; Wed, 01 Sep 2021 09:45:31 -0700 (PDT) Received: from [10.137.0.18] (ip-86-49-241-1.net.upcbroadband.cz. [86.49.241.1]) by smtp.gmail.com with ESMTPSA id o15sm10694wra.3.2021.09.01.09.45.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 Sep 2021 09:45:30 -0700 (PDT) Subject: Re: PoC/WIP: Extended statistics on expressions To: Justin Pryzby Cc: Dean Rasheed , hackers References: <3fbbd68b-0b52-cb5a-d468-7e7280c33e6c@enterprisedb.com> <4654037b-8df8-77fb-2981-67be93aebdf1@enterprisedb.com> <20201123022604.GD24052@telsasoft.com> <6455e0ad-2a2e-bdb0-e1f8-fd4983d1c8aa@enterprisedb.com> <087086de-1384-8374-4efc-2e7915071d5c@enterprisedb.com> <20210816013255.GS10479@telsasoft.com> <20210824131320.GO10479@telsasoft.com> From: Tomas Vondra Message-ID: <6bc0ab0c-a807-2b7f-7df8-ac90c7839013@enterprisedb.com> Date: Wed, 1 Sep 2021 18:45:29 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210824131320.GO10479@telsasoft.com> Content-Type: multipart/mixed; boundary="------------00F233D60B21C1EF52C220FE" Content-Language: en-US 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 This is a multi-part message in MIME format. --------------00F233D60B21C1EF52C220FE Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 8/24/21 3:13 PM, Justin Pryzby wrote: > On Mon, Aug 16, 2021 at 05:41:57PM +0200, Tomas Vondra wrote: >>> This still seems odd: >>> >>> 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 >>> >>> It seems wrong that the command works with added parens, but builds expression >>> stats on a simple column (which is redundant with what analyze does without >>> extended stats). >> >> Well, yeah. But I think this is a behavior that was discussed somewhere in >> this thread, and the agreement was that it's not worth the complexity, as >> this comment explains >> >> * XXX We do only the bare minimum to separate simple attribute and >> * complex expressions - for example "(a)" will be treated as a complex >> * expression. No matter how elaborate the check is, there'll always be >> * a way around it, if the user is determined (consider e.g. "(a+0)"), >> * so it's not worth protecting against it. >> >> Patch 0001 fixes the "double parens" issue discussed elsewhere in this >> thread, and patch 0002 tweaks CREATE STATISTICS to treat "(a)" as a simple >> column reference. > > 0002 refuses to create expressional stats on a simple column reference like > (a), which I think is helps to avoid a user accidentally creating useless ext > stats objects (which are redundant with the table's column stats). > > 0002 does not attempt to refuse cases like (a+0), which I think is fine: > we don't try to reject useless cases if someone insists on it. > See 240971675, 701fd0bbc. > > So I am +1 to apply both patches. > > I added this as an Opened Item for increased visibility. > I've pushed both fixes, so the open item should be resolved. However while polishing the second patch, I realized we're allowing statistics on expressions referencing system attributes. So this fails; CREATE STATISTICS s ON ctid, x FROM t; but this passes: CREATE STATISTICS s ON (ctid::text), x FROM t; IMO we should reject such expressions, just like we reject direct references to system attributes - patch attached. Furthermore, I wonder if we should reject expressions without any Vars? This works now: CREATE STATISTICS s ON (11:text) FROM t; but it seems rather silly / useless, so maybe we should reject it. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------00F233D60B21C1EF52C220FE Content-Type: text/x-patch; charset=UTF-8; name="0001-Reject-extended-statistics-referencing-system-attrib.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Reject-extended-statistics-referencing-system-attrib.pa"; filename*1="tch" From a3ade67b8b12cdbfa585bf351ca5569599977b41 Mon Sep 17 00:00:00 2001 From: Tomas Vondra Date: Wed, 1 Sep 2021 17:28:21 +0200 Subject: [PATCH] Reject extended statistics referencing system attributes Extended statistics are not allowed to reference system attributes, but we only enforced that for simple columns. For expressions, it was possible to define the statistics on an expression defining the system attribute, e.g. CREATE STATISTICS s ON (ctid::text) FROM t; which seems strange. This adds a check rejection expressions referencing system attributes, just like we do for simple columns. Backpatch to 14, where extended statistics on expressions were added. Backpath-through: 14 --- src/backend/commands/statscmds.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 59369f8736..bf01840d8a 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -288,9 +288,24 @@ CreateStatistics(CreateStatsStmt *stmt) Node *expr = selem->expr; Oid atttype; TypeCacheEntry *type; + Bitmapset *attnums = NULL; + int k; Assert(expr != NULL); + /* Disallow expressions referencing system attributes. */ + pull_varattnos(expr, 1, &attnums); + + k = -1; + while ((k = bms_next_member(attnums, k)) >= 0) + { + AttrNumber attnum = k + FirstLowInvalidHeapAttributeNumber; + if (attnum <= 0) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("statistics creation on system columns is not supported"))); + } + /* * Disallow data types without a less-than operator. * -- 2.31.1 --------------00F233D60B21C1EF52C220FE--