Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tTDn2-0049au-Ok for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Jan 2025 05:30:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tTDm2-00CD4W-6B for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Jan 2025 05:29:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tTDm1-00CD4O-U6 for pgsql-bugs@lists.postgresql.org; Thu, 02 Jan 2025 05:29:33 +0000 Received: from mail-ot1-x32e.google.com ([2607:f8b0:4864:20::32e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tTDlx-002m83-TD for pgsql-bugs@lists.postgresql.org; Thu, 02 Jan 2025 05:29:31 +0000 Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-71e10e6a1ceso2144927a34.0 for ; Wed, 01 Jan 2025 21:29:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1735795768; x=1736400568; darn=lists.postgresql.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=vSmhWYybN+gLigHAdK8fquddrUOQQb99Ycth94UAvh0=; b=SYzgSrDj3dkcWVTUTzBptIMvw7SLlMLDDN8kAEYjZk/Q7XdTai+QvW07ox+JxO/RIl dnTv825y9M5a1uXbMWD5MuRdTuvyJbZldBPnGg+t3tjd67hCa7ZzSrFvRdt1//Ax94KM A9gwn0wds/nHE98p3w3hBdVWS0JXB9tzbPTqM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735795768; x=1736400568; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vSmhWYybN+gLigHAdK8fquddrUOQQb99Ycth94UAvh0=; b=r2hLlyRurb5uokBzfDkGqkfKCjdNyYrRxyEBQyB+UWpZS1GL5SLzjoL4vJiZR0yP/q 5FKFWaaX4vO+b1Z0UjbNuP8rN1dbaas+T9LVfVfQL/dYriDAqrg1ML8DhGhEUifHCM9P UxviitA4WP0MJdjtPUfG+Vzil1dcuRig6qd7vEw3Pi1L5YrQfobExvGbFXy/Yb8yk7kz Gpt/ExVYrhryrNKd7ydS4sCFjTJA1tq5aUPvWMsqacW5NDrV2DHhbV1pya8dezi6Qd7D 6FhAsYH8sQeiTxyUmvrrCR+ByQ4rfSa83iF/SrZFV0lib9CLL6nvckc5DW7Tw6xv4b/K NI1Q== X-Forwarded-Encrypted: i=1; AJvYcCWwgjzGzfBSy3jk2J4T8nyorVe/MtWQwZtnBpKUnI7/hiId6S+HJcJV+fWOvvFp+zjrNtsa+CagrpCi@lists.postgresql.org X-Gm-Message-State: AOJu0Yy8yVNw+VhNKinQZeTKZHAg+yDKlTV/hxkikoKh6eRtpqSfirzF wvk/VB393ERAuJcKHoMshiTTevVwR9iti40legfAlpuJEtkvHSpHBlzfVuLT3A== X-Gm-Gg: ASbGnctKfZWv4ZX4zQxaXhZp5uVNg0fTEhihbSH0cP8Ya42hkLTm5aQCDNmJhecisCT wNnLI/OezUeHznAnXjonMBEKV0g+5FjVt/ZCCEHhw19jdJD5ZGGi4+iyXFZ+7Oy+7Io5g2wCUcr rYXifUzaFGopJigd0F4wGsyzr9Gu/mlyrAsLNbhO0LGg6l4YLrUOjZWYAwIIHCaYMEWQn3VLEs8 NvXG6pZ6/qsoumykC/2JmpjZymuqh+dFTubUXdpBsDX8904cbQ= X-Google-Smtp-Source: AGHT+IGC4nSp4UmNuCJCmvmLMSS7KmdxB7/FVi1HoIbJqd0BntcPMHqCngz6qLBXT+o3wM0ezUwe7Q== X-Received: by 2002:a05:6871:d114:b0:296:e10f:af14 with SMTP id 586e51a60fabf-2a7fb4c9d62mr19994736fac.39.1735795768072; Wed, 01 Jan 2025 21:29:28 -0800 (PST) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-2a7d74c6a03sm8707975fac.18.2025.01.01.21.29.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Jan 2025 21:29:27 -0800 (PST) Date: Wed, 1 Jan 2025 21:29:24 -0800 From: Noah Misch To: David Rowley Cc: Laurenz Albe , Tyler , pgsql-bugs@lists.postgresql.org Subject: Re: Column changes such as an increase in varchar size, can cause extremely slow queries, and postgres should run analyze automatically. Message-ID: <20250102052924.2e.nmisch@google.com> References: <263af164-52f5-4b9c-bac3-5309795fe802@gmx.com> <3a24172c1ed0082e279905392bcd46e3062f81e5.camel@cybertec.at> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Nov 01, 2024 at 10:03:08PM +1300, David Rowley wrote: > On Fri, 1 Nov 2024 at 19:32, Laurenz Albe wrote: > > Would it be an option to clear pg_class.reltuples and pg_stat_all_tables.n_mod_since_analyze > > whenever the statistics for a table are cleared? Then autoanalyze would trigger after 50 > > modifications. > > I wondered about that too. I also wondered which cases we could have > ATExecAlterColumnType() not call RemoveStatistics(). If the table is > going to be rewritten, then we need to, but there must be plenty of > cases where we could forego removing the stats when there's no > rewrite. In the absence of a rewrite, stanullfrac and stawidth always remain valid. The conditions for keeping other stats are like CheckIndexCompatible() deciding to skip an index rebuild. Specifically, I think keeping pg_statistic entries would require (1) staop and stacoll haven't changed and (2) if staop is polymorphic, the argument type hasn't changed. Those conditions would also suffice to keep non-expression pg_statistic_ext_data. That may be enough. If not, type-specific rules would identify more cases where discarding stats has no value. For example, if staop is "<" and the column type is changing between timestamp and timestamptz, none of the "<" comparisons will change. Similarly, staop of "=" works the same for oid and for int4. Beyond those cases where discarding stats has no value, there may be cases where the staop behaves differently before and after, but you could argue that it's better to keep sometimes-incorrect stats than to remove them. For example, <(oid,oid) and <(int4,int4) behave differently due to signed vs. unsigned. Still, the last histogram may be better than no histogram. Keeping pg_statistic_ext_data for expression stats would require expression introspection that CheckIndexCompatible() hasn't bothered with. If we ever implement such expression introspection, I expect it would cover both CheckIndexCompatible() and pg_statistic_ext_data.