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 1htRIY-0000Iu-7j for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Aug 2019 06:44:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1htRIW-0005UV-Sh for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Aug 2019 06:44:16 +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 1htRIW-0005Kf-F3 for pgsql-hackers@lists.postgresql.org; Fri, 02 Aug 2019 06:44:16 +0000 Received: from mx0b-00296801.pphosted.com ([148.163.153.148]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1htRIS-0000bx-Se for pgsql-hackers@postgresql.org; Fri, 02 Aug 2019 06:44:14 +0000 Received: from pps.filterd (m0114585.ppops.net [127.0.0.1]) by mx0b-00296801.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id x726b8f6022559 for ; Fri, 2 Aug 2019 06:44:11 GMT Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by mx0b-00296801.pphosted.com with ESMTP id 2u2s9ab35e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 02 Aug 2019 06:44:11 +0000 Received: by mail-pl1-f197.google.com with SMTP id y9so41077920plp.12 for ; Thu, 01 Aug 2019 23:44:10 -0700 (PDT) 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=+xIOGhloIgK0yztjso3eBMMyWGJgM/gAmefTWG/4sRc=; b=pksYB5b0tQt+HhB+5Le12m0shglH1lshUzEPeBjfojORDLeqQOT6aYxtIlC4W6XWTy orW6u/86xfOhMQoRaz2EXwGXloFErt6MGd4fXOdpiMI9Pt+vKT+g50fJGm0D2ZDwsd9N ozhha94G+V5QycJeUz8FR7Unx9/y2Yn3PoECulb8hH15qBH0K2VyCrnNeS1YDb1xBQvZ 65qZTbfoebR9RZ8077vW26RONW41R3GVOnyrPsVSrBiT4MwOWKvN/GVPJ/KEvBSf8Yth 5cj8nXA64GnLfCXUTxk666lVZaXYpQOC1XlGey2I2yUQV9eU7gnNOYkAH7J1CCNBmaNs Ub6g== X-Gm-Message-State: APjAAAWDSf2OY2PrgPpYXPL2zliTJjaTz3lBvf1pT89mOFmoEa91+LDV qIvGpwsimFJ4LJUSZOGFt04vUgjLlSyValWOg0gRxXIraO6UtpizSqlBc3DsPylFRXEZ/U/UE4y Yb6I/pAZbJY/QMyZqkCv+nFy6nGtBBIOhhYNs2UbJH2JWoY7BZw== X-Received: by 2002:a17:902:e011:: with SMTP id ca17mr132330222plb.328.1564728249409; Thu, 01 Aug 2019 23:44:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqw/EyIw3rOGkCDvl5ZHjgRIK31Y49t7yyBWRx6RIqkicBugPuk8G5j+vDMLsI/BgOSrcK71gQ== X-Received: by 2002:a17:902:e011:: with SMTP id ca17mr132330208plb.328.1564728249157; Thu, 01 Aug 2019 23:44:09 -0700 (PDT) Received: from localhost (li1732-115.members.linode.com. [172.104.122.115]) by smtp.gmail.com with ESMTPSA id g2sm120750677pfq.88.2019.08.01.23.44.07 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 01 Aug 2019 23:44:08 -0700 (PDT) Date: Fri, 2 Aug 2019 14:44:05 +0800 From: Adam Lee To: Jeff Davis Cc: Melanie Plageman , pgsql-hackers@postgresql.org Subject: Re: Memory-Bounded Hash Aggregation Message-ID: <20190802064405.GA94036@mars.local> References: <507ac540ec7c20136364b5272acbcd4574aa76ef.camel@j-davis.com> <20190703001753.s5e5cme3ucjgeu6c@development> <20190711155520.zqipi23o3yybx7ym@development> <9be86fc1adc315f69b8af4b379087ab451008d8a.camel@j-davis.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9be86fc1adc315f69b8af4b379087ab451008d8a.camel@j-davis.com> User-Agent: Mutt/1.12.1 (2019-06-15) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:5.22.84,1.0.8 definitions=2019-08-02_04:2019-07-31,2019-08-02 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxscore=0 lowpriorityscore=0 malwarescore=0 mlxlogscore=767 clxscore=1011 bulkscore=0 spamscore=0 adultscore=0 phishscore=0 priorityscore=1501 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-1906280000 definitions=main-1908020068 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > High-level approaches: > > 1. When the in-memory hash table fills, keep existing entries in the > hash table, and spill the raw tuples for all new groups in a > partitioned fashion. When all input tuples are read, finalize groups > in memory and emit. Now that the in-memory hash table is cleared (and > memory context reset), process a spill file the same as the original > input, but this time with a fraction of the group cardinality. > > 2. When the in-memory hash table fills, partition the hash space, and > evict the groups from all partitions except one by writing out their > partial aggregate states to disk. Any input tuples belonging to an > evicted partition get spilled to disk. When the input is read > entirely, finalize the groups remaining in memory and emit. Now that > the in-memory hash table is cleared, process the next partition by > loading its partial states into the hash table, and then processing > its spilled tuples. I'm late to the party. These two approaches both spill the input tuples, what if the skewed groups are not encountered before the hash table fills up? The spill files' size and disk I/O could be downsides. Greenplum spills all the groups by writing the partial aggregate states, reset the memory context, process incoming tuples and build in-memory hash table, then reload and combine the spilled partial states at last, how does this sound? -- Adam Lee