Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql - return entire row as array

Tags:

sql

postgresql

Is there a way how you can cast the following result to array?

select pg_tables from pg_tables

This will return one column only, however the data type is not array.

Edit: I'm using PostgreSql 9.1.4

Update: I need an equivalent of the following SQL statement, without the need to write column names, applicable for every table:

select 
    string_to_array(
    schemaname || '|' ||
    tablename || '|' || 
    tableowner || '|' ||
    coalesce(tablespace,'') || '|' ||
    hasindexes || '|' ||
    hasrules || '|' ||
    hastriggers
    ,'|')
from 
    pg_tables
like image 579
Tomas Greif Avatar asked Jul 13 '12 10:07

Tomas Greif


People also ask

How do I return an array in PostgreSQL?

CREATE OR REPLACE FUNCTION get_numbers(maxNo INTEGER) RETURNS TEXT[] AS $$ DECLARE counter INTEGER := 0; nums TEXT[] := ARRAY[]::TEXT[]; BEGIN LOOP EXIT WHEN counter = maxNo; counter = counter + 1; nums = array_append(nums, counter::TEXT); END LOOP; RETURN nums; END ; $$ LANGUAGE plpgsql; – nick w.

What is [] in PostgreSQL?

As we know that, each data type has its companion array type, such as a character has character[] array type, integer has an integer[] array type, etc. PostgreSQL allows us to specify a column as an array of any valid data type, which involves user-defined data type, enumerated data type, and built-in datatype.

What is Array_agg in Postgres?

PostgreSQL ARRAY_AGG() function is an aggregate function that accepts a set of values and returns an array where each value in the input set is assigned to an element of the array.

What is Unnest in PostgreSQL?

Unnest function generates a table structure of an array in PostgreSQL. Unnest array function is beneficial in PostgreSQL for expanding the array into the set of values or converting the array into the structure of the rows. PostgreSQL offers unnest() function.


1 Answers

Might be this: http://www.sqlfiddle.com/#!1/d41d8/364

select translate(string_to_array(x.*::text,',')::text,'()','')::text[] 
from pg_tables as x

How it works (inside-out), 5 steps:

1st:

select x.*::text from pg_tables as x;

Sample Output:

|                                                            X |
----------------------------------------------------------------
|                    (pg_catalog,pg_statistic,postgres,,t,f,f) |
|                         (pg_catalog,pg_type,postgres,,t,f,f) |

2nd:

select string_to_array(x.*::text,',') from pg_tables as x;

Sample Output:

|                           STRING_TO_ARRAY |
---------------------------------------------
| (pg_catalog,pg_statistic,postgres,,t,f,f) |
|      (pg_catalog,pg_type,postgres,,t,f,f) |

3rd:

select string_to_array(x.*::text,',')::text from pg_tables as x;

Sample Output:

|                               STRING_TO_ARRAY |
-------------------------------------------------
| {(pg_catalog,pg_statistic,postgres,"",t,f,f)} |
|      {(pg_catalog,pg_type,postgres,"",t,f,f)} |

4th:

select translate( string_to_array(x.*::text,',')::text, '()', '') from pg_tables as x

Sample Output:

|                                   TRANSLATE |
-----------------------------------------------
| {pg_catalog,pg_statistic,postgres,"",t,f,f} |
|      {pg_catalog,pg_type,postgres,"",t,f,f} |

Finally:

select translate( string_to_array(x.*::text,',')::text, '()', '')::text[] 
from pg_tables as x

Sample Output:

|                               TRANSLATE |
-------------------------------------------
| pg_catalog,pg_statistic,postgres,,t,f,f |
|      pg_catalog,pg_type,postgres,,t,f,f |

Live test: http://www.sqlfiddle.com/#!1/d41d8/373

To prove that it works:

with a as 
(
  select translate( string_to_array(x.*::text,',')::text, '()', '')::text[] as colArray 
  from pg_tables as x
)
select row_number() over(), unnest(colArray)
from a;

Sample output:

| ROW_NUMBER |                  UNNEST |
----------------------------------------
|          1 |              pg_catalog |
|          1 |            pg_statistic |
|          1 |                postgres |
|          1 |                         |
|          1 |                       t |
|          1 |                       f |
|          1 |                       f |
|          2 |              pg_catalog |
|          2 |                 pg_type |
|          2 |                postgres |
|          2 |                         |
|          2 |                       t |
|          2 |                       f |
|          2 |                       f |
like image 132
Michael Buen Avatar answered Oct 10 '22 18:10

Michael Buen