Skip to main content
The CREATE CHANGEFEED statement creates a new changefeed, which targets an allowlist of tables called “watched rows”. Every change to a watched row is emitted as a record in a configurable format (JSON or Avro) to a configurable sink or directly to the SQL session. When a changefeed emits messages to a sink, it works as a job. You can create, pause, resume, alter, or cancel a changefeed job. To get started with changefeeds, refer to the Create and Configure Changefeeds page for important usage considerations. For details on how changefeeds emit messages, refer to the Changefeed Messages page. The examples on this page provide the foundational syntax of the CREATE CHANGEFEED statement. For examples on more specific use cases with changefeeds, refer to the following pages: Cockroach Labs recommends monitoring your changefeeds to track retryable errors and protected timestamp usage. Refer to the Monitor and Debug Changefeeds page for more information.

Required privileges

As of v25.1, viewing and managing a changefeed job by users with the CHANGEFEED privilege is deprecated. This functionality of the CHANGEFEED privilege will be removed in a future release.We recommend transitioning users that need to view and manage running changefeed jobs to roles that own the jobs or granting them the VIEWJOB or CONTROLJOB privilege. For more details, refer to View and manage changefeed jobs.

Privilege model

For fine-grained access control, we recommend using the system-level privileges CHANGEFEED and CONTROLJOB / VIEWJOB.
The following summarizes the operations users can run depending on whether the assigned privileges are at the job or table level:

CHANGEFEED privilege

Viewing and managing changefeed jobs with the CHANGEFEED privilege is deprecated as of v25.1. Instead, transition users that need to view and manage running changefeed jobs to roles that own the jobs or granting them the VIEWJOB or CONTROLJOB privilege. For more details, refer to View and manage changefeed jobs.
You can grant a user the CHANGEFEED privilege to allow them to create changefeeds on a specific table:
When you grant a user the CHANGEFEED privilege on a set of tables, they can create changefeeds on the target tables even if the user does not have the CONTROLCHANGEFEED role option or the SELECT privilege on the tables. These users will be able to create changefeeds, but they will not be able to run a SELECT query on that data directly. However, they could still read this data indirectly if they have read access to the sink. You can add CHANGEFEED to the user or role’s default privileges with ALTER DEFAULT PRIVILEGES:
To restrict a user’s access to changefeed data and sink credentials, enable the changefeed.permissions.require_external_connection_sink.enabled cluster setting. When you enable this setting, users with the CHANGEFEED privilege on a set of tables can only create changefeeds into external connections.

View and manage changefeed jobs

Users can view and manage changefeed jobs when one of the following are met:
  • Job ownership: They own the job, or are a member of a role that owns a job.
  • Global privileges: They are assigned CONTROLJOB or VIEWJOB.
To give a set of users access to a specific job, or set of jobs, assign them to a role that owns the job(s). You can transfer ownership of a job to a role or user using the ALTER JOB statement:

Synopsis

create_changefeed syntax diagram

Parameters

CDC query parameters

Change data capture queries allow you to define the change data emitted to your sink when you create a changefeed. See the Change Data Capture Queries page for detail on the functionality, syntax, and use cases for changefeeds created with queries.

Sink URI

To form the URI for each sink:
For more comprehensive detail of using and configuring each sink, refer to: You can create an external connection to represent a changefeed sink URI. This allows you to specify the external connection’s name in statements rather than the provider-specific URI. For detail on using external connections, see the CREATE EXTERNAL CONNECTION page.

Query parameters

Parameters should always be URI-encoded before they are included the changefeed’s URI, as they often contain special characters. Use Javascript’s encodeURIComponent function or Go language’s url.QueryEscape function to URI-encode the parameters. Other languages provide similar functions to URI-encode special characters.
Query parameters include: For example: CREATE CHANGEFEED FOR TABLE users INTO 'gs://...?AUTH...&partition_format=hourly'

Default: daily S3_STORAGE_CLASS | Amazon S3 cloud storage sink | STRING | Specify the Amazon S3 storage class for files created by the changefeed. See Create a changefeed with an S3 storage class for the available classes and an example.

Default: STANDARD sasl_aws_iam_role_arn | Amazon MSK | STRING | The ARN for the IAM role that has the permissions to create a topic and send data to the topic. For more details on setting up an Amazon MSK cluster with an IAM role, refer to the AWS documentation. sasl_aws_iam_session_name | Amazon MSK | STRING | The user-specified string that identifies the session in AWS. sasl_aws_region | Amazon MSK | STRING | The region of the Amazon MSK cluster. sasl_client_id | Kafka | STRING | Client ID for OAuth authentication from a third-party provider. This parameter is only applicable with sasl_mechanism=OAUTHBEARER. sasl_client_secret | Kafka | STRING | Client secret for OAuth authentication from a third-party provider. This parameter is only applicable with sasl_mechanism=OAUTHBEARER. Note: You must base64 encode this value when passing it in as part of a sink URI. sasl_enabled | Amazon MSK, Azure Event Hubs, Kafka, Confluent Cloud | BOOL | If true, set the authentication protocol with the sasl_mechanism parameter. You must have tls_enabled set to true to use SASL.

