最近研究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