虽然绝不是一个明确的答案 - 即。 “是否在所有 PostgreSQL 安装中都定义了行为?”这个 SQL(用于 SQL Server)检查 GUID 中每个字节的顺序。可能需要对 PostgreSQL 进行一些调整。
生成这样的映射应该允许人们查看特定的 UUID 结构(明确定义的类型之一或其他)是否在 PostgreSQL 中“以特定方式排序”。
With UIDs As (-- 0 1 2 3 4 5 6 7 8 9 A B C D E F
Select ID = 'F', UID = cast ('00000000-0000-0000-0000-000000000011' as uniqueidentifier)
Union Select ID = 'E', UID = cast ('00000000-0000-0000-0000-000000001100' as uniqueidentifier)
Union Select ID = 'D', UID = cast ('00000000-0000-0000-0000-000000110000' as uniqueidentifier)
Union Select ID = 'C', UID = cast ('00000000-0000-0000-0000-000011000000' as uniqueidentifier)
Union Select ID = 'B', UID = cast ('00000000-0000-0000-0000-001100000000' as uniqueidentifier)
Union Select ID = 'A', UID = cast ('00000000-0000-0000-0000-110000000000' as uniqueidentifier)
Union Select ID = '9', UID = cast ('00000000-0000-0000-0011-000000000000' as uniqueidentifier)
Union Select ID = '8', UID = cast ('00000000-0000-0000-1100-000000000000' as uniqueidentifier)
Union Select ID = '7', UID = cast ('00000000-0000-0011-0000-000000000000' as uniqueidentifier)
Union Select ID = '6', UID = cast ('00000000-0000-1100-0000-000000000000' as uniqueidentifier)
Union Select ID = '5', UID = cast ('00000000-0011-0000-0000-000000000000' as uniqueidentifier)
Union Select ID = '4', UID = cast ('00000000-1100-0000-0000-000000000000' as uniqueidentifier)
Union Select ID = '3', UID = cast ('00000011-0000-0000-0000-000000000000' as uniqueidentifier)
Union Select ID = '2', UID = cast ('00001100-0000-0000-0000-000000000000' as uniqueidentifier)
Union Select ID = '1', UID = cast ('00110000-0000-0000-0000-000000000000' as uniqueidentifier)
Union Select ID = '0', UID = cast ('11000000-0000-0000-0000-000000000000' as uniqueidentifier)
)
Select * From UIDs Order By UID desc
在 SQL Server(2014,and matches that in SQL Server 2005)中,降序的顺序是:
Position by highest-to-lowest value:
A B C D E F | 8 9 | 7 6 | 5 4 | 3 2 1 0
由于 SQL Server 的 newsequentialid 将这种排序用于索引友好的 GUID 生成,因此行为可能永远不会改变。 SQL Server 还必须在所有系统中保持这种行为以支持复制。因此,如果问题是关于 SQL Server,我肯定会说“in SQL Server 的 GUID 的顺序是一致的”,这绝对可以依赖 in SQL Server。
但是,此排序不同于 .NET 的 GUID 排序,如果 PostgreSQL 中的排序不同,我不会感到惊讶。 SQL Server 中的“翻转”差异是因为它遵循COM GUIDs 的“变体 2”(又名 little-endian)排序;甚至对于“变体 1”UUID 也是如此。 (然而,为什么这些组本身是从右到左排序的,这似乎更随意:更多的微软历史?)
有趣的问题仍然存在:在哪里/如何这个指定要在in PostgreSQL 中订购?如果没有很好的规范,实现是否仍然可以被视为行为公理?
还有see this question for more details about SQL Server's UUIDs;以及“为什么”存在这些差异的精彩细节。