From fbfef4db060db2791ed3005a68ca66cd4d173cac Mon Sep 17 00:00:00 2001 From: Groovie | Mango <95291500+grooviegermanikus@users.noreply.github.com> Date: Thu, 11 Jan 2024 17:39:44 +0100 Subject: [PATCH] slow sqlfunction (#42) * rework array_dedup_append function * array_dedup_append: implemented faster version uses nested array remove for merge+deduplication --- migration.sql | 32 +++++++++++++++----------------- src/postgres.rs | 2 +- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/migration.sql b/migration.sql index 72d8e24..7540c80 100644 --- a/migration.sql +++ b/migration.sql @@ -132,23 +132,21 @@ CREATE TABLE banking_stage_results_2.accounts_map_transaction_latest( tx_ids BIGINT[] ); -CREATE OR REPLACE FUNCTION __array_reverse(anyarray) RETURNS anyarray AS $$ - SELECT ARRAY( - SELECT $1[i] - FROM generate_subscripts($1,1) AS s(i) - ORDER BY i DESC - ); -$$ LANGUAGE SQL STRICT IMMUTABLE; - - CREATE OR REPLACE FUNCTION array_dedup_append(base bigint[], append bigint[], n_limit int) RETURNS bigint[] AS $$ - SELECT __array_reverse(array_agg(val)) FROM ( - SELECT val FROM ( - SELECT DISTINCT ON (val) pos, val FROM unnest(__array_reverse(array_cat(base, append))) WITH ORDINALITY as t(val, pos) - ) AS deduped - ORDER BY pos - LIMIT n_limit - ) AS result -$$ LANGUAGE SQL STRICT IMMUTABLE; +DECLARE + tmplist bigint[]; + el bigint; + len int; +BEGIN + tmplist := base; + FOREACH el IN ARRAY append LOOP + tmplist := array_remove(tmplist, el); + tmplist := tmplist || el; + END LOOP; + + len := CARDINALITY(tmplist); + RETURN tmplist[(len + 1 - n_limit):]; +END +$$ LANGUAGE plpgsql IMMUTABLE CALLED ON NULL INPUT; diff --git a/src/postgres.rs b/src/postgres.rs index 92adb2b..9c8b485 100644 --- a/src/postgres.rs +++ b/src/postgres.rs @@ -454,7 +454,7 @@ impl PostgresSession { SELECT acc_id, array_dedup_append( - COALESCE((SELECT tx_ids FROM banking_stage_results_2.accounts_map_transaction_latest WHERE acc_id=amt_new.acc_id ), array[]::bigint[]), + (SELECT tx_ids FROM banking_stage_results_2.accounts_map_transaction_latest WHERE acc_id=amt_new.acc_id), amt_new.tx_agged, {limit}) AS tx_ids_agg FROM amt_new