Re: Increasing a NUMERIC column precision doesn't cause a table rewrite. Why?
| От | Weck, Luis |
|---|---|
| Тема | Re: Increasing a NUMERIC column precision doesn't cause a table rewrite. Why? |
| Дата | |
| Msg-id | IA3PR10MB8113F22114E96E0ECA96354F8DC7A@IA3PR10MB8113.namprd10.prod.outlook.com обсуждение исходный текст |
| Ответ на | Increasing a NUMERIC column precision doesn't cause a table rewrite. Why? (Marcelo Fernandes <marcefern7@gmail.com>) |
| Список | pgsql-general |
From: Marcelo Fernandes <marcefern7@gmail.com>Date: Monday, November 3, 2025 at 5:24 AMTo: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>Subject: Increasing a NUMERIC column precision doesn't cause a table rewrite. Why?Hi everyone,A numeric field is defined as: NUMERIC(precision, scale) [0].After doing some playing around with different operations against a numericcolumn, namely: increasing/decreasing the precision or increasing/decreasingthe scale, I noticed that the table is rewritten in all cases except whenincreasing the precision number.Take the following snippet as example (tested on postgres 15):```sqlCREATE DATABASE test_precision_field;-- \c test_precision_fieldSET client_min_messages=debug1;DROP TABLE IF EXISTS foo;CREATE TABLE foo (id SERIAL PRIMARY KEY, bar NUMERIC(10, 2));-- Only increase precision but keep the scale:-- This does not rewrite the tableALTER TABLE foo ALTER COLUMN bar TYPE numeric(20, 2);-- Only decrease the precision but keep the scale:-- This rewrites the table.ALTER TABLE foo ALTER COLUMN bar TYPE numeric(10, 2);-- DEBUG: rewriting table "foo"-- DEBUG: building index "foo_pkey" on table "foo" serially-- DEBUG: index "foo_pkey" can safely use deduplication-- Only increase the scale, but keep the precision:-- This rewrites the table.ALTER TABLE foo ALTER COLUMN bar TYPE numeric(10, 4);-- DEBUG: rewriting table "foo"-- DEBUG: building index "foo_pkey" on table "foo" serially-- DEBUG: index "foo_pkey" can safely use deduplication-- Only decrease the scale, but keep the precision:-- This rewrites the table.ALTER TABLE foo ALTER COLUMN bar TYPE numeric(10, 2);-- DEBUG: rewriting table "foo"-- DEBUG: building index "foo_pkey" on table "foo" serially-- DEBUG: index "foo_pkey" can safely use deduplication```My assumption is that once the precision is increased, Postgres can stillinterpret the old values that were generated with a lower precision, whereasthis is not possible for any other types of changes on the numeric field.Is that assumption correct? How can I verify it?For example, is there a catalog table I can have a look to see how these valuesare defined, along with some form of documentation to understand how they areinterpreted by Postgres?Thank you,Marcelo.
My understanding is that Postgres can guarantee that all the values in the table will fit the new precision without having to check. If you change the scale, it might be the case that some values won’t fit anymore and Postgres must return an error. Numeric by itself is a variable length type, similar to TEXT (in a sense). That’s why we can modify its size without requiring a table rewrite at all!
В списке pgsql-general по дате отправления: