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 1hyac3-0007Xg-Us for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Aug 2019 11:41:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hyac2-0008Cd-RC for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Aug 2019 11:41:42 +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 1hyac2-0008Ao-9L for pgsql-hackers@lists.postgresql.org; Fri, 16 Aug 2019 11:41:42 +0000 Received: from cyclops.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1hyaby-0007Yb-QH for pgsql-hackers@postgresql.org; Fri, 16 Aug 2019 11:41:41 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id E427621C4086; Fri, 16 Aug 2019 14:41:36 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.27.200] (gw.postgrespro.ru [93.174.131.141]) (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 933DD21C4059; Fri, 16 Aug 2019 14:41:36 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1565955696; bh=7G+xOsAN6IFJJantnmBN7UGDlqxGKaOPKP3wetUYnzM=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=nhy6wD9FigVLRxIVRTq7a+oTLwGDUaJX1Op4j5eFH3Dydn37efK/QpnKiK25mz/7e 0OLaewH8cfENarY3kKkbqX574J7rRQZd1ymmxGsD+YozmQwXoyADB2TuUsEFiHvGFA Uw0J5D00UFTjMbG0TuKuMRS1faYf3w9pwNd1qXwc= Subject: Re: Global temporary tables To: Craig Ringer Cc: Pavel Stehule , PostgreSQL Hackers References: <73954ab7-44d3-b37b-81a3-69bdcbb446f7@postgrespro.ru> <8b4bdfa6-7a90-95e2-1fd0-243767e5b357@postgrespro.ru> <94a19e7b-d99a-110c-8e5b-f5068682b474@postgrespro.ru> <614c1e9f-c0e6-332c-ba2b-85a7e1efb956@postgrespro.ru> <5a711f75-e999-a809-60e1-c74c8c9e7915@postgrespro.ru> <21f047d6-bb2d-950c-2aff-2fee301d5851@postgrespro.ru> <91de2094-165a-04f1-9b3d-348473394345@postgrespro.ru> From: Konstantin Knizhnik Message-ID: <758e5293-3ccd-3a6a-8464-6373bb441994@postgrespro.ru> Date: Fri, 16 Aug 2019 14:41:36 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------2319A5AF8C8CF82C64E59B6B" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------2319A5AF8C8CF82C64E59B6B Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 16.08.2019 11:32, Craig Ringer wrote: > > You ignore the costs of evicting non-temporary data from > shared_buffers, i.e. contention for space. Also increased chance of > backends being forced to do direct write-out due to lack of s_b space > for dirty buffers. > > In case of pulling all content of temp table in memory (pg_prewarm) > global temp table with shared buffers becomes faster. > > Who would ever do that? > > I decided to redo my experiments and now get different results which illustrates advantages of global temp tables with shared buffer. I performed the following test at my desktop with SSD and 16GB of RAM and Postgres with default configuration except shared-buffers increased to 1Gb. postgres=# create table big(pk bigint primary key, val bigint); CREATE TABLE postgres=# insert into big values (generate_series(1,100000000),generate_series(1,100000000)/100); INSERT 0 100000000 postgres=# select * from buffer_usage limit 3;     relname     |  buffered  | buffer_percent | percent_of_relation ----------------+------------+----------------+---------------------  big            | 678 MB     |           66.2 |                16.1  big_pkey       | 344 MB     |           33.6 |                16.1  pg_am          | 8192 bytes |            0.0 |                20.0 postgres=# create temp table lt(key bigint, count bigint); postgres=# \timing Timing is on. postgres=# insert into lt (select count(*),val as key from big group by val); INSERT 0 1000001 Time: 43265.491 ms (00:43.265) postgres=# select sum(count) from lt;      sum --------------  500000500000 (1 row) Time: 94.194 ms postgres=# insert into gt (select count(*),val as key from big group by val); INSERT 0 1000001 Time: 42952.671 ms (00:42.953) postgres=# select sum(count) from gt;      sum --------------  500000500000 (1 row) Time: 35.906 ms postgres=# select * from buffer_usage limit 3;  relname  | buffered | buffer_percent | percent_of_relation ----------+----------+----------------+---------------------  big      | 679 MB   |           66.3 |                16.1  big_pkey | 300 MB   |           29.3 |                14.0  gt       | 42 MB    |            4.1 |               100.0 So time of storing result in global temp table is slightly smaller than time of storing it in local temp table and time of scanning global temp table is twice smaller! -- Konstantin Knizhnik Postgres Professional: http://www.postgrespro.com The Russian Postgres Company --------------2319A5AF8C8CF82C64E59B6B Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

On 16.08.2019 11:32, Craig Ringer wrote:

You ignore the costs of evicting non-temporary data from shared_buffers, i.e. contention for space. Also increased chance of backends being forced to do direct write-out due to lack of s_b space for dirty buffers.
 
> In case of pulling all content of temp table in memory (pg_prewarm) global temp table with shared buffers becomes faster.

Who would ever do that?



I decided to redo my experiments and now get different results which illustrates advantages of global temp tables with shared buffer.
I performed the following test at my desktop with SSD and 16GB of RAM and Postgres with default configuration except shared-buffers increased to 1Gb.


postgres=# create table big(pk bigint primary key, val bigint);
CREATE TABLE
postgres=# insert into big values (generate_series(1,100000000),generate_series(1,100000000)/100);
INSERT 0 100000000
postgres=# select * from buffer_usage limit 3;
    relname     |  buffered  | buffer_percent | percent_of_relation
----------------+------------+----------------+---------------------
 big            | 678 MB     |           66.2 |                16.1
 big_pkey       | 344 MB     |           33.6 |                16.1
 pg_am          | 8192 bytes |            0.0 |                20.0

postgres=# create temp table lt(key bigint, count bigint);
postgres=# \timing
Timing is on.
postgres=# insert into lt (select count(*),val as key from big group by val);
INSERT 0 1000001
Time: 43265.491 ms (00:43.265)
postgres=# select sum(count) from lt;
     sum     
--------------
 500000500000
(1 row)

Time: 94.194 ms
postgres=# insert into gt (select count(*),val as key from big group by val);
INSERT 0 1000001
Time: 42952.671 ms (00:42.953)
postgres=# select sum(count) from gt;
     sum     
--------------
 500000500000
(1 row)

Time: 35.906 ms
postgres=# select * from buffer_usage limit 3;
 relname  | buffered | buffer_percent | percent_of_relation
----------+----------+----------------+---------------------
 big      | 679 MB   |           66.3 |                16.1
 big_pkey | 300 MB   |           29.3 |                14.0
 gt       | 42 MB    |            4.1 |               100.0


So time of storing result in global temp table is slightly smaller than time of storing it in local temp table and time of scanning global temp table is twice smaller!


-- 
Konstantin Knizhnik
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company 
--------------2319A5AF8C8CF82C64E59B6B--