Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y1GjM-0006FJ-Gj for pgsql-performance@arkaria.postgresql.org; Wed, 17 Dec 2014 15:41:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y1GjL-0003Ug-Vt for pgsql-performance@arkaria.postgresql.org; Wed, 17 Dec 2014 15:41:40 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y1GjK-0003Rn-3V for pgsql-performance@postgresql.org; Wed, 17 Dec 2014 15:41:38 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y1GjG-0006J9-RJ for pgsql-performance@postgresql.org; Wed, 17 Dec 2014 15:41:36 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id sBHFfVwW007360; Wed, 17 Dec 2014 10:41:31 -0500 From: Tom Lane To: Torsten Zuehlsdorff cc: pgsql-performance@postgresql.org, Alessandro Ipe Subject: Re: Excessive memory used for INSERT In-reply-to: <5491A0A8.7010506@toco-domains.de> References: <2163931.4eVnXNjHa3@snow.oma.be> <5491A0A8.7010506@toco-domains.de> Comments: In-reply-to Torsten Zuehlsdorff message dated "Wed, 17 Dec 2014 16:26:32 +0100" Date: Wed, 17 Dec 2014 10:41:31 -0500 Message-ID: <7359.1418830891@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Torsten Zuehlsdorff writes: > How many rows is "(SELECT * FROM upsert)" returning? Without knowing > more i would guess, that the result-set is very big and that could be > the reason for the memory usage. Result sets are not ordinarily accumulated on the server side. Alessandro didn't show the trigger definition, but my guess is that it's an AFTER trigger, which means that a trigger event record is accumulated in server memory for each inserted/updated row. If you're trying to update a huge number of rows in one command (or one transaction, if it's a DEFERRED trigger) you'll eventually run out of memory for the event queue. An easy workaround is to make it a BEFORE trigger instead. This isn't really nice from a theoretical standpoint; but as long as you make sure there are no other BEFORE triggers that might fire after it, it'll work well enough. Alternatively, you might want to reconsider the concept of updating hundreds of millions of rows in a single operation ... regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance