Skip to main content

Your SQL query AI copilot

Project description

Precinct

About

Precinct is a SQL query LLM copilot that helps analyze your queries, pick up indices and other optimizations and provide other actionable feedback. Currently only Postgres is supported.

Features

  • Input from a file or plain text.
  • Checks for relevant tables.
  • Retrieves table indices.
  • Runs EXPLAIN ANALYZE for query diagnosis.
  • Offers concise, actionable feedback.

See Usage for more details.

Setup

Requirements

  • Python >=3.9
  • PostgreSQL (More databases support coming soon)

Installation

pip install precinct

For developers, you can install locally in edit mode with optional extras:

pip install -e .[dev]

Usage

Here is the --help output:

Usage: precinct [OPTIONS] QUERY

  Precinct: A SQL query LLM copilot for analyzing queries, suggesting indices,
  and providing optimizations. Currently supports PostgreSQL.

  Examples:

      precinct "SELECT * FROM table;"

      precinct "path/to/your/file.sql"

Options:
  --uri TEXT                     PostgreSQL connection URI, ie. 'postgresql://
                                 username:password@host:port/database'.
                                 Mutually exclusive with --service.
  --service TEXT                 PostgreSQL service name as located in
                                 ~/.pg_service.conf or at specified path.
                                 Mutually exclusive with --uri.
  --service-file PATH            Path to PGSERVICEFILE. Optionally provide in
                                 conjunction with --service.
  --model [gpt-4|gpt-3.5-turbo]  Model to use.
  --rows INTEGER                 Number of rows to return from query at most.
                                 Typically used for previewing query results.
  --json                         Enable VSCode optimized JSON I/O mode.
  --help                         Show this message and exit.

Getting started

To analyze a query from a file:

python precinct.py "path/to/your/file.sql"

To analyze a plaintext query:

python precinct.py "SELECT * FROM table;"

Specify intent

Precinct will then analyze your query and extract an intent, which you can either accept or clarify. For example:

Query: select * from businesses
Intent: The intent of the query is simply to retrieve all data from all columns in the 'businesses' table.
Issue clarification to intent (y) to proceed, or (q) to quit (y/q/[intent]): y

To specify another intent, just type it in and press enter:

Issue clarification to intent (y) to proceed, or (q) to quit (y/q/[intent]): I want to find all businesses in the 'restaurants' category.

Optimize

Precinct will then analyze your query and provide feedback on how to optimize it. For example:

Optimized query: select * from businesses limit 10
Explanation: The current query is already highly efficient, retrieving only 10 rows of data in a matter of milliseconds. Since the 'LIMIT' clause restricts the number of records returned, the query ensures minimal data transfer from the database to the application. Optimization refinements like adding WHERE clause or ordering of data will not make significant improvements in this specific scenario. Indices won't have an impact as the data fetched is minimal and not filtered or sorted. Therefore, optimizing this query is not necessary.
Run query now with `r`, copy to clipboard with `c`, or cancel with `q` (r/c/q): c

From here, you can run the query and preview some rows (the --rows parameter can be used to specify the max number to preview, although there is only rudimentary support for this at the moment). You can also copy the query to your clipboard and paste it into your editor.

Authentication

For database authentication, Precinct supports the following methods:

Connection Service File

PostgreSQL's Connection Service File is the recommended method. It allows for secure and easily configurable database connections. To use it, create a .pg_service.conf file in your home directory or specify its location using the PGSERVICEFILE environment variable or via the --service-file command line argument.

In this file, you can define your database connection parameters like so:

[my_service]
dbname=mydb
user=myuser
password=mypassword
host=localhost
port=5432

You can then connect to the database using the service name:

precinct --service my_service "SELECT * FROM table;"

Connection URI

You can also connect using a connection URI. This is a commonly used method among database clients. To connect using a URI, use the --uri command line argument:

precinct --uri "postgresql://myuser:mypassword@localhost:5432/mydb" "SELECT * FROM table;"

Environment Variables

Finally, you can connect using environment variables. To connect using environment variables, just set them and Precinct will default if a service file or URI is not available.

precinct "SELECT * FROM table;"

License

GPL-3.0-or-later

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

precinct-0.1.3.tar.gz (49.5 kB view details)

Uploaded Source

Built Distribution

precinct-0.1.3-py3-none-any.whl (36.9 kB view details)

Uploaded Python 3

File details

Details for the file precinct-0.1.3.tar.gz.

File metadata

  • Download URL: precinct-0.1.3.tar.gz
  • Upload date:
  • Size: 49.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for precinct-0.1.3.tar.gz
Algorithm Hash digest
SHA256 378afea01d7b7f74604ab0e61595bcaafd0e63140e913ef7a3a4ffba2bc4089d
MD5 761cf9ebfefe4dc7f338b908e81719f7
BLAKE2b-256 bda2fd6d69c2acee592eef5cb3a8d7fce58555ef7fdd4f72326373a34416b1f0

See more details on using hashes here.

File details

Details for the file precinct-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: precinct-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 36.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for precinct-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7423762acd97529a2b457b372ecf527ef6bc9ce1891f7b113bc5b01553e8fab9
MD5 3f552b8886b15c1835b74bce2a6d40ee
BLAKE2b-256 03dede715208df9509a876ef00672617816054d691a113a88af52294ae0e2ad4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page