Where is the postgres sql 'cast the tuple' identity document documented?

I found an expression in postgres.org that seems useful, but whose documentation I could not find.

select t.*::text from t 

Here the ::text cast is similar to each of the columns, and the resulting result displayed by psql is a bit odd. Perhaps someone can educate guys like me and solve this mystery.

Edit: Curiously, section 4.2.9. Type Casts does not reference this syntax

Edit: Finally! The relevant documentation is found in 4.2.13. Line constructors

+5
source share
2 answers

I can describe what is happening. Consider this syntax:

 select (1, 2) 

This returns a record (or tuple) with two columns. You can convert a tuple to text using cast() or ::

 select (1, 2)::text 

The same thing happens with t.* . It is interpreted as:

 select (t.*)::text 

You will get the same result:

 select cast(t.* as text) 
+4
source

Just use the name (or alias) of table t directly:

 SELECT t::text FROM t 

t.* will be used to decompose the line, but since you are passing the entire line to text, this step is excess noise.

cast is possible because everything can be added to text in Postgres. (There must be a text representation for input / output.)

You get a textual representation of the string anyway. That is, the output is a valid string literal, which you can return back to the registered string type, for example:

 SELECT '(123,"some text",,"2017-01-03 02:27:27.930164+01")'::t 

t is the name of the table or the (materialized) view (visible in the current search_path or you have for the schema) or any other registered (string) type.

This works with table names out of the box because Postgres registers the row type for each table created.

Note. For anonymous entries (for example, ROW(1,2) or simply (1,2) ), t.* Notation or rollback is not possible to denote anonymous entries. There is no information on their structure in system catalogs.

on this topic:

+2
source

Source: https://habr.com/ru/post/1271513/


All Articles