简体   繁体   English

Postgres 排序规则

[英]Postgres COLLATION FOR

I have tried the function collation for(argument) which is described in the PostgreSQL documentation .我已经尝试了 PostgreSQL 文档中描述的函数collation for(argument) But when I try to execute the query which is given in the same PostgreSQL documentation, I get an error.但是当我尝试执行同一 PostgreSQL 文档中给出的查询时,出现错误。 Actually, the statement that I ran was实际上,我运行的语句是

SELECT collation for ('sample') FROM pg_description LIMIT 1;
  1. It could be better if someone gives the exact correct query and the correct parameters for the function.如果有人为该函数提供完全正确的查询和正确的参数,那可能会更好。
  2. What is the use case of this function?这个功能的用例是什么?
  3. If the function name was wrong, please give me the correct function name.如果函数名错误,请给我正确的函数名。

Strictly speaking, COLLATION FOR is not a "function", but a syntax element of SQL.严格来说, COLLATION FOR不是一个“函数”,而是SQL 的一个语法元素。 The underlying function in Postgres is pg_collation_for() . Postgres 中的底层函数是pg_collation_for()

All required information is in the manual, even for your outdated version Postgres 9.5 .所有必需的信息都在手册中,即使是您的过时版本 Postgres 9.5 也是如此 Search for "collation for" on that manual page and you'll find an example with explanation.在该手册页上搜索“collation for” ,您会找到一个带有解释的示例。

But it's more clearly documented in the current version (quote from pg 15):但它在当前版本中有更清楚的记录(引自第 15 页):

Returns the name of the collation of the value that is passed to it.返回传递给它的值的排序规则的名称。 The value is quoted and schema-qualified if necessary.如有必要,该值会被引用和模式限定。 If no collation was derived for the argument expression, then NULL is returned.如果没有为参数表达式派生排序规则,则返回 NULL。 If the argument is not of a collatable data type, then an error is raised.如果参数不是可整理数据类型,则会引发错误。

Bold emphasis mine.大胆强调我的。

Basically, only string types like text and varchar are collatable.基本上,只有像textvarchar这样的字符串类型是可整理的。

No error should happen for the example query from the manual as pg_description.description is type text .手册中的示例查询不应发生错误,因为pg_description.description是类型text But you ran a different one:但是你运行了一个不同的:

SELECT collation for ('sample') FROM pg_description LIMIT 1;

Which can be shortened to just:可以缩短为:

SELECT collation for ('sample');

'sample' is an untyped string literal, ie type "unknonwn", but it will be coerced to text by default, so it should not error out, either. 'sample'是一个无类型的字符串文字,即类型为“unknonwn”,但默认情况下它会被强制转换为text ,因此它也不应该出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM