Re: change type from NUMERIC(14,4) to NUMERIC(24,12)

From: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: 'Torsten Förtsch *EXTERN*' <tfoertsch123(at)gmail(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: change type from NUMERIC(14,4) to NUMERIC(24,12)
Date: 2017-01-24 10:48:51
Message-ID: A737B7A37273E048B164557ADEF4A58B539D093C@ntex2010i.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Torsten Förtsch wrote:
> we have a large table and want to change the type of one column from NUMERIC(14,4) to NUMERIC(24,12).
> If the new type is just NUMERIC without any boundaries, the operation is fast. If (24,12) is
> specified, it takes ages.
>
> I think it takes so long because the database wants to check that all data in the table is compatible
> with the new type. But the old type has stricter boundaries both before and after the dot. So, it is
> compatible. It has to be.
>
> Is there a way to change the type skipping the additional check?
>
> This is 9.6.

If you don't mind doing something unsupported, you could just modify
the attribute metadata in the catalog:

test=# CREATE TABLE a(x numeric(14,4));
CREATE TABLE
test=# INSERT INTO a VALUES (1234567890.1234);
INSERT 0 1
test=# UPDATE pg_attribute
SET atttypmod = atttypmod + (24 - 14) * 65536 + (12 - 4)
WHERE attrelid = 'a'::regclass AND attname = 'x';
UPDATE 1
test=# \d a
Table "public.a"
Column | Type | Modifiers
--------+----------------+-----------
x | numeric(24,12) |

test=# SELECT * FROM a;
x
-----------------
1234567890.1234
(1 row)

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Torsten Förtsch 2017-01-24 11:07:49 Re: change type from NUMERIC(14,4) to NUMERIC(24,12)
Previous Message Torsten Förtsch 2017-01-24 09:53:55 change type from NUMERIC(14,4) to NUMERIC(24,12)