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 1oKk0R-0004ov-74 for pgsql-general@arkaria.postgresql.org; Sun, 07 Aug 2022 17:24:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oKk0O-00016i-Km for pgsql-general@arkaria.postgresql.org; Sun, 07 Aug 2022 17:24:00 +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 1oKk0N-00015D-CA for pgsql-general@lists.postgresql.org; Sun, 07 Aug 2022 17:24:00 +0000 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oKk0J-0008Ir-N0 for pgsql-general@postgresql.org; Sun, 07 Aug 2022 17:23:58 +0000 Received: by mail-ed1-x52a.google.com with SMTP id z20so6964704edb.9 for ; Sun, 07 Aug 2022 10:23:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc; bh=Sr1bKakbwJttWZZHRm9nQzZNJ1x/R//Zlmn85aCYxY0=; b=CkMIujN8x/x+molMiqm93lTZYfeZT8n9t7+NvT20mkhFWXPr8bC+fV9vEkblSlj0iK BRgSjDkNWIggpF1lXRCKg0u8OC79YCC6F35I6sD/H0gEDioHYQKk0QTKGQgF2n3y59G6 4hRdi7RJXy84lfdEoonfoUUvCr8Ajuts/yi2oXvjDwI3T1XEB0eDpds5Y3uODMK4e2S7 +Fvm1k7GoKsfnRHrD52ZfMd9LBnp7NSfgaWioXloifzs/IM1uWNvPjBJ2MKVopZKE11f rdrWEnClx5CS4IB8RFfky18fY85FrxiHrMN7IaVCcCrkFs0jSs4IzSuWiEyVhR3D9KGX JVzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=Sr1bKakbwJttWZZHRm9nQzZNJ1x/R//Zlmn85aCYxY0=; b=l2PUvKlsI/JOeG49n5fgPkBUOko6+Hm7EKAFScW3XE3xZJmw3Luqoj1jR/mvrfUt80 gLeX70j4EEhD2+r9CulHTjm/d7kODlES1IrNbl/uwtEPlsGatdI5ySmdKykr2dQhV2wT K0KmxHyFGQ2pHWsLc9VoR6OfOAdBZF5C/Q0nyeSpTh9RUW/8CFylKgt3LI2tknsyzJls LjxcBUkJ2VkeVDfcxyrG+UQL6yby+AYci8xogdB667rLR5lls2/k4U5W9/qZbw94bkCF CC6q2/GbMY9DEvabZj/ZTHPGQxv4BnXV42urEFynkSBfvNHAZpwHQ/ZCHz10eFtRJHFF Ugyg== X-Gm-Message-State: ACgBeo2x88hZNxdQ64iPAaBC0ZkgCi0KsH7w9EAYaB0j9iva40EdHT5T itEulakqaA+3WpgzzOFQgaXQ5Qk6vuJkcXogkbaoEVrRL0F+uqnNgVs8GCV6nG4ojBSvAFAkD2i t/ipJb7JYx88MhGCpp8AQ2OXzXQsQPdbrAnNkLGtHhtzZWgDHn5fK6qZTtpUhiCi3HG/b9+hC22 PRSNbF0vLKKGVs8wvGA95Dxo/G7K5W+oVEZRsgIi45oDOn X-Google-Smtp-Source: AA6agR5O2TwnFTJJA1JuH+rZWTrZD77AY2RuMZPQ4iTs9+8MhtIHJi9AMGVQirq53yOOTFHb1YQpLw== X-Received: by 2002:a05:6402:278d:b0:43d:cf90:c91a with SMTP id b13-20020a056402278d00b0043dcf90c91amr15269065ede.186.1659893034118; Sun, 07 Aug 2022 10:23:54 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-250-151.bb.vodafone.cz. [86.49.250.151]) by smtp.gmail.com with ESMTPSA id bf17-20020a0564021a5100b0043df042bfc6sm3387851edb.47.2022.08.07.10.23.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 07 Aug 2022 10:23:53 -0700 (PDT) Message-ID: <0936f584-a910-1d49-6806-dd0a8303c3e6@enterprisedb.com> Date: Sun, 7 Aug 2022 19:23:52 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: Expr. extended stats are skipped with equality operator Content-Language: en-US To: Danny Shemesh , pgsql-general@postgresql.org References: From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 8/5/22 11:50, Danny Shemesh wrote: > Hey all ! > > I'm on a quest to help the planner (on pg14) use the best of several > partial, expressional indices we have on some large tables (few TBs in > size, billions of records). > > As we know, stats for expressions in partial indices aren't gathered by > default - so I'm tinkering with expressional extended stats to cover for > those. > > I've tackled two interesting points there: > 1. Seems like expressional stats involving the equality operator are > skipped or mismatched (fiddle > ) > Let's take the following naive example: > /create table t1 (x integer[]); > insert into t1 select array[1]::integer[] from generate_series(1, > 100000, 1); > create statistics s1 on (x[1] = 1) from t1; > analyze t1; > / > /explain analyze select * from t1 where x[1] = 1;/ > /> Seq Scan on t1 (cost=0.00..1986.00 rows=500 width=29) (actual > time=0.009..36.035 rows=100000 loops=1)/ > / > / > Now, of course one can just create the stat on x[1] directly in this > case, but I have a more complex use case where an equality operator is > beneficial;  > should the above case be supported ? feels like I'm just missing > something fundamental. > Hmmm. The problem here is that the expression may be interpreted either as an operator clause (A=B), or as a boolean expression ((A=B)=TRUE). In principle we might check which option matches extended statistics, but that's not for free :-( So what the current code does is trying to match the more specific cases first, leaving the "bool expression" as a last resort. That seems like a reasonable trade off to me, but I'll think about this a bit more. There are probably other ways to match expressions, and we can't possibly explore all of them. For example you may create statistics on (x=1 AND y=2) and I doubt we'll match that, because we'll try matching individual clauses not some arbitrary combinations of clauses. (Maybe we shouldn't even allow creating such statistics ...) > 2. Less important, just a minor note - feel free to ignore - although > the eq. operator above seems to be skipped when matching the ext. stats, > I can work around this by using a CASE expression (fiddle > ); > Building on the above example, we can: > /create statistics s2 on (case x[1] when 1 then true else false end) > from t1;/ > /explain analyze select * from t1 where (case x[1] when 1 then true else > false end/ > />  Seq Scan on t1 (cost=0.00..1986.00 rows=100000 width=25) (actual > time=0.011..33.721 rows=100000 loops=1)/ > / Yes, because this end ups not being matches as opclause, and therefore goes all the way to the boolvarsel() in clause_selectivity_ext. > / > What's a bit problematic here, though, is that if we mix other dependent > columns to the extended stat, and specifically if we create an mcv,  > queries involving the CASE expression throw with `error: unknown clause > type 130`, where clause type == T_CaseExpr. > > The second point for me would be that I've found it a bit non intuitive > that creating an extended statistic can fail queries at query time; it > makes sense that the mcv wouldn't work for case expressions, but it > might've been a bit clearer to: > > a. Fail this at statistic creation time, potentially, or  > b. Convert the type numeric in the above error to its text > representation, if we can extract it out at runtime somehow -  > I couldn't find a mapping of clause type numerics to their names, and as > the node tags are generated at compile time, it could be build-dependent > and a bit hard to track down if one doesn't control the build flags > Yeah, this seems like a clear bug - we should not fail queries like this. It's a sign statext_is_compatible_clause() and the MCV code disagrees which clauses are compatible. Can you share an example triggering this? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company