diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c index f1e9a38..a4b47a8 100644 --- a/src/backend/libpq/hba.c +++ b/src/backend/libpq/hba.c @@ -1813,8 +1813,7 @@ load_hba(void) FreeFile(file); /* Now parse all the lines */ - Assert(PostmasterContext); - hbacxt = AllocSetContextCreate(PostmasterContext, + hbacxt = AllocSetContextCreate(CurrentMemoryContext, "hba parser context", ALLOCSET_SMALL_SIZES); oldcxt = MemoryContextSwitchTo(hbacxt); @@ -1870,14 +1869,24 @@ load_hba(void) } /* Loaded new file successfully, replace the one we use */ - if (parsed_hba_context != NULL) - MemoryContextDelete(parsed_hba_context); + discard_hba(); parsed_hba_context = hbacxt; parsed_hba_lines = new_parsed_lines; return true; } +void +discard_hba(void) +{ + if (parsed_hba_context != NULL) + { + MemoryContextDelete(parsed_hba_context); + parsed_hba_context = NULL; + parsed_hba_lines = NIL; + } +} + /* * Parse one tokenised line from the ident config file and store the result in * an IdentLine structure, or NULL if parsing fails. @@ -2189,8 +2198,7 @@ load_ident(void) FreeFile(file); /* Now parse all the lines */ - Assert(PostmasterContext); - ident_context = AllocSetContextCreate(PostmasterContext, + ident_context = AllocSetContextCreate(CurrentMemoryContext, "ident parser context", ALLOCSET_SMALL_SIZES); oldcxt = MemoryContextSwitchTo(ident_context); @@ -2241,6 +2249,19 @@ load_ident(void) } /* Loaded new file successfully, replace the one we use */ + discard_ident(); + parsed_ident_context = ident_context; + parsed_ident_lines = new_parsed_lines; + + return true; +} + +void +discard_ident(void) +{ + ListCell *parsed_line_cell; + IdentLine *newline; + if (parsed_ident_lines != NIL) { foreach(parsed_line_cell, parsed_ident_lines) @@ -2249,14 +2270,14 @@ load_ident(void) if (newline->ident_user[0] == '/') pg_regfree(&newline->re); } + parsed_ident_lines = NIL; } + if (parsed_ident_context != NULL) + { MemoryContextDelete(parsed_ident_context); - - parsed_ident_context = ident_context; - parsed_ident_lines = new_parsed_lines; - - return true; + parsed_ident_context = NULL; + } } diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 824d5ab..b93b307 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -192,17 +192,6 @@ PerformAuthentication(Port *port) * FIXME: [fork/exec] Ugh. Is there a way around this overhead? */ #ifdef EXEC_BACKEND - - /* - * load_hba() and load_ident() want to work within the PostmasterContext, - * so create that if it doesn't exist (which it won't). We'll delete it - * again later, in PostgresMain. - */ - if (PostmasterContext == NULL) - PostmasterContext = AllocSetContextCreate(TopMemoryContext, - "Postmaster", - ALLOCSET_DEFAULT_SIZES); - if (!load_hba()) { /* @@ -738,6 +727,14 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username, } /* + * We don't need the HBA and ident data going forward, but we can't rely + * on release of PostmasterContext to clean that up, so discard them + * explicitly here. + */ + discard_hba(); + discard_ident(); + + /* * If we're trying to shut down, only superusers can connect, and new * replication connections are not allowed. */ diff --git a/src/include/libpq/hba.h b/src/include/libpq/hba.h index dc7d257..d0a8ad2 100644 --- a/src/include/libpq/hba.h +++ b/src/include/libpq/hba.h @@ -102,6 +102,10 @@ typedef struct Port hbaPort; extern bool load_hba(void); extern bool load_ident(void); + +extern void discard_hba(void); +extern void discard_ident(void); + extern void hba_getauthmethod(hbaPort *port); extern int check_usermap(const char *usermap_name, const char *pg_role, const char *auth_user,