Repository navigation
2.1: lock analysis, query shape, tokens, operation summary, PL/pgSQL query parsing - #26
Merged
Merged
Conversation
…L query parsing - GetLocks: the table locks a statement takes and what they block, following PostgreSQL's documented lock rules and, for ALTER TABLE, AlterTableGetLockLevel and the storage option table of PostgreSQL 18 - Column references carry their clause; GetOutputColumns names a SELECT's result columns as PostgreSQL does (a port of FigureColname) - Parser.Tokenize classifies tokens for display - Parser.OperationSummary gives the low-cardinality form OpenTelemetry's db.query.summary asks for - PlpgsqlNode.ParseQuery and PlpgsqlFunction.ParseQueries parse the SQL inside a function body in the mode PL/pgSQL uses for it - GetReferences no longer reports the targets of FOR UPDATE OF as tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The third set of general-purpose capabilities, still under 2.1.0. All additive.
Locks
tree.GetLocks()(also on aRawStmt) returns the table locks each statement takes on the tables it names, one entry per table and statement with the strongest mode.LockModehasConflictsWith,BlocksReadsandBlocksWrites.The rules come from PostgreSQL itself, not from memory:
AlterTableGetLockLevelin PostgreSQL 18'stablecmds.c, sub-command by sub-command, with the strongest one deciding.ALTER TABLE ... SET (...)): the per-option lock levels fromreloptions.c(9 options need ACCESS EXCLUSIVE, 31 need SHARE UPDATE EXCLUSIVE).Covered: SELECT (with and without FOR UPDATE and similar), INSERT, UPDATE, DELETE, MERGE, COPY, EXPLAIN, TRUNCATE, LOCK, CREATE INDEX, CREATE TRIGGER, CREATE STATISTICS, ALTER TABLE, DROP TABLE and similar, VACUUM, ANALYZE, CLUSTER, REINDEX TABLE, REFRESH MATERIALIZED VIEW, COMMENT ON TABLE or COLUMN, and the table a new foreign key points to.
Not covered, and stated in the docs: locks on a table reached through a view, an index name (DROP INDEX, REINDEX INDEX, ALTER INDEX), a partition or an inheritance child, locks taken by triggers and functions, and row-level locks. Those need the catalog.
Query shape
ColumnReference.Clause: SelectList, From, JoinCondition, Where, GroupBy, Having, OrderBy, Returning, Set or Other, relative to the nearest enclosing statement. Columns in Where and JoinCondition are the ones that filter rows, which is the input for index advice.select.GetOutputColumns(): the result columns of a SELECT with the names PostgreSQL gives them (a port ofFigureColnameInternalfromparse_target.c),*flagged as such, the first branch for set operations, andcolumn1,column2for VALUES.Text
Parser.Tokenize: tokens classified as Keyword, Identifier, StringLiteral, NumericLiteral, Parameter, Operator, Punctuation or Comment, with offsets into the string. Works on SQL that does not parse.Parser.OperationSummary: the operation and the tables it touches, such asSELECT customers orders, with no literals, parameters or columns. This is the form OpenTelemetry'sdb.query.summaryasks for; it is cut at a word boundary to a maximum length (255 by default).PL/pgSQL
PlpgsqlNode.ParseQuery()andPlpgsqlFunction.ParseQueries()parse the SQL inside a function body, each piece in the mode PL/pgSQL uses for it (query, expression or assignment), soGetReferences,GetLocksand the rest work on function bodies too.A fix to merged code
GetReferencesreported the targets ofFOR UPDATE OF xas tables. They name items of the FROM clause, so they are no longer returned. Found by the lock tests.Verification
InsightTests; 482 pass on .NET 8, 9 and 10 locally.Limits
OperationSummarynames: the six planned statement kinds and 14 common DDL forms get a full name (CREATE INDEX,DROP TABLE); other statements get their first keyword (GRANT,VACUUM).?column?.REINDEX TABLEis reported as SHARE on the table, following the REINDEX documentation (it blocks writes, not reads), where the general lock-mode page lists REINDEX under ACCESS EXCLUSIVE, which applies to the index.