Today I Learned

hashrocket A Hashrocket project

Prepare / Execute

You can store a SQL query with prepare.

db=# prepare posts_search as select title from posts limit 5;
PREPARE

Call the method with execute.

db=# execute posts_search;
               title
-----------------------------------
 Hello World!
 My First Pull Request: HAML
 My First Pull Request: Sinatra
 My First Pull Request: Capistrano
 My First Pull Request: SASS
(5 rows)

Deallocate the query with deallocate, and you can set it again.

db=# deallocate posts_search;
DEALLOCATE
db=# prepare posts_search as select title from posts limit 10;
PREPARE
See More #sql TILs
Looking for help? Hashrocket developers believe that data quality is as important as code quality. We enjoy all the challenges of relational databases, from finding the fastest index, to structuring data to fit the needs of an application. We're eager to share our experiences; check out PG Casts, our series of free PostgreSQL screencasts.