Skip to content

adapter: notice when single-replica sources meet multiple replicas - #38139

Merged
ggevay merged 3 commits into
MaterializeInc:mainfrom
ggevay:gabor/single-replica-source-notice
Aug 11, 2026
Merged

ggevay merged 3 commits into
MaterializeInc:mainfrom
ggevay:gabor/single-replica-source-notice

Conversation

@ggevay

@ggevay ggevay commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Emit a pgwire notice whenever a command puts a cluster in a state where it has more than one replica while containing sources that run on only one replica (PostgreSQL, MySQL, SQL Server): raising the replication factor of a cluster with such a source, adding a replica to it, or creating such a source on a cluster that already has multiple replicas. The extra replicas do not make these sources more fault tolerant and do not increase their ingestion throughput, so call that out.

Tested by a new pg-cdc mzcompose workflow that captures notices over pgwire and checks all three directions plus negative cases.

Motivation: incident-1223

Example

materialize=> ALTER CLUSTER oltp_cluster SET (REPLICATION FACTOR 2);
WARNING:  cluster "oltp_cluster" has more than one replica, but the following sources in it always run on only the first replica: "materialize.public.pg_orders"
DETAIL:  Adding replicas to the cluster does not make these sources more fault tolerant and does not increase their ingestion throughput.
HINT:  To achieve fault tolerance for other objects in the cluster, consider moving these sources to a separate cluster with a single replica.
ALTER CLUSTER

@ggevay ggevay added A-ADAPTER Topics related to the ADAPTER layer T-sources Theme: Sources labels Aug 10, 2026
@ggevay
ggevay force-pushed the gabor/single-replica-source-notice branch from cb0fef6 to 9c1e1ce Compare August 10, 2026 13:38
@ggevay
ggevay marked this pull request as ready for review August 10, 2026 14:08
@ggevay
ggevay requested a review from a team as a code owner August 10, 2026 14:08
@ggevay
ggevay requested a review from a team August 10, 2026 14:52

@martykulma martykulma 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.

Thanks @ggevay! I only had minor comments, nothing blocking!

