Fix search_path cache initialization.
authorJeff Davis <jdavis@postgresql.org>
Tue, 8 Oct 2024 00:51:14 +0000 (17:51 -0700)
committerJeff Davis <jdavis@postgresql.org>
Tue, 8 Oct 2024 00:51:14 +0000 (17:51 -0700)
The cache needs to be available very early, so don't rely on
InitializeSearchPath() to initialize the it.

Reported-by: Murat Efendioğlu
Discussion: https://postgr.es/m/CACbCzujQ4zS8MM1bx-==+tr+D3Hk5G1cjN4XkUQ+Q=cEpwhzqg@mail.gmail.com
Backpatch-through: 17

src/backend/catalog/namespace.c

index 43b707699d72f7df14930ccf944e9e600c38c6dd..30807f919047456072c8d4392e1c6581d82ef9d4 100644 (file)
@@ -305,17 +305,32 @@ static SearchPathCacheEntry *LastSearchPathCacheEntry = NULL;
 static void
 spcache_init(void)
 {
-   Assert(SearchPathCacheContext);
-
    if (SearchPathCache && searchPathCacheValid &&
        SearchPathCache->members < SPCACHE_RESET_THRESHOLD)
        return;
 
-   /* make sure we don't leave dangling pointers if nsphash_create fails */
+   searchPathCacheValid = false;
+   baseSearchPathValid = false;
+
+   /*
+    * Make sure we don't leave dangling pointers if a failure happens during
+    * initialization.
+    */
    SearchPathCache = NULL;
    LastSearchPathCacheEntry = NULL;
 
-   MemoryContextReset(SearchPathCacheContext);
+   if (SearchPathCacheContext == NULL)
+   {
+       /* Make the context we'll keep search path cache hashtable in */
+       SearchPathCacheContext = AllocSetContextCreate(TopMemoryContext,
+                                                      "search_path processing cache",
+                                                      ALLOCSET_DEFAULT_SIZES);
+   }
+   else
+   {
+       MemoryContextReset(SearchPathCacheContext);
+   }
+
    /* arbitrary initial starting size of 16 elements */
    SearchPathCache = nsphash_create(SearchPathCacheContext, 16, NULL);
    searchPathCacheValid = true;
@@ -4742,11 +4757,6 @@ InitializeSearchPath(void)
    }
    else
    {
-       /* Make the context we'll keep search path cache hashtable in */
-       SearchPathCacheContext = AllocSetContextCreate(TopMemoryContext,
-                                                      "search_path processing cache",
-                                                      ALLOCSET_DEFAULT_SIZES);
-
        /*
         * In normal mode, arrange for a callback on any syscache invalidation
         * that will affect the search_path cache.