From: Amit Kapila Date: Thu, 6 Mar 2025 08:49:38 +0000 (+0530) Subject: Avoid invalidating all RelationSyncCache entries on publication change. X-Git-Tag: REL_18_BETA1~668 X-Git-Url: http://git.postgresql.org/gitweb/?a=commitdiff_plain;h=588acf6d0ec15d9384c2f712585c0f56936d7bac;p=postgresql.git Avoid invalidating all RelationSyncCache entries on publication change. On change of publication via ALTER PUBLICATION ... SET/ADD/DROP commands, we were invalidating all the relations present in relation sync cache maintained by pgoutput. We need to invalidate only the relation entries that are changed as part of publication DDL. We have ensured that the publication DDL execution generated the invalidations required to invalidate impacted relation sync entries in RelationSyncCache. This improves the performance by avoiding building the cache entries for the cases where a publication has many tables but only one of them is dropped. Author: Shlok Kyal Author: Hayato Kuroda Reviewed-by: Hou Zhijie Reviewed-by: Amit Kapila Discussion: https://postgr.es/m/OSCPR01MB14966C09AA201EFFA706576A7F5C92@OSCPR01MB14966.jpnprd01.prod.outlook.com --- diff --git a/src/backend/replication/pgoutput/pgoutput.c b/src/backend/replication/pgoutput/pgoutput.c index 7d464f656aa..9063af6e1df 100644 --- a/src/backend/replication/pgoutput/pgoutput.c +++ b/src/backend/replication/pgoutput/pgoutput.c @@ -1965,23 +1965,18 @@ init_rel_sync_cache(MemoryContext cachectx) /* * Flush all cache entries after a pg_namespace change, in case it was a * schema rename affecting a relation being replicated. + * + * XXX: It is not a good idea to invalidate all the relation entries in + * RelationSyncCache on schema rename. We can optimize it to invalidate + * only the required relations by either having a specific invalidation + * message containing impacted relations or by having schema information + * in each RelationSyncCache entry and using hashvalue of pg_namespace.oid + * passed to the callback. */ CacheRegisterSyscacheCallback(NAMESPACEOID, rel_sync_cache_publication_cb, (Datum) 0); - /* - * Flush all cache entries after any publication changes. (We need no - * callback entry for pg_publication, because publication_invalidation_cb - * will take care of it.) - */ - CacheRegisterSyscacheCallback(PUBLICATIONRELMAP, - rel_sync_cache_publication_cb, - (Datum) 0); - CacheRegisterSyscacheCallback(PUBLICATIONNAMESPACEMAP, - rel_sync_cache_publication_cb, - (Datum) 0); - relation_callbacks_registered = true; } @@ -2397,8 +2392,7 @@ rel_sync_cache_relation_cb(Datum arg, Oid relid) /* * Publication relation/schema map syscache invalidation callback * - * Called for invalidations on pg_publication, pg_publication_rel, - * pg_publication_namespace, and pg_namespace. + * Called for invalidations on pg_publication and pg_namespace. */ static void rel_sync_cache_publication_cb(Datum arg, int cacheid, uint32 hashvalue)