views:

98

answers:

1

I am writing a PL/pgSQL function. The function has input parameters which specify (indirectly), which tables to read filtering information from.

The function embeds business logic which allows it to select data from different tables based on the input arguments. The function dynamically builds a subquery which returns filtering data which is then used to run the main query.

My questions are:

  1. Is it 'legal' to use a dynamic subquery in a PL/pgSQL function. I cant see why not - but this question is related to the next one.

  2. AFAIK, PL/pgSQL are cached or precompiled by the query engine. How does having a function that generates dynamic subqueries impact the work of the query engine?

+1  A: 

"38.5.4. Executing Dynamic Commands"

Milen A. Radev