sqlpostgresqltable-alias

What does this "t" do?


I'm having trouble understanding the following example from the PostgreSQL documentation:

-- set returning function WITH ORDINALITY
SELECT * FROM pg_ls_dir('.') WITH ORDINALITY AS t(ls,n);
       ls        | n
-----------------+----
 pg_serial       |  1
 pg_twophase     |  2
 postmaster.opts |  3
 pg_notify       |  4
...

The things inside the parentheses of the t(...) become the column names, but what is the t itself? I'm asking here because the docs don't explain it, and a single-letter function is ungoogleable. In fact, the docs don't even explain what is supposed to come after AS; the only thing we get is this one example.

It seems I can replace t by any other identifier, and it still works.


Solution

  • The syntax you're looking for is:

    function_call [WITH ORDINALITY] [[AS] table_alias [(column_alias [, ... ])]]
    

    https://www.postgresql.org/docs/10/static/queries-table-expressions.html#QUERIES-TABLEFUNCTIONS

    t is an arbitrary table alias; you can give it any valid name you want.