From cd731a5ae3424f238ba99b56a8b645d7acb6fbe0 Mon Sep 17 00:00:00 2001 From: Nisha Moond Date: Fri, 21 Mar 2025 09:36:30 +0530 Subject: [PATCH v7 2/2] Stats collection for the multiple_unique_conflicts The patch adds a new column 'confl_multiple_unique_conflicts' in view pg_stat_subscription_stats to support stats collection for this conflict type. --- doc/src/sgml/monitoring.sgml | 12 ++++++++++++ src/backend/catalog/system_views.sql | 1 + src/backend/replication/logical/conflict.c | 4 +--- src/backend/utils/adt/pgstatfuncs.c | 6 ++++-- src/include/catalog/pg_proc.dat | 6 +++--- src/include/replication/conflict.h | 2 +- src/test/regress/expected/rules.out | 3 ++- src/test/subscription/t/035_conflicts.pl | 18 ++++++++++++++++++ 8 files changed, 42 insertions(+), 10 deletions(-) diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index aaa6586d3a4..0960f5ba94a 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -2250,6 +2250,18 @@ description | Waiting for a newly initialized WAL file to reach durable storage + + + confl_multiple_unique_conflicts bigint + + + Number of times a row insertion or an updated row values violated multiple + NOT DEFERRABLE unique constraints during the + application of changes. See + for details about this conflict. + + + stats_reset timestamp with time zone diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index a4d2cfdcaf5..31d269b7ee0 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -1384,6 +1384,7 @@ CREATE VIEW pg_stat_subscription_stats AS ss.confl_update_missing, ss.confl_delete_origin_differs, ss.confl_delete_missing, + ss.confl_multiple_unique_conflicts, ss.stats_reset FROM pg_subscription as s, pg_stat_get_subscription_stats(s.oid) as ss; diff --git a/src/backend/replication/logical/conflict.c b/src/backend/replication/logical/conflict.c index b1614d3aaf6..63d28900a7f 100644 --- a/src/backend/replication/logical/conflict.c +++ b/src/backend/replication/logical/conflict.c @@ -129,9 +129,7 @@ ReportApplyConflict(EState *estate, ResultRelInfo *relinfo, int elevel, &err_detail); } - /* Conflict stats are not gathered for multiple_unique_conflicts */ - if (type != CT_MULTIPLE_UNIQUE_CONFLICTS) - pgstat_report_subscription_conflict(MySubscription->oid, type); + pgstat_report_subscription_conflict(MySubscription->oid, type); ereport(elevel, errcode_apply_conflict(type), diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index 662ce46cbc2..97af7c6554f 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -2171,7 +2171,7 @@ pg_stat_get_replication_slot(PG_FUNCTION_ARGS) Datum pg_stat_get_subscription_stats(PG_FUNCTION_ARGS) { -#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS 10 +#define PG_STAT_GET_SUBSCRIPTION_STATS_COLS 11 Oid subid = PG_GETARG_OID(0); TupleDesc tupdesc; Datum values[PG_STAT_GET_SUBSCRIPTION_STATS_COLS] = {0}; @@ -2203,7 +2203,9 @@ pg_stat_get_subscription_stats(PG_FUNCTION_ARGS) INT8OID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 9, "confl_delete_missing", INT8OID, -1, 0); - TupleDescInitEntry(tupdesc, (AttrNumber) 10, "stats_reset", + TupleDescInitEntry(tupdesc, (AttrNumber) 10, "confl_multiple_unique_conflicts", + INT8OID, -1, 0); + TupleDescInitEntry(tupdesc, (AttrNumber) 11, "stats_reset", TIMESTAMPTZOID, -1, 0); BlessTupleDesc(tupdesc); diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 890822eaf79..0d29ef50ff2 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -5647,9 +5647,9 @@ { oid => '6231', descr => 'statistics: information about subscription stats', proname => 'pg_stat_get_subscription_stats', provolatile => 's', proparallel => 'r', prorettype => 'record', proargtypes => 'oid', - proallargtypes => '{oid,oid,int8,int8,int8,int8,int8,int8,int8,int8,timestamptz}', - proargmodes => '{i,o,o,o,o,o,o,o,o,o,o}', - proargnames => '{subid,subid,apply_error_count,sync_error_count,confl_insert_exists,confl_update_origin_differs,confl_update_exists,confl_update_missing,confl_delete_origin_differs,confl_delete_missing,stats_reset}', + proallargtypes => '{oid,oid,int8,int8,int8,int8,int8,int8,int8,int8,int8,timestamptz}', + proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o}', + proargnames => '{subid,subid,apply_error_count,sync_error_count,confl_insert_exists,confl_update_origin_differs,confl_update_exists,confl_update_missing,confl_delete_origin_differs,confl_delete_missing,confl_multiple_unique_conflicts,stats_reset}', prosrc => 'pg_stat_get_subscription_stats' }, { oid => '6118', descr => 'statistics: information about subscription', proname => 'pg_stat_get_subscription', prorows => '10', proisstrict => 'f', diff --git a/src/include/replication/conflict.h b/src/include/replication/conflict.h index 06d5d05c560..a467bd86c69 100644 --- a/src/include/replication/conflict.h +++ b/src/include/replication/conflict.h @@ -51,7 +51,7 @@ typedef enum */ } ConflictType; -#define CONFLICT_NUM_TYPES (CT_DELETE_MISSING + 1) +#define CONFLICT_NUM_TYPES (CT_MULTIPLE_UNIQUE_CONFLICTS + 1) /* diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index 62f69ac20b2..47478969135 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2157,9 +2157,10 @@ pg_stat_subscription_stats| SELECT ss.subid, ss.confl_update_missing, ss.confl_delete_origin_differs, ss.confl_delete_missing, + ss.confl_multiple_unique_conflicts, ss.stats_reset FROM pg_subscription s, - LATERAL pg_stat_get_subscription_stats(s.oid) ss(subid, apply_error_count, sync_error_count, confl_insert_exists, confl_update_origin_differs, confl_update_exists, confl_update_missing, confl_delete_origin_differs, confl_delete_missing, stats_reset); + LATERAL pg_stat_get_subscription_stats(s.oid) ss(subid, apply_error_count, sync_error_count, confl_insert_exists, confl_update_origin_differs, confl_update_exists, confl_update_missing, confl_delete_origin_differs, confl_delete_missing, confl_multiple_unique_conflicts, stats_reset); pg_stat_sys_indexes| SELECT relid, indexrelid, schemaname, diff --git a/src/test/subscription/t/035_conflicts.pl b/src/test/subscription/t/035_conflicts.pl index f1417e313db..e0607e849cb 100644 --- a/src/test/subscription/t/035_conflicts.pl +++ b/src/test/subscription/t/035_conflicts.pl @@ -91,6 +91,15 @@ ok( $node_subscriber->log_contains( # Truncate table to get rid of the error $node_subscriber->safe_psql('postgres', "TRUNCATE conf_tab;"); +# Confirm multiple_unique_conflicts in pg_stat_subscription_stats +is( $node_subscriber->safe_psql( + 'postgres', + qq(SELECT confl_multiple_unique_conflicts = 1 + FROM pg_stat_subscription_stats + WHERE subname = 'sub_tab')), + qq(t), + qq(Confirm that multiple_unique_conflicts has encounted 1 time.)); + ################################################## # Test multiple_unique_conflicts due to UPDATE ################################################## @@ -130,4 +139,13 @@ ok( $node_subscriber->log_contains( 'multiple_unique_conflicts detected during update for conf_tab_c_key (c) = (8)' ); +# Confirm multiple_unique_conflicts in pg_stat_subscription_stats +is( $node_subscriber->safe_psql( + 'postgres', + qq(SELECT confl_multiple_unique_conflicts = 2 + FROM pg_stat_subscription_stats + WHERE subname = 'sub_tab')), + qq(t), + qq(Confirm that multiple_unique_conflicts has encounted 2 times.)); + done_testing(); -- 2.34.1