4

Are there any available functions for work with big integers?

I've found a module intarray, but functions from this module only work with integer, not bigint.

I miss a function for removing an item from an array. Something like the implementation of a "minus" operator in mentioned module:

int[] - int (remove entries matching right argument from array)

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Radek Simko
  • 15,886
  • 17
  • 69
  • 107

1 Answers1

5

Postgres 9.3 or newer

With the added array_remove(anyarray, anyelement), the task is simple now:

test=> SELECT array_remove('{1,3,4,3}'::bigint[], 3);
 array_remove 
--------------
 {1,4}

Postgres 9.2 or older

Create a custom function. This one is reasonably fast:

CREATE OR REPLACE FUNCTION arr_subtract(int8[], int8[])
  RETURNS int8[]
  LANGUAGE sql IMMUTABLE AS
$func$
SELECT ARRAY(
   SELECT a
   FROM   unnest($1) WITH ORDINALITY x(a, ord)
   WHERE  a <> ALL ($2)
   ORDER  BY ord
   );
$func$;

Call:

SELECT arr_subtract('{3,5,6,7,8,9}':: int8[], '{3,4,8}'::int8[]);

Result:

{5,6,7,9}

Allows to remove multiple values at once.
Keeps the original order of the array.
Does not work for NULL values.

Related:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228