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 1sQUiM-00A5AZ-Ub for pgsql-hackers@arkaria.postgresql.org; Sun, 07 Jul 2024 16:26:15 +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 1sQUiL-00Dmph-72 for pgsql-hackers@arkaria.postgresql.org; Sun, 07 Jul 2024 16:26:13 +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 1sQUiK-00DmpZ-Sm for pgsql-hackers@lists.postgresql.org; Sun, 07 Jul 2024 16:26:12 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sQUiG-000uHB-Ns for pgsql-hackers@lists.postgresql.org; Sun, 07 Jul 2024 16:26:11 +0000 Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-58b0dddab8cso4660875a12.0 for ; Sun, 07 Jul 2024 09:26:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1720369566; x=1720974366; 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=hnDYZMUOIBkzS08sQtbNVQpNnfjM/bPwZGJ4643BAcE=; b=l2zAAScecc/nzCHEYlo1bSeCKhoO5OD4U8kD4tL67dGBfGuku6X/u1eCyA/GrBLDq4 chmjAuJ7ek7K3dqVfxlxlvhPB2T+gpO4tAiK3xs1X33WFiem+Y+Jln1FMq9IBgMBeoQu eXWkVVyw4+9pvxRyHFOseoU5GyCxRlHAA2zmwyqffkg1Fbb70DGn9SBQ1Ie7/rMjziwQ 3GVaWfzjb9kCuHYEwKQpSgnAirlK1WsM3LZS6pe/OMhsEnSiGKwSXRkMZsxpJZvdjnWa 4seOGMX0+pCX4UTD9vH1TcBzIKszFRe4mjM1wOA3Tbi3xW42/ONhIKcZfIaNdlLLgrqP hnQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720369566; x=1720974366; 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=hnDYZMUOIBkzS08sQtbNVQpNnfjM/bPwZGJ4643BAcE=; b=rYPagcFqOtIlqqt2vacF6VOUiYWvWMv1xEVCc4LrhzhnKGg2Dgz5sIRsdJ4SftwW7f LP7NZ0+0/OXnkPmPOMdzWY6OFDEmd3/qSDiX5wy4Ow5m5bB8uo+q5P9HohWZJh+hNZZI Z7Mj0pcItlR0XeEIikFClySaFwBEPWSPejCW1tU8Xb/UQZXqMfyYJie2u1OD6YAvRKS3 5lRRJl2oF0LB5Ev5YQdMTj93p7Jx+VJHW7/ORROOp8zXH8IvV8l8q/wEc/pEzEc+oSB7 ljLk20ClYz8/yk7sMIOQtrjjY9PqQUq6CogNrLc5g99u0Lwz1vxemnYd/OB5Ckl/Q5M/ zcbQ== X-Gm-Message-State: AOJu0YxWI6U/JoPKggjKiw2RH/LvU0FyMDZAvOUr5m6LDQXZE/BUrrsx VYsnMEDpE3zltIfuiQdaUMHCHGUspgctwdjSWRd4txYwsQ4Y0MF/Vyo+eADAODFnB1mDCcZJ9cJ vJQ== X-Google-Smtp-Source: AGHT+IF0l9QpwY6jF+yc7YPMdiItkJnbWvvohjnx7xGUH6Lv3RgjVLL1OU1d/PIuuCYbp3Er+6V/ow== X-Received: by 2002:a05:6402:13d5:b0:58b:73f4:2ed with SMTP id 4fb4d7f45d1cf-58e5b4aede0mr6133436a12.35.1720369566345; Sun, 07 Jul 2024 09:26:06 -0700 (PDT) Received: from [10.137.0.18] ([212.89.239.249]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-58dfd5aeda6sm4632489a12.78.2024.07.07.09.26.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 07 Jul 2024 09:26:05 -0700 (PDT) Message-ID: <531c2afd-6118-4582-8d0a-7bd2ddbde6c5@enterprisedb.com> Date: Sun, 7 Jul 2024 18:26:03 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Parallel CREATE INDEX for GIN indexes To: Matthias van de Meent Cc: pgsql-hackers@lists.postgresql.org 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> Content-Language: en-US From: Tomas Vondra In-Reply-To: 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 7/5/24 21:45, Matthias van de Meent wrote: > On Wed, 3 Jul 2024 at 20:36, Matthias van de Meent > wrote: >> >> On Mon, 24 Jun 2024 at 02:58, Tomas Vondra >> wrote: >>> So I've switched this to use the regular data-type comparisons, with >>> SortSupport etc. There's a bit more cleanup remaining and testing >>> needed, but I'm not aware of any bugs. > > I've hit assertion failures in my testing of the combined patches, in > AssertCheckItemPointers: it assumes it's never called when the buffer > is empty and uninitialized, but that's wrong: we don't initialize the > items array until the first tuple, which will cause the assertion to > fire. By updating the first 2 assertions of AssertCheckItemPointers, I > could get it working. > Yeah, sorry. I think I broke this assert while doing the recent cleanups. The assert used to be called only when doing a sort, and then it certainly can't be empty. But then I moved it to be called from the generic GinTuple assert function, and that broke this assumption. >> --- >>> +++ b/src/backend/utils/sort/tuplesortvariants.c >> >> I was thinking some more about merging tuples inside the tuplesort. I >> realized that this could be implemented by allowing buffering of tuple >> writes in writetup. This would require adding a flush operation at the >> end of mergeonerun to store the final unflushed tuple on the tape, but >> that shouldn't be too expensive. This buffering, when implemented >> through e.g. a GinBuffer in TuplesortPublic->arg, could allow us to >> merge the TID lists of same-valued GIN tuples while they're getting >> stored and re-sorted, thus reducing the temporary space usage of the >> tuplesort by some amount with limited overhead for other >> non-deduplicating tuplesorts. >> >> I've not yet spent the time to get this to work though, but I'm fairly >> sure it'd use less temporary space than the current approach with the >> 2 tuplesorts, and could have lower overall CPU overhead as well >> because the number of sortable items gets reduced much earlier in the >> process. > > I've now spent some time on this. Attached the original patchset, plus > 2 incremental patches, the first of which implement the above design > (patch no. 8). > > Local tests show it's significantly faster: for the below test case > I've seen reindex time reduced from 777455ms to 626217ms, or ~20% > improvement. > > After applying the 'reduce the size of GinTuple' patch, index creation > time is down to 551514ms, or about 29% faster total. This all was > tested with a fresh stock postgres configuration. > > """ > CREATE UNLOGGED TABLE testdata > AS SELECT sha256(i::text::bytea)::text > FROM generate_series(1, 15000000) i; > CREATE INDEX ON testdata USING gin (sha256 gin_trgm_ops); > """ > Those results look really promising. I certainly would not have expected such improvements - 20-30% speedup on top of the already parallel run seems great. I'll do a bit more testing to see how much this helps for the "more realistic" data sets. I can't say I 100% understand how the new stuff in tuplesortvariants.c works, but that's mostly because my knowledge of tuplesort internals is fairly limited (and I managed to survive without that until now). What makes me a bit unsure/uneasy is that this seems to "inject" custom code fairly deep into the tuplesort logic. I'm not quite sure if this is a good idea ... regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company