Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1muzF6-00074H-Vj for pgsql-hackers@arkaria.postgresql.org; Wed, 08 Dec 2021 15:52:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1muzF5-0004IW-3P for pgsql-hackers@arkaria.postgresql.org; Wed, 08 Dec 2021 15:52:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1muzF4-0004IM-P3 for pgsql-hackers@lists.postgresql.org; Wed, 08 Dec 2021 15:52:26 +0000 Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1muzF2-0000vI-9U for pgsql-hackers@lists.postgresql.org; Wed, 08 Dec 2021 15:52:26 +0000 Received: by mail-wr1-x431.google.com with SMTP id u17so4868726wrt.3 for ; Wed, 08 Dec 2021 07:52:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aiven.io; s=google; h=from:to:cc:subject:date:message-id:organization:in-reply-to :references:mime-version:content-transfer-encoding; bh=lcD/qUy8U/fMFjB4yN1u8gjITIbKxIesUup5Ju21wHo=; b=pJYD6bRVNbIt8/I8JxAk2G1t6qZBjGfPwAGtwPtZJ7Xpjqt4zjzLQTghKKA8MCO0b8 WHQvZaK5UcMFp5a/H9spppI36JwO0pZE40TtjFX+13iQqjtS1UEOaCtqfp2o2iFJOFLS RTnepgyk9fCUcqKWgW7k8TndAGl4xAo89neFM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:organization :in-reply-to:references:mime-version:content-transfer-encoding; bh=lcD/qUy8U/fMFjB4yN1u8gjITIbKxIesUup5Ju21wHo=; b=Xse2yAWcKdSwZGnvCWXknF4BeixBkP5u9UPvNHBBYiGGrgRB0pKBjXcq5M/oFx/1VD cwyv/A8ev5LqJ7IocszWuEaLjaVa9J3UBgbeEaYWWvrbw+z7fX72JLRSpfj+gcbfn2Wf FXD2KS/ub6eJntf/rI4Ty7jdqD433d+jFznRdpxD0ksL7r2Sq25gqzGAh5Je8ISDO5Hv 2zsNHdS39OO3vfeJ72m1eqHlxWDfRNRc6K/MHdBAd48kWJgbUTwKR8c8j/WkGjfo3UDC LA/yeYIXP4yupoDA/MFJHLZyggfAXz62kfpfj0yDyihb32I2DCGY4Fzawxp/fWa3YF9m Qurg== X-Gm-Message-State: AOAM530MgYXloUcS/sZbH0v/hCCin/r59eD19eul80o0CYlG8HK0d3gY ulzRNZU0o1VHYtsjqsquf/bEJw== X-Google-Smtp-Source: ABdhPJxYh8p0XxSi60siQNkaeapsO19Cz5rkl+9JEA4+sUjZA2Cjssb/YeXG2ZKOkDyfscv1BJT8Lw== X-Received: by 2002:a5d:4582:: with SMTP id p2mr61188937wrq.364.1638978742401; Wed, 08 Dec 2021 07:52:22 -0800 (PST) Received: from aivenronan.localnet (static-176-158-121-96.ftth.abo.bbox.fr. [176.158.121.96]) by smtp.gmail.com with ESMTPSA id n1sm3282020wmq.6.2021.12.08.07.52.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 07:52:22 -0800 (PST) From: Ronan Dunklau To: David Rowley , pgsql-hackers@lists.postgresql.org Cc: Andres Freund , Tomas Vondra , PostgreSQL Developers , Tomas Vondra Subject: Re: Use generation context to speed up tuplesorts Date: Wed, 08 Dec 2021 16:51:17 +0100 Message-ID: <8046109.NyiUUSuA9g@aivenronan> Organization: aiven In-Reply-To: References: <36829a8a-63d0-c428-89d5-07e49561973e@enterprisedb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Le jeudi 9 septembre 2021, 15:37:59 CET Tomas Vondra a =E9crit : > And now comes the funny part - if I run it in the same backend as the > "full" benchmark, I get roughly the same results: >=20 > block_size | chunk_size | mem_allocated | alloc_ms | free_ms > ------------+------------+---------------+----------+--------- > 32768 | 512 | 806256640 | 37159 | 76669 >=20 > but if I reconnect and run it in the new backend, I get this: >=20 > block_size | chunk_size | mem_allocated | alloc_ms | free_ms > ------------+------------+---------------+----------+--------- > 32768 | 512 | 806158336 | 233909 | 100785 > (1 row) >=20 > It does not matter if I wait a bit before running the query, if I run it > repeatedly, etc. The machine is not doing anything else, the CPU is set > to use "performance" governor, etc. I've reproduced the behaviour you mention. I also noticed asm_exc_page_fault showing up in the perf report in that cas= e. Running an strace on it shows that in one case, we have a lot of brk calls,= =20 while when we run in the same process as the previous tests, we don't. My suspicion is that the previous workload makes glibc malloc change it's=20 trim_threshold and possibly other dynamic options, which leads to constantl= y=20 moving the brk pointer in one case and not the other. Running your fifo test with absurd malloc options shows that indeed that mi= ght=20 be the case (I needed to change several, because changing one disable the=20 dynamic adjustment for every single one of them, and malloc would fall back= to=20 using mmap and freeing it on each iteration): mallopt(M_TOP_PAD, 1024 * 1024 * 1024); mallopt(M_TRIM_THRESHOLD, 256 * 1024 * 1024); mallopt(M_MMAP_THRESHOLD, 4*1024*1024*sizeof(long)); I get the following results for your self contained test. I ran the query=20 twice, in each case, seeing the importance of the first allocation and the= =20 subsequent ones: With default malloc options: block_size | chunk_size | mem_allocated | alloc_ms | free_ms=20 =2D-----------+------------+---------------+----------+--------- 32768 | 512 | 795836416 | 300156 | 207557 block_size | chunk_size | mem_allocated | alloc_ms | free_ms=20 =2D-----------+------------+---------------+----------+--------- 32768 | 512 | 795836416 | 211942 | 77207 With the oversized values above: block_size | chunk_size | mem_allocated | alloc_ms | free_ms=20 =2D-----------+------------+---------------+----------+--------- 32768 | 512 | 795836416 | 219000 | 36223 block_size | chunk_size | mem_allocated | alloc_ms | free_ms=20 =2D-----------+------------+---------------+----------+--------- 32768 | 512 | 795836416 | 75761 | 78082 (1 row) I can't tell how representative your benchmark extension would be of real l= ife=20 allocation / free patterns, but there is probably something we can improve= =20 here. I'll try to see if I can understand more precisely what is happening. =2D-=20 Ronan Dunklau