postgresql `SELECT DISTINCT` 查询是确定性的吗?

Are postgresql `SELECT DISTINCT` queries deterministic?

Postgres SELECT DISTINCT 查询是确定性的吗?

如果 table(和整个数据库)保持不变,SELECT DISTINCT somecolumn FROM sometable return 会得到相同的结果(包括顺序)吗?

Select Query Documentation描述部分注释:

If the ORDER BY clause is specified, the returned rows are sorted in the specified order. If ORDER BY is not given, the rows are returned in whatever order the system finds fastest to produce.

他们在 DISTINCT ON 子句部分添加:

Note that the "first row" of each set is unpredictable unless ORDER BY is used to ensure that the desired row appears first.

一般来说,当数据库未更改时,这仍然是正确的吗?

这个答案假定 select 中的表达式是确定性的。否则,这个问题似乎微不足道。

未指定顺序,因此它可能会在查询的运行之间或在不同的系统上发生变化。但是,结果集应该是一样的。

您从文档中引用的第二个引文是针对 distinct on 的。这不是确定性的,除非您使用的是稳定排序。

注意:如果您使用不区分大小写的排序规则,您可能会得到不确定的结果。内置排序规则区分大小写;不区分大小写意味着原始表达式不是确定性的。