Search code examples
sqlpostgresqlsplitset-returning-functions

Split column into multiple rows in Postgres


Suppose I have a table like this:

subject flag
this is a test 2

subject is of type text, and flag is of type int. I would like to transform this table to something like this in Postgres:

token flag
this 2
is 2
a 2
test 2

Is there an easy way to do this?


Solution

  • Use a LATERAL join - with string_to_table() in Postgres 14+.
    Minimal form:

    SELECT token, flag
    FROM   tbl, string_to_table(subject, ' ') token
    WHERE  flag = 2;
    

    The comma in the FROM list is (almost) equivalent to CROSS JOIN, LATERAL is automatically assumed for set-returning functions (SRF) in the FROM list. Why "almost"? See:

    The alias "token" for the derived table is also assumed as column alias for a single anonymous column, and we assumed distinct column names across the query. Equivalent, more verbose and less error-prone:

    SELECT s.token, t.flag
    FROM   tbl t
    CROSS  JOIN LATERAL string_to_table(subject, ' ') AS s(token)
    WHERE  t.flag = 2;
    

    Or move the SRF to the SELECT list, which is allowed in Postgres (but not in standard SQL), to (almost) the same effect:

    SELECT string_to_table(subject, ' ') AS token, flag
    FROM   tbl
    WHERE  flag = 2;
    

    The last one seems acceptable since SRF in the SELECT list have been sanitized in Postgres 10. See:

    If string_to_table() does not return any rows (empty or null subject), the (implicit) join eliminates the row from the result. Use LEFT JOIN ... ON true to keep qualifying rows from tbl. See:

    We could also use regexp_split_to_table(), but that's slower. Regular expressions are powerful but expensive. See:

    In Postgres 13 or older use unnest(string_to_array(subject, ' ')) instead of string_to_table(subject, ' ').