I found this explanation of how prepared statements are compiled, but I'm still unsure.

What exactly is "compiled" in the context of a prepared statement? Is a C compiler executed to create a custom executable?

If so, is that limited by prepareThreshold?

If not, what exactly is it compiled into, and are there any projects for converting prepared statements into executables?

1 Answer 1


The post you found is from 2007. Rather start with the current manual:

When the PREPARE statement is executed, the specified statement is parsed, analyzed, and rewritten. When an EXECUTE command is subsequently issued, the prepared statement is planned and executed. This division of labor avoids repetitive parse analysis work, while allowing the execution plan to depend on the specific parameter values supplied.


If a prepared statement is executed enough times, the server may eventually decide to save and re-use a generic plan rather than re-planning each time.

Bold emphasis mine. Read more details in the manual.

I wouldn't know of projects trying to convert prepared statements into executables.

  • Are there definitions for these terms, or can you link me to the source code that handles prepared statements? Thank you in advance.
    – Jim Bob
    Commented Feb 27, 2015 at 23:26
  • @JimBob: The source code for this is in postgres.c, the "main" module of the postgres backend: github.com/postgres/postgres/blob/… Commented Feb 27, 2015 at 23:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.