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)

1
  • arrays are immutable in pg, you have to do copy without requested numbers Commented Dec 20, 2011 at 20:02

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

Comments

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.