pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Revise RelationBuildRowSecurity() to avoid memory leaks.
Date: Sat, 26 Sep 2020 20:04:58 +0000
Message-ID: <E1kMGRG-0003Xo-Or@gemulon.postgresql.org> (raw)

Revise RelationBuildRowSecurity() to avoid memory leaks.

This function leaked some memory while loading qual clauses for
an RLS policy.  While ordinarily negligible, that could build up
in some repeated-reload cases, as reported by Konstantin Knizhnik.
We can improve matters by borrowing the coding long used in
RelationBuildRuleLock: build stringToNode's result directly in
the target context, and remember to explicitly pfree the
input string.

This patch by no means completely guarantees zero leaks within
this function, since we have no real guarantee that the catalog-
reading subroutines it calls don't leak anything.  However,
practical tests suggest that this is enough to resolve the issue.
In any case, any remaining leaks are similar to those risked by
RelationBuildRuleLock and other relcache-loading subroutines.
If we need to fix them, we should adopt a more global approach
such as that used by the RECOVER_RELATION_BUILD_MEMORY hack.

While here, let's remove the need for an expensive PG_TRY block by
using MemoryContextSetParent to reparent an initially-short-lived
context for the RLS data.

Back-patch to all supported branches.

Discussion: https://postgr.es/m/21356c12-8917-8249-b35f-1c447231922b@postgrespro.ru

Branch
------
REL9_6_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/acab757ce9ead47a39b4d2d25b91ed2532b8444a

Modified Files
--------------
src/backend/commands/policy.c | 204 ++++++++++++++++++++----------------------
1 file changed, 95 insertions(+), 109 deletions(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1kMGRG-0003Xo-Or@gemulon.postgresql.org>
Permalink:  ../E1kMGRG-0003Xo-Or@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1kMGRG-0003Xo-Or@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Revise RelationBuildRowSecurity() to avoid memory leaks.
  In-Reply-To: <E1kMGRG-0003Xo-Or@gemulon.postgresql.org>
  References: <E1kMGRG-0003Xo-Or@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox