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 1sQzV3-00Cl1V-7K for pgsql-hackers@arkaria.postgresql.org; Tue, 09 Jul 2024 01:18:33 +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 1sQzV1-00BO85-Sl for pgsql-hackers@arkaria.postgresql.org; Tue, 09 Jul 2024 01:18:31 +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 1sQzV1-00BO7v-44 for pgsql-hackers@lists.postgresql.org; Tue, 09 Jul 2024 01:18:31 +0000 Received: from m15.mail.163.com ([45.254.50.219]) by makus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sQzUw-0017g9-58 for pgsql-hackers@postgresql.org; Tue, 09 Jul 2024 01:18:29 +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=2SINEj/rlOxvNS2QddjPBfSULGLTbuwSEPoi6WKAQlg=; b=oWSc6RQN/zRHX3xDjcu2ypGzF6PuGiBuAtf0inPQhMRg56Sjcc8HdTCoN4Cv4X Hz35ocPYLln45/2lyIeE79zuQZMIbK6uVVd0IywHznblUVqx0hua2s2DyODgG5ZB jfL17Hs/oLqhR+6ruiOGTTqbO8NBsPsopg+rbf6R7A6RU= Received: from lovely-coding (unknown [101.227.46.166]) by gzga-smtp-mta-g0-5 (Coremail) with SMTP id _____wDnTxvTj4xmTaaNCA--.60249S3; Tue, 09 Jul 2024 09:18:11 +0800 (CST) From: Andy Fan To: Tomas Vondra Subject: Re: Parallel CREATE INDEX for GIN indexes In-Reply-To: <87zfqrjrvd.fsf@163.com> (Andy Fan's message of "Tue, 09 Jul 2024 09:06:46 +0800") References: <6ab4003f-a8b8-4d75-a67f-f25ad98582dc@enterprisedb.com> <87pltvmgdm.fsf@163.com> <3b721981-6fa3-4698-a9b6-70b2d8e8fa3b@enterprisedb.com> <87y18ektdn.fsf@163.com> <87jzjes2ia.fsf@163.com> <03abcca0-47b2-4bc1-be05-6c1a3f1c5511@enterprisedb.com> <74ef5493-c837-4861-afb0-7d07e3a35407@enterprisedb.com> <6db057fa-3990-4778-9578-aabc20f05db3@enterprisedb.com> <87o77gveoy.fsf@163.com> <875xtiu5wx.fsf@163.com> <87zfqrjrvd.fsf@163.com> cc: PostgreSQL-development Date: Tue, 09 Jul 2024 09:18:11 +0800 Message-ID: <87o777jrcc.fsf@163.com> MIME-Version: 1.0 Content-Type: text/plain X-CM-TRANSID: _____wDnTxvTj4xmTaaNCA--.60249S3 X-Coremail-Antispam: 1Uf129KBjvJXoWxWFyUWw47GFW3AFy8CF4xXrb_yoW5Kr47pF Z0kayvkrWkGa4jkw1S9r40qr1Sk34rtrsxXFyrWr4DArs8Xas2vrW0yr98ua1Dur4vka1q yw1DAasF9390yFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zt2M8NUUUUU= X-Originating-IP: [101.227.46.166] X-CM-SenderInfo: x2klx3xlid0iqsrtqiywtou0bp/1tbiNhYXU2XAmWCcPwAAsb List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Andy Fan writes: I just realize all my replies is replied to sender only recently, probably because I upgraded the email cient and the short-cut changed sliently, resent the lastest one only.... >>> Suppose RBTree's output is: >>> >>> batch-1 at RBTree: >>> 1 [tid1, tid8, tid100] >>> 2 [tid1, tid9, tid800] >>> ... >>> 78 [tid23, tid99, tid800] >>> >>> batch-2 at RBTree >>> 1 [tid1001, tid1203, tid1991] >>> ... >>> ... >>> 97 [tid1023, tid1099, tid1800] >>> >>> Since all the tuples in each batch (1, 2, .. 78) are sorted already, we >>> can just flush them into tuplesort as a 'run' *without any sorts*, >>> however within this way, it is possible to produce more 'runs' than what >>> you did in your patch. >>> >> >> Oh! Now I think I understand what you were proposing - you're saying >> that when dumping the RBTree to the tuplesort, we could tell the >> tuplesort this batch of tuples is already sorted, and tuplesort might >> skip some of the work when doing the sort later. >> >> I guess that's true, and maybe it'd be useful elsewhere, I still think >> this could be left as a future improvement. Allowing it seems far from >> trivial, and it's not quite clear if it'd be a win (it might interfere >> with the existing sort code in unexpected ways). > > Yes, and I agree that can be done later and I'm thinking Matthias's > proposal is more promising now. > >>> new way: the No. of batch depends on size of RBTree's batch size. >>> existing way: the No. of batch depends on size of work_mem in tuplesort. >>> Usually the new way would cause more no. of runs which is harmful for >>> mergeruns. so I can't say it is an improve of not and not include it in >>> my previous patch. >>> >>> however case 1 sounds a good canidiates for this method. >>> >>> Tuples from state->bs_worker_state after the perform_sort and ctid >>> merge: >>> >>> 1 [tid1, tid8, tid100, tid1001, tid1203, tid1991] >>> 2 [tid1, tid9, tid800] >>> 78 [tid23, tid99, tid800] >>> 97 [tid1023, tid1099, tid1800] >>> >>> then when we move tuples to bs_sort_state, a). we don't need to sort at >>> all. b). we can merge all of them into 1 run which is good for mergerun >>> on leader as well. That's the thing I did in the previous patch. >>> >> >> I'm sorry, I don't understand what you're proposing. Could you maybe >> elaborate in more detail? > > After we called "tuplesort_performsort(state->bs_worker_sort);" in > _gin_process_worker_data, all the tuples in bs_worker_sort are sorted > already, and in the same function _gin_process_worker_data, we have > code: > > while ((tup = tuplesort_getgintuple(worker_sort, &tuplen, true)) != NULL) > { > > ....(1) > > tuplesort_putgintuple(state->bs_sortstate, ntup, ntuplen); > > } > > and later we called 'tuplesort_performsort(state->bs_sortstate);'. Even > we have some CTID merges activity in '....(1)', the tuples are still > ordered, so the sort (in both tuplesort_putgintuple and > 'tuplesort_performsort) are not necessary, what's more, in the each of > 'flush-memory-to-disk' in tuplesort, it create a 'sorted-run', and in > this case, acutally we only need 1 run only since all the input tuples > in the worker is sorted. The reduction of 'sort-runs' in worker will be > helpful to leader's final mergeruns. the 'sorted-run' benefit doesn't > exist for the case-1 (RBTree -> worker_state). > > If Matthias's proposal is adopted, my optimization will not be useful > anymore and Matthias's porposal looks like a more natural and effecient > way. -- Best Regards Andy Fan