Skip to content

Instantly share code, notes, and snippets.

@MichaelLeeHobbs
Created January 29, 2026 21:31
Show Gist options
  • Select an option

  • Save MichaelLeeHobbs/0f9c8b10a68b1c319a26db99903e8845 to your computer and use it in GitHub Desktop.

Select an option

Save MichaelLeeHobbs/0f9c8b10a68b1c319a26db99903e8845 to your computer and use it in GitHub Desktop.
Mirth 4.5.0 Slow Response Message Report

Mirth Connect: Check Slow Channels

A PostgreSQL function to quickly identify Mirth channels where the Source Connector is taking a long time to process messages. It aggregates statistics and lists specific Message IDs for easy troubleshooting.

1. Installation

Run this once to create the function in your Mirth database.

CREATE OR REPLACE FUNCTION check_slow_channels(
    threshold_seconds INT DEFAULT 15, 
    lookback_days INT DEFAULT 1
)
RETURNS TABLE (
    channel_name TEXT,
    slow_count BIGINT,
    max_duration TEXT,
    avg_duration TEXT,
    latest_occurrence TEXT,
    message_ids TEXT
) AS $$
DECLARE
    r RECORD;
    tbl_name TEXT;
    calc_threshold INTERVAL := (threshold_seconds || ' seconds')::interval;
    calc_lookback  INTERVAL := (lookback_days || ' days')::interval;
BEGIN
    FOR r IN 
        SELECT D.local_channel_id, C.name 
        FROM d_channels D
        INNER JOIN channel C ON C.id = D.channel_id
    LOOP
        tbl_name := 'd_mm' || r.local_channel_id;

        IF EXISTS (SELECT FROM pg_tables WHERE tablename = tbl_name AND schemaname = 'public') THEN
            RETURN QUERY EXECUTE format('
                SELECT 
                    %L::TEXT,
                    COUNT(*),
                    TO_CHAR(MAX(response_date - received_date), ''HH24:MI:SS.MS''),
                    TO_CHAR(AVG(response_date - received_date), ''HH24:MI:SS.MS''),
                    TO_CHAR(MAX(received_date), ''YYYY-MM-DD HH24:MI:SS''),
                    STRING_AGG(message_id::text, '', '' ORDER BY message_id DESC)
                FROM %I
                WHERE connector_name = ''Source''
                  AND response_date IS NOT NULL
                  AND (response_date - received_date) >= %L::interval
                  AND received_date > NOW() - %L::interval
                HAVING COUNT(*) > 0
            ', r.name, tbl_name, calc_threshold, calc_lookback);
        END IF;
    END LOOP;
END;
$$ LANGUAGE plpgsql;

2. Usage

Default (Threshold: >15 seconds, Lookback: 24 hours):

SELECT * FROM check_slow_channels();

Custom (Threshold: >30 seconds, Lookback: 2 days):

SELECT * FROM check_slow_channels(30, 2);

3. Example Output

channel_name slow_count max_duration avg_duration latest_occurrence message_ids
PS360 Batch Report Processor 2 00:01:10.807 00:00:53.473 2026-01-28 16:39:44 751074, 750984
10000 HL7 ROUTER 19 00:02:15.604 00:00:56.023 2026-01-29 19:42:01 8449868, 8449867, 8449866...
10001 API 8 00:02:16.232 00:01:33.779 2026-01-29 19:33:31 9927831, 9927830, 9900241...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment