postgresql/lightdb行构造器ROW函数的使用
最近研究PG源码时,遇到行构造器的特性。官方文档https://www.postgresql.org/docs/current/functions-comparisons.html#ROW-WISE-COMPARISON并未给出ROW构造器的示例。
ROW()对应的实现是RowExpr(里面解析后会存储各种字段的明细信息以及对应的record的oid),其实就是匿名记录类型。8.1之后也支持row(t.*),t为表名,主要是为了针对集合比较写代码更省事。
lightdb@oradb=# select row(t.*) from big_table t where row(t.*) is not null limit 3; row ------------------------------------------ (1,57ccab02-9546-4784-99e1-7b7b8eadda56) (2,a0d871d5-e3a9-494d-8aec-12d739e0b1ce) (3,3e08a87c-55b4-438f-b78c-cd0d7f69d720) (3 rows)
lightdb@oradb=# select row(t.*) from big_table t where row(t.*) in (select * from big_table limit 2); row ------------------------------------------ (1,57ccab02-9546-4784-99e1-7b7b8eadda56) (2,a0d871d5-e3a9-494d-8aec-12d739e0b1ce) (2 rows)
SELECT ROW(1,2.5,'this is a test') = ROW(1, 3, 'not the same'); SELECT ROW(table.*) IS NULL FROM table; -- detect all-null rows
https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-ROW-CONSTRUCTORS
http://www.java2s.com/Code/PostgreSQL/Select-Query/RowConstructors.htm