CDC: Option to emit commit timestamp during backfill
Is your feature request related to a problem? Please describe. For large tables with complex downstream processing, sending data downstream can be costly. In some use cases, users only care about data committed after a certain time. When creating a changefeed, the initial dump of data from the backfill uses the time of the start of the backfill for the commit timestamp- making it impossible to differentiate data commit times downstream.
Describe the solution you'd like Optionally emit the commit timestamp of the row during backfill.
Describe alternatives you've considered A better solution may be to filter upstream, with an option to only emit rows commit after a certain time.
Additional Details This issue came from a user request
Jira issue: CRDB-3351
Hi @amruss, I've guessed the C-ategory of your issue and suitably labeled it. Please re-label if inaccurate.
While you're here, please consider adding an A- label to help keep our repository tidy.
:owl: Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.
Some notes from a brief team discussion on this:
- The
When creating a changefeed, the initial dump of data from the backfill uses the time of the start of the backfill for the commit timestampbehavior was implemented (for theupdatedfield) to make it semantically clearer that a row emitted during a backfill is a snapshot at the time of the backfill, not a replay of an older change. - Adding another field that doesn't have this behavior is easy, I have this working locally.
- Naming it is hard. @miretskiy made a case for splitting
updatedinto something likelogical_updatedandphysical_updated. - A potentially surprising caveat:
physical_updatedwill still update when the KV is affected by a logical backfill from a schema change. For example if someone doesalter table article add column topic string default 'blog', all existing KVs in the table will get the timestamp of the schema change, rather than the last time they were individually updated. I don't yet know if there's a cheap way to get the actual commit timestamp of a row in this case. - We are concerned for performance optimization reasons about encouraging clients to request both the
logical_updatedandphysical_updatedfields, as they'll be always identical after the initial backfill so it's a waste of bytes.
At this point, given we looking towards 21.2 the solution to the backfill updates should be to coordinate with the schema team to get rid of the "column backfill" and thus eliminate all writes to the live primary index. Caveat: after a column set change, the history of when a row was updated will be lost, at least in the naive implementation. There's probably something we could do about that too.
To summarize my understanding and hopefully backfill (;-) some context:
-
Changefeeds scan and re-emit (backfill) all rows from a target table in two cases: initial changefeed startup and some schema changes (add column, drop column, etc).
-
Users can skip the initial backfill using 2 different options:
no_initial_scanorcursor=CURSOR. In the case of providingcursor, all rows with an MVCC timestamp greater than the cursor will be emitted. -
When a schema change such as ADD COLUMN or DROP COLUMN occur, a changefeed will emit 2 backfills. The first is the result of actual writes that happen to the underlying KVs during the schema change (sometimes called the "column backfill"). The second is a result of code inside changefeeds that detects the schema change and performs a backfill (sometimes called the "logical backfill").
-
Users can disable the "logical backfill" (as of 21.1) by using the
schema_change_policy=nobackfilloption.
Given this (and please correct any of it if I've missed something), we might talk about a few different things the user wants to do:
- Skip the initial backfill
- Emit but detect the initial backfill: For example, to be able to skip some portion of the downstream processing in a changefeed consumer.
- Skip schema change backfills
- Emit but detect schema change backfills
(1) is covered by existing options that work well. (3) is partially possible with the schema_change_policy option but not completely possible because of the column backfill.
As @ajwerner mentioned the schema team is responsible for addressing the column backfill if that is necessary to address (we could of course help do the work). If we did this, then (3) would be solved by the existing schema_change_policy option.
This leaves (2) and (4) to consider.
IIRC, the demo @HonoreDB did allowed users to do (2) by emitting two timestamps: the mvcc timestamp and the logical timestamp that we currently emit during a scan/backfill. It partially allowed them to do (4) for the logical backfill but is also complicated by the column backfill.
This leads to a few comments and questions:
-
Does the customer's needs align to the uses I outlined above? If so, which ones? Have I missed or misunderstood a use case?
-
The initial description mentions
"A better solution may be to filter upstream, with an option to only emit rows commit after a certain time."
That is possible currently with the cursor option. Is this option insufficient for some reason?
-
If they also care about schema-change induced backfills, is this work only useful if we solve the "column backfill" issue? If so, we should probably start there because the changefeed-side changes around logical backfill's feel much lower risk and smaller.
-
Apart from concerns around the column backfill and the overhead of emitting another timestamp, Aaron's demo seems like a reasonable solution. We could alternatively add some flag field that would only be output during a logical backfill.
- Does the customer's needs align to the uses I outlined above? If so, which ones? Have I missed or misunderstood a use case?
I'm not sure what you mean by the 4th option, but when talking to the customer, (2) was an acceptable solution, though the ideal solution would look something like an unbounded cursor option (see below)
- That is possible currently with the cursor option. Is this option insufficient for some reason?
Cursor option is limited right now: "When starting a changefeed at a specific cursor, the cursor cannot be before the configured garbage collection window". Default is 25 hour window where you can use the cursor option
3 & 4
Followed up on this via slack
I'm not sure what you mean by the 4th option,
(4) is the same as (2) but for schema changes. Less of an options and more of something the user might be interested in doing.
Cursor option is limited right now: "When starting a changefeed at a specific cursor, the cursor cannot be before the configured garbage collection window". Default is 25 hour window where you can use the cursor option
I see the cursor option isn't really what they would want there but rather some way to do a subset of the backfill. An interesting note is that if we had predicate filtering, that feature would come along for the ride because they could add a WHERE crdb_internal_mvcc_timestamp > FOO
@amruss is this something we still might want to do ?
@miretskiy I think we did this. You can add the mvcc_timestamp option and get the mvcc_timestamp for each message.