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 1hUbI7-0003f8-88 for pgsql-performance@arkaria.postgresql.org; Sat, 25 May 2019 18:21:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hUbI5-0003xy-2E for pgsql-performance@arkaria.postgresql.org; Sat, 25 May 2019 18:21:09 +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 1hUbI4-0003xr-FP for pgsql-performance@lists.postgresql.org; Sat, 25 May 2019 18:21:08 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hUbHx-0001TV-Du for pgsql-performance@lists.postgresql.org; Sat, 25 May 2019 18:21:07 +0000 Received: by mail-wr1-x443.google.com with SMTP id r7so12940734wrr.13 for ; Sat, 25 May 2019 11:21:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=subject:to:cc:references:from:openpgp:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=fKGD4APjTo6ASPuOpBs9QRhR1GN8iVllW7Ru00pBoMQ=; b=bqm8UkmqYYoH4v0b8UcqJ6TM73n0+XwMtfYidCvA4u+t3ldbVquz1JrzQDbG/DWyYT FUp5C6n/DQi5T9E9ZVmPe28UtH9MsU4KmoSO0NRQXQQSqg4hWQI3abHTKDeWUSG5cO5m CMOVzORk1By8yKnLOAMAgk68lAfcsz/huAcf+d8vJ2XkXuE8zhWp1vWWLKe63+wKdbW4 QC8Gez5GEh84cxjUAiEUYT23KDBSKDD2/xRR9YzMBAOAtL0hqjix3iEjckDFr6kZDQ7C qmZ7Lzx5TjW/ZAQtlHG2T2sAyYvgWK/lh/tVBHlFqGogTkcSKYs+tpR21yN0fL2+vrhA rhKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:openpgp:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=fKGD4APjTo6ASPuOpBs9QRhR1GN8iVllW7Ru00pBoMQ=; b=gDy1YLOxX7xaS4cjScjyM8kTUsIwhu+JgnomJKu299GDFaAoxt74X8tH1PIdk/FsoQ SLUqfpMkzwNBx1n+eKMU6nh32x//UDcHud63XR2E1LQ7mdlPj3x0tXKk9tWOVFV38f09 eEQ28VjkRjN6+nLM4NTmEbrSg9zbc1+JImYzJ/CR2tWq6kxD/oJdxQhYvwJR67cItQf3 Xd4lpeCeLmlufMcyvMAlXHwDTE1Bi6fATFeXIeAuWpjNGIOgEUF9zCkf7hlt3JRKW5o3 6/Qn6sXGu0ITuMPvmqpXpxi1XIcDGdmuusUutQ4y6DO/BttiXx9u5t++fbANBr90fdI/ eDmA== X-Gm-Message-State: APjAAAVGDkMhGob4rXoiPKoz7whTBobsmRPSm4AhKak+/3wK7Qis+Vua SmWAkEgEUjX/VaU+8sZEC+tNAxCm X-Google-Smtp-Source: APXvYqzH6/T8DLfoMQYufFFgN4wIcBRvD3QiunlG1kyhSC3vHq0g+BqSgER2UeTRvjk3C3wjwuuhZg== X-Received: by 2002:adf:9c8a:: with SMTP id d10mr14988379wre.9.1558808459195; Sat, 25 May 2019 11:20:59 -0700 (PDT) Received: from [192.168.66.78] (pD9582420.dip0.t-ipconnect.de. [217.88.36.32]) by smtp.googlemail.com with ESMTPSA id p16sm11053993wrg.49.2019.05.25.11.20.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 25 May 2019 11:20:58 -0700 (PDT) Subject: Re: UUID v1 optimizations... To: Tom Lane Cc: pgsql-performance@lists.postgresql.org References: <8d5f4b67-d540-8d3d-9979-808b9c1e1ca7@googlemail.com> <20635.1558796270@sss.pgh.pa.us> From: Ancoron Luciferis Openpgp: preference=signencrypt Autocrypt: addr=ancoron.luciferis@googlemail.com; prefer-encrypt=mutual; keydata= mQINBE3Z7bUBEAC3ZYDTnxOiu0JWJR+6XzFQmJEVncGMehyuE9GBfyaNYnsNKmFITkubNVB9 tN/xgn5FJ9eg0WediTaWXigHNDDOPSNHZGaf1nHu0AQNOgpEyJETtX/A56PXQ82+VwoKCNUN v8+m5CJ8njWZlZMCVA3AcQ1vR2VRR6O0pzEVPDCDMutPytkRl+XrPoPhxMTVEmwM4jfWwmfI NgiOZnsueU78VW8Go1qTN20YdVOdVpbPWfyp8VIGYBNjEEefv5f/l+zt4Z+CQMmvunDUdaKY 85opNIMLqC3dDY0AQG+FpcDLhHWLcuGLmE+Ghiix0G3kX4QX/fH0/2KtVNdfnZtcpq+5rim0 E97PVQe4EixVD/DivpwvNMWqQ2GJlOCx52+J13ZSfe/63XpjL7TNqhR3wMnNuMidLMgLoH5z RtQ2FH9zeYlKgqMUkmwvEHVI+M4oQRE0rzgqx6xHbkPIFEov9ZziQVwCtct+Ja9ho/BXoe/T VIFVOGEp7DG12QSaiLILhzTRUkDXqa+BH9H4V2Hsv77nT2JQ7KO9pM5gb4wq4gtLFo7MMTUq jlqjD2vOm3NY3tg1s7HTggVNgGsWhh9JlNR3v9SRGckKVIIvwDxvzo+Z4iji2SLElWb9BWiH Xku5IEuWGTmC+nOzSvbSxTaC53JcppC5kKXj11N73uw5eR4s3wARAQABtDRBbmNvcm9uIEx1 Y2lmZXJpcyA8YW5jb3Jvbi5sdWNpZmVyaXNAZ29vZ2xlbWFpbC5jb20+iQI4BBMBAgAiBQJN 2e21AhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAAKCRBzrR1GWXG7SN0DD/9qJR1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a103-b991-ff20-b73852085f30@googlemail.com> Date: Sat, 25 May 2019 20:20:58 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20635.1558796270@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 25/05/2019 16:57, Tom Lane wrote: > Ancoron Luciferis writes: >> So I investigated the PostgreSQL code to see how it is handling UUID's >> with respect to storage, sorting, aso. but all I could find was that it >> basically falls back to the 16-byte. > > Yup, they're just blobs to us. > >> After struggling to find a way to optimize things inside the database, I >> reverted to introduce a hack into the application by not shuffling the >> timestamp bytes for the UUID's, which makes it look quite serial in >> terms of byte order. > >> So, my question now is: Would it make sense for you to handle these >> time-based UUID's differently internally? Specifically un-shuffling the >> timestamp before they are going to storage? > > No, because > > (1) UUID layout is standardized; You mean the presentation at the byte-level is. ;) > > (2) such a change would break on-disk compatibility for existing > databases; Yes, that certainly is a show-stopper. > > (3) particularly for the case of application-generated UUIDs, we do > not have enough information to know that this would actually do anything > useful; Well, not only the layout is standardized, but also there is a semantic to it depending on the version. Specifically for version 1, it has: 1. a timestamp 2. a clock sequence 3. a node id Ans as PostgreSQL already provides this pretty concrete data type, it could be a natural consequence to also support the semantic of it. E.g. the network types also come with a lot of additional operators and functions. So I don't see a reason not to respect the additional capabilities of a UUID. For other versions of UUID's, functions like timestamp would certainly not be available (return NULL?), respecting the concrete semantic. > > (4) it in fact *wouldn't* do anything useful, because we'd still have > to sort UUIDs in the same order as today, meaning that btree index behavior > would remain the same as before. Plus UUID comparison would get a lot > more complicated and slower than it is now. I get your first sentence, but not your second. I know that when changing the internal byte order we'd have to completed re-compute everything on-disk (from table to index data), but why would the sorting in the index have to be the same? And actually, comparison logic wouldn't need to be changed at all if the byte order is "changed" when the UUID is read in when reading the representation into the internal UUID's byte array. Function: string_to_uuid(const char *source, pg_uuid_t *uuid); ^^ here I would apply the change. And of course, reverse it for generating the textual representation. That would slow down writes a bit, but that shouldn't be the case because index insertions are speed up even more. But still, on-disk change is still a show-stopper, I guess. > > (5) even if we ignored all that and did it anyway, it would only help > for version-1 UUIDs. The index performance issue would still remain for > version-4 (random) UUIDs, which are if anything more common than v1. Yes, I am aware that the changes might be of very limited gain. V4 UUID's are usually used for external identifiers. For internal ones, they don't make sense to me (too long, issues with randomness/enthropie under high load, ...). ;) I just recently used these UUID's also together with a function for TimescaleDB auto-partitioning to provide the timestamp for the partitioning logic instead of the need for a separate timestamp column. This is also one of the reasons why I was also asking for native support functions to extract the timestamp. I am apparently not very good at C so I am currently using Python and/or PgPLSQL for it, which is pretty slow. > > > FWIW, I don't know what tool you're using to get those "bloat" numbers, > but just because somebody calls it bloat doesn't mean that it is. > The normal, steady-state load factor for a btree index is generally > understood to be about 2/3rds, and that looks to be about what > you're getting for the regular-UUID-format index. The fact that the > serially-loaded index has nearly no dead space is because we hack the > page split logic to make that happen --- but that is a hack, and it's > not without downsides. It should *not* be taken to be an indication > of what you can expect for any other insertion pattern. OK, understood. I was actually a bit surprised by those numbers myself as these "serial" UUID's still only have the timestamp bytes in ascending order, the clock sequence and node is still pretty random (but not inside a single transaction, which might help the hack). > > The insertion-speed aspect is a real problem, but the core of that problem > is that use of any sort of standard-format UUID turns applications that > might have had considerable locality of reference into applications that > have none. If you can manage to keep your whole index in RAM that would > not hurt too much, but as soon as it doesn't fit you have a problem. > When your app has more or less predictable reference patterns it's best > to design a unique key that matches that, instead of expecting that > essentially-random keys will work well. The system was configured to have more than enough space for the index and table data to fit into memory, but I am not sure. How can I verify that? An EXPLAIN on the INSERT apparently doesn't include index insertion. > > Or in short, hacking up the way your app generates UUIDs is exactly > the right way to proceed here. OK. Glad to hear that. One last question, though: Would it make sense to create a specialized UUID v1 type (e.g. with an extension) that does the transformation and delegates for all other things to the existing UUID type support? > > regards, tom lane >