Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the postgres sql 'cast a tuple' idiom documented?

Tags:

sql

postgresql

I found an expression in a postgres.org email, which seems useful, but whose documentation I've not been able to find.

select t.*::text from t

Here, the ::text cast seems to apply to the each of the columns, and the resulting output as displayed by psql is a bit odd. Perhaps someone could educate guys like me and unravel that mystery.

Edit: Curiously, the section 4.2.9. Type Casts makes no reference to this syntax

Edit: Finally! Related documentation found at 4.2.13. Row Constructors

like image 209
artejera Avatar asked Sep 05 '17 01:09

artejera


People also ask

How do I cast an expression in PostgreSQL?

PostgreSQL supports a CAST operator that is used to convert a value of one type to another. Syntax: CAST ( expression AS target_type ); Let's analyze the above syntax: First, specify an expression that can be a constant, a table column, an expression that evaluates to a value.

What is tuple in PostgreSQL?

A tuple is PostgreSQL's internal representation of a row in a table. A single row may have many tuples representing it, but only one of these tuples will be applicable at any single point in time.

How do I get unique records in PostgreSQL?

Removing duplicate rows from a query result set in PostgreSQL can be done using the SELECT statement with the DISTINCT clause. It keeps one row for each group of duplicates. The DISTINCT clause can be used for a single column or for a list of columns.

What is record in PostgreSQL?

PostgreSQL uses record type variables which simply act as placeholders for rows of a result set, similar to a row type variable. However, unlike row type variables, they do not have a predefined structure. Their structure is only determined after assigning a row to them.


2 Answers

I can describe what's happening. Consider this syntax:

select (1, 2)

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

select (1, 2)::text

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

select (t.*)::text

You'll get the same result with:

select cast(t.* as text)
like image 56
Gordon Linoff Avatar answered Sep 19 '22 12:09

Gordon Linoff


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

SELECT t::text FROM t

t.* would be used to decompose the row, but since you cast the whole row to text, the step is redundant noise.

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

You get a text representation of the row either way. I.e. the output is the valid string literal, which you can cast back to the registered row type, like:

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

t being the name of a table or (materialized) view (visible in the current search_path or you have to schema-qualify) or any other registered (row) type.

This works with table names out of the box because Postgres registers a row type for every table you create.

Note, neither the t.* notation nor the cast back is possible for anonymous records (like ROW(1,2) or just (1,2) for short). There is no information about their structure in the system catalogs.

Related:

  • How to concatenate all results from table row?
like image 43
Erwin Brandstetter Avatar answered Sep 21 '22 12:09

Erwin Brandstetter