Skip to content

Implement support for binary replication - #4064

Merged
roji merged 1 commit into
npgsql:mainfrom
roji:BinaryReplication2
Nov 7, 2021
Merged

roji merged 1 commit into
npgsql:mainfrom
roji:BinaryReplication2

Conversation

@roji

@roji roji commented Oct 25, 2021 •

Copy link
Copy Markdown
Member

Summary

  • Buffering vs. streaming
    • I considered providing buffering access to the row, via an API such as IDataRecord (as @Brar started doing) or even DbDataReader. I ultimately think it's a bad idea, mainly since the replication data does not prefix the entire message with the length, like it does for normal queries in DataRow. This means that to pre-buffer everything, we need to start reading, and when we run out of space, allocate a new buffer and copy from the old buffer to the new. This could happen multiple times in a single row, and simply doesn't seem like a good idea. Basically the PG protocol is telling us not to do this.
    • Note that the same problem affects message cloning (see below).
    • We also think that most applications wouldn't know about specific schemas of tables, but rather consume replication data in a generic way; such applications really should stream and not buffer. Applications which do know the schema and know that rows are small can still use alternate approaches, e.g. copy out all the tuple values (after decoding - not raw bytes).
    • As a result, a row is simply an IAsyncEnumerable<ReplicationValue>, where ReplicationValue provides access to a specific column.
  • When receiving e.g. an Insert message, we currently expose only the relation ID; this places the burden upon the users to remember previously-received Relation messages, index them by their ID, and look them up each time a new data message comes in. This seems needlessly complicated, especially now that we remember Relation messages internally for our own purposes (to have RowDescriptions with handlers for binary replication). As a result, Insert/Update/Delete/Truncate messages now simply expose a reference to the appropriate Relation message.
  • Message cloning
    • Cloning worked well when messages were fully read from the wire, but is problematic with the new streaming API. We could still implement cloning as long as the user hasn't started consuming the message, but we'd run against the same difficulty as with buffering above (no row length prefix, lots of potential allocation and copying).
    • The fact that we expose Relation messages on Insert/Update/Delete/Truncate should remove the need for users to clone Relation messages themselves.
    • It's worth mentioning that no cloning is supported with regular queries, e.g. on NpgsqlDataReader; it's a bit odd for us to provide this functionality in replication and not there (what would be the specific justification..).
    • We only used Clone ourselves in the test to skip empty transactions. It's actually not needed there, assuming PG doesn't transmit two consecutive Begin messages, which seems correct.
    • As a result, for now I've removed the cloning methods (in a separate commit in case we want to revert). We could provide them for non-streaming messages (i.e. Insert/Update/Delete), though that would again be oddly inconsistent.
  • Constructors on message types are messy; the ones for "streaming" messages are now internal, since they require an NpgsqlConnector etc. For now I've left the others public, though it's inconsistent and probably not very useful.
  • Unrelated: I also converted some bytes we exposed to enums: RelationalMessage.Column.Flags, CommitMessage.Flags, and others.

Open/later questions

  • Do we know what TypeMessage is for?
  • Our naming on messages is NewRow/OldRow, where PostgreSQL uses the term "tuple". This is important in key messages, where the key tuple isn't a row, I think (I did this rename). We may want to fully align with the PostgreSQL naming and just talk about tuples.
  • We can expose TupleEnumerable (call it ReplicationTuple or Row), and have GetValues(object[]) on it (possibly also an overload for string[]). This could help for simpler scenarios; but it's also trivial for users to just enumerate externally and do it themselves... Maybe let's leave it for later.

Closes #4049
Closes #4068
Closes #4051

Continues work from #4024

@roji
roji requested review from Brar and vonzshik October 25, 2021 11:47
@roji
roji force-pushed the BinaryReplication2 branch from 9ca0d57 to 1df042e Compare October 25, 2021 11:53

@vonzshik vonzshik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Haven't got to the interesting stuff yet (will do later today), but here's something to get things going.

