John
John

Reputation: 634

How to select a table from a function param

I have the function:

test(IN _partition)

Inside my function I would like to append the '_partition' to the beginning of my table name.

FROM table_ + '_partition'

The end result:

select * from test(12345);

This function will query the table: table_12345

Upvotes: 0

Views: 34

Answers (1)

Craig Ringer
Craig Ringer

Reputation: 324475

You need PL/PgSQL dynamic SQL with EXECUTE and the format function, e.g.

RETURN QUERY EXECUTE format('SELECT * FROM %I', _partition);

However, this is a mark of a design that will probably be painful to work with.

Have you looked at PostgreSQL's table inheritance features?

Upvotes: 1

Related Questions