0

I have a table in a PG 14 database having a column containing Infinity values in numeric[] arrays as follow:

SELECT id, factors_list FROM mytable ORDER BY id ASC LIMIT 2;

id | factors_list
---+-------------
1  | {Infinity,1,2.91825,2.2911174796669,1.58367915763394,1.96345397169765,1.41599564744287}
2  | {Infinity,1,1.0625,2.114,4.25,2.18021276595745}

The data type of this column is ARRAY (numeric[]) and the length of the array is variable (with some records being NULL):

SELECT column_name, data_type FROM information_schema.columns WHERE 
table_name = 'mytable' AND column_name = 'factors_list';


   column_name  | data_type 
----------------+-----------
 factors_list   | ARRAY

In order to restore this database table into an older (<14) PG database, I need to replace all Infinity values by any valid number, let's say 99999999.

How could I achieve that in an efficient way? (I have roughly 200'000 rows)

3
  • Why would you need to replace the Infinity values? Support for Infinity goes back until Postgres 8.2 (and possibly even earlier). What is this "older PG database" that doesn't support Infinity? Commented Jan 18, 2022 at 7:44
  • "PostgreSQL 13.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.2.1_pre1) 10.2.1 20201203, 64-bit" : ERROR: cannot convert infinity to numeric SQL state: 0A000 Commented Jan 19, 2022 at 22:20
  • 1
    Indeed numeric doesn't support infinity - but your column must be defined as float[] or double precision[] - you didn't mention that you want to convert this to numeric Commented Jan 19, 2022 at 22:24

1 Answer 1

2

This simple update statement should do the job:

UPDATE mytable 
   SET factors_list = array_replace(factors_list, 'Infinity', 99999999)
 WHERE TRUE;
Sign up to request clarification or add additional context in comments.

3 Comments

recommend to first copy some rows into a test table and update those, SELECT * INTO test_table FROM columns LIMIT 1000. UPDATEs always make me nervous.
@bfris: Then using transactions and no auto commit may help you... ;)
It's definitely not recommended to be nervous with databases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.