I want to verify the syntax of files containing sql queries before they can be committed in my CVS project.
In order to do that, I have a commitinfo script, but I have trouble finding out if the sql commands are valid. psql
does not seem to have a dryrun mode, and constructing my own postgresql-dialact tester from the grammar (that is in the source) seems like a long stretch.
The scripts may contain multiple queries, so an EXPLAIN
cannot be wrapped around them.
Any hints?
I recently wrote up a utility to statically check the syntax of SQL for PostgreSQL. It leverages ecpg, the embedded SQL C preproccessor for postgres, to check the SQL syntax, so it uses the exact same parser that is built in to Postgres itself.
You can check it out on github: http://github.com/markdrago/pgsanity. You can give the README a skim to get a better idea of how it works and to get directions for how to install it. Here's a short example of how pgsanity can be used:
You can run queries iside postgresql function and raise exception in the end. All changes will be rolled back. For example:
Another sollution -
plpgsql_check
extension (on github), the next incarnation of pgpsql_lintI'm usually use Mimer online SQL validator, the only thing is that it check SQL syntax for standard SQL :
and not specific for the PostgreSQL ... However if you write code following the standard you can use it and it work well ...
EXPLAIN (without ANALYZE) will parse the query and prepare an execution plan, without actually executing it.
https://www.postgresql.org/docs/current/static/sql-explain.html
One way would be to put it into a transaction that you roll back at the end:
Be aware that there are some effects that cannot be rolled back, like dblink calls, or anything written to the file system or incremented sequences.
I would advise cloning your database for testing purposes.
A wonderful utility to verify SQL syntax: SQL Fiddle
Supports MySQL, Oracle, PostgreSQL, SQLite, MS SQL.