Create a sample table:
CREATE TEMP TABLE foo (id int, a text, b text, c text); INSERT INTO foo VALUES (1, 'ant', 'cat', 'chimp'), (2, 'grape', 'mint', 'basil');
With UNION ALL, you can disable or uncrosstab:
SELECT id, 'a' AS colname, a AS thing FROM foo UNION ALL SELECT id, 'b' AS colname, b AS thing FROM foo UNION ALL SELECT id, 'c' AS colname, c AS thing FROM foo ORDER BY id;
This starts 3 different subqueries on foo , one for each column that we want to disable, and returns in a single table each record from each of the subqueries.
But this scans the table N times, where N is the number of columns you want to disable. This is inefficient and a big problem when, for example, you are working with a very large table, which takes a lot of time to scan.
Use instead:
SELECT id, unnest(array['a', 'b', 'c']) AS colname, unnest(array[a, b, c]) AS thing FROM foo ORDER BY id;
It is easier to write and it will only scan once.
array[a, b, c] returns an array object with the values ββa, b and c as its elements. unnest(array[a, b, c]) splits the results on one line for each of the elements of the array.
Hope this helps!