I am quite new to the postgresql.
what is the best way to achieve this?
SELECT get_columns()
FROM table_name;
get_columns(
Since you are using COPY FROM to a known large table, CREATE a FUNCTION which returns SETOF bigtable and SELECTs all the columns from the specific type, use NULL AS fieldname for the fields which are not required in that specific case, something like:
# \d SMALL
Table "public.small"
Column | Type | Modifiers
--------+---------+-----------
a | integer |
b | integer |
c | integer |
d | integer |
# \d LARGE
Table "public.large"
Column | Type | Modifiers
--------+---------+-----------
a | integer |
b | integer |
c | integer |
d | integer |
# CREATE OR REPLACE FUNCTION myData()
RETURNS SETOF large LANGUAGE SQL AS $$
SELECT a,
CASE WHEN a = 1
THEN b
ELSE
NULL
END as b,
CASE WHEN a = 2
THEN c
ELSE
NULL
END AS c,
d
FROM small;
$$;
# SELECT * FROM mydata();
# COPY (SELECT * FROM myData()) TO STDOUT;
Obviously SQL might not be the best language to use, so PL/PgSQL or PL/Perl (or whatever) may be appropriate.