🚧 This is in active development and not ready for use yet.
A Language Server for Postgres. Not SQL with flavours, just Postgres.
Despite an ever rising popularity of Postgres, support for the PostgreSQL language in the IDE or editor of your choice is still very sparse. There are a few proprietary ones (e.g. DataGrip) that work well, but are only available within the respective IDE. Open Source attempts (e.g. sql-language-server, pgFormatter, sql-parser-cst) mostly attempt to provide a generic SQL language server, and implement the Postgres syntax only as a flavor of their parser. This always falls short due to the ever evolving and complex syntax of PostgreSQL. This project only ever wants to support PostgreSQL, and leverages parts of the PostgreSQL server source (see libg_query) to parse the source code reliabaly. This is slightly crazy, but is the only reliable way of parsing all valid PostgreSQL queries. You can find a longer rationale on why This is the wayâ„¢ here. Of course, libg_query was built to execute SQL, and not to build a language server, but all of the resulting shortcomings were successfully mitigated in the parser crate. Read the commented source code for more details on the inner workings of the parser.
Once the parser is stable, and a robust and scalable data model is implemented, the language server will not only provide basic features such as semantic highlighting, code completion and syntax error diagnostics, but also serve as the user interface for all the great tooling of the Postgres ecosystem.
At this point however, this is merely a proof of concept for building both a concrete syntax tree and an abstract syntax tree from a potentially malformed PostgreSQL source code. The postgres_lsp crate was created only to proof that it works e2e, and is just a very basic language server with semantic highlighting and error diagnostics. Before actual feature development can start, we have to do a bit of groundwork.
- Finish the parser - The parser works, but the enum values for all the different syntax elements and internal conversations are manually written or copied, and, in some places, only cover a few elements required for a simple select statement. To have full coverage without possibilities for a copy and paste error, they should be generated from pg_query.rs source code.
- There are a few cases such as nested and named dollar quoted strings that cause the parser to fail due to limitations of the regex-based lexer. Nothing that is impossible to fix, or requires any fundamental change in the parser though.
 
- Implement a robust and scalable data model - This is still in a research phase
- A great rationale on the importance of the data model in a language server can be found here
- rust-analyzers- base-dbcrate will serve as a role model
- The salsacrate will most likely be the underlying data structure
 
- Setup the language server properly - This is still in a research phase
- Once again rust-analyzerwill serve as a role model, and we will most likely implement the same queueing and cancellation approach
 
- Implement basic language server features - Semantic Highlighting
- Syntax Error Diagnostics
- Show SQL comments on hover
- Auto-Completion
- Code Actions, such as Execute the statement under the cursor, orExecute the current file
- ... anything you can think of really
 
- Integrate all the existing open source tooling - Show migration file lint errors from squawk
- Show plpsql lint errors from plpsql_check
 
- Build missing pieces - An optionated code formatter (think prettier for PostgreSQL)
 
- (Maybe) Support advanced features with declarative schema management - Jump to definition
- ... anything you can think of really
 
Add the postgres_lsp executable to your path, and add the following to your config to use it.
require('lspconfig.configs').postgres_lsp = { default_config = { name = 'postgres_lsp', cmd = {'postgres_lsp'}, filetypes = {'sql'}, single_file_support = true, root_dir = util.root_pattern 'root-file.txt' } } lsp.configure("postgres_lsp", {force_setup = true})- rust-analyzer for implementing such a robust, well documented, and feature-rich language server. Great place to learn from.
- squawk and pganalyze for inspiring the use of libg_query.
- copple for the support and trust.