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 1sGhNh-00G5nf-Qn for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Jun 2024 15:56:26 +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 1sGhNf-00DnUi-9K for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Jun 2024 15:56:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sGhNe-00DnUZ-VB for pgsql-hackers@lists.postgresql.org; Mon, 10 Jun 2024 15:56:23 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sGhNc-000ecm-Hc for pgsql-hackers@postgresql.org; Mon, 10 Jun 2024 15:56:22 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 45AFuJq0735134; Mon, 10 Jun 2024 11:56:19 -0400 From: Tom Lane To: Ilia Evdokimov cc: pgsql-hackers@postgresql.org Subject: Re: list_free in addRangeTableEntryForJoin In-reply-to: References: Comments: In-reply-to Ilia Evdokimov message dated "Mon, 10 Jun 2024 13:35:39 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <735132.1718034979.1@sss.pgh.pa.us> Date: Mon, 10 Jun 2024 11:56:19 -0400 Message-ID: <735133.1718034979@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Ilia Evdokimov writes: > I have identified a potential memory leak in the > `addRangeTableEntryForJoin()` function. The second parameter of > `addRangeTableEntryForJoin()`, `colnames`, is a `List*` that is > concatenated with another `List*`, `eref->colnames`, using > `list_concat()`. We need to pass only the last `numaliases` elements of > the list, for which we use `list_copy_tail`. This function creates a > copy of the `colnames` list, resulting in `colnames` pointing to the > current list that will not be freed. Consequently, a new list is already > concatenated. > To address this issue, I have invoked `list_free(colnames)` afterwards. Sadly, I think this is basically a waste of development effort. The parser, like the planner and rewriter and many other Postgres subsystems, leaks tons of small allocations like this. That's *by design*, and *it's okay*, because we run these steps in short- lived memory contexts that will be reclaimed in toto as soon as the useful output data structures are no longer needed. It's not worth the sort of intellectual effort you've put in in this thread to prove whether individual small structures are no longer needed. Plus, in many cases that isn't obvious, and/or it'd be notationally messy to reclaim things explicitly at a suitable point. If we were talking about a potentially-very-large data structure, or one that we might create very many instances of during one parsing pass, then it might be worth the trouble to free explicitly; but I don't see that concern applying here. You might find src/backend/utils/mmgr/README to be worth reading. regards, tom lane