Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q1uUC-0005Cr-Cv for pgsql-hackers@arkaria.postgresql.org; Wed, 24 May 2023 19:49:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q1uUA-0008Gp-CV for pgsql-hackers@arkaria.postgresql.org; Wed, 24 May 2023 19:49:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q1uUA-0008GN-2M for pgsql-hackers@lists.postgresql.org; Wed, 24 May 2023 19:49:26 +0000 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q1uU2-001h63-FM for pgsql-hackers@lists.postgresql.org; Wed, 24 May 2023 19:49:24 +0000 Received: by mail-ed1-x52a.google.com with SMTP id 4fb4d7f45d1cf-510eb980ce2so2363062a12.2 for ; Wed, 24 May 2023 12:49:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1684957756; x=1687549756; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=G2yLZNwwcsewXQf2ic5d6fsYpeJYaAgwpEZceW8LC3I=; b=R8v8wlHjXQgH2ftK/xt4xrnHM+CrSkCuyvyondh08LZasB6x5yRg41v0FRrGUeQ/nA jSECTmRg3/Adv21FBruTwqG0tezWpQkCwXENqq2J4GO6eA5D4cfSfAiwGr0ra0AR1QQe jEeL2EGiRS4hZ001LDrqgD6ctVAN9TP38poFKO8UECbn01iOF/+j88q8Go/QIAinsMth hyz91maVpkRl+P7p+E5Uh1uL38rSpI4xshqYh/tLzMByzs4At8dF4MppULcYmwEiOTbe BlgYKdL4rcGOr0nnwQCyQysLbYHjSoMXHb9IgO9WA0/01UVwx/GnJit2VbW6mGII8V4O bKZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684957756; x=1687549756; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=G2yLZNwwcsewXQf2ic5d6fsYpeJYaAgwpEZceW8LC3I=; b=epG1cnKdG8whvlNAQg0aqBKFSSeKTHybqD7Bc3eblx30bhbFhCOLV3bOHFSPF5G9Ex IsZdRKqRE4W16d9pFJm1iC/U9WcEfwK1d9LcZidmkFh+jLVL3HB0LHDxQyp6sgfskOtn V+3uDiUCclWDtnPXEjGBAcoMLTup8xnDeqbQWKMQf0Boum1oxpwL4G1AScKbVXHn92u1 PIdMLUDN/gYqTWdYOcu+h+z/42nQ4+uUqSFRX0Lji3yrJbXWFmgGnrxAQbAKfk8Tjr6T lyXPyj/fHuhcutIspfJUsyPYGYp+u0F7FQLJS03MMerlIGBYed4mSgU0Jpgv0YkcKg53 FYtQ== X-Gm-Message-State: AC+VfDwBsjd98nv5A3cr0vN/FJJ8K3rKlekO5amqnYmjmWQ+m2Uez52J ttuHtZBtFTL9Ua30y7LI/eSGWwOYdDix0uueJZVzcAqcsCMS0Fv9oGEYo5UGsqC8CmnAKjw3L1C qRumjIkNMWMNYhsnewywAG9I9JvzLHNqmgx8QcRnhssLrG0DLY0NaHwKL0gbhJNktaLzbdb/ojh 05zcGsQtaIdNiBOqdqsZLTo+TsDxbTY/26rcYemw4LX2pppswd+UjObqXxHLbhYJEyOQ== X-Google-Smtp-Source: ACHHUZ6H3wV5PD4CZJq6dwzdTb4dAl4gHLSqeemINdO6CkiOjKWSvgFAy51mMJpWnOwOuTjEM39brA== X-Received: by 2002:a17:907:a406:b0:967:d161:61c6 with SMTP id sg6-20020a170907a40600b00967d16161c6mr18531611ejc.3.1684957755729; Wed, 24 May 2023 12:49:15 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-225-198.bb.vodafone.cz. [86.49.225.198]) by smtp.gmail.com with ESMTPSA id gw20-20020a170906f15400b0094f109a5b3asm6122045ejb.135.2023.05.24.12.49.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 May 2023 12:49:15 -0700 (PDT) Message-ID: Date: Wed, 24 May 2023 21:49:13 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: memory leak in trigger handling (since PG12) To: Andres Freund Cc: Tom Lane , PostgreSQL Hackers References: <222a3442-7f7d-246c-ed9b-a76209d19239@enterprisedb.com> <960113.1684859980@sss.pgh.pa.us> <20230524185559.un4dqfem3u3ktons@awork3.anarazel.de> Content-Language: en-US From: Tomas Vondra In-Reply-To: <20230524185559.un4dqfem3u3ktons@awork3.anarazel.de> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 5/24/23 20:55, Andres Freund wrote: > Hi, > > On 2023-05-24 15:38:41 +0200, Tomas Vondra wrote: >> I looked at this again, and I think GetPerTupleMemoryContext(estate) >> might do the trick, see the 0002 part. > > Yea, that seems like the right thing here. > > >> Unfortunately it's not much >> smaller/simpler than just freeing the chunks, because we end up doing >> >> oldcxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); >> updatedCols = ExecGetAllUpdatedCols(relinfo, estate); >> MemoryContextSwitchTo(oldcxt); > > We could add a variant of ExecGetAllUpdatedCols that switches the context. > Yeah, we could do that. I was thinking about backpatching, and modifying ExecGetAllUpdatedCols signature would be ABI break, but adding a variant should be fine. > >> and then have to pass updatedCols elsewhere. It's tricky to just switch >> to the context (e.g. in ExecASUpdateTriggers/ExecARUpdateTriggers), as >> AfterTriggerSaveEvent allocates other bits of memory too (in a longer >> lived context). > > Hm - on a quick look the allocations in trigger.c itself are done with > MemoryContextAlloc(). > > I did find a problematic path, namely that ExecGetChildToRootMap() ends up > building resultRelInfo->ri_ChildToRootMap in CurrentMemoryContext. > > That seems like a flat out bug to me - we can't just store data in a > ResultRelInfo without ensuring the memory is lives long enough. Nearby places > like ExecGetRootToChildMap() do make sure to switch to es_query_cxt. > > > Did you see other bits of memory getting allocated in CurrentMemoryContext? > No, I simply tried to do the context switch and then gave up when it crashed on the ExecGetRootToChildMap info. I haven't looked much further, but you may be right it's the only bit. It didn't occur to me it might be a bug - I think the code simply assumes it gets called with suitable memory context, just like we do in various other places. Maybe it should document the assumption. > >> So we'd have to do another switch again. Not sure how >> backpatch-friendly would that be. > > Yea, that's a valid concern. I think it might be reasonable to use something > like ExecGetAllUpdatedColsCtx() in the backbranches, and switch to a > short-lived context for the trigger invocations in >= 16. > WFM regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company