HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Where does the magic column "name" come from?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
thecomecolumnmagicwherenamedoesfrom

Problem

I got this by accident:

db=> select name from site;
ERROR:  column "name" does not exist
LINE 1: select name from site;
               ^
db=> select site.name from site;
     name
---------------
 (1,mysitename)
(1 row)


The second query return a tuple containing a whole row. Using postgres 9.0.1.

Edit: the definition of site by request. I doesn't really matter, this quirk works for any table.

db=> \d site
                         Table "public.site"
 Column |  Type   |                     Modifiers
--------+---------+---------------------------------------------------
 id     | integer | not null default nextval('site_id_seq'::regclass)
 title  | text    | not null

Solution

NAME is actually a function. It's a quirk of Postgres that a function with one argument e.g. function(arg) can also be called as arg.function. From the docs:


The equivalence between functional
notation and attribute notation makes
it possible to use functions on
composite types to emulate "computed
fields".

NAME is an internal type for object names, and this function is casting its argument to that type and returning it.

Context

StackExchange Database Administrators Q#2231, answer score: 11

Revisions (0)

No revisions yet.