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.92) (envelope-from ) id 1j7YxN-0002ah-Qc for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Feb 2020 06:17:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j7YxM-0005tp-2x for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Feb 2020 06:17:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j7YxL-0005ti-Q2 for pgsql-hackers@lists.postgresql.org; Fri, 28 Feb 2020 06:17:03 +0000 Received: from mx0a-00296801.pphosted.com ([148.163.150.38]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j7YxD-0004Hv-IW for pgsql-hackers@postgresql.org; Fri, 28 Feb 2020 06:17:02 +0000 Received: from pps.filterd (m0114581.ppops.net [127.0.0.1]) by mx0a-00296801.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 01S6Dx06011481 for ; Fri, 28 Feb 2020 06:16:51 GMT Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) by mx0a-00296801.pphosted.com with ESMTP id 2yepvx0966-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 28 Feb 2020 06:16:51 +0000 Received: by mail-pg1-f198.google.com with SMTP id 37so1166345pgq.0 for ; Thu, 27 Feb 2020 22:16:51 -0800 (PST) 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; bh=DdmpOoZxErgaeoKElQognVmIiR8duTGhEV23vyIlukg=; b=OW4eEIhnOxVwbVBUi4BAbqaMWy2zK9u0ecv/f200WZpHvsDg5oV/tDuEQpRr4TJF4I PVybUwXiHAunrNa8D9GOioTHiU84ULUhY+xj9rQdTDO2B5BxjoFUiMd8vRedyGmaqxRA f8H2qRL7w88jyFJOzUUUYqsWFp7EGxLYoWj4+AQqyjQVpkljAN+LXbdKKzhiMHJ0whb9 3oCTFseJGJiAia45/s6mtuyoX6azT3U6EUwE98eFtiJZkeOFznZQz6qhI7VxTkPL5pdl KswMey9kMKAIrOL9D7NjJK8Ni757Fry1fNujlXz8TJ93bbPSWZcj5+JEoqQHUytgWp9R MRBA== X-Gm-Message-State: APjAAAUJULW/+LaY4UNPF067Bh5TNZwIT7/iAjPRn5Le244r+tJUegdn slw2P5lAYWioGZTCJ2zFgLIMqo5Ut0Biu14rTiFVYof5uPk+GTCOcvCKXT87yj8V0iyf/oRMBSY nTEr/rQXbx2mCCRr9OciIq7bdVU2BMJw9W8WBm57ckBk0Df0RbQ== X-Received: by 2002:a62:7d81:: with SMTP id y123mr2911115pfc.86.1582870610748; Thu, 27 Feb 2020 22:16:50 -0800 (PST) X-Google-Smtp-Source: APXvYqy+JHEXhEaBsaPI6EWxS75kTEy5qD6WMe/uA9sNg2fNFXnmPZrmSbi6JSKe7Hp+wyU8Todjfg== X-Received: by 2002:a62:7d81:: with SMTP id y123mr2911081pfc.86.1582870610337; Thu, 27 Feb 2020 22:16:50 -0800 (PST) Received: from localhost (li1619-9.members.linode.com. [172.104.39.9]) by smtp.gmail.com with ESMTPSA id r13sm738085pjp.14.2020.02.27.22.16.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Feb 2020 22:16:49 -0800 (PST) Date: Fri, 28 Feb 2020 14:16:41 +0800 From: Adam Lee To: Jeff Davis Cc: pgsql-hackers@postgresql.org, Peter Geoghegan , Heikki Linnakangas Subject: Re: Add LogicalTapeSetExtend() to logtape.c Message-ID: <20200228061641.GA883@earth.local> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138,18.0.572 definitions=2020-02-28_01:2020-02-26,2020-02-28 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=619 phishscore=0 clxscore=1015 mlxscore=0 adultscore=0 spamscore=0 impostorscore=0 lowpriorityscore=0 priorityscore=1501 suspectscore=1 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2001150001 definitions=main-2002280054 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Feb 27, 2020 at 01:01:08PM -0800, Jeff Davis wrote: > Attached is a patch that exports a new function from logtape.c: > > extern LogicalTapeSet *LogicalTapeSetExtend( > LogicalTapeSet *lts, int nAdditional); > > The purpose is to allow adding new tapes dynamically for the upcoming > Hash Aggregation work[0]. HashAgg doesn't know in advance how many > tapes it will need, though only a limited number are actually active at > a time. > > This was proposed and originally written by Adam Lee[1] (extract only > the changes to logtape.c/h from his posted patch). Strangely, I'm > seeing ~5% regression with his version when doing: > > -- t20m_1_int4 has 20 million random integers > select * from t20m_1_int4 order by i offset 100000000; > > Which seems to be due to using a pointer rather than a flexible array > member (I'm using gcc[2]). My version (attached) still uses a flexible > array member, which doesn't see the regression; but I repalloc the > whole struct so the caller needs to save the new pointer to the tape > set. > > That doesn't entirely make sense to me, and I'm wondering if someone > else can repro that result and/or make a suggestion, because I don't > have a good explanation. I'm fine with my version of the patch, but it > would be nice to know why there's such a big difference using a pointer > versus a flexible array member. > > Regards, > Jeff Davis I noticed another difference, I was using palloc0(), which could be one of the reason, but not sure. Tested your hashagg-20200226.patch on my laptop(Apple clang version 11.0.0), the average time is 25.9s: ``` create table t20m_1_int4(i int); copy t20m_1_int4 from program 'shuf -i 1-4294967295 -n 20000000'; analyze t20m_1_int4; ``` ``` explain analyze select * from t20m_1_int4 order by i offset 100000000; QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=3310741.20..3310741.20 rows=1 width=4) (actual time=25666.471..25666.471 rows=0 loops=1) -> Sort (cost=3260740.96..3310741.20 rows=20000096 width=4) (actual time=20663.065..24715.269 rows=20000000 loops=1) Sort Key: i Sort Method: external merge Disk: 274056kB -> Seq Scan on t20m_1_int4 (cost=0.00..288496.96 rows=20000096 width=4) (actual time=0.075..2749.385 rows=20000000 loops=1) Planning Time: 0.109 ms Execution Time: 25911.542 ms (7 rows) ``` But if use the palloc0() or do the MemSet() like: ``` lts = (LogicalTapeSet *) palloc0(offsetof(LogicalTapeSet, tapes) + ntapes * sizeof(LogicalTape)); ... MemSet(lts->tapes, 0, lts->nTapes * sizeof(LogicalTape)); <--- this line doesn't matter as I observed, which makes a little sense the compiler might know it's already zero. ``` The average time goes up to 26.6s: ``` explain analyze select * from t20m_1_int4 order by i offset 100000000; QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=3310741.20..3310741.20 rows=1 width=4) (actual time=26419.712..26419.712 rows=0 loops=1) -> Sort (cost=3260740.96..3310741.20 rows=20000096 width=4) (actual time=21430.044..25468.661 rows=20000000 loops=1) Sort Key: i Sort Method: external merge Disk: 274056kB -> Seq Scan on t20m_1_int4 (cost=0.00..288496.96 rows=20000096 width=4) (actual time=0.060..2839.452 rows=20000000 loops=1) Planning Time: 0.111 ms Execution Time: 26652.255 ms (7 rows) ``` -- Adam Lee