0

I have a base table with many derived tables in PostgreSQL 9.2. For each derived table there is a function that returns "SETOF derived_table". These functions are generated code and they don't need to know the derived column names, because they simply call

SELECT *
FROM derived_table
WHERE [some conditions on base columns]

I now want the functions to modify the returned values for some of the base columns, but continue to return all the derived columns, ie. something like this:

SELECT 'something else' AS base_col1, base_col2, base_col3, [all derived columns]
FROM derived_table
WHERE ...

Is there a way to express this in Postgres without listing all derived column names?

Alternatively, I could return the data I want in separate columns if only there was a way to define the function to return something like "SETOF my_special_column+derived_table". Again, I want to somehow avoid listing all the derived column names, so the functions can still be easily auto-generated.

1 Answer 1

1

If you create a view of the modified columns

create view view_derived_table as
select 'something else' as new_column, *
from derived_table;

Then you can return a setof view_derived_table

create function f_view_derived_table()
returns setof view_derived_table as $$
    select *
    from view_derived_table
    where some_condition;
$$ language sql;
Sign up to request clarification or add additional context in comments.

2 Comments

An interesting idea! As posted, this still requires me to manually list the derived columns, but if I add a new column instead the view can be defined as: SELECT 'something else' AS new_column, * FROM derived_table
@EM Yes, that is what I was my intention but I just copied your code and forgot to change it to , *

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.