-
Notifications
You must be signed in to change notification settings - Fork 189
feat(resharding): use binary protocol for replication #1016
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
meskill
wants to merge
1
commit into
main
Choose a base branch
from
feat/replication-binary
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.
+116
−11
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| use std::str::from_utf8; | ||
|
|
||
| use bytes::BytesMut; | ||
| use tracing::warn; | ||
|
|
||
| use crate::net::bind::Parameter; | ||
| use crate::net::Bind; | ||
|
|
@@ -96,18 +97,22 @@ impl TupleData { | |
| /// Used by [`Table`](crate::backend::replication::logical::publisher::Table) DML methods | ||
| /// — the `$N` they emit must agree with the column ordering of the tuple passed here. | ||
| pub fn to_bind(&self, name: &str) -> Bind { | ||
| let params = self | ||
| let (params, codes): (Vec<_>, Vec<_>) = self | ||
| .columns | ||
| .iter() | ||
| .map(|c| { | ||
| if c.identifier == Identifier::Null { | ||
| Parameter::new_null() | ||
| } else { | ||
| Parameter::new(&c.data) | ||
| .map(|c| match &c.identifier { | ||
| Identifier::Null => (Parameter::new_null(), Format::Text), | ||
| Identifier::Toasted => { | ||
| warn!( | ||
| "to_bind: toasted column reached Bind construction; \ | ||
| caller should strip or fill toasted columns first — sending NULL" | ||
| ); | ||
| (Parameter::new_null(), Format::Text) | ||
| } | ||
| Identifier::Format(fmt) => (Parameter::new(&c.data), *fmt), | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
| Bind::new_params(name, ¶ms) | ||
| .unzip(); | ||
| Bind::new_params_codes(name, ¶ms, &codes) | ||
|
Contributor
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. Not entirely relevant to this PR since you didn't add this function, but this smells like we should use a builder |
||
| } | ||
|
|
||
| /// Does this tuple contain any unchanged-TOAST (`'u'`) column? | ||
|
|
@@ -265,6 +270,15 @@ pub(crate) fn toasted_col() -> Column { | |
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| pub(crate) fn binary_col(data: &[u8]) -> Column { | ||
| Column { | ||
| identifier: Identifier::Format(Format::Binary), | ||
| len: data.len() as i32, | ||
| data: bytes::Bytes::copy_from_slice(data), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::*; | ||
|
|
@@ -393,4 +407,65 @@ mod test { | |
| "column count mismatch must return Err, not panic" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn to_bind_all_text_columns_produce_text_format_codes() { | ||
| let tuple = TupleData { | ||
| columns: vec![text_col("hello"), text_col("world")], | ||
| }; | ||
| let bind = tuple.to_bind("__pgdog_1"); | ||
| assert_eq!(bind.parameter_format(0).unwrap(), Format::Text); | ||
| assert_eq!(bind.parameter_format(1).unwrap(), Format::Text); | ||
| } | ||
|
|
||
| #[test] | ||
| fn to_bind_binary_columns_produce_binary_format_codes() { | ||
| // Simulate a bigint (8 bytes, big-endian) arriving as a binary column. | ||
| let val: i64 = 42; | ||
| let tuple = TupleData { | ||
| columns: vec![binary_col(&val.to_be_bytes())], | ||
| }; | ||
| let bind = tuple.to_bind("__pgdog_1"); | ||
| assert_eq!(bind.parameter_format(0).unwrap(), Format::Binary); | ||
| // Data must be forwarded verbatim — the destination decodes it as binary. | ||
| assert_eq!( | ||
| bind.parameter(0).unwrap().unwrap().data(), | ||
| &val.to_be_bytes() | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn to_bind_null_and_toasted_use_text_format_code() { | ||
| let tuple = TupleData { | ||
| columns: vec![ | ||
| Column { | ||
| identifier: Identifier::Null, | ||
| len: -1, | ||
| data: Bytes::new(), | ||
| }, | ||
| toasted_col(), | ||
| ], | ||
| }; | ||
| let bind = tuple.to_bind("__pgdog_1"); | ||
| // Format code is irrelevant for absent values, but must not be Binary | ||
| // to avoid confusing the destination server. | ||
| assert_eq!(bind.parameter_format(0).unwrap(), Format::Text); | ||
| assert_eq!(bind.parameter_format(1).unwrap(), Format::Text); | ||
| } | ||
|
|
||
| #[test] | ||
| fn to_bind_mixed_columns_produce_per_column_format_codes() { | ||
| let val: i32 = 99; | ||
| let tuple = TupleData { | ||
| columns: vec![ | ||
| text_col("label"), | ||
| binary_col(&val.to_be_bytes()), | ||
| text_col("suffix"), | ||
| ], | ||
| }; | ||
| let bind = tuple.to_bind("__pgdog_1"); | ||
| assert_eq!(bind.parameter_format(0).unwrap(), Format::Text); | ||
| assert_eq!(bind.parameter_format(1).unwrap(), Format::Binary); | ||
| assert_eq!(bind.parameter_format(2).unwrap(), Format::Text); | ||
| } | ||
| } | ||
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.
I know we're not passing user input, but seeing
format!instead of prepared statements still makes me wince 😅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.
The logical replication protocol doesn't support prepared statements.
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.
RIP
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.
Looking at it again I don't even know why my brain parsed this as a SQL query.
