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 1kMDCy-0005up-6I for pgsql-bugs@arkaria.postgresql.org; Sat, 26 Sep 2020 16:38:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kMDCu-0002HF-Gm for pgsql-bugs@arkaria.postgresql.org; Sat, 26 Sep 2020 16:37:56 +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 1kMDCt-0002H8-Uq for pgsql-bugs@lists.postgresql.org; Sat, 26 Sep 2020 16:37:56 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kMDCm-0007m4-Qb for pgsql-bugs@lists.postgresql.org; Sat, 26 Sep 2020 16:37:54 +0000 Received: from [192.168.28.74] (cyclops.postgrespro.ru [93.174.131.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id B710521C384D; Sat, 26 Sep 2020 19:37:45 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1601138265; bh=gWVMy/H3uhSPF8pODqeB9WZMMawxu0DuZqcd5tJ+Kw4=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=S3RYvncOdp08TkDOqxH/zLd4AxB8GRKtMOxADAd30gSh6bcaJpswgmFSPZ3fspQJS 2o/e/VC1/u05egKpb+CrMg2vbEbgC72VunsKfEneRNhBfZCZVzXVd7mMm8rN255KsF cpbeF5LJ9lr7QIW3THFeoQHlwWmNp3esIc/BQ02Y= Subject: Re: Memory leak in RelationBuildRowSecurity To: Tom Lane Cc: pgsql-bugs@lists.postgresql.org References: <21356c12-8917-8249-b35f-1c447231922b@postgrespro.ru> <1852787.1601041699@sss.pgh.pa.us> <3f670fa6-5e81-fbd9-b880-eaa275f15e1c@postgrespro.ru> <1937613.1601057234@sss.pgh.pa.us> From: Konstantin Knizhnik Message-ID: Date: Sat, 26 Sep 2020 19:37:45 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <1937613.1601057234@sss.pgh.pa.us> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 25.09.2020 21:07, Tom Lane wrote: > Konstantin Knizhnik writes: >> On 25.09.2020 16:48, Tom Lane wrote: >>> Hm ... this smells a whole lot like the issue we fixed recently for >>> partition expressions, namely: what the devil are we doing reloading >>> these expressions during RelationClearRelation? We should delay >>> populating that cache until the value is requested. >> Sorry, this stack trace was obtained at 9.6 version of Postgres which >> the customer is using. >> So may be the problem with cache invalidation is already fixed. > Not sure if we've changed anything much in that area. I've occasionally > wondered if we should not increase the size of the sinval message queue, > but it hasn't happened yet. > > Anyway, looking more closely at RelationBuildRowSecurity, it doesn't > seem to be doing anything more dangerous than RelationBuildRuleLock > is; it's definitely not risking recursion, as the partition stuff did. > So I take back the idea that we need to postpone it till the data is > referenced. However, it is leaking memory. > > Attached is a proposed rewrite that borrows some ideas from > RelationBuildRuleLock, and also makes use of MemoryContextSetParent > so we don't need a PG_TRY block. In a very simple test > (using regress_rls_schema.rls_tbl from the regression database), > this doesn't seem to leak any memory in the caller context, > and it also doesn't make the rscxt any bigger than it was before. > >> But at least calling pfree for a tree is very strange idea: we should >> not do it all or allocate tree in separate memory context. > Yeah, that's pretty useless. But also I think the string forms of > the expressions are leaking as much memory as the tree forms. > > This is against HEAD but I think it wouldn't be hard to back-patch. > Are you in a position to see if it fixes the problem in your customer's > environment? Thank you. Your patch fixes the problem.