Home Explore Blog CI



postgresql

82th chunk of `doc/src/sgml/ref/psql-ref.sgml`
6d3bf12b7665b2b2aae921877dd8546937a947a3fe35e6c70000000100000d3c
 format options can be set for just one query by using
  <literal>\g</literal>:
<programlisting>
peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table</userinput>
peter@localhost testdb-&gt; <userinput>\g (format=aligned tuples_only=off expanded=on)</userinput>
-[ RECORD 1 ]-
first  | 1
second | one
-[ RECORD 2 ]-
first  | 2
second | two
-[ RECORD 3 ]-
first  | 3
second | three
-[ RECORD 4 ]-
first  | 4
second | four
</programlisting>
  </para>

  <para>
   Here is an example of using the <command>\df</command> command to
   find only functions with names matching <literal>int*pl</literal>
   and whose second argument is of type <type>bigint</type>:
<programlisting>
testdb=&gt; <userinput>\df int*pl * bigint</userinput>
                          List of functions
   Schema   |  Name   | Result data type | Argument data types | Type
------------+---------+------------------+---------------------+------
 pg_catalog | int28pl | bigint           | smallint, bigint    | func
 pg_catalog | int48pl | bigint           | integer, bigint     | func
 pg_catalog | int8pl  | bigint           | bigint, bigint      | func
(3 rows)
</programlisting>
  </para>

  <para>
   Here, the <literal>+</literal> option is used to display additional
   information about one of these functions, and <literal>x</literal> is used
   to display the results in expanded mode:
<programlisting>
testdb=&gt; <userinput>\df+x int*pl integer bigint</userinput>
List of functions
-[ RECORD 1 ]-------+-----------------------------
Schema              | pg_catalog
Name                | int48pl
Result data type    | bigint
Argument data types | integer, bigint
Type                | func
Volatility          | immutable
Parallel            | safe
Owner               | postgres
Security            | invoker
Leakproof?          | no
Access privileges   |
Language            | internal
Internal name       | int48pl
Description         | implementation of + operator
</programlisting>
  </para>

  <para>
  When suitable, query results can be shown in a crosstab representation
  with the <command>\crosstabview</command> command:
<programlisting>
testdb=&gt; <userinput>SELECT first, second, first &gt; 2 AS gt2 FROM my_table;</userinput>
 first | second | gt2
-------+--------+-----
     1 | one    | f
     2 | two    | f
     3 | three  | t
     4 | four   | t
(4 rows)

testdb=&gt; <userinput>\crosstabview first second</userinput>
 first | one | two | three | four
-------+-----+-----+-------+------
     1 | f   |     |       |
     2 |     | f   |       |
     3 |     |     | t     |
     4 |     |     |       | t
(4 rows)
</programlisting>

This second example shows a multiplication table with rows sorted in reverse
numerical order and columns with an independent, ascending numerical order.
<programlisting>
testdb=&gt; <userinput>SELECT t1.first as "A", t2.first+100 AS "B", t1.first*(t2.first+100) as "AxB",</userinput>
testdb-&gt; <userinput>row_number() over(order by t2.first) AS ord</userinput>
testdb-&gt; <userinput>FROM my_table t1 CROSS JOIN my_table t2 ORDER BY 1 DESC</userinput>
testdb-&gt; <userinput>\crosstabview "A" "B" "AxB" ord</userinput>
 A | 101 | 102 | 103 | 104
---+-----+-----+-----+-----
 4 | 404 | 408 | 412 | 416
 3 | 303 | 306 | 309 | 312
 2 | 202 | 204 | 206 | 208
 1 | 101 | 102 | 103 | 104
(4 rows)
</programlisting></para>

 </refsect1>

</refentry>

Title: psql: Using \g for Output Formatting, \df for Function Listing, and \crosstabview for Crosstab Representations
Summary
The text provides examples of advanced psql commands. It shows how to use `\g` to apply specific output formatting options to a single query. It demonstrates how to use `\df` to list functions based on name and argument types, including using `\df+x` to display detailed information in expanded mode. Finally, it explains and demonstrates the use of `\crosstabview` to display query results in a crosstab format, including sorting rows and columns.