/// sources that run on only one replica. Call after a command that added a
/// replica or such a source.
pub(crate) fn notify_single_replica_sources(&self, session: &Session, cluster: &Cluster) {
if cluster.replicas().count() <= 1 {

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.

Probably worth a note that this includes replicas in the pending state. If someone runs ALTER CLUSTER to change the replica size and during the replica change, run CREATE SOURCE against that cluster, my understanding is they will see the NOTICE.

@ggevay ggevay Aug 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, and we went one step further: rather than noting the behavior, the decision now excludes reconfiguration replicas entirely. For managed clusters the check counts the replication factor (taking the target of an in-progress reconfiguration over the realized one) plus INTERNAL/BILLED AS replicas, instead of counting replicas. That excludes the hydrate-overlap replicas of both mechanisms, the legacy graceful alter (which marks them pending) and the cluster controller (which creates them as ordinary replicas of the target shape), so a CREATE SOURCE during a resize no longer warns spuriously.

if cluster.replicas().count() <= 1 {
return;
}
let sources = self.single_replica_source_names(cluster);

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.

Should we cap the length of sources here? If the customer had 20 sources on a cluster, this is a long notice.

@ggevay ggevay Aug 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, the message now lists at most 3 sources and appends ", and N more" beyond that.

Comment on lines +1716 to +1730
.filter(|id| {
self.catalog().get_entry(id).source().is_some_and(|source| {
match &source.data_source {
DataSourceDesc::Ingestion { desc, .. }
| DataSourceDesc::OldSyntaxIngestion { desc, .. } => {
desc.connection.prefers_single_replica()
}
_ => false,
}
})
})
.map(|id| {
let name = self.catalog().get_entry(id).name();
self.catalog().resolve_full_name(name, None).to_string()
})

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.

nit: could merge these into a filter_map, since these both rely on self.catalog().get_entry(id).

@ggevay ggevay Aug 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

@def- def- 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.

Added a failing test, notice seems to be missing:

diff --git a/test/pg-cdc/mzcompose.py b/test/pg-cdc/mzcompose.py
index 8c4447e35a..ec7bddea04 100644
--- a/test/pg-cdc/mzcompose.py
+++ b/test/pg-cdc/mzcompose.py
@@ -364,6 +364,7 @@ def workflow_single_replica_source_notice(
             cur.execute("DROP CLUSTER IF EXISTS c1 CASCADE")
             cur.execute("DROP CLUSTER IF EXISTS c2 CASCADE")
             cur.execute("DROP CLUSTER IF EXISTS c3 CASCADE")
+            cur.execute("DROP CLUSTER IF EXISTS c4 CASCADE")
             notices.clear()

             cur.execute("CREATE SECRET pgpass AS 'postgres'")
@@ -416,6 +417,20 @@ def workflow_single_replica_source_notice(
             )
             expect_notice(notices, "c2", '"materialize.public.src2"')

+            # Creating such a source in a DDL transaction uses the staged
+            # catalog when deciding which sources to include in the notice.
+            cur.execute(
+                f"CREATE CLUSTER c4 (SIZE '{size}', REPLICATION FACTOR 2)".encode()
+            )
+            expect_no_notice(notices)
+            cur.execute("BEGIN")
+            cur.execute(
+                "CREATE SOURCE src4 IN CLUSTER c4 "
+                "FROM POSTGRES CONNECTION pg (PUBLICATION 'mz_source')"
+            )
+            cur.execute("COMMIT")
+            expect_notice(notices, "c4", '"materialize.public.src4"')
+
             # Adding a replica to an unmanaged cluster with such a source emits
             # the notice.
             cur.execute(f"CREATE CLUSTER c3 REPLICAS (r1 (SIZE '{size}'))".encode())

Running bin/mzcompose --find pg-cdc run single-replica-source-notice:

==> mzcompose: test case workflow-single-replica-source-notice failed: builtins.AssertionError: expected notice 'cluster "c4" has more than one replica, but the following sources in it always run on only the first replica: "materialize.public.src4"' in []
Traceback (most recent call last):
  File "/home/deen/git/materialize3/misc/python/materialize/mzcompose/composition.py", line 733, in test_case
    yield
  File "/home/deen/git/materialize3/misc/python/materialize/cli/mzcompose.py", line 868, in handle_composition
    composition.workflow(
    ~~~~~~~~~~~~~~~~~~~~^
        workflow_name, *args.unknown_subargs[1:], *extra_args
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/home/deen/git/materialize3/misc/python/materialize/mzcompose/composition.py", line 615, in workflow
    func(self, parser)
    ~~~~^^^^^^^^^^^^^^
  File "/home/deen/git/materialize3/test/pg-cdc/mzcompose.py", line 432, in workflow_single_replica_source_notice
    expect_notice(notices, "c4", '"materialize.public.src4"')
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/deen/git/materialize3/test/pg-cdc/mzcompose.py", line 347, in expect_notice
    expected in collected
AssertionError: expected notice 'cluster "c4" has more than one replica, but the following sources in it always run on only the first replica: "materialize.public.src4"' in []

ggevay and others added 2 commits August 11, 2026 12:30
Emit a pgwire notice whenever a command puts a cluster in a state where
it has more than one replica while containing sources that run on only
one replica (PostgreSQL, MySQL, SQL Server): raising the replication
factor of a cluster with such a source, adding a replica to it, or
creating such a source on a cluster that already has multiple replicas.
The extra replicas do not make these sources more fault tolerant and do
not increase their ingestion throughput, so call that out.

Tested by a new pg-cdc mzcompose workflow that captures notices over
pgwire and checks all three directions plus negative cases.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018LtD2Q4pdLfPiSHSsR6YAN
Count the replication factor plus INTERNAL and BILLED AS replicas instead
of counting replicas for managed clusters, so the hydrate-overlap replicas
of an in-progress reconfiguration do not trigger the notice. Include the
created source in the notice explicitly, fixing the missing notice when
CREATE SOURCE is staged in a DDL transaction. List at most 3 sources in
the notice message.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018LtD2Q4pdLfPiSHSsR6YAN
@ggevay
ggevay force-pushed the gabor/single-replica-source-notice branch from 33af171 to 21dbd05 Compare August 11, 2026 10:35
@ggevay

ggevay commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor Author

@def- Thanks for the failing test! Fixed: the notice now carries the name of the source being created explicitly instead of relying on it being visible in the catalog, so the staged-in-a-DDL-transaction case emits it too. Adopted your test scenario.

A size-only reshape and a CREATE SOURCE during its overlap emit no
spurious notice, and a shape change combined with a replication factor
raise emits the notice via the reconfiguration path.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018LtD2Q4pdLfPiSHSsR6YAN
@ggevay

ggevay commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the reviews!

@ggevay
ggevay merged commit a4e7587 into MaterializeInc:main Aug 11, 2026
78 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ADAPTER Topics related to the ADAPTER layer T-sources Theme: Sources

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants