]> granicus.if.org Git - postgresql/blobdiff - src/backend/utils/init/miscinit.c
Repair some REINDEX problems per recent discussions. The relcache is
[postgresql] / src / backend / utils / init / miscinit.c
index 8f20f25dc19dff4b467a4d6ef28863a212224c44..22baac3706f6e4f2ae21afdda46578235509386f 100644 (file)
@@ -3,12 +3,12 @@
  * miscinit.c
  *       miscellaneous initialization support stuff
  *
- * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.108 2003/07/28 00:09:16 tgl Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.114 2003/09/24 18:54:01 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -48,16 +48,14 @@ ProcessingMode Mode = InitProcessing;
 static char directoryLockFile[MAXPGPATH];
 static char socketLockFile[MAXPGPATH];
 
-#ifdef CYR_RECODE
-static unsigned char RecodeForwTable[128];
-static unsigned char RecodeBackTable[128];
-
-static void GetCharSetByHost(char *TableName, int host, const char *DataDir);
-#endif
-
 
 /* ----------------------------------------------------------------
  *             ignoring system indexes support stuff
+ *
+ * NOTE: "ignoring system indexes" means we do not use the system indexes
+ * for lookups (either in hardwired catalog accesses or in planner-generated
+ * plans).  We do, however, still update the indexes when a catalog
+ * modification is made.
  * ----------------------------------------------------------------
  */
 
@@ -68,15 +66,14 @@ static bool isIgnoringSystemIndexes = false;
  *             True if ignoring system indexes.
  */
 bool
-IsIgnoringSystemIndexes()
+IsIgnoringSystemIndexes(void)
 {
        return isIgnoringSystemIndexes;
 }
 
 /*
  * IgnoreSystemIndexes
- *     Set true or false whether PostgreSQL ignores system indexes.
- *
+ *             Set true or false whether PostgreSQL ignores system indexes.
  */
 void
 IgnoreSystemIndexes(bool mode)
@@ -84,6 +81,53 @@ IgnoreSystemIndexes(bool mode)
        isIgnoringSystemIndexes = mode;
 }
 
