Skip to content
Advertisement

get name of table in function

I need get name of table inside function:

SELECT f(name) FROM table;

how can i get table’s name (table) in function f?

Advertisement

Answer

You cannot get that implicitly, because there are no ties between the table in the FROM clause and the values fed to the function.

Imagine a query like

SELECT f(a.col1 + b.col2)
FROM a CROSS JOIN b;

There is no single table involved!

If you need to pass a table to a function, you have to explicitly pass it:

CREATE FUNCTION f(p_name text, p_table regclass) RETURNS ...
AS ...

You could call that like

SELECT f(name, a.tableoid) FROM atable AS a;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement