pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tomas.vondra@2ndquadrant.com>
To: Josef Machytka <josef.machytka@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: memory problems and crash of db when deleting data from table with thousands of partitions
Date: Wed, 30 Oct 2019 19:16:12 +0100
Message-ID: <20191030181612.733kmg5g4ts45qyc@development> (raw)
In-Reply-To: <CAGvVEFtA2QAPp5bwjf=uT1pKBQEy5ks54Q3QwAaSuhud3-3=cQ@mail.gmail.com>
References: <CAGvVEFueqXeYL0z2fTQMWYSz7Gc9czSrOvCSNubb9rHHVL-2OA@mail.gmail.com>
	<20191023124751.pnzcos7i3ei2vjxc@development>
	<CAGvVEFsTp8cqALqPaOVxXaXMXx9V6TXtCAGobvWBb-ezjDbRtA@mail.gmail.com>
	<20191028162446.wlktskknjpsrj4rt@development>
	<CAGvVEFtA2QAPp5bwjf=uT1pKBQEy5ks54Q3QwAaSuhud3-3=cQ@mail.gmail.com>

On Wed, Oct 30, 2019 at 03:35:24PM +0100, Josef Machytka wrote:
>Here are scripts which you can use to simulate problem:
>
>- create_tables.sql - creates all partitions
>- generate_data.sql - generates some data (technically you need only a few
>records, delete command will fail anyway)
>
>and try command:
>DELETE FROM bi.test_multilevel WHERE period_name = '....';
>
>PostgreSQL 12 will start to use more and more memory and will stop
>operation with "out of memory" (PostgreSQL 11 would crash)
>

Thanks for the scripts, I'm able to reproduce the issue. It does seem
most of the memory is allocated in inheritance_planner, where we do this
(around line 1500)

    foreach(lc, child_appinfos)
    {
        ...

        /*
         * Generate modified query with this rel as target.  We first apply
         * adjust_appendrel_attrs, which copies the Query and changes
         * references to the parent RTE to refer to the current child RTE,
         * then fool around with subquery RTEs.
         */
        subroot->parse = (Query *)
	    adjust_appendrel_attrs(subroot,
                                   (Node *) parent_parse,
                                   1, &appinfo);

        ...
    }


This unfortunately allocates a Query struct that is about ~4.3MB *per
partition*, and you have ~10000 of them, so 43GB in total.

Unfortunately, this does not seem like a memory leak - we actually do
need the structure, and it happens to be pretty large :-( So IMHO it's
working as designed, it just wasn't optimized for cases with many
partitions yet :-(

Chances are we'll improve this in future releases (13+), but I very much
doubt we can do much in existing releases - we tend not to make big
changes there, and I don't see a simple change addressing this.


regards

-- 
Tomas Vondra                  http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services 





view thread (6+ messages)

Message-ID: <20191030181612.733kmg5g4ts45qyc@development>
Permalink:  ../20191030181612.733kmg5g4ts45qyc@development/
Also on:    postgresql.org/message-id/20191030181612.733kmg5g4ts45qyc@development

 · 

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-bugs@postgresql.org
  Cc: tomas.vondra@2ndquadrant.com, josef.machytka@gmail.com
  Subject: Re: memory problems and crash of db when deleting data from table with thousands of partitions
  In-Reply-To: <20191030181612.733kmg5g4ts45qyc@development>
  References: <CAGvVEFueqXeYL0z2fTQMWYSz7Gc9czSrOvCSNubb9rHHVL-2OA@mail.gmail.com> <20191023124751.pnzcos7i3ei2vjxc@development> <CAGvVEFsTp8cqALqPaOVxXaXMXx9V6TXtCAGobvWBb-ezjDbRtA@mail.gmail.com> <20191028162446.wlktskknjpsrj4rt@development> <CAGvVEFtA2QAPp5bwjf=uT1pKBQEy5ks54Q3QwAaSuhud3-3=cQ@mail.gmail.com> <20191030181612.733kmg5g4ts45qyc@development>

* 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