+/* ----------------------------------------------------------------
+ *             system index reindexing support
+ *
+ * When we are busy reindexing a system index, this code provides support
+ * for preventing catalog lookups from using that index.
+ * ----------------------------------------------------------------
+ */
+
+static Oid     currentlyReindexedHeap = InvalidOid;
+static Oid     currentlyReindexedIndex = InvalidOid;
+
+/*
+ * ReindexIsProcessingHeap
+ *             True if heap specified by OID is currently being reindexed.
+ */
+bool
+ReindexIsProcessingHeap(Oid heapOid)
+{
+       return heapOid == currentlyReindexedHeap;
+}
+
+/*
+ * ReindexIsProcessingIndex
+ *             True if index specified by OID is currently being reindexed.
+ */
+bool
+ReindexIsProcessingIndex(Oid indexOid)
+{
+       return indexOid == currentlyReindexedIndex;
+}
+
+/*
+ * SetReindexProcessing
+ *             Set flag that specified heap/index are being reindexed.
+ *             Pass InvalidOid to indicate that reindexing is not active.
+ */
+void
+SetReindexProcessing(Oid heapOid, Oid indexOid)
+{
+       /* Args should be both, or neither, InvalidOid */
+       Assert((heapOid == InvalidOid) == (indexOid == InvalidOid));
+       /* Reindexing is not re-entrant. */
+       Assert(indexOid == InvalidOid || currentlyReindexedIndex == InvalidOid);
+       currentlyReindexedHeap = heapOid;
+       currentlyReindexedIndex = indexOid;
+}
+
 /* ----------------------------------------------------------------
  *                             database path / name support stuff
  * ----------------------------------------------------------------
@@ -181,295 +225,6 @@ SetDataDir(const char *dir)
        DataDir = new;
 }
 
-/* ----------------------------------------------------------------
- *                             CYR_RECODE support
- * ----------------------------------------------------------------
- */
-
-#ifdef CYR_RECODE
-
-void
-SetCharSet(void)
-{
-       FILE       *file;
-       char       *filename;
-       char       *map_file;
-       char            buf[MAX_TOKEN];
-       int                     i;
-       unsigned char FromChar,
-                               ToChar;
-       char            ChTable[MAX_TOKEN];
-
-       for (i = 0; i < 128; i++)
-       {
-               RecodeForwTable[i] = i + 128;
-               RecodeBackTable[i] = i + 128;
-       }
-
-       if (IsUnderPostmaster)
-       {
-               GetCharSetByHost(ChTable, MyProcPort->raddr.in.sin_addr.s_addr, DataDir);
-               filename = ChTable;
-       }
-       else
-               filename = getenv("PG_RECODETABLE");
-
-       if (filename && *filename != '\0')
-       {
-               map_file = palloc(strlen(DataDir) + strlen(filename) + 2);
-               sprintf(map_file, "%s/%s", DataDir, filename);
-               file = AllocateFile(map_file, "r");
-               pfree(map_file);
-               if (file == NULL)
-                       return;
-
-               while (!feof(file))
-               {
-                       next_token(file, buf, sizeof(buf));
-                       if (buf[0] != '\0')
-                       {
-                               FromChar = strtoul(buf, 0, 0);
-                               /* Read the ToChar */
-                               next_token(file, buf, sizeof(buf));
-                               if (buf[0] != '\0')
-                               {
-                                       ToChar = strtoul(buf, 0, 0);
-                                       RecodeForwTable[FromChar - 128] = ToChar;
-                                       RecodeBackTable[ToChar - 128] = FromChar;
-
-                                       /* read to EOL */
-                                       while (!feof(file) && buf[0])
-                                       {
-                                               next_token(file, buf, sizeof(buf));
-                                               elog(LOG, "unexpected token %s in file %s",
-                                                        buf, filename);
-                                       }
-                               }
-                       }
-               }
-               FreeFile(file);
-       }
-}
-
-
-char *
-convertstr(unsigned char *buff, int len, int dest)
-{
-       int                     i;
-       char       *ch = buff;
-
-       for (i = 0; i < len; i++, buff++)
-       {
-               if (*buff > 127)
-               {
-                       if (dest)
-                               *buff = RecodeForwTable[*buff - 128];
-                       else
-                               *buff = RecodeBackTable[*buff - 128];
-               }
-       }
-       return ch;
-}
-
-#define CHARSET_FILE "charset.conf"
-#define MAX_CHARSETS   10
-#define KEY_HOST          1
-#define KEY_BASE          2
-#define KEY_TABLE         3
-
-struct CharsetItem
-{
-       char            Orig[MAX_TOKEN];
-       char            Dest[MAX_TOKEN];
-       char            Table[MAX_TOKEN];
-};
-
-
-static bool
-CharSetInRange(char *buf, int host)
-{
-       int                     valid,
-                               i,
-                               FromAddr,
-                               ToAddr,
-                               tmp;
-       struct in_addr file_ip_addr;
-       char       *p;
-       unsigned int one = 0x80000000,
-                               NetMask = 0;
-       unsigned char mask;
-
-       p = strchr(buf, '/');
-       if (p)
-       {
-               *p++ = '\0';
-               valid = inet_aton(buf, &file_ip_addr);
-               if (valid)
-               {
-                       mask = strtoul(p, 0, 0);
-                       FromAddr = ntohl(file_ip_addr.s_addr);
-                       ToAddr = ntohl(file_ip_addr.s_addr);
-                       for (i = 0; i < mask; i++)
-                       {
-                               NetMask |= one;
-                               one >>= 1;
-                       }
-                       FromAddr &= NetMask;
-                       ToAddr = ToAddr | ~NetMask;
-                       tmp = ntohl(host);
-                       return ((unsigned) tmp >= (unsigned) FromAddr &&
-                                       (unsigned) tmp <= (unsigned) ToAddr);
-               }
-       }
-       else
-       {
-               p = strchr(buf, '-');
-               if (p)
-               {
-                       *p++ = '\0';
-                       valid = inet_aton(buf, &file_ip_addr);
-                       if (valid)
-                       {
-                               FromAddr = ntohl(file_ip_addr.s_addr);
-                               valid = inet_aton(p, &file_ip_addr);
-                               if (valid)
-                               {
-                                       ToAddr = ntohl(file_ip_addr.s_addr);
-                                       tmp = ntohl(host);
-                                       return ((unsigned) tmp >= (unsigned) FromAddr &&
-                                                       (unsigned) tmp <= (unsigned) ToAddr);
-                               }
-                       }
-               }
-               else
-               {
-                       valid = inet_aton(buf, &file_ip_addr);
-                       if (valid)
-                       {
-                               FromAddr = file_ip_addr.s_addr;
-                               return (unsigned) FromAddr == (unsigned) host;
-                       }
-               }
-       }
-       return false;
-}
-
-
-static void
-GetCharSetByHost(char *TableName, int host, const char *DataDir)
-{
-       FILE       *file;
-       char            buf[MAX_TOKEN],
-                               BaseCharset[MAX_TOKEN],
-                               OrigCharset[MAX_TOKEN],
-                               DestCharset[MAX_TOKEN],
-                               HostCharset[MAX_TOKEN],
-                          *map_file;
-       int                     key,
-                               ChIndex = 0,
-                               i,
-                               bufsize;
-       struct CharsetItem *ChArray[MAX_CHARSETS];
-
-       *TableName = '\0';
-       bufsize = (strlen(DataDir) + strlen(CHARSET_FILE) + 2) * sizeof(char);
-       map_file = (char *) palloc(bufsize);
-       snprintf(map_file, bufsize, "%s/%s", DataDir, CHARSET_FILE);
-       file = AllocateFile(map_file, "r");
-       pfree(map_file);
-       if (file == NULL)
-       {
-               /* XXX should we log a complaint? */
-               return;
-       }
-
-       while (!feof(file))
-       {
-               next_token(file, buf, sizeof(buf));
-               if (buf[0] != '\0')
-               {
-                       key = 0;
-                       if (strcasecmp(buf, "HostCharset") == 0)
-                               key = KEY_HOST;
-                       else if (strcasecmp(buf, "BaseCharset") == 0)
-                               key = KEY_BASE;
-                       else if (strcasecmp(buf, "RecodeTable") == 0)
-                               key = KEY_TABLE;
-                       else
-                               elog(LOG, "unrecognized tag %s in file %s",
-                                        buf, CHARSET_FILE);
-
-                       switch (key)
-                       {
-                               case KEY_HOST:
-                                       /* Read the host */
-                                       next_token(file, buf, sizeof(buf));
-                                       if (buf[0] != '\0')
-                                       {
-                                               if (CharSetInRange(buf, host))
-                                               {
-                                                       /* Read the charset */
-                                                       next_token(file, buf, sizeof(buf));
-                                                       if (buf[0] != '\0')
-                                                               strcpy(HostCharset, buf);
-                                               }
-                                       }
-                                       break;
-                               case KEY_BASE:
-                                       /* Read the base charset */
-                                       next_token(file, buf, sizeof(buf));
-                                       if (buf[0] != '\0')
-                                               strcpy(BaseCharset, buf);
-                                       break;
-                               case KEY_TABLE:
-                                       /* Read the original charset */
-                                       next_token(file, buf, sizeof(buf));
-                                       if (buf[0] != '\0')
-                                       {
-                                               strcpy(OrigCharset, buf);
-                                               /* Read the destination charset */
-                                               next_token(file, buf, sizeof(buf));
-                                               if (buf[0] != '\0')
-                                               {
-                                                       strcpy(DestCharset, buf);
-                                                       /* Read the table filename */
-                                                       next_token(file, buf, sizeof(buf));
-                                                       if (buf[0] != '\0')
-                                                       {
-                                                               ChArray[ChIndex] =
-                                                                       (struct CharsetItem *) palloc(sizeof(struct CharsetItem));
-                                                               strcpy(ChArray[ChIndex]->Orig, OrigCharset);
-                                                               strcpy(ChArray[ChIndex]->Dest, DestCharset);
-                                                               strcpy(ChArray[ChIndex]->Table, buf);
-                                                               ChIndex++;
-                                                       }
-                                               }
-                                       }
-                                       break;
-                       }
-
-                       /* read to EOL */
-                       while (!feof(file) && buf[0])
-                       {
-                               next_token(file, buf, sizeof(buf));
-                               elog(LOG, "unrecognized tag %s in file %s",
-                                        buf, CHARSET_FILE);
-                       }
-               }
-       }
-       FreeFile(file);
-
-       for (i = 0; i < ChIndex; i++)
-       {
-               if (strcasecmp(BaseCharset, ChArray[i]->Orig) == 0 &&
-                       strcasecmp(HostCharset, ChArray[i]->Dest) == 0)
-                       strncpy(TableName, ChArray[i]->Table, 79);
-               pfree(ChArray[i]);
-       }
-}
-#endif   /* CYR_RECODE */
-
-
 
 /* ----------------------------------------------------------------
  *     User ID things
@@ -482,9 +237,9 @@ GetCharSetByHost(char *TableName, int host, const char *DataDir)
  * restore the current user id if you need to change it.
  * ----------------------------------------------------------------
  */
-static AclId   AuthenticatedUserId = 0;
-static AclId   SessionUserId = 0;
-static AclId   CurrentUserId = 0;
+static AclId AuthenticatedUserId = 0;
+static AclId SessionUserId = 0;
+static AclId CurrentUserId = 0;
 
 static bool AuthenticatedUserIsSuperuser = false;
 
@@ -620,7 +375,7 @@ SetSessionAuthorization(AclId userid, bool is_superuser)
                !AuthenticatedUserIsSuperuser)
                ereport(ERROR,
                                (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
-                                errmsg("permission denied")));
+                         errmsg("permission denied to set session authorization")));
 
        SetSessionUserId(userid);
        SetUserId(userid);
@@ -685,15 +440,16 @@ GetUserNameFromId(AclId userid)
 static void
 UnlinkLockFile(int status, Datum filename)
 {
-  char *fname = (char *)DatumGetPointer(filename);
-  if( fname != NULL )
-    {
-      if( unlink(fname) != 0 )
+       char       *fname = (char *) DatumGetPointer(filename);
+
+       if (fname != NULL)
        {
-         /* Should we complain if the unlink fails? */
+               if (unlink(fname) != 0)
+               {
+                       /* Should we complain if the unlink fails? */
+               }
+               free(fname);
        }
-      free(fname);
-    }
 }
 
 /*
@@ -789,11 +545,11 @@ CreateLockFile(const char *filename, bool amPostmaster,
                                                 errmsg("lock file \"%s\" already exists",
                                                                filename),
                                                 isDDLock ?
-                                                errhint("Is another %s (pid %d) running in \"%s\"?",
-                                                                (encoded_pid < 0 ? "postgres" : "postmaster"),
-                                                                (int) other_pid, refName) :
+                                        errhint("Is another %s (pid %d) running in \"%s\"?",
+                                                  (encoded_pid < 0 ? "postgres" : "postmaster"),
+                                                        (int) other_pid, refName) :
                                                 errhint("Is another %s (pid %d) using \"%s\"?",
-                                                                (encoded_pid < 0 ? "postgres" : "postmaster"),
+                                                  (encoded_pid < 0 ? "postgres" : "postmaster"),
                                                                 (int) other_pid, refName)));
                        }
                }
@@ -821,14 +577,14 @@ CreateLockFile(const char *filename, bool amPostmaster,
                                        if (PGSharedMemoryIsInUse(id1, id2))
                                                ereport(FATAL,
                                                                (errcode(ERRCODE_LOCK_FILE_EXISTS),
-                                                                errmsg("pre-existing shared memory block "
-                                                                               "(key %lu, id %lu) is still in use",
-                                                                               id1, id2),
-                                                                errhint("If you're sure there are no old "
-                                                                                "backends still running, remove "
-                                                                                "the shared memory block with "
-                                                                                "ipcrm(1), or just delete \"%s\".",
-                                                                                filename)));
+                                                          errmsg("pre-existing shared memory block "
+                                                                         "(key %lu, id %lu) is still in use",
+                                                                         id1, id2),
+                                                          errhint("If you're sure there are no old "
+                                                                          "backends still running, remove "
+                                                                          "the shared memory block with "
+                                                                          "ipcrm(1), or just delete \"%s\".",
+                                                                          filename)));
                                }
                        }
                }
@@ -844,7 +600,7 @@ CreateLockFile(const char *filename, bool amPostmaster,
                                         errmsg("could not remove old lock file \"%s\": %m",
                                                        filename),
                                         errhint("The file seems accidentally left over, but "
-                                                        "I couldn't remove it. Please remove the file "
+                                                 "I couldn't remove it. Please remove the file "
                                                         "by hand and try again.")));
        }
 
@@ -865,7 +621,7 @@ CreateLockFile(const char *filename, bool amPostmaster,
                errno = save_errno ? save_errno : ENOSPC;
                ereport(FATAL,
                                (errcode_for_file_access(),
-                                errmsg("could not write lock file \"%s\": %m", filename)));
+                         errmsg("could not write lock file \"%s\": %m", filename)));
        }
        close(fd);
 
@@ -912,17 +668,17 @@ TouchSocketLockFile(void)
        if (socketLockFile[0] != '\0')
        {
                /*
-                * utime() is POSIX standard, utimes() is a common alternative;
-                * if we have neither, fall back to actually reading the file
-                * (which only sets the access time not mod time, but that should
-                * be enough in most cases).  In all paths, we ignore errors.
+                * utime() is POSIX standard, utimes() is a common alternative; if
+                * we have neither, fall back to actually reading the file (which
+                * only sets the access time not mod time, but that should be
+                * enough in most cases).  In all paths, we ignore errors.
                 */
 #ifdef HAVE_UTIME
                utime(socketLockFile, NULL);
-#else /* !HAVE_UTIME */
+#else                                                  /* !HAVE_UTIME */
 #ifdef HAVE_UTIMES
                utimes(socketLockFile, NULL);
-#else /* !HAVE_UTIMES */
+#else                                                  /* !HAVE_UTIMES */
                int                     fd;
                char            buffer[1];
 
@@ -932,8 +688,8 @@ TouchSocketLockFile(void)
                        read(fd, buffer, sizeof(buffer));
                        close(fd);
                }
-#endif /* HAVE_UTIMES */
-#endif /* HAVE_UTIME */
+#endif   /* HAVE_UTIMES */
+#endif   /* HAVE_UTIME */
        }
 }
 
@@ -1072,13 +828,13 @@ ValidatePgVersion(const char *path)
 
        ret = fscanf(file, "%ld.%ld", &file_major, &file_minor);
        if (ret != 2)
-                       ereport(FATAL,
-                                       (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                                        errmsg("\"%s\" is not a valid data directory",
-                                                       path),
-                                        errdetail("File \"%s\" does not contain valid data.",
-                                                          full_path),
-                                        errhint("You may need to initdb.")));
+               ereport(FATAL,
+                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                errmsg("\"%s\" is not a valid data directory",
+                                               path),
+                                errdetail("File \"%s\" does not contain valid data.",
+                                                  full_path),
+                                errhint("You may need to initdb.")));
 
        FreeFile(file);
 
@@ -1087,7 +843,7 @@ ValidatePgVersion(const char *path)
                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                 errmsg("database files are incompatible with server"),
                                 errdetail("The data directory was initialized by PostgreSQL version %ld.%ld, "
-                                                  "which is not compatible with this version %s.",
+                                                "which is not compatible with this version %s.",
                                                   file_major, file_minor, version_string)));
 }
 
@@ -1098,6 +854,7 @@ ValidatePgVersion(const char *path)
 
 #if defined(__mc68000__) && defined(__ELF__)
 typedef int32 ((*func_ptr) ());
+
 #else
 typedef char *((*func_ptr) ());
 #endif
@@ -1142,8 +899,9 @@ process_preload_libraries(char *preload_libraries_string)
                if (sep)
                {
                        /*
-                        * a colon separator implies there is an initialization function
-                        * that we need to run in addition to loading the library
+                        * a colon separator implies there is an initialization
+                        * function that we need to run in addition to loading the
+                        * library
                         */
                        size_t          filename_len = sep - tok;
                        size_t          funcname_len = strlen(tok) - filename_len - 1;
@@ -1165,9 +923,9 @@ process_preload_libraries(char *preload_libraries_string)
                }
 
                initfunc = (func_ptr) load_external_function(filename, funcname,
-                                                                                                        false, NULL);
+                                                                                                        true, NULL);
                if (initfunc)
-                       (*initfunc)();
+                       (*initfunc) ();
 
                if (funcname)
                        ereport(LOG,
@@ -1186,4 +944,3 @@ process_preload_libraries(char *preload_libraries_string)
        pfree(rawstring);
        freeList(elemlist);
 }
-