For Confluent Cloud and Azure Event Hubs sinks, this is set to true by default.

Default: false sasl_grant_type | Kafka | STRING | Override the default OAuth client credentials grant type for other implementations. This parameter is only applicable with sasl_mechanism=OAUTHBEARER. sasl_handshake | Azure Event Hubs, Kafka, Confluent Cloud | BOOL | For Confluent Cloud and Azure Event Hubs sinks, this is set to true by default. sasl_mechanism | Amazon MSK, Azure Event Hubs, Kafka, Confluent Cloud | STRING | Can be set to OAUTHBEARER, SCRAM-SHA-256, SCRAM-SHA-512, or PLAIN. A sasl_user and sasl_password are required for PLAIN and SCRAM authentication.

For Amazon MSK clusters, set to AWS_MSK_IAM. sasl_aws_iam_role_arn, sasl_aws_iam_session_name, and sasl_aws_region are also required in the sink uri.

Refer to the Connect to a Changefeed Kafka sink with OAuth Using Okta tutorial for detail setting up OAuth using Okta.

For Confluent Cloud and Azure Event Hubs sinks, sasl_mechanism=PLAIN is required but set automatically by CockroachDB.

Default: PLAIN sasl_scopes | Kafka | STRING | A list of scopes that the OAuth token should have access for. This parameter is only applicable with sasl_mechanism=OAUTHBEARER. sasl_token_url | Kafka | STRING | Client token URL for OAuth authentication from a third-party provider. Note: You must URL encode this value before passing in a URI. This parameter is only applicable with sasl_mechanism=OAUTHBEARER. sasl_user | Amazon MSK, Kafka | STRING | Your SASL username. sasl_password | Amazon MSK, Kafka | STRING | Your SASL password. Note: Passwords should be URL encoded since the value can contain characters that would cause authentication to fail. shared_access_key | Azure Event Hubs | STRING | The URL-encoded key for your Event Hub shared access policy. shared_access_key_name | Azure Event Hubs | STRING | The name of your Event Hub shared access policy. tls_enabled | Amazon MSK, Kafka, Confluent Cloud | BOOL | If true, enable Transport Layer Security (TLS) on the connection to Kafka. This can be used with a ca_cert (see below).

For Confluent Cloud and Azure Event Hubs sinks, this is set to true by default.

Default: false topic_name | Azure Event Hubs, Kafka, Confluent Cloud, GC Pub/Sub | STRING | Allows arbitrary topic naming for Kafka and GC Pub/Sub topics. See the Kafka topic naming limitations or GC Pub/Sub topic naming for detail on supported characters etc.

For example, CREATE CHANGEFEED FOR foo,bar INTO 'kafka://sink?topic_name=all' will emit all records to a topic named all. Note that schemas will still be registered separately. When using Kafka, this parameter can be combined with the topic_prefix parameter (this is not supported for GC Pub/Sub).

Default: table name. topic_prefix | Azure Event Hubs, Kafka, Confluent Cloud | STRING | Adds a prefix to all topic names.

For example, CREATE CHANGEFEED FOR TABLE foo INTO 'kafka://...?topic_prefix=bar_' would emit rows under the topic bar_foo instead of foo.

Options

Using the format=avro, envelope=key_only, and updated options together is rejected. envelope=key_only prevents any rows with updated fields from being emitted, which makes the updated option meaningless.

Files

The files emitted to a sink use the following naming conventions:
The timestamp format is YYYYMMDDHHMMSSNNNNNNNNNLLLLLLLLLL.

General file format

For example:
When emitting changefeed messages to a cloud storage sink, you can specify a partition format for your files using the partition_format query parameter. This will result in the following file path formats:
  • daily: This is the default option and will follow the same pattern as the previous general file format.
  • hourly: This will partition into an hourly directory as the changefeed emits messages, like the following:
  • flat: This will result in no file partitioning. The cloud storage path you specify when creating a changefeed will store all of the message files with no additional directories created.

Resolved file format

For example:

Examples

Before running any of the examples in this section it is necessary to enable the kv.rangefeed.enabled cluster setting. If you are working on a CockroachDB Standard or Basic cluster, this cluster setting is enabled by default. The following examples show the syntax for managing changefeeds and starting changefeeds with different use cases and features. The Options table on this page provides a list of all the available options. For information on sink-specific query parameters and configurations, refer to the Changefeed Sinks page. You can create an external connection to represent a changefeed sink URI. This allows you to specify the external connection’s name in statements rather than the provider-specific URI. For detail on using external connections, see the CREATE EXTERNAL CONNECTION page. We recommend limiting the number of changefeeds per cluster to 80.

Create a changefeed connected to a sink

