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 1s52kK-002o2Z-6j for pgsql-hackers@arkaria.postgresql.org; Thu, 09 May 2024 12:19:36 +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 1s52kH-006j8Z-Rd for pgsql-hackers@arkaria.postgresql.org; Thu, 09 May 2024 12:19:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s52kH-006j8Q-HQ for pgsql-hackers@lists.postgresql.org; Thu, 09 May 2024 12:19:33 +0000 Received: from mail-lf1-x12d.google.com ([2a00:1450:4864:20::12d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s52k9-000BDY-7C for pgsql-hackers@lists.postgresql.org; Thu, 09 May 2024 12:19:32 +0000 Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-51f1b378ca5so1354272e87.1 for ; Thu, 09 May 2024 05:19:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1715257164; x=1715861964; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=4qJiyhNvlbxajoyxFN1fIBYRZu/oVwm6ehSsYo5pbYc=; b=h1vD9X9DSy9r5wod4yvSPM3rfeNVv1uS+OvgZO6N9j47nMRvH/tcYkDH2qedFPTRWl bzQdAi5k7C+UrfUqITC+ZpsFXUSMrRdTC9gWxZagTS3XPl+0SSwLUqHocoKvZp72vfQP HSC/YYd44YDahw0lML+nXl60Gcci18f5CI2uq+YzdCtvbJiR4EleJ2WnhmIpzmZ6SGBr hUFxHj7maE5LbBT0zLkBO1xh7jI9EmC3a6yJ+ZN81GEpEmOeI3mdOv+C8wU6PlLjE6cu ZkKG/go2PGovDJBI8DRv4iyR9n/5wuPi4cvUuWULUd6ExThbmaube1Tn/o6rpgSDIHIX ZLDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715257164; x=1715861964; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4qJiyhNvlbxajoyxFN1fIBYRZu/oVwm6ehSsYo5pbYc=; b=VzgtvClu7sPlc+3U8Rl0pcx4y+lDPJOMOMZGcL4s7fUOdN0c7VCpMaD0tIIWAUm2bm CbwSik+IvmqV3oBmkFJ8WD7JSI6hxPsZbtYD/ts6XSUW/ptN6xT5etopzXux2EA1g48j HsK6xukVUCdASTPe2m4yWSPHvBaHo4DO0sSsB0n3EJiGOBVmUumiFk9HQBFQZhTTnTUD BsrELu8fXnMpS0aLAmtbwYPhORS9bMuiASl1T2x/LkL4Y97Q894Jac62INS7FhuycqNM Pv9objHP28hfm9TYTpS+PgbUb8XBFG33nLq0qpK2TgRHVLELNQju6/lSeUCBZgXLyrBo Dmng== X-Gm-Message-State: AOJu0YzypnodbV4SoJ5WpG6sf5OFT9eTXkaWWhe4QsEj+9K+z0Yo4m8x cUkDlAv+T0MbVluLqx3AXJKECqMdbacS/jwGpG4urHWaxQIerXj+Ym1xRAS+ssout33qfZDfCHv SFg== X-Google-Smtp-Source: AGHT+IEec5EAIZjCGFB7ZqHGUNgGlkd5rRnTEllIEXGxuqgbg1Sr0/ZXegdRtCZQvCf/RLOe8JosHQ== X-Received: by 2002:ac2:4a71:0:b0:519:e878:9385 with SMTP id 2adb3069b0e04-5217c5671a9mr4412562e87.18.1715257164229; Thu, 09 May 2024 05:19:24 -0700 (PDT) Received: from [10.137.0.18] (ip-86-49-229-30.bb.vodafone.cz. [86.49.229.30]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a5a178a9f23sm68917266b.90.2024.05.09.05.19.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 09 May 2024 05:19:23 -0700 (PDT) Message-ID: <3b721981-6fa3-4698-a9b6-70b2d8e8fa3b@enterprisedb.com> Date: Thu, 9 May 2024 14:19:23 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Parallel CREATE INDEX for GIN indexes To: Andy Fan Cc: pgsql-hackers@lists.postgresql.org References: <6ab4003f-a8b8-4d75-a67f-f25ad98582dc@enterprisedb.com> <87pltvmgdm.fsf@163.com> Content-Language: en-US From: Tomas Vondra In-Reply-To: <87pltvmgdm.fsf@163.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 5/9/24 12:14, Andy Fan wrote: > > Tomas Vondra writes: > >> 3) v20240502-0003-Remove-the-explicit-pg_qsort-in-workers.patch >> >> In 0002 the workers still do an explicit qsort() on the TID list before >> writing the data into the shared tuplesort. But we can do better - the >> workers can do a merge sort too. To help with this, we add the first TID >> to the tuplesort tuple, and sort by that too - it helps the workers to >> process the data in an order that allows simple concatenation instead of >> the full mergesort. >> >> Note: There's a non-obvious issue due to parallel scans always being >> "sync scans", which may lead to very "wide" TID ranges when the scan >> wraps around. More about that later. > > This is really amazing. > >> 7) v20240502-0007-Detect-wrap-around-in-parallel-callback.patch >> >> There's one more efficiency problem - the parallel scans are required to >> be synchronized, i.e. the scan may start half-way through the table, and >> then wrap around. Which however means the TID list will have a very wide >> range of TID values, essentially the min and max of for the key. >> >> Without 0006 this would cause frequent failures of the index build, with >> the error I already mentioned: >> >> ERROR: could not split GIN page; all old items didn't fit > > I have two questions here and both of them are generall gin index questions > rather than the patch here. > > 1. What does the "wrap around" mean in the "the scan may start half-way > through the table, and then wrap around". Searching "wrap" in > gin/README gets nothing. > The "wrap around" is about the scan used to read data from the table when building the index. A "sync scan" may start e.g. at TID (1000,0) and read till the end of the table, and then wraps and returns the remaining part at the beginning of the table for blocks 0-999. This means the callback would not see a monotonically increasing sequence of TIDs. Which is why the serial build disables sync scans, allowing simply appending values to the sorted list, and even with regular flushes of data into the index we can simply append data to the posting lists. > 2. I can't understand the below error. > >> ERROR: could not split GIN page; all old items didn't fit > > When the posting list is too long, we have posting tree strategy. so in > which sistuation we could get this ERROR. > AFAICS the index build relies on the assumption that we only append data to the TID list on a leaf page, and when the page gets split, the "old" part will always fit. Which may not be true, if there was a wrap around and we're adding low TID values to the list on the leaf page. FWIW the error in dataBeginPlaceToPageLeaf looks like this: if (!append || ItemPointerCompare(&maxOldItem, &remaining) >= 0) elog(ERROR, "could not split GIN page; all old items didn't fit"); It can fail simply because of the !append part. I'm not sure why dataBeginPlaceToPageLeaf() relies on this assumption, or with GIN details in general, and I haven't found any explanation. But AFAIK this is why the serial build disables sync scans. >> issue with efficiency - having such a wide TID list forces the mergesort >> to actually walk the lists, because this wide list overlaps with every >> other list produced by the worker. > > If we split the blocks among worker 1-block by 1-block, we will have a > serious issue like here. If we can have N-block by N-block, and N-block > is somehow fill the work_mem which makes the dedicated temp file, we > can make things much better, can we? > I don't understand the question. The blocks are distributed to workers by the parallel table scan, and it certainly does not do that block by block. But even it it did, that's not a problem for this code. The problem is that if the scan wraps around, then one of the TID lists for a given worker will have the min TID and max TID, so it will overlap with every other TID list for the same key in that worker. And when the worker does the merging, this list will force a "full" merge sort for all TID lists (for that key), which is very expensive. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company