Skip to content

Instantly share code, notes, and snippets.

@slitayem
Last active January 26, 2017 15:40
Show Gist options
  • Save slitayem/225a9a4da1db3d81660cca518a4d773e to your computer and use it in GitHub Desktop.
Save slitayem/225a9a4da1db3d81660cca518a4d773e to your computer and use it in GitHub Desktop.
[Postgresql] Test array update efficiency
CREATE OR REPLACE FUNCTION random_int_array(dim integer, min integer, max integer) RETURNS integer[] AS $BODY$
begin
return (select array_agg(round(random() * (max - min)) + min) from generate_series (0, dim));
end
$BODY$ LANGUAGE plpgsql;
CREATE EXTENSION pgcrypto;
CREATE TABLE test_table as select id, md5(random()::text) || id as name, random_int_array(500000, 100, 10000) as arr, gen_random_uuid() uid from generate_Series(1,3) id
;
\timing
update test_table set arr=arr[10:array_length(arr,1)];
--- get only last two elements from array
select arr[array_upper(arr, 1) -(2 - 1):array_upper(arr, 1)] from test_table;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment