Npgquery 1.1.0

dotnet add package Npgquery --version 1.1.0
                    
NuGet\Install-Package Npgquery -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Npgquery" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Npgquery" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Npgquery" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Npgquery --version 1.1.0
                    
#r "nuget: Npgquery, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Npgquery@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Npgquery&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Npgquery&version=1.1.0
                    
Install as a Cake Tool

Npgquery - PostgreSQL Query Parser for .NET

A high-performance .NET library for parsing PostgreSQL queries using the official PostgreSQL parser via libpg_query.

Key Features

  • Query Validation & Utilities: Validate SQL and extract common query metadata
  • Query Normalization: Standardize queries for comparison
  • Query Fingerprinting: Generate unique identifiers for query patterns
  • Query Tokenization: Analyze SQL tokens and keywords
  • Statement Splitting: Split multi-statement SQL into individual statements
  • PL/pgSQL Support: Parse PL/pgSQL functions and procedures
  • Batch Processing: Process multiple queries efficiently
  • Parse SQL to AST: Convert PostgreSQL queries to JSON or Protobuf AST
  • AST Deparsing: Convert JSON or Protobuf ASTs back to SQL
  • Async Operations: Full async/await support
  • Cross-Platform: Works on Windows, Linux (macOS coming soon!)

Installation

Install Npgquery from NuGet with the .NET CLI:

dotnet add package Npgquery

Or with the NuGet Package Manager Console:

Install-Package Npgquery

The package includes the native libpg_query runtime assets for supported platforms. Application users should not install or build native parser libraries separately.

Then import the namespace in your application:

using Npgquery;

Quick Start

For simple one-off operations, use the static quick methods to parse PostgreSQL SQL into an abstract syntax tree (AST):

var result = Parser.QuickParse("SELECT * FROM users WHERE id = 1");
if (result.IsSuccess)
{
    Console.WriteLine(result.ParseTree?.RootElement.ToString());
}
else
{
    Console.WriteLine(result.Error);
}

The quick methods are available for the core capabilities:

var parseResult = Parser.QuickParse("SELECT now()");
var normalized = Parser.QuickNormalize("SELECT * FROM users WHERE id = 42");
var fingerprint = Parser.QuickFingerprint("SELECT * FROM users WHERE id = 42");
var split = Parser.QuickSplit("SELECT 1; SELECT 2;");
var scan = Parser.QuickScan("SELECT COUNT(*) FROM users");

For repeated parsing calls, advanced operations, or more granular lifecycle management, instantiate Parser directly and dispose it when finished:

using var parser = new Parser();

var first = parser.Parse("SELECT * FROM users WHERE id = 1");
var second = parser.Normalize("SELECT * FROM users WHERE id = 2");
var third = parser.Fingerprint("SELECT * FROM users WHERE id = 3");

Capability Examples

Normalize and fingerprint queries so structurally similar statements can be grouped even when literal values differ:

var normalized = Parser.QuickNormalize("SELECT * FROM users WHERE id = 42");
var first = Parser.QuickFingerprint("SELECT * FROM users WHERE id = 1");
var second = Parser.QuickFingerprint("SELECT * FROM users WHERE id = 2");

Console.WriteLine(normalized.NormalizedQuery);
Console.WriteLine(first.Fingerprint == second.Fingerprint); // True for the same query shape

Split multi-statement SQL and inspect individual statements:

var split = Parser.QuickSplit("SELECT 1; INSERT INTO audit_log(action) VALUES ('login');");
foreach (var statement in split.Statements ?? Array.Empty<SqlStatement>())
{
    Console.WriteLine(statement.Statement);
}

Tokenize SQL for lightweight analysis of keywords, identifiers, and token positions:

var scan = Parser.QuickScan("SELECT COUNT(*) FROM users");
foreach (var token in scan.Tokens ?? Array.Empty<SqlToken>())
{
    Console.WriteLine($"{token.TokenKind}: {token.Text}");
}

Use async helpers when integrating with asynchronous application code:

var asyncResult = await ParserAsync.QuickParseAsync("SELECT * FROM orders WHERE created_at > now() - interval '1 day'");
Console.WriteLine(asyncResult.IsSuccess);

Npgquery also exposes protobuf parsing/deparsing and PL/pgSQL parsing for advanced scenarios that need a managed representation of PostgreSQL parser output.

Documentation

Visit the GitHub repository for complete documentation, examples, and API reference.

License

MIT License - see LICENSE file for details.

This library is built on libpg_query, which embeds the official PostgreSQL parser.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 241 6/18/2026
1.0.0 141 6/15/2026

Initial release of Npgquery v1.0.0 with full PostgreSQL query parsing capabilities.