Package: precinct
PrecinctAboutPrecinct 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.FeaturesInput from a file or plain text.Checks for relevant tables.Retrieves table indices.RunsEXPLAIN ANALYZEfor query diagnosis.Offers concise, actionable feedback.SeeUsagefor more details.SetupRequirementsPython >=3.9PostgreSQL (More databases support coming soon)InstallationpipinstallprecinctFor developers, you can install locally in edit mode with optional extras:pipinstall-e.[dev]UsageHere is the--helpoutput:Usage:precinct[OPTIONS]QUERYPrecinct:ASQLqueryLLMcopilotforanalyzingqueries,suggestingindices,andprovidingoptimizations.CurrentlysupportsPostgreSQL.Examples:precinct"SELECT * FROM table;"precinct"path/to/your/file.sql"Options:--uriTEXTPostgreSQLconnectionURI,ie.'postgresql://username:password@host:port/database'.Mutuallyexclusivewith--service.--serviceTEXTPostgreSQLservicenameaslocatedin~/.pg_service.conforatspecifiedpath.Mutuallyexclusivewith--uri.--service-filePATHPathtoPGSERVICEFILE.Optionallyprovideinconjunctionwith--service.--model[gpt-4|gpt-3.5-turbo]Modeltouse.--rowsINTEGERNumberofrowstoreturnfromqueryatmost.Typicallyusedforpreviewingqueryresults.--jsonEnableVSCodeoptimizedJSONI/Omode.--helpShowthismessageandexit.Getting startedTo analyze a query from a file:pythonprecinct.py"path/to/your/file.sql"To analyze a plaintext query:pythonprecinct.py"SELECT * FROM table;"Specify intentPrecinct will then analyze your query and extract an intent, which you can either accept or clarify. For example:Query:select*frombusinesses
Intent:Theintentofthequeryissimplytoretrievealldatafromallcolumnsinthe'businesses'table.
Issueclarificationtointent(y)toproceed,or(q)toquit(y/q/[intent]):yTo specify another intent, just type it in and press enter:Issueclarificationtointent(y)toproceed,or(q)toquit(y/q/[intent]):Iwanttofindallbusinessesinthe'restaurants'category.OptimizePrecinct will then analyze your query and provide feedback on how to optimize it. For example:Optimizedquery:select*frombusinesseslimit10Explanation:Thecurrentqueryisalreadyhighlyefficient,retrievingonly10rowsofdatainamatterofmilliseconds.Sincethe'LIMIT'clauserestrictsthenumberofrecordsreturned,thequeryensuresminimaldatatransferfromthedatabasetotheapplication.OptimizationrefinementslikeaddingWHEREclauseororderingofdatawillnotmakesignificantimprovementsinthisspecificscenario.Indiceswon'thaveanimpactasthedatafetchedisminimalandnotfilteredorsorted.Therefore,optimizingthisqueryisnotnecessary.
Runquerynowwith`r`,copytoclipboardwith`c`,orcancelwith`q`(r/c/q):cFrom here, you can run the query and preview some rows (the--rowsparameter 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.AuthenticationFor database authentication, Precinct supports the following methods:Connection Service FileConnection URIEnvironment VariablesConnection Service FilePostgreSQL's Connection Service File is the recommended method. It allows for secure and easily configurable database connections. To use it, create a.pg_service.conffile in your home directory or specify its location using thePGSERVICEFILEenvironment variable or via the--service-filecommand line argument.In this file, you can define your database connection parameters like so:[my_service]dbname=mydbuser=myuserpassword=mypasswordhost=localhostport=5432You can then connect to the database using the service name:precinct--servicemy_service"SELECT * FROM table;"Connection URIYou can also connect using a connection URI. This is a commonly used method among database clients. To connect using a URI, use the--uricommand line argument:precinct--uri"postgresql://myuser:mypassword@localhost:5432/mydb""SELECT * FROM table;"Environment VariablesFinally, 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;"LicenseGPL-3.0-or-later