From c0ed8aac8e566d6049464d507ce8917a4175d40d Mon Sep 17 00:00:00 2001 From: Marcin Ickiewicz Date: Mon, 27 Oct 2025 11:24:15 +0100 Subject: [PATCH 1/3] speedup sync with prunned haf When hivemind is synced together with HAF in prunned mode then it must started to collect notifications 90 days before expected hive head, not 90 days before current irreversible block which is only as heigh as currently synsed block. --- hive/db/sql_scripts/head_block_time.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hive/db/sql_scripts/head_block_time.sql b/hive/db/sql_scripts/head_block_time.sql index 70c1cc9a7..2f5caed34 100644 --- a/hive/db/sql_scripts/head_block_time.sql +++ b/hive/db/sql_scripts/head_block_time.sql @@ -24,5 +24,5 @@ RETURNS hivemind_app.blocks_view.num%TYPE LANGUAGE 'sql' STABLE AS $BODY$ - SELECT hive.app_get_irreversible_block( 'hivemind_app' ) - CAST( extract(epoch from _time)/3 as INTEGER ); + SELECT hive.get_estimated_hive_head_block() - CAST( extract(epoch from _time)/3 as INTEGER ); $BODY$; -- GitLab From c80db30296c2d34046beda44848abc9c3b9d40c6 Mon Sep 17 00:00:00 2001 From: Marcin Ickiewicz Date: Tue, 28 Oct 2025 08:21:50 +0100 Subject: [PATCH 2/3] for testing purpose pretend that current head block is 5M --- scripts/ci/wrapper_for_app_next_block.sql | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/scripts/ci/wrapper_for_app_next_block.sql b/scripts/ci/wrapper_for_app_next_block.sql index 6a7ce5a9d..0d9e95d7f 100644 --- a/scripts/ci/wrapper_for_app_next_block.sql +++ b/scripts/ci/wrapper_for_app_next_block.sql @@ -30,3 +30,14 @@ BEGIN RETURN __result; END; $BODY$; + +CREATE OR REPLACE FUNCTION hive.get_estimated_hive_head_block() + RETURNS INTEGER + LANGUAGE 'plpgsql' + STABLE +AS +$body$ +BEGIN + RETURN 5000000; +END; +$body$; \ No newline at end of file -- GitLab From 4b82889d6cfbc47b6e9ddd6ef871b6c2e5dd5961 Mon Sep 17 00:00:00 2001 From: Marcin Ickiewicz Date: Fri, 31 Oct 2025 13:06:40 +0100 Subject: [PATCH 3/3] Fix inefficient plans in enum_blocks4hivemind by using LATERAL join During performance tests, the planner occasionally produced suboptimal plans for the per-block aggregation query, resulting in repeated GroupAggregate executions and excessive CPU time. Switching to a LATERAL join makes the aggregation deterministic and consistently fast (~2s ? ~300ms per 1k blocks). --- hive/db/sql_scripts/hafapp_api.sql | 51 +++++++++++++++--------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/hive/db/sql_scripts/hafapp_api.sql b/hive/db/sql_scripts/hafapp_api.sql index af4832949..300115e4a 100644 --- a/hive/db/sql_scripts/hafapp_api.sql +++ b/hive/db/sql_scripts/hafapp_api.sql @@ -68,34 +68,33 @@ AS $function$ BEGIN RETURN QUERY - SELECT -- hivemind_app.hive_api_hivemind_blocks - hb.num - , hb.hash - , hb.prev as prev - , to_char( created_at, 'YYYY-MM-DDThh24:MI:SS' ) as date - , COALESCE( oper.operations, '{}'::jsonb[] ) as operations - FROM hivemind_app.blocks_view hb - LEFT JOIN ( + SELECT + hb.num, + hb.hash, + hb.prev, + to_char(hb.created_at, 'YYYY-MM-DD"T"HH24:MI:SS') AS date, + COALESCE(oper.operations, '{}'::jsonb[]) AS operations + FROM hivemind_app.blocks_view AS hb + LEFT JOIN LATERAL ( SELECT - op.block_num - , ARRAY_AGG( to_jsonb(op) ORDER BY op.id ) as operations - FROM - (SELECT - ho.id - , ho.block_num - , ho.op_type_id as operation_type_id - , ho.op_type_id >= 50 AS is_virtual - , ho.body::VARCHAR - FROM hivemind_app.operations_view ho - WHERE ho.block_num BETWEEN _first_block AND _last_block - AND (ho.op_type_id < 50 - OR ho.op_type_id in (51, 53, 61, 72, 73) - ) - ) as op - GROUP BY op.block_num - ) as oper ON oper.block_num = hb.num + ARRAY_AGG( + to_jsonb(op) ORDER BY op.id + ) AS operations + FROM ( + SELECT + ho.id, + ho.block_num, + ho.op_type_id AS operation_type_id, + ho.op_type_id >= 50 AS is_virtual, + ho.body::VARCHAR + FROM hivemind_app.operations_view ho + WHERE ho.block_num = hb.num + AND (ho.op_type_id < 50 + OR ho.op_type_id IN (51, 53, 61, 72, 73)) + ) AS op + ) AS oper ON TRUE WHERE hb.num BETWEEN _first_block AND _last_block - ORDER by hb.num + ORDER BY hb.num ; END $function$ -- GitLab