You can connect a changefeed to the following sinks:

Create a changefeed that filters and transforms change data

CDC queries can filter and transform change data before emitting it to a sink or a SQL client. You can adapt a changefeed with CDC queries by including SELECT and WHERE clauses in your CREATE statement:
CDC queries can only run on a single table per changefeed.

Create a sinkless changefeed

You can create a changefeed that will send messages to the SQL client rather than a sink:
To create a sinkless changefeed using CDC queries:

Use an external connection to specify a changefeed sink

External connections provide a way to define a name for a sink, which you can use instead of the provider-specific URI. To restrict a user’s access to changefeed data and sink credentials, enable the changefeed.permissions.require_external_connection_sink.enabled cluster setting. When you enable this setting, users with the CHANGEFEED privilege on a set of tables can only create changefeeds into external connections. External connections support all changefeed sinks.
In the changefeed statement, you specify the external connection name:

Filter changefeeds for tables using row-level TTL

Use the ttl_disable_changefeed_replication table storage parameter to prevent changefeeds from sending DELETE messages issued by row-level TTL jobs for a table. Include the storage parameter when you create or alter the table. For example:
You can also widen the scope to the cluster by setting the sql.ttl.changefeed_replication.disabled cluster setting to true. This will prevent changefeeds from emitting deletes issued by all TTL jobs on a cluster. If you want to have a changefeed ignore the storage parameter or cluster setting that disables changefeed replication, you can set the changefeed option ignore_disable_changefeed_replication to true:
This is useful when you have multiple use cases for different changefeeds on the same table. For example, you have a table with a changefeed streaming changes to another database for analytics workflows in which you do not want to reflect row-level TTL deletes. Secondly, you have a changefeed on the same table for audit-logging purposes for which you need to persist every change through the changefeed. For guidance on how to filter changefeed messages to emit row-level TTL deletes only, refer to Change Data Capture Queries.

Disallow schema changes on tables to improve changefeed performance

Use the schema_locked storage parameter to indicate that a schema change is not currently ongoing on a watched table. This allows the changefeed to take a fast path that avoids checking if there are schema changes that could require synchronization between changefeed aggregators. This helps to decrease the latency between a write committing to a table and it emitting to the changefeed’s sink. Enable schema_locked on the watched table with the ALTER TABLE statement:
CockroachDB attempts to automatically unset schema_locked before performing a schema change and reapply it when done. However, certain schema changes (such as ALTER TABLE ... SET LOCALITY) cannot automatically unset it. For these cases, you must manually unlock the table with schema_locked = false, complete the schema change, and then lock the table again with schema_locked = true. The changefeed will run as normal while schema_locked is unset, but it will not benefit from the performance optimization.

Manage a changefeed

For changefeed jobs, use SHOW CHANGEFEED JOBS to check the status:
Use the following SQL statements to pause, resume, or cancel a changefeed.

Pause a changefeed

For more information, see PAUSE JOB.

Resume a paused changefeed

For more information, see RESUME JOB.

Cancel a changefeed

For more information, see CANCEL JOB.

Modify a changefeed

To modify a changefeed, pause the job and then use:
You can add new table targets, remove them, set new changefeed options, and unset them. For more information, see ALTER CHANGEFEED.

Configuring all changefeeds

It is useful to be able to pause all running changefeeds during troubleshooting, testing, or when a decrease in CPU load is needed. To pause all running changefeeds:
This will change the status for each of the running changefeeds to paused, which can be verified with SHOW CHANGEFEED JOBS. To resume all running changefeeds:
This will resume the changefeeds and update the status for each of the changefeeds to running.

Start a new changefeed where another ended

In some situations, you may want to start a changefeed where a previously running changefeed ended. For example, a changefeed could encounter an error it cannot recover from, such as when a TRUNCATE is performed, and you need to restart the changefeed.
  1. Use SHOW CHANGEFEED JOB to find the high-water timestamp for the ended changefeed:
If a changefeed has failed, you must restart the changefeed from a timestamp after the event that caused the failure.
  1. Use the high_water_timestamp to start the new changefeed:
When you use the cursor option to start a changefeed, it will not perform an initial scan.

Create a changefeed with an S3 storage class

To associate the changefeed message files with a specific storage class in your Amazon S3 bucket, use the S3_STORAGE_CLASS parameter with the class. For example, the following S3 connection URI specifies the INTELLIGENT_TIERING storage class:
Use the parameter to set one of these storage classes listed in Amazon’s documentation. For more general usage information, see Amazon’s Using Amazon S3 storage classes documentation.

Define a key to determine the changefeed sink partition

With the key_column option, you can define the key used in message metadata that determines the partition for the changefeed message at your downstream sink. This option overrides the default primary key:
key_column does not preserve ordering of messages from CockroachDB to the downstream sink, therefore you must include the unordered option. It does not affect per-key ordering guarantees or the output of key_in_value.

See also