Вы можете использовать агрегирование:
select concat_ws(' ',
'select',
string_agg(column_name, ', ' order by ordinal_position),
'from',
table_schema || '.' || table_name
) q
from information_schema.columns
where table_name = 'foo'
group by table_schema, table_name
Или без предложения group by
:
select concat_ws(' ',
'select',
string_agg(column_name, ', ' order by ordinal_position),
'from',
max(table_schema) || '.' || max(table_name)
) q
from information_schema.columns
where table_name = 'foo' and table_schema = 'public'
Демонстрация на DB Fiddle :
| q |
| :-------------------------- |
| select i, j from public.foo |