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 1r2ebf-000EZS-Vy for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Nov 2023 21:36:32 +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 1r2ebe-001xI4-Lm for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Nov 2023 21:36:30 +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 1r2ebe-001xGE-9J for pgsql-hackers@lists.postgresql.org; Mon, 13 Nov 2023 21:36:30 +0000 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r2ebb-006HgP-Iw for pgsql-hackers@postgresql.org; Mon, 13 Nov 2023 21:36:29 +0000 Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-543456dbd7bso12006315a12.1 for ; Mon, 13 Nov 2023 13:36:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1699911386; x=1700516186; darn=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=MSAhb9N20usfKC1o+8BEQMVfuERU2iXwoYsKp5y9BGQ=; b=TC4lATR7+Ovyo6q7Xe2gUKHLb7OJJLkwoq0FnhM+B1jq7D0YJtaz8HlwcYiGkP2EOU ctY9rXW3dyKpr/Z4lJUIDAaEzXl0IX6cRjr/QVQDYl7FQB7piWmXKkbUdXU1OMRaqiq1 yuYBuxAVeBeWPABoEyPYrerPJpZSuCLOIr62rz7qn5jSVItdZuvvzNX0yqOZm7IlzXLx zV/SEmcxlbixczY4Olor7Q0+emN8VbqwoyDDW0BYQtf9I/lp3hadsk+hoy4qr0yxGfdR 7fxphcH+c3Si0lSk2e9pBTTZIPqxKEWmI4TyiKN6vEu3QjbtsRb+vu9CDQrGTUuzvcKP /mZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699911386; x=1700516186; 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=MSAhb9N20usfKC1o+8BEQMVfuERU2iXwoYsKp5y9BGQ=; b=l5eFj7di7IENjPPPSu8Tisw5KsIzu+3SPgnhLV3ywEg3SLwvWG77QN0YuRRM3O4336 HiJI//oo3FO2Z5LGyvd1QAp4YVUwGXu3aIX5HMZtBPRMoDpQEnLIlwIROQnZs9lF4J3C 83xK6Pti1y8Ng6dCT2+i3ztC9/tMSBlVbmfu6vlhM99DGQtJr448OHvCwHFjLmsWHj+d sq0zA3W6iuiWKTXGYk/cP/+NIo5EWa3cdcVJ67t3h0FTlnMtpo93kiSTNBXeukG6iHkc 0OkBTJlqKCWt2pnwpPfKWrn8x7MUtqm2fMCLXdJ5upyEkDJdZjjUwOp/tHp26QhB770b Zu4A== X-Gm-Message-State: AOJu0YxR7xwnXRF8vlLWZCq7nFeB0rx3kdYAQpyLgrDrACP+v/5eAMGq bg46yNAdRYL3yCp3GX7DBA7mtg== X-Google-Smtp-Source: AGHT+IFAZJnJG1zufpnRYPB6Akcp4j+eP4aTLNTCiPr18F8M+/t6nvSmpUcJOXbgQxYkze4DtvL/dQ== X-Received: by 2002:a17:906:ba88:b0:9ec:fbbc:ae7e with SMTP id cu8-20020a170906ba8800b009ecfbbcae7emr577797ejd.3.1699911385806; Mon, 13 Nov 2023 13:36:25 -0800 (PST) Received: from [10.137.0.17] (ip-86-49-250-157.bb.vodafone.cz. [86.49.250.157]) by smtp.gmail.com with ESMTPSA id i20-20020a170906a29400b009ada9f7217asm4506267ejz.88.2023.11.13.13.36.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Nov 2023 13:36:25 -0800 (PST) Message-ID: <89e23afc-97b7-bf4c-44fb-3fa0a90dad58@enterprisedb.com> Date: Mon, 13 Nov 2023 22:36:24 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: Why do indexes and sorts use the database collation? To: Andres Freund , Jeff Davis Cc: pgsql-hackers@postgresql.org References: <20231111011943.ktfppxrcqtjo66bg@alap3.anarazel.de> <78ae1ecb0ad181e1a9352ec0d497d88304c974bb.camel@j-davis.com> <20231113180247.vud4c5uy77ojf6sx@awork3.anarazel.de> Content-Language: en-US From: Tomas Vondra In-Reply-To: <20231113180247.vud4c5uy77ojf6sx@awork3.anarazel.de> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/13/23 19:02, Andres Freund wrote: > Hi, > > On 2023-11-11 23:19:55 -0800, Jeff Davis wrote: >> On Fri, 2023-11-10 at 17:19 -0800, Andres Freund wrote: >>> I guess you are arguing that the user didn't intend to create an >>> index here? >> >> No, obviously the user should expect an index when a primary key is >> created. But that doesn't mean that it necessarily needs to be ordered >> according to the database collation. >> >> Unfortunately, right now the planner doesn't understand that an index >> in the "C" locale can satisfy equality searches and constraint >> enforcement for "en_US" (or any other deterministic collation). That's >> probably the first thing to fix. >> >> Inequalities and ORDER BYs can't benefit from an index with a different >> collation, but lots of indexes don't need that. > > But we don't know whether the index is used for that. If we just change the > behaviour, there will be lots of pain around upgrades, because queries will > continue to work but be dog slow. > Yeah. I don't quite agree with the initial argument that not specifying the collation explicitly in CREATE TABLE or a query means the user does not care about the collation. We do have the sensible behavior that if you don't specify a collation, you get the database one as a default. I don't think we can just arbitrarily override the default because we happen to think "C" is going to be faster. If we could prove that using "C" is going to produce exactly the same results as for the implicit collation (for a given operation), then we can simply do that. Not sure if such proof is possible, though. For example, I don't see how we could arbitrarily override the collation for indexes backing primary keys, because how would you know the user will never do a sort on it? Not that uncommon with natural primary keys, I think (not a great practice, but people do that). Perhaps we could allow the PK index to have a different collation, say by supporting something like this: ALTER TABLE distributors ADD PRIMARY KEY (dist_id COLLATE "C"); And then the planner would just pick the right index, I think. > >>> Also, wouldn't the intent to use a different collation for the column >>> be >>> expressed by changing the column's collation? >> >> The column collation expresses the semantics of that column. If the >> user has a database collation of "en_US", they should expect ORDER BY >> on that column to be according to that locale unless otherwise >> specified. > > That makes no sense to me. Either the user cares about ordering, in which case > the index needs to be in that ordering for efficient ORDER BY, or they don't, > in which neither index nor column needs a non-C collation. You partially > premised your argument on the content of primary keys typically making non-C > collations undesirable! > I may be missing something, but what's the disagreement here? If the user cares about ordering, they'll specify ORDER BY with either an explicit or the default collation. If the index collation matches, it may be useful for the ordering. Of course, if we feel entitled to create the primary key index with a collation of our choosing, that'd make this unpredictable. > >>> OTOH, if we are choosing a groupagg, we might be able to implement >>> that using >>> an index, which is more likey to exist in the databases collation. >>> Looks like >>> we even just look for indexes that are in the database collation. >>> >>> Might be worth teaching the planner additional smarts here. >> >> Yeah, we don't need to force anything, we could just create a few paths >> with appropriate path key information and cost them. > > I'm not sure it's quite that easy. One issue is obviously that this could lead > to a huge increase in paths we need to keep around due to differing path > keys. We might need to be a bit more aggressive about pruning such paths than > I think we would be today. > Right. There's also the challenge that we determine "interesting pathkeys" very early, and I'm not sure if we can decide which pathkeys (for different collations) are cheaper at that point. > >>> - Teach the planner to use cheaper collations when ordering for >>> reasons other >>>   than the user's direct request (e.g. DISTINCT/GROUP BY, merge >>> joins). >> >> +1. Where "cheaper" comes from is an interesting question -- is it a >> property of the provider or the specific collation? Or do we just call >> "C" special? > > I'd think the specific collation. Even if we initially perhaps just get the > default cost from the provider such, it structurally seems the sanest place to > locate the cost. > ISTM it's about how complex the rules implemented by the collation are, so I agree the cost should be a feature of collations not providers. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company