Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s50bV-002ZDa-GL for pgsql-hackers@arkaria.postgresql.org; Thu, 09 May 2024 10:02:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s50aV-0062Kv-Gq for pgsql-hackers@arkaria.postgresql.org; Thu, 09 May 2024 10:01:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s50aU-0062Km-Jf for pgsql-hackers@lists.postgresql.org; Thu, 09 May 2024 10:01:19 +0000 Received: from m16.mail.163.com ([117.135.210.3]) by makus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s50aN-0009lB-VC for pgsql-hackers@lists.postgresql.org; Thu, 09 May 2024 10:01:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version: Content-Type; bh=MitIs54+mSGtlR8uFUOGSvq+kxDDJuJUXYTZXzn4ufY=; b=TldRKxHqw4ASq2R8UCwrb7GFlD78eJHEb1ideuwwagoYqclhNqXzk9GfXXC8uV /C9uinbKjqe8QSqQka+QHcDBS4OXlXVuS5sLza3OREr5q6BN9FRdOmDOsuW7umRW 3tWHutaXitnz7rNZ047AxHc5EO0JmVLZYgWwgmmmIMDOY= Received: from 8235eee8a2a0 (unknown [140.205.118.37]) by gzga-smtp-mta-g2-4 (Coremail) with SMTP id _____wD3vy3cnjxmwaHPBQ--.2501S3; Thu, 09 May 2024 18:01:01 +0800 (CST) References: <6ab4003f-a8b8-4d75-a67f-f25ad98582dc@enterprisedb.com> <5b5a059d-0699-4cc2-b9a0-32053ea19a65@enterprisedb.com> User-agent: mu4e 1.10.7; emacs 29.1 From: Andy Fan To: Tomas Vondra Cc: Matthias van de Meent , pgsql-hackers@lists.postgresql.org Subject: Re: Parallel CREATE INDEX for GIN indexes Date: Thu, 09 May 2024 17:44:49 +0800 In-reply-to: <5b5a059d-0699-4cc2-b9a0-32053ea19a65@enterprisedb.com> Message-ID: <87ttj7mhib.fsf@163.com> MIME-Version: 1.0 Content-Type: text/plain X-CM-TRANSID: _____wD3vy3cnjxmwaHPBQ--.2501S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7CF4xKry3GF1kKw15Jr43Jrb_yoW8tw17pa y3KFWUtF4kGa13Cr17Zw4xtFyFka97Jw13Ja4rA3s8C390gF9FyFyrtw1UuFyDWr1kCw4j vr48Gw12kws0yaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ztg4kNUUUUU= X-Originating-IP: [140.205.118.37] X-CM-SenderInfo: x2klx3xlid0iqsrtqiywtou0bp/1tbiNg-ZU2XAlXJrxgABsd List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello Tomas, >>> 2) v20240502-0002-Use-mergesort-in-the-leader-process.patch >>> >>> The approach implemented by 0001 works, but there's a little bit of >>> issue - if there are many distinct keys (e.g. for trigrams that can >>> happen very easily), the workers will hit the memory limit with only >>> very short TID lists for most keys. For serial build that means merging >>> the data into a lot of random places, and in parallel build it means the >>> leader will have to merge a lot of tiny lists from many sorted rows. >>> >>> Which can be quite annoying and expensive, because the leader does so >>> using qsort() in the serial part. It'd be better to ensure most of the >>> sorting happens in the workers, and the leader can do a mergesort. But >>> the mergesort must not happen too often - merging many small lists is >>> not cheaper than a single qsort (especially when the lists overlap). >>> >>> So this patch changes the workers to process the data in two phases. The >>> first works as before, but the data is flushed into a local tuplesort. >>> And then each workers sorts the results it produced, and combines them >>> into results with much larger TID lists, and those results are written >>> to the shared tuplesort. So the leader only gets very few lists to >>> combine for a given key - usually just one list per worker. >> >> Hmm, I was hoping we could implement the merging inside the tuplesort >> itself during its own flush phase, as it could save significantly on >> IO, and could help other users of tuplesort with deduplication, too. >> > > Would that happen in the worker or leader process? Because my goal was > to do the expensive part in the worker, because that's what helps with > the parallelization. I guess both of you are talking about worker process, if here are something in my mind: *btbuild* also let the WORKER dump the tuples into Sharedsort struct and let the LEADER merge them directly. I think this aim of this design is it is potential to save a mergeruns. In the current patch, worker dump to local tuplesort and mergeruns it and then leader run the merges again. I admit the goal of this patch is reasonable, but I'm feeling we need to adapt this way conditionally somehow. and if we find the way, we can apply it to btbuild as well. -- Best Regards Andy Fan