diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c index cb6b5e5..93dd836 100644 --- a/src/backend/replication/syncrep.c +++ b/src/backend/replication/syncrep.c @@ -68,6 +68,7 @@ #include "storage/proc.h" #include "tcop/tcopprot.h" #include "utils/builtins.h" +#include "utils/memutils.h" #include "utils/ps_status.h" /* User-settable parameters for sync rep */ @@ -361,11 +362,6 @@ SyncRepInitConfig(void) { int priority; - /* Update the config data of synchronous replication */ - SyncRepFreeConfig(SyncRepConfig); - SyncRepConfig = NULL; - SyncRepUpdateConfig(); - /* * Determine if we are a potential sync standby and remember the result * for handling replies from standby. @@ -868,42 +864,19 @@ SyncRepUpdateSyncStandbysDefined(void) } /* - * Parse synchronous_standby_names and update the config data - * of synchronous standbys. - */ -void -SyncRepUpdateConfig(void) -{ - int parse_rc; - - if (!SyncStandbysDefined()) - return; - - /* - * check_synchronous_standby_names() verifies the setting value of - * synchronous_standby_names before this function is called. So - * syncrep_yyparse() must not cause an error here. - */ - syncrep_scanner_init(SyncRepStandbyNames); - parse_rc = syncrep_yyparse(); - Assert(parse_rc == 0); - syncrep_scanner_finish(); - - SyncRepConfig = syncrep_parse_result; - syncrep_parse_result = NULL; -} - -/* * Free a previously-allocated config data of synchronous replication. */ void -SyncRepFreeConfig(SyncRepConfigData *config) +SyncRepFreeConfig(SyncRepConfigData *config, bool itself) { if (!config) return; list_free_deep(config->members); - pfree(config); + + if (itself) + free(config); + } #ifdef USE_ASSERT_CHECKING @@ -954,6 +927,10 @@ check_synchronous_standby_names(char **newval, void **extra, GucSource source) if (*newval != NULL && (*newval)[0] != '\0') { + MemoryContext oldcontext; + + oldcontext = MemoryContextSwitchTo(TopMemoryContext); + syncrep_scanner_init(*newval); parse_rc = syncrep_yyparse(); syncrep_scanner_finish(); @@ -1012,17 +989,36 @@ check_synchronous_standby_names(char **newval, void **extra, GucSource source) } /* - * syncrep_yyparse sets the global syncrep_parse_result as side effect. - * But this function is required to just check, so frees it - * after parsing the parameter. + * syncrep_yyparse sets the global syncrep_parse_result. + * Save syncrep_parse_result to extra in order to use it in + * assign_synchronous_standby_names hook as well. */ - SyncRepFreeConfig(syncrep_parse_result); + *extra = (void *)syncrep_parse_result; + + MemoryContextSwitchTo(oldcontext); } return true; } void +assign_synchronous_standby_names(const char *newval, void *extra) +{ + SyncRepConfigData *myextra = extra; + + /* + * Free members of SyncRepConfig if it already refers somewhere, + * but SyncRepConfig itself is freed by set_extra_field. + */ + if (SyncRepConfig) + SyncRepFreeConfig(SyncRepConfig, false); + + SyncRepConfig = myextra; + + return; +} + +void assign_synchronous_commit(int newval, void *extra) { switch (newval) diff --git a/src/backend/replication/syncrep_gram.y b/src/backend/replication/syncrep_gram.y index 380fedc..de25a40 100644 --- a/src/backend/replication/syncrep_gram.y +++ b/src/backend/replication/syncrep_gram.y @@ -76,7 +76,7 @@ static SyncRepConfigData * create_syncrep_config(char *num_sync, List *members) { SyncRepConfigData *config = - (SyncRepConfigData *) palloc(sizeof(SyncRepConfigData)); + (SyncRepConfigData *) malloc(sizeof(SyncRepConfigData)); config->num_sync = atoi(num_sync); config->members = members; diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index e4a0119..a9c982b 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -2780,23 +2780,12 @@ pg_stat_get_wal_senders(PG_FUNCTION_ARGS) MemoryContextSwitchTo(oldcontext); /* - * Allocate and update the config data of synchronous replication, - * and then get the currently active synchronous standbys. + * Get the currently active synchronous standbys. */ - SyncRepUpdateConfig(); LWLockAcquire(SyncRepLock, LW_SHARED); sync_standbys = SyncRepGetSyncStandbys(NULL); LWLockRelease(SyncRepLock); - /* - * Free the previously-allocated config data because a backend - * no longer needs it. The next call of this function needs to - * allocate and update the config data newly because the setting - * of sync replication might be changed between the calls. - */ - SyncRepFreeConfig(SyncRepConfig); - SyncRepConfig = NULL; - for (i = 0; i < max_wal_senders; i++) { WalSnd *walsnd = &WalSndCtl->walsnds[i]; diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index fb091bc..3ce83bf 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -3484,7 +3484,7 @@ static struct config_string ConfigureNamesString[] = }, &SyncRepStandbyNames, "", - check_synchronous_standby_names, NULL, NULL + check_synchronous_standby_names, assign_synchronous_standby_names, NULL }, { diff --git a/src/include/replication/syncrep.h b/src/include/replication/syncrep.h index 14b5664..e1706f6 100644 --- a/src/include/replication/syncrep.h +++ b/src/include/replication/syncrep.h @@ -59,13 +59,13 @@ extern void SyncRepReleaseWaiters(void); /* called by wal sender and user backend */ extern List *SyncRepGetSyncStandbys(bool *am_sync); -extern void SyncRepUpdateConfig(void); -extern void SyncRepFreeConfig(SyncRepConfigData *config); +extern void SyncRepFreeConfig(SyncRepConfigData *config, bool itself); /* called by checkpointer */ extern void SyncRepUpdateSyncStandbysDefined(void); extern bool check_synchronous_standby_names(char **newval, void **extra, GucSource source); +extern void assign_synchronous_standby_names(const char *newval, void *extra); extern void assign_synchronous_commit(int newval, void *extra); /*