-
Notifications
You must be signed in to change notification settings - Fork 69
add support for logical replication protocol v3 #68
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
heavycrystal
wants to merge
3
commits into
jackc:master
Choose a base branch
from
heavycrystal:protocol-v3
base: master
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -0,0 +1,215 @@ | ||
package pglogrepl | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type MessageDecoderV3 interface { | ||
DecodeV3(src []byte, inStream bool) error | ||
} | ||
|
||
type BeginPrepareMessageV3 struct { | ||
baseMessage | ||
PrepareLSN LSN | ||
TransactionEndLSN LSN | ||
// The time at which the transaction was prepared. | ||
PrepareTime time.Time | ||
// The transaction ID of the prepared transaction. | ||
Xid uint32 | ||
// The user defined GID of the prepared transaction. | ||
Gid string | ||
} | ||
|
||
func (m *BeginPrepareMessageV3) DecodeV3(src []byte, _ bool) (err error) { | ||
if len(src) < 29 { | ||
return m.lengthError("BeginPrepareMessage", 29, len(src)) | ||
} | ||
|
||
var low, used int | ||
m.PrepareLSN, used = m.decodeLSN(src) | ||
low += used | ||
m.TransactionEndLSN, used = m.decodeLSN(src[low:]) | ||
low += used | ||
m.PrepareTime, used = m.decodeTime(src[low:]) | ||
low += used | ||
m.Xid, used = m.decodeUint32(src[low:]) | ||
low += used | ||
m.Gid, _ = m.decodeString(src[low:]) | ||
m.SetType(MessageTypeBeginPrepare) | ||
|
||
return nil | ||
} | ||
|
||
type PrepareMessageV3 struct { | ||
baseMessage | ||
// Flags currently unused (must be 0). | ||
Flags uint8 | ||
PrepareLSN LSN | ||
TransactionEndLSN LSN | ||
// The time at which the transaction was prepared. | ||
PrepareTime time.Time | ||
// The transaction ID of the prepared transaction. | ||
Xid uint32 | ||
// The user defined GID of the prepared transaction. | ||
Gid string | ||
} | ||
|
||
func (m *PrepareMessageV3) DecodeV3(src []byte, _ bool) (err error) { | ||
if len(src) < 30 { | ||
return m.lengthError("PrepareMessage", 30, len(src)) | ||
} | ||
|
||
var low, used int | ||
m.Flags = src[low] | ||
low += 1 | ||
m.PrepareLSN, used = m.decodeLSN(src[low:]) | ||
low += used | ||
m.TransactionEndLSN, used = m.decodeLSN(src[low:]) | ||
low += used | ||
m.PrepareTime, used = m.decodeTime(src[low:]) | ||
low += used | ||
m.Xid, used = m.decodeUint32(src[low:]) | ||
low += used | ||
m.Gid, _ = m.decodeString(src[low:]) | ||
m.SetType(MessageTypePrepare) | ||
|
||
return nil | ||
} | ||
|
||
type CommitPreparedMessageV3 struct { | ||
baseMessage | ||
// Flags currently unused (must be 0). | ||
Flags uint8 | ||
CommitLSN LSN | ||
TransactionEndLSN LSN | ||
CommitTime time.Time | ||
Xid uint32 | ||
// The user defined GID of the prepared transaction. | ||
Gid string | ||
} | ||
|
||
func (m *CommitPreparedMessageV3) DecodeV3(src []byte, _ bool) (err error) { | ||
if len(src) < 30 { | ||
return m.lengthError("CommitPreparedMessage", 30, len(src)) | ||
} | ||
|
||
var low, used int | ||
m.Flags = src[low] | ||
low += 1 | ||
m.CommitLSN, used = m.decodeLSN(src[low:]) | ||
low += used | ||
m.TransactionEndLSN, used = m.decodeLSN(src[low:]) | ||
low += used | ||
m.CommitTime, used = m.decodeTime(src[low:]) | ||
low += used | ||
m.Xid, used = m.decodeUint32(src[low:]) | ||
low += used | ||
m.Gid, _ = m.decodeString(src[low:]) | ||
m.SetType(MessageTypeCommitPrepared) | ||
|
||
return nil | ||
} | ||
|
||
type RollbackPreparedMessageV3 struct { | ||
baseMessage | ||
// Flags currently unused (must be 0). | ||
Flags uint8 | ||
TransactionEndLSN LSN | ||
// The end LSN of the rollback of the prepared transaction. | ||
TransactionRollbackLSN LSN | ||
PrepareTime time.Time | ||
RollbackTime time.Time | ||
Xid uint32 | ||
// The user defined GID of the prepared transaction. | ||
Gid string | ||
} | ||
|
||
func (m *RollbackPreparedMessageV3) DecodeV3(src []byte, _ bool) (err error) { | ||
if len(src) < 38 { | ||
return m.lengthError("RollbackPreparedMessage", 38, len(src)) | ||
} | ||
|
||
var low, used int | ||
m.Flags = src[low] | ||
low += 1 | ||
m.TransactionEndLSN, used = m.decodeLSN(src[low:]) | ||
low += used | ||
m.TransactionRollbackLSN, used = m.decodeLSN(src[low:]) | ||
low += used | ||
m.PrepareTime, used = m.decodeTime(src[low:]) | ||
low += used | ||
m.RollbackTime, used = m.decodeTime(src[low:]) | ||
low += used | ||
m.Xid, used = m.decodeUint32(src[low:]) | ||
low += used | ||
m.Gid, _ = m.decodeString(src[low:]) | ||
m.SetType(MessageTypeRollbackPrepared) | ||
|
||
return nil | ||
} | ||
|
||
type StreamPrepareMessageV3 struct { | ||
baseMessage | ||
// Flags currently unused (must be 0). | ||
Flags uint8 | ||
PrepareLSN LSN | ||
TransactionEndLSN LSN | ||
PrepareTime time.Time | ||
Xid uint32 | ||
// The user defined GID of the prepared transaction. | ||
Gid string | ||
} | ||
|
||
func (m *StreamPrepareMessageV3) DecodeV3(src []byte, _ bool) (err error) { | ||
if len(src) < 30 { | ||
return m.lengthError("StreamPrepareMessage", 30, len(src)) | ||
} | ||
|
||
var low, used int | ||
m.Flags = src[low] | ||
low += 1 | ||
m.PrepareLSN, used = m.decodeLSN(src[low:]) | ||
low += used | ||
m.TransactionEndLSN, used = m.decodeLSN(src[low:]) | ||
low += used | ||
m.PrepareTime, used = m.decodeTime(src[low:]) | ||
low += used | ||
m.Xid, used = m.decodeUint32(src[low:]) | ||
low += used | ||
m.Gid, _ = m.decodeString(src[low:]) | ||
m.SetType(MessageTypeStreamPrepare) | ||
|
||
return nil | ||
} | ||
|
||
// ParseV3 parse a logical replication message from protocol version #3 | ||
// it accepts a slice of bytes read from PG and inStream parameter | ||
// inStream must be true when StreamStartMessageV2 has been read | ||
// it must be false after StreamStopMessageV2 has been read | ||
func ParseV3(data []byte, inStream bool) (m Message, err error) { | ||
var decoder MessageDecoderV3 | ||
msgType := MessageType(data[0]) | ||
|
||
switch msgType { | ||
case MessageTypeBeginPrepare: | ||
decoder = new(BeginPrepareMessageV3) | ||
case MessageTypePrepare: | ||
decoder = new(PrepareMessageV3) | ||
case MessageTypeCommitPrepared: | ||
decoder = new(CommitPreparedMessageV3) | ||
case MessageTypeRollbackPrepared: | ||
decoder = new(RollbackPreparedMessageV3) | ||
case MessageTypeStreamPrepare: | ||
decoder = new(StreamPrepareMessageV3) | ||
default: | ||
// all messages from V2 are unchanged in V3 | ||
// so we can just call ParseV2 | ||
return ParseV2(data, inStream) | ||
} | ||
|
||
if err = decoder.DecodeV3(data[1:], inStream); err != nil { | ||
return nil, err | ||
} | ||
|
||
return decoder.(Message), nil | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
why remove this? similarly, why not have DecoderV3 inherit DecoderV2? It seems this is because V3 introduces new message types, & Decode was passing here off of
baseMesssage
?would it be better to pass version as a parameter to Decode?
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.
Talking more, idea is that ParseV2 should return result of Parse like ParseV3 returns ParseV2 rather than using getCommonDecoder to have a V2 or V1 decoder where we call V2 (ignoring V1 method) if V2 implemented, otherwise call V1