From 1d8312aae6ec0dc63dbec2b67cf88f1125ae910e Mon Sep 17 00:00:00 2001 From: bovenshi Date: Mon, 19 Dec 2022 14:16:35 +0800 Subject: [PATCH] Add cache invalid flag in pgoutput Before PG 14, we use to execute all the invalidations at each command end as we had no way of knowing which invalidations happened before that command. Due to this, transactions involving large amounts of DDLs use to take more time and also lead to high CPU usage. There is no particularly good way to solve this problem. However, we can make optimization for pgoutput to get better performance. Author: Bowen Shi Discussion: https://www.postgresql.org/message-id/17716-1fe42e7b44fc2f25%40postgresql.org --- src/backend/replication/pgoutput/pgoutput.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/backend/replication/pgoutput/pgoutput.c b/src/backend/replication/pgoutput/pgoutput.c index 7992421..9a6aade 100644 --- a/src/backend/replication/pgoutput/pgoutput.c +++ b/src/backend/replication/pgoutput/pgoutput.c @@ -99,6 +99,7 @@ static RelationSyncEntry *get_rel_sync_entry(PGOutputData *data, Oid relid); static void rel_sync_cache_relation_cb(Datum arg, Oid relid); static void rel_sync_cache_publication_cb(Datum arg, int cacheid, uint32 hashvalue); +static bool all_cache_invalid = false; /* * Specify output plugin callbacks @@ -835,6 +836,8 @@ get_rel_sync_entry(PGOutputData *data, Oid relid) entry->publish_as_relid = publish_as_relid; entry->replicate_valid = true; + + all_cache_invalid = false; } return entry; @@ -913,9 +916,13 @@ rel_sync_cache_publication_cb(Datum arg, int cacheid, uint32 hashvalue) * There is no way to find which entry in our cache the hash belongs to so * mark the whole cache as invalid. */ - hash_seq_init(&status, RelationSyncCache); - while ((entry = (RelationSyncEntry *) hash_seq_search(&status)) != NULL) - entry->replicate_valid = false; + if (!all_cache_invalid) + { + hash_seq_init(&status, RelationSyncCache); + while ((entry = (RelationSyncEntry *) hash_seq_search(&status)) != NULL) + entry->replicate_valid = false; + all_cache_invalid = true; + } } /* -- 2.9.3