Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cb psql query menu. #153

Merged
merged 1 commit into from
Jan 30, 2024
Merged

Add cb psql query menu. #153

merged 1 commit into from
Jan 30, 2024

Conversation

abrightwell
Copy link
Member

@abrightwell abrightwell commented Jan 26, 2024

This is an initial phase of supporting a builtin menu of useful queries when using cb psql. Currently, it only supports predefined queries. To access this menu, simply enter :menu into psql and it will present the available query options.

Example:

Example Team/example-cluster/postgres=> :menu
Cache
  1 – Cache and index hit rates
Size Information
  2 – Database sizes
  3 – Table sizes
Query Performance
  4 – Queries consuming the most system time
  5 – Queries running over 1 minute
  6 – Slowest average queries
Connection Management
  7 – Connection count by state
  8 – Connection count by user and application
Indexes
  9 – Duplicate indexes
  10 – List of indexes
  11 – Unused indexes
Locks
  12 – Blocking queries
Extensions
  13 – Available extensions
  14 – Installed extensions

Type choice and press <Enter> (q to quit):

@abrightwell abrightwell requested a review from a team as a code owner January 26, 2024 15:33
@abrightwell abrightwell force-pushed the abrightwell/query-menu branch from f073198 to c47fa27 Compare January 26, 2024 15:36
@abrightwell abrightwell marked this pull request as draft January 26, 2024 15:37
@abrightwell abrightwell force-pushed the abrightwell/query-menu branch 2 times, most recently from 6223176 to 28cbab2 Compare January 29, 2024 17:57
@abrightwell abrightwell marked this pull request as ready for review January 29, 2024 18:25
@abrightwell abrightwell requested a review from ngaumont January 29, 2024 18:25
WHEN :'choice'::text = '<%= option %>' THEN
'\i `echo <%= query.path %>`'
'\echo'
'\i <%= @path %>'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, we want to loop after a successful query.
I find it that is burying the useful information.

Ideally, I would prefer to have :menu display the options only once and if we do loop add the entry ?,h or help to list possible entry.

Also, I would love if after entry I would be able to \e to see the actual query.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so I was on the fence about this one. Initially I had implemented it to only display the options once. I'll get some feedback and update accordingly.

Comment on lines +4 to +8
# Metadata annoation for a query.
#
# A `Query` must have a `label` and `category` field defined using this
# annotation.
annotation Metadata; end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm completely new to annotation. So, this is more a request to learn than a comment.
Could the Metadata be removed in favor of a standard Query class stating that each instance should have a label, category and raw_sql_path?

Query.all would hold the list, and would then be something like :

[
  Query.new(label: "Table sizes", category: CATEGORY_SIZE_INFORMATION), raw_sql_path("#{__DIR__}/sql/size_information_table_size.sql"),
  …
]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this was more of an experimental approach than anything else. This pattern was used in the scope feature (which I believe might have been for the same purpose). Though, part of my purpose was to learn how we might be able to use annotations for looking up things dynamically without having to instantiate the class.

For instance, I could see where we might want to list the queries available and don't want to load them all into memory. Or maybe only load specific ones. Something like @type.annotation(Metadata)[:category] == CATEGORY_CACHE.

Does the use case exist yet? Not really, but I was curious about it and that's part of what took me down this path. 🤷‍♂️

This is an initial phase of supporting a builtin menu of useful queries
when using `cb psql`. Currently, it only supports predefined queries. To
access this menu, simply enter `:menu` into psql and it will present the
available query options.

Example:

```
Example Team/example-cluster/postgres=> :menu
Cache
  1 – Cache and index hit rates
Size Information
  2 – Database sizes
  3 – Table sizes
Query Performance
  4 – Queries consuming the most system time
  5 – Queries running over 1 minute
  6 – Slowest average queries
Connection Management
  7 – Connection count by state
  8 – Connection count by user and application
Indexes
  9 – Duplicate indexes
  10 – List of indexes
  11 – Unused indexes
Locks
  12 – Blocking queries
Extensions
  13 – Available extensions
  14 – Installed extensions

Type choice and press <Enter> (q to quit):

```
@abrightwell abrightwell force-pushed the abrightwell/query-menu branch from 28cbab2 to 3ddfe79 Compare January 30, 2024 21:10
@abrightwell abrightwell merged commit 49b512a into main Jan 30, 2024
4 checks passed
@abrightwell abrightwell deleted the abrightwell/query-menu branch January 30, 2024 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants