Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s9smP-005l0u-IS for pgsql-novice@arkaria.postgresql.org; Wed, 22 May 2024 20:41:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s9smO-003SV8-VM for pgsql-novice@arkaria.postgresql.org; Wed, 22 May 2024 20:41:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s9smO-003SUz-MR for pgsql-novice@lists.postgresql.org; Wed, 22 May 2024 20:41:44 +0000 Received: from mail-ot1-x32f.google.com ([2607:f8b0:4864:20::32f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s9smL-000G4j-QS for pgsql-novice@postgresql.org; Wed, 22 May 2024 20:41:43 +0000 Received: by mail-ot1-x32f.google.com with SMTP id 46e09a7af769-6f10092c8c7so3536106a34.1 for ; Wed, 22 May 2024 13:41:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716410500; x=1717015300; darn=postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=3TcgPg9MyGKio7AHMqUT+OK4r15B0G1PZ+32I9UyDmQ=; b=S1oGf6Ugpb+N4eP3b7dN2vUnIhcKpsS0eGOTA/YibamL7yWAhcl/Szt5AnVFx1qFRQ N2xYhupc/HhzI8JFmY1vxZZISpGK9Wa6BxWOjQ3sC4kwpmEcIcsi4804m8FnUgkVloAn +6Ww98RgbnKKAHQ8jh4BmrvakwtaU0L1rXthWEMwalvctvP56kEALQqG5kxkMMJCU4xX NQd2/d6uu7SnJrkFHZvHKM12vinwmC3pDxpde9DYozDsyVWYIQisG1mAq6pfjdJPXC6B f5f93ycLtq3BztGw4ddjKj6k/KHF/2bJHorOEcfmHwq3h4JgshIeblaDVYoA1r3Ges67 +q4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716410500; x=1717015300; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=3TcgPg9MyGKio7AHMqUT+OK4r15B0G1PZ+32I9UyDmQ=; b=jzSwnsfh4uCBGwUhTV3IlgfNcnYeyQlYlc9FAw5TeepznminIJpOzJ528tNg5saPd8 clvWIZSPvQZCJyHH8dF0jipkLU7r4g41+72V1AxJHs1tuYh1SXnNsPVjEF6XRQLN/H9Z 3y5GQlv7QDMWrSnv1ePb4Tw3d6E9OYvdczqkMx92cQ7kpFJWKHke65i6XlvEKZS9I3mP fc3Tl5OMJc9BeLbVj5ZxQr2Nm02CFgiI8eHotnNqlfuEyQRnPddutxhWUVC7QW23zAN/ cHMhDIhMlAaQ+MY27uKyfIjWJH8+oK5mVvKvteXBY0rTdeSGSdTHn25vSmqF8YTqSblA kJ+Q== X-Gm-Message-State: AOJu0Yyqy7u+HJUVnCbQCDEPh/KtlRnpuEDfa8f61OcCYqFoaGiNvEfW 23GuNMurg2R7pkuqDNVVfGhOzQ+u83Oe4ddwqpDc2EzmAnHLtKK3VKBkZlycr2Q9mdMBpc1wDYL aWadG9buxEGB4ZQnUigFCKQozAtKOfVbE X-Google-Smtp-Source: AGHT+IGjdRqXMuIfHs4PgiyJlJmdwP0oXQoLwbGiyGentqwNHk/qHanXRtKjyfMG4um+eJdHFj8vT47HK5aLCi5/r1U= X-Received: by 2002:a05:6870:c085:b0:229:f022:ef83 with SMTP id 586e51a60fabf-24c68d240femr4327935fac.43.1716410500219; Wed, 22 May 2024 13:41:40 -0700 (PDT) MIME-Version: 1.0 From: Rita Date: Wed, 22 May 2024 16:41:29 -0400 Message-ID: Subject: selecting a materialized view function, plpgsql To: pgsql-novice@postgresql.org Content-Type: multipart/alternative; boundary="000000000000278b2e061910f6b5" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000278b2e061910f6b5 Content-Type: text/plain; charset="UTF-8" Hello. I am using timescaledb with grafana. I am at a point where my telemetry data is very large. I collect a metric every 1 second so per 1 day I have close to (3600*24), 86400 points. Graphing them on grafana has become a challenge because postgresql can't keep up. Especially when I want to view a 7 day or 30 day summary. To fix this, I created materialized views and they seem to be working but its quite error prone when I try to implement this in Grafana. So I decided to take the plpgsql route. I have a function which looks like this create or replace function nview(from_millis BIGINT,to_millis BIGINT) RETURNS SETOF record as $$ DECLARE day_diff INTEGER; query TEXT; BEGIN day_diff := (to_millis - from_millis) / (1000*3600*24); IF day_diff >=10 THEN -- run query which has 1 day averages ELSIF day_diff BETWEEN 4 AND 10 THEN -- run query which has 6 hour summaries ELSE -- run whatever END IF; RETURN QUERY EXECUTE query; END; $$ LANGUAGE plpgsql; Was wondering is this a good approach? Have anyone else used Grafana with Postgresql? -- --- Get your facts first, then you can distort them as you please.-- --000000000000278b2e061910f6b5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello.=C2=A0

I am using time= scaledb with grafana. I am at a point where my telemetry data is very large= . I collect a metric every 1 second so per 1 day I have close to (3600*24),= 86400 points. Graphing them on grafana has become a challenge because post= gresql can't keep up. Especially=C2=A0when I want to view a 7 day or 30= day summary.=C2=A0

To fix this, I created materia= lized=C2=A0views and they seem to be working but its quite error prone when= I try to implement this in Grafana. So I decided to take the plpgsql route= .=C2=A0

I have a function which looks like this
create or replace function nview(from_millis BIGINT,to_millis BIGIN= T)
RETURNS SETOF record as $$
DECLARE
=C2=A0 = day_diff INTEGER;
=C2=A0 query TEXT;
BEGIN
= =C2=A0 day_diff :=3D (to_millis - from_millis) / (1000*3600*24);
= =C2=A0 IF day_diff >=3D10 THEN
=C2=A0 =C2=A0 =C2=A0-- run quer= y which has 1 day averages
=C2=A0 ELSIF day_diff BETWEEN 4 AND 10= THEN
=C2=A0 =C2=A0 -- run query which has 6 hour summaries
=
=C2=A0 ELSE
=C2=A0 =C2=A0 -- run whatever
=C2=A0 E= ND IF;
RETURN QUERY EXECUTE query;
END;
$$ LA= NGUAGE plpgsql;

Was wondering is this a good appro= ach? Have anyone else used Grafana with Postgresql?=C2=A0


-- =
--- Get your facts first, then you can distort them as you ple= ase.--
--000000000000278b2e061910f6b5--