Postgres features and tips
craigkerstiens.com
craigkerstiens.com
CTE's (WITH clauses) are optimisation barriers — the query planner in Postgres won't inline CTEs, but instead will materialise their result into a temporary table first, preventing qual (WHERE clause) push-down and other optimisations.
I know this because it bit me, hard — I went nuts and wrote a lot of the queries in a new system to use WITH clauses, for readability. Performance was atrocious, and required refactoring to inline all the WITH clauses as sub-SELECTS.
CTE's are useful, but should not be used to improve readability.
That being said, does anyone know if there are any plans to address them being optimisation barriers, or what the challenges are? Seems like even a naive rewrite into subselects before even creating the plan would reap a lot of benefits.
.. which indicates that reliance on them being optimization fences may be too ingrained to change. I had assumed that they were treated as sub-selects (given the generally advanced optimizer that PostgreSQL has), and was quite surprised to realize that they aren't. I think this is a major shortcoming of PostgreSQL, especially given the readability advantages of CTEs makes them quite attractive to use (I usually recommend heavy use of CTEs to beginning SQL-ers).
I don't know much about Postgres, but for some things CTEs are a good tool.
I also like the information and catalog schemas. Not something I use all the time, but all of the tables in each schema are worth exploring and seeing what information can be found. When they are needed, they can save a lot of time.
Call me crazy, but I also like using PL/pgSQL. A helpful function I have takes two table or view name arguments and compares if they are the same result set. Total life-saver and a major time boost for redesigning queries and checking to make sure table transfers won't cause corruption.
I love the error messages. They are so clear and always correct. I've found myself reading them and thinking "that can't be right" and sure enough, I was wrong.
Configuration is a breeze. The hba and conf files are fully documented and simple to understand. The location of the files are sensible and easy to find. Seeing how other database systems deal with configuration is eye-opening to say the least.
Last but not least, the documentation is just incredible. I've been asked about good PostgreSQL books, and I always say "read the docs; you'll see why there are few books around."
Window functions can be a big win too.
SequelPro is incredible - easy to use, beautiful, reliable, powerful - but it only works with MySQL (or MariaDB). I've tried Toad but it's very buggy and ugly. The AppStore has SQLPro for Postgres, but I've not heard any reviews, so I'm reticent to spend $30. I've used a Navicat trial for MySQL and it was good but costs around $200.
And actually content copied as well to make it easier to digest:
If you're looking for a list of other clients some of the others ones include:
- Postico OSX - https://itunes.apple.com/us/app/postico/id1031280567?ls=1&mt...
- JackDB (web based) - https://www.jackdb.com/
- SQL Pro for Postgres - http://www.hankinsoft.com/SQLProPostgres/
- PGAdmin - Slightly outdated but still feature rich and fully cross platform - http://www.pgadmin.org/
And of course there's always psql which is all CLI, but incredibly flexible.
I've tried to compile a list of OS X Postgres clients in the docs for Postgres.app: http://postgresapp.com/documentation/gui-tools.html
If you know of any other, let me know.
There's also the list of GUI tools in the wiki: https://wiki.postgresql.org/wiki/Community_Guide_to_PostgreS...