Avoid invalidating all RelationSyncCache entries on publication change.
authorAmit Kapila <[email protected]>
Thu, 6 Mar 2025 08:49:38 +0000 (14:19 +0530)
committerAmit Kapila <[email protected]>
Thu, 6 Mar 2025 08:49:38 +0000 (14:19 +0530)
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 <[email protected]>
Author: Hayato Kuroda <[email protected]>
Reviewed-by: Hou Zhijie <[email protected]>
Reviewed-by: Amit Kapila <[email protected]>
Discussion: https://postgr.es/m/OSCPR01MB14966C09AA201EFFA706576A7F5C92@OSCPR01MB14966.jpnprd01.prod.outlook.com

src/backend/replication/pgoutput/pgoutput.c

index 7d464f656aaa6a86637b7d57fc1460d39ebe011f..9063af6e1df1adb46b47b817e35fa4fd43a5c7fd 100644 (file)
@@ -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)