Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iPsW8-0003MH-Ud for pgsql-bugs@arkaria.postgresql.org; Wed, 30 Oct 2019 18:16:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iPsW7-00084D-DN for pgsql-bugs@arkaria.postgresql.org; Wed, 30 Oct 2019 18:16:23 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iPsW6-000845-V1 for pgsql-bugs@lists.postgresql.org; Wed, 30 Oct 2019 18:16:23 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iPsW3-00037S-CS for pgsql-bugs@lists.postgresql.org; Wed, 30 Oct 2019 18:16:21 +0000 Received: by mail-wm1-x342.google.com with SMTP id q70so3133867wme.1 for ; Wed, 30 Oct 2019 11:16:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=M+YPtgqtidCBp3W2+65OIw3foPMqNIRscfOzeIgDCew=; b=Ll/ep87oq1qY+YtNliXo5Xn3TsP6dAeMj9KiSsEX0wkiKRQSZSjJVCzP9XRzTuAR7f JDd1yTVo8f+UA5qKKO5Db9V4nSBNi7773ImwfVDDwZob4ag2RXppJjbCQq7W/ep4/N97 GpHq7g5/OQUl90sqXm4Rby2hq82YqfB+uJbdTBa7cGMx6k6INitDB0Yi2PdPQ9nZXG5n WYyxr0z0WWIG2uGkxKuWKhz8sbnp2SPKeUNFvm8zjTTlfqQY3deEyZmNFEQrZ/0IjFxa pFNxCERGLZ3njUJ7D5NwsrKx6UpEPqwPvoETwIPFN1HZTXxRH3DR8Q92qs9JdTt2E0fL yRxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=M+YPtgqtidCBp3W2+65OIw3foPMqNIRscfOzeIgDCew=; b=gY12RNjRHcsRAG7bnWYONYUgiqtt2WFUPf4rGua3qgUke1DvGfkTtqZA+tfzKjgM7r tN4oVxH0vd2lmDHmEI41acySZtBXunC7aDFaZmNZpbTpG9/lYrzeUQROiELAjoS0xPgh rWt49/wxgdkhxgu+B/Vjt/orzCK65/HH9l6bbiuIJqA9BNWOrIO71ByGoLVRZzbejxOJ +TA2exeE+MMl9Ha3yewa1l3HLruDwOzEH/oN9D1veiwU22Z/xGr6jnLwm3Z4A9BPwIs9 tdMjGDbY9iiTSXY5AdZ8bj1nqn8nVFCYveuRKJQNoT7ycOIK0ypat5hXbfbUXY/+lslg joVA== X-Gm-Message-State: APjAAAU5GNYrkhuatvoWIO09lS7APGJ2xgSdhz8RlDDYY+IV0Rt9ucfB WEQlhO8ciozSm78Ygq8I0uajHfAWFjg= X-Google-Smtp-Source: APXvYqz1awXiZFmXhQ/1hXxWgRCRVRCwXJmo6qs6FHYQNgByhi6aLCRcDbk8gw3xnL2rNKI3W126gA== X-Received: by 2002:a7b:c011:: with SMTP id c17mr753588wmb.95.1572459376584; Wed, 30 Oct 2019 11:16:16 -0700 (PDT) Received: from localhost (ip-86-49-253-183.net.upcbroadband.cz. [86.49.253.183]) by smtp.gmail.com with ESMTPSA id l26sm738203wmg.3.2019.10.30.11.16.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Oct 2019 11:16:13 -0700 (PDT) Date: Wed, 30 Oct 2019 19:16:12 +0100 From: Tomas Vondra To: Josef Machytka Cc: pgsql-bugs@lists.postgresql.org Subject: Re: memory problems and crash of db when deleting data from table with thousands of partitions Message-ID: <20191030181612.733kmg5g4ts45qyc@development> References: <20191023124751.pnzcos7i3ei2vjxc@development> <20191028162446.wlktskknjpsrj4rt@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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