Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0523f41

Browse files
author
Commitfest Bot
committed
[CF 5663] Selectively invalidate caches in pgoutput when pg_namespace is modified
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5663 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/OSCPR01MB14966D958533C269CE996E69AF5A72@OSCPR01MB14966.jpnprd01.prod.outlook.com Author(s): Hayato Kuroda
2 parents e050af2 + f7e2d1d commit 0523f41

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/backend/replication/pgoutput/pgoutput.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ typedef struct RelationSyncEntry
185185
* row filter expressions, column list, etc.
186186
*/
187187
MemoryContext entry_cxt;
188+
189+
/* Hash value of schema OID. Used for pg_namespace syscache callback */
190+
uint32 schema_hashvalue;
188191
} RelationSyncEntry;
189192

190193
/*
@@ -227,8 +230,7 @@ static void send_relation_and_attrs(Relation relation, TransactionId xid,
227230
LogicalDecodingContext *ctx,
228231
RelationSyncEntry *relentry);
229232
static void rel_sync_cache_relation_cb(Datum arg, Oid relid);
230-
static void rel_sync_cache_publication_cb(Datum arg, int cacheid,
231-
uint32 hashvalue);
233+
static void rel_sync_cache_schema_cb(Datum arg, int cacheid, uint32 hashvalue);
232234
static void set_schema_sent_in_streamed_txn(RelationSyncEntry *entry,
233235
TransactionId xid);
234236
static bool get_schema_sent_in_streamed_txn(RelationSyncEntry *entry,
@@ -1974,18 +1976,11 @@ init_rel_sync_cache(MemoryContext cachectx)
19741976
CacheRegisterRelcacheCallback(rel_sync_cache_relation_cb, (Datum) 0);
19751977

19761978
/*
1977-
* Flush all cache entries after a pg_namespace change, in case it was a
1979+
* Flush related entries after a pg_namespace change, in case it was a
19781980
* schema rename affecting a relation being replicated.
1979-
*
1980-
* XXX: It is not a good idea to invalidate all the relation entries in
1981-
* RelationSyncCache on schema rename. We can optimize it to invalidate
1982-
* only the required relations by either having a specific invalidation
1983-
* message containing impacted relations or by having schema information
1984-
* in each RelationSyncCache entry and using hashvalue of pg_namespace.oid
1985-
* passed to the callback.
19861981
*/
19871982
CacheRegisterSyscacheCallback(NAMESPACEOID,
1988-
rel_sync_cache_publication_cb,
1983+
rel_sync_cache_schema_cb,
19891984
(Datum) 0);
19901985

19911986
relation_callbacks_registered = true;
@@ -2057,6 +2052,7 @@ get_rel_sync_entry(PGOutputData *data, Relation relation)
20572052
entry->publish_as_relid = InvalidOid;
20582053
entry->columns = NULL;
20592054
entry->attrmap = NULL;
2055+
entry->schema_hashvalue = 0;
20602056
}
20612057

20622058
/* Validate the entry */
@@ -2303,6 +2299,10 @@ get_rel_sync_entry(PGOutputData *data, Relation relation)
23032299
list_free(schemaPubids);
23042300
list_free(rel_publications);
23052301

2302+
entry->schema_hashvalue =
2303+
GetSysCacheHashValue1(NAMESPACEOID,
2304+
ObjectIdGetDatum(RelationGetNamespace(relation)));
2305+
23062306
entry->replicate_valid = true;
23072307
}
23082308

@@ -2401,12 +2401,12 @@ rel_sync_cache_relation_cb(Datum arg, Oid relid)
24012401
}
24022402

24032403
/*
2404-
* Publication relation/schema map syscache invalidation callback
2404+
* schema syscache invalidation callback
24052405
*
24062406
* Called for invalidations on pg_namespace.
24072407
*/
2408-
static void
2409-
rel_sync_cache_publication_cb(Datum arg, int cacheid, uint32 hashvalue)
2408+
void
2409+
rel_sync_cache_schema_cb(Datum arg, int cacheid, uint32 hashvalue)
24102410
{
24112411
HASH_SEQ_STATUS status;
24122412
RelationSyncEntry *entry;
@@ -2420,13 +2420,15 @@ rel_sync_cache_publication_cb(Datum arg, int cacheid, uint32 hashvalue)
24202420
return;
24212421

24222422
/*
2423-
* We have no easy way to identify which cache entries this invalidation
2424-
* event might have affected, so just mark them all invalid.
2423+
* Identify entries which belongs to the specified schema, and invalidate
2424+
* it.
24252425
*/
24262426
hash_seq_init(&status, RelationSyncCache);
24272427
while ((entry = (RelationSyncEntry *) hash_seq_search(&status)) != NULL)
24282428
{
2429-
entry->replicate_valid = false;
2429+
/* hashvalue == 0 means a cache reset, must clear all state */
2430+
if (hashvalue == 0 || entry->schema_hashvalue == hashvalue)
2431+
entry->replicate_valid = false;
24302432
}
24312433
}
24322434

0 commit comments

Comments
 (0)