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.96) (envelope-from ) id 1w4fo2-002R4c-1u for pgsql-admin@arkaria.postgresql.org; Mon, 23 Mar 2026 13:58:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w4fo0-000Zwm-39 for pgsql-admin@arkaria.postgresql.org; Mon, 23 Mar 2026 13:58:57 +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.96) (envelope-from ) id 1w4fn8-000WCz-2i for pgsql-admin@lists.postgresql.org; Mon, 23 Mar 2026 13:58:03 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w4fn6-00000000hIL-2fue for pgsql-admin@lists.postgresql.org; Mon, 23 Mar 2026 13:58:02 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 62NDvvBB3531046; Mon, 23 Mar 2026 09:57:57 -0400 From: Tom Lane To: Mauricio Martini cc: "pgsql-admin@lists.postgresql.org" Subject: Re: Technical validation on altering atttypmod for numeric column in PostgreSQL In-reply-to: References: Comments: In-reply-to Mauricio Martini message dated "Mon, 23 Mar 2026 13:02:42 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3531044.1774274277.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 23 Mar 2026 09:57:57 -0400 Message-ID: <3531045.1774274277@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Mauricio Martini writes: > I am evaluating an approach to change the scale of a numeric column from= numeric(18,2) to numeric(18,4) in a large table, aiming to avoid the cost= of a full table rewrite. This cannot work, per the comments for numeric_support(): * Planner support function for the numeric() length coercion function. * * Flatten calls that solely represent increases in allowable precision. * Scale changes mutate every datum, so they are unoptimizable. Some valu= es, * e.g. 1E-1001, can only fit into an unconstrained numeric, so a change f= rom * an unconstrained numeric to any constrained numeric is also unoptimizab= le. If it were safe, then that infrastructure would already be recognizing that the ALTER TABLE doesn't require a rewrite. Now, the "mutate every datum" bit is just referring to the dscale field, and maybe you're okay with the dscale not changing (so that existing entries would continue to print with 2 not 4 decimal places, with follow-on implications for the number of decimal places in arithmetic results). But there's another problem: (18,2) accepts some values that (18,4) will not, since you're taking two digits away from the integer part to give them to the fraction part. It would have to be (20,4) to be certain that you didn't create a column containing values it should not. If you want to pursue this, I strongly urge experimenting with a test table in a scratch database. regards, tom lane