@Brar

Brar commented Oct 25, 2021

Copy link
Copy Markdown
Member

We also think that most applications wouldn't know about specific schemas of tables, but rather consume replication data in a generic way; such applications really should stream and not buffer.

IIRC, at least in PostgreSQL logical replication you have to create the target tables before starting replication so the gold standard knows about specific schemas of tables. OTOH I don't see why knowing about table schemas would invalidate your argument.

Do we know what TypeMessage is for?

Yes. It is sent before a relation message if the relation contains a custom type like a domain or an enum.

Constructors on message types are messy; the ones for "streaming" messages are now internal, since they require an NpgsqlConnector etc. For now I've left the others public, though it's inconsistent and probably not very useful.

Personally I'd make them consistently private.

@roji

roji commented Oct 25, 2021 •

Copy link
Copy Markdown
Member Author

We also think that most applications wouldn't know about specific schemas of tables, but rather consume replication data in a generic way; such applications really should stream and not buffer.

IIRC, at least in PostgreSQL logical replication you have to create the target tables before starting replication so the gold standard knows about specific schemas of tables.

Sure, but I'm thinking about a generic utility that you point at an arbitrary PG database, and it uses replication to e.g. write audit data in some format, or transfer changes to some other database in some way. In this category of scenarios, the replication client is not the application creating and using the tables (that's another user application), and so generally doesn't know anything about them beforehand.

OTOH I don't see why knowing about table schemas would invalidate your argument.

Yeah, it doesn't really invalidate it, but... If I'm writing a replication client specifically for my application (the opposite of the above), and I know my schema because I'm coding against it, then I also know if it's safe to buffer entire rows (i.e. are they big or not). That kind of client would benefit from the usability of buffering, e.g. they could interact with replication rows just like they do with DbDataReader when sending a query (using column names in random order). So I do think there's some usability value for this category of scenarios, but it still doesn't make sense to me to provide buffering for them (for the reasons above).

Do we know what TypeMessage is for?

Yes. It is sent before a relation message if the relation contains a custom type like a domain or an enum.

Ahhh ok, thanks. I opened #4066 with some thoughts and possible further work here.

Constructors on message types are messy; the ones for "streaming" messages are now internal, since they require an NpgsqlConnector etc. For now I've left the others public, though it's inconsistent and probably not very useful.

Personally I'd make them consistently private.

I guess you're right - I'll do that.

@roji

roji commented Oct 25, 2021

Copy link
Copy Markdown
Member Author

Constructors on message types are messy; the ones for "streaming" messages are now internal, since they require an NpgsqlConnector etc. For now I've left the others public, though it's inconsistent and probably not very useful.

Personally I'd make them consistently private.

I guess you're right - I'll do that.

One thing is that people may want to mock the replication API to test their clients, and we may be making that hard/impossible. But let's worry about that when people ask for it.

@Brar Brar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO this is a pretty clean and focused API that also polishes some rough edges we had in 5.0.
Despite my two minor comments my only problem is, that it is incomplete with respect to custom types (see #4066).
You can probably temporarily copy and adapt the (overly complicated for long term purposes) tests from #4024 to test for completeness.
One thing we also should have some test coverage for, before we consider the API as ready, is custom type handlers and -mappings.
In this context we may also want to think about global and per connection handlers/mappings.

@roji
roji force-pushed the BinaryReplication2 branch from 099a204 to fca2a36 Compare November 7, 2021 21:18
@roji
roji force-pushed the BinaryReplication2 branch from fca2a36 to 287cf13 Compare November 7, 2021 21:19
@roji
roji enabled auto-merge (squash) November 7, 2021 21:20
@roji
roji merged this pull request into npgsql:main Nov 7, 2021
@roji
roji deleted the BinaryReplication2 branch November 7, 2021 21:30
roji added a commit that referenced this pull request Nov 8, 2021
Closes #4049
Closes #4051
Closes #4068

Co-authored-by: Brar Piening <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants