-
Notifications
You must be signed in to change notification settings - Fork 161
feat: Extend \dt psql command output with shard metadata (#709)
#812
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
Open
Adi-Goll
wants to merge
1
commit into
pgdogdev:main
Choose a base branch
from
Adi-Goll:adigoll-dt-intercept-shard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ use crate::{ | |
| router::parser::{explain_trace::ExplainTrace, rewrite::statement::plan::RewriteResult}, | ||
| }, | ||
| net::{ | ||
| DataRow, FromBytes, Message, Protocol, ProtocolMessage, Query, ReadyForQuery, | ||
| DataRow, Field, FromBytes, Message, Protocol, ProtocolMessage, Query, ReadyForQuery, | ||
| RowDescription, ToBytes, TransactionState, | ||
| }, | ||
| state::State, | ||
|
|
@@ -36,7 +36,7 @@ impl QueryEngine { | |
| // We need to run a query now. | ||
| if context.in_transaction() { | ||
| // Connect to one shard if not sharded or to all shards | ||
| // for a cross-shard tranasction. | ||
| // for a cross-shard transaction. | ||
| if !self.connect_transaction(context).await? { | ||
| return Ok(()); | ||
| } | ||
|
|
@@ -123,8 +123,23 @@ impl QueryEngine { | |
| ) -> Result<(), Error> { | ||
| self.streaming = message.streaming(); | ||
|
|
||
| let should_rewrite_for_display_table = | ||
| if let Some(route) = context.client_request.route.as_ref() { | ||
| route.display_table() | ||
| } else { | ||
| false | ||
| }; | ||
|
|
||
| let code = message.code(); | ||
| let payload = if code == 'T' { | ||
| if should_rewrite_for_display_table { | ||
| let mut fields = RowDescription::from_bytes(message.payload()) | ||
| .unwrap() | ||
| .fields | ||
| .to_vec(); | ||
| fields.push(Field::text("Shard")); | ||
| message = RowDescription::new(&fields).message()?; | ||
| } | ||
| Some(message.payload()) | ||
| } else { | ||
| None | ||
|
|
@@ -152,6 +167,38 @@ impl QueryEngine { | |
| self.pending_explain = None; | ||
| } | ||
|
|
||
| if code == 'D' { | ||
| if should_rewrite_for_display_table { | ||
| let mut dr = DataRow::from_bytes(message.payload()).unwrap(); | ||
| let col = dr.column(1).unwrap(); | ||
|
|
||
| let shard_map = self.backend.forward_with_shard(); | ||
| let table_lookup = std::str::from_utf8(&col).unwrap(); | ||
|
|
||
| if let Some(map) = shard_map { | ||
| if self.seen_tables.contains(table_lookup) { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| self.seen_tables.insert(table_lookup.to_string()); | ||
|
|
||
| let mut new_col = String::new(); | ||
| for (i, val) in map[table_lookup].iter().enumerate() { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking, maybe it would be better to have a more unique key for the HashMap, maybe instead of just the table name I should use the schema + table name? Thoughts? |
||
| if i > 0 { | ||
| new_col.push_str(", ") | ||
| } | ||
| new_col.push_str(&val.to_string()); | ||
| } | ||
| dr.add(new_col); | ||
| } else { | ||
| dr.add(None); | ||
| } | ||
|
|
||
| message = dr.message()?; | ||
| Some(message.payload()); | ||
| } | ||
| } | ||
|
|
||
| // Messages that we need to send to the client immediately. | ||
| // ReadyForQuery (B) | CopyInResponse (B) | ErrorResponse(B) | NoticeResponse(B) | NotificationResponse (B) | ||
| let flush = matches!(code, 'Z' | 'G' | 'E' | 'N' | 'A') | ||
|
|
||
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's reasonable to add this for \dt because the original command doesn't do any aggregating or deduplication handling