Subject: ERROR: stack depth limit exceeded

From: Csányi Pál <[email-masked]>
Date: Wed, 19 Feb 2025 11:23:18 +0100

Hi,

I am running postgresql 15 on Debian 12 operating system.
I also installed PgAdmin4 and am using the desktop version of it.
I have created a trigger function and a trigger which will run the
following code, see below.
Before this trigger would run, I tried this code.
When running the code I get the following error message:
ERROR: stack depth limit exceeded HINT: Increase the configuration
parameter "max_stack_depth" (currently 2048kB), after ensuring the
platform's stack depth limit is adequate.

Where am I making a mistake, and what can I do to make this code and
this trigger run successfully?

-- Insert the RSD saved between my last two pensions in the
haavi_megtakaritasaink table.
-- INSERT INTO x(a) VALUES (tsrange($1, $2, '[)'));
-- Includes the first date [, the second date is not ).
INSERT INTO
    vagyonunk_kezelese.public.havonkenti_megtakaritasaink
(ezen_idokozben, megtakaritva_rsd, kelt)
VALUES ((select tsrange((
        SELECT
            kelt
        FROM
            javaink_forgalma
        WHERE
            EN_KAPTAM = 'true'
            AND AZ_ARU_NEVE = 'nyugdíjam'
        ORDER BY
            KELT DESC
        LIMIT
            1
        OFFSET
            1
    ),(
        SELECT
            KELT
        FROM
            JAVAINK_FORGALMA
        WHERE
            EN_KAPTAM = 'true'
            AND AZ_ARU_NEVE = 'nyugdíjam'
        ORDER BY
            KELT DESC
        LIMIT
            1
        OFFSET
            0
    ),'[)')),(SELECT
    SUM(EGYSEGAR * ENNYI_EGYSEG)
FROM
    JAVAINK_FORGALMA
WHERE
    A_PENZNEM = 'RSD'
    AND KELT >= (
        SELECT
            KELT
        FROM
            JAVAINK_FORGALMA
        WHERE
            EN_KAPTAM = 'true'
            AND AZ_ARU_NEVE = 'nyugdíjam'
        ORDER BY
            KELT DESC
        LIMIT
            1
        OFFSET
            1
    )
    AND KELT < (
        SELECT
            KELT
        FROM
            JAVAINK_FORGALMA
        WHERE
            EN_KAPTAM = 'true'
            AND AZ_ARU_NEVE = 'nyugdíjam'
        ORDER BY
            KELT DESC
        LIMIT
            1
        OFFSET
            0
    )), DEFAULT)
;

Thanks for all the advice!

--
Best wishes by Paul Chanyi!

---

From: Greg Sabino Mullane <[email-masked]>
Date: Wed, 19 Feb 2025 09:31:33 -0500

Not related to the main question, but that query could use a little
adjustment. Something like:

WITH x AS (
  select kelt from javaink_forgalma
  where en_kaptam is true and az_aru_neve = 'nyugdíjam'
  order by kelt desc limit 2
)
,y AS (select min(kelt) from x)
,z AS (select max(kelt) from x)
INSERT INTO public.havonkenti_megtakaritasaink (ezen_idokozben,
megtakaritva_rsd, kelt)
VALUES (
  (select tsrange(min,max,'[)') FROM y,z)
, (select sum(egysegar * ennyi_egyseg) from javaink_forgalma, y, z
   where a_penzem = 'RSD' and kelt >= min and kelt < max)
, DEFAULT
);

Also, it's best to use "timestamp with time zone" NOT "timestamp without
time zone" whenever possible (i.e. timestamptz not timestamp)

Cheers,
Greg

--
Crunchy Data - https://www.crunchydata.com
Enterprise Postgres Software Products & Tech Support