LCOV - code coverage report
Current view: top level - src/backend/bootstrap - bootstrap.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 85.7 % 343 294
Test Date: 2026-11-25 14:16:11 Functions: 100.0 % 18 18
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 56.4 % 220 124

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * bootstrap.c
       4                 :             :  *    routines to support running postgres in 'bootstrap' mode
       5                 :             :  *  bootstrap mode is used to create the initial template database
       6                 :             :  *
       7                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       8                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       9                 :             :  *
      10                 :             :  * IDENTIFICATION
      11                 :             :  *    src/backend/bootstrap/bootstrap.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : #include "postgres.h"
      16                 :             : 
      17                 :             : #include <unistd.h>
      18                 :             : #include <signal.h>
      19                 :             : 
      20                 :             : #include "access/genam.h"
      21                 :             : #include "access/heapam.h"
      22                 :             : #include "access/htup_details.h"
      23                 :             : #include "access/tableam.h"
      24                 :             : #include "access/toast_compression.h"
      25                 :             : #include "access/xact.h"
      26                 :             : #include "bootstrap/bootstrap.h"
      27                 :             : #include "catalog/index.h"
      28                 :             : #include "catalog/pg_authid.h"
      29                 :             : #include "catalog/pg_collation.h"
      30                 :             : #include "catalog/pg_proc.h"
      31                 :             : #include "catalog/pg_type.h"
      32                 :             : #include "common/link-canary.h"
      33                 :             : #include "miscadmin.h"
      34                 :             : #include "nodes/makefuncs.h"
      35                 :             : #include "port/pg_getopt_ctx.h"
      36                 :             : #include "postmaster/postmaster.h"
      37                 :             : #include "storage/bufpage.h"
      38                 :             : #include "storage/checksum.h"
      39                 :             : #include "storage/fd.h"
      40                 :             : #include "storage/ipc.h"
      41                 :             : #include "storage/proc.h"
      42                 :             : #include "storage/shmem_internal.h"
      43                 :             : #include "utils/builtins.h"
      44                 :             : #include "utils/fmgroids.h"
      45                 :             : #include "utils/guc.h"
      46                 :             : #include "utils/memutils.h"
      47                 :             : #include "utils/rel.h"
      48                 :             : #include "utils/relmapper.h"
      49                 :             : 
      50                 :             : 
      51                 :             : static void CheckerModeMain(void);
      52                 :             : static void bootstrap_signals(void);
      53                 :             : static Form_pg_attribute AllocateAttribute(void);
      54                 :             : static void InsertOneProargdefaultsValue(const char *value);
      55                 :             : static void populate_typ_list(void);
      56                 :             : static Oid  gettype(char *type);
      57                 :             : static void cleanup(void);
      58                 :             : 
      59                 :             : /* ----------------
      60                 :             :  *      global variables
      61                 :             :  * ----------------
      62                 :             :  */
      63                 :             : 
      64                 :             : Relation    boot_reldesc;       /* current relation descriptor */
      65                 :             : 
      66                 :             : Form_pg_attribute attrtypes[MAXATTR];   /* points to attribute info */
      67                 :             : int         numattr;            /* number of attributes for cur. rel */
      68                 :             : 
      69                 :             : 
      70                 :             : /*
      71                 :             :  * Basic information associated with each type.  This is used before
      72                 :             :  * pg_type is filled, so it has to cover the datatypes used as column types
      73                 :             :  * in the core "bootstrapped" catalogs.
      74                 :             :  *
      75                 :             :  *      XXX several of these input/output functions do catalog scans
      76                 :             :  *          (e.g., F_REGPROCIN scans pg_proc).  this obviously creates some
      77                 :             :  *          order dependencies in the catalog creation process.
      78                 :             :  */
      79                 :             : struct typinfo
      80                 :             : {
      81                 :             :     char        name[NAMEDATALEN];
      82                 :             :     Oid         oid;
      83                 :             :     Oid         elem;
      84                 :             :     int16       len;
      85                 :             :     bool        byval;
      86                 :             :     char        align;
      87                 :             :     char        storage;
      88                 :             :     Oid         collation;
      89                 :             :     Oid         inproc;
      90                 :             :     Oid         outproc;
      91                 :             : };
      92                 :             : 
      93                 :             : static const struct typinfo TypInfo[] = {
      94                 :             :     {"bool", BOOLOID, 0, 1, true, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
      95                 :             :     F_BOOLIN, F_BOOLOUT},
      96                 :             :     {"bytea", BYTEAOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
      97                 :             :     F_BYTEAIN, F_BYTEAOUT},
      98                 :             :     {"char", CHAROID, 0, 1, true, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
      99                 :             :     F_CHARIN, F_CHAROUT},
     100                 :             :     {"cstring", CSTRINGOID, 0, -2, false, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
     101                 :             :     F_CSTRING_IN, F_CSTRING_OUT},
     102                 :             :     {"int2", INT2OID, 0, 2, true, TYPALIGN_SHORT, TYPSTORAGE_PLAIN, InvalidOid,
     103                 :             :     F_INT2IN, F_INT2OUT},
     104                 :             :     {"int4", INT4OID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     105                 :             :     F_INT4IN, F_INT4OUT},
     106                 :             :     {"int8", INT8OID, 0, 8, true, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
     107                 :             :     F_INT8IN, F_INT8OUT},
     108                 :             :     {"float4", FLOAT4OID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     109                 :             :     F_FLOAT4IN, F_FLOAT4OUT},
     110                 :             :     {"float8", FLOAT8OID, 0, 8, true, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
     111                 :             :     F_FLOAT8IN, F_FLOAT8OUT},
     112                 :             :     {"name", NAMEOID, CHAROID, NAMEDATALEN, false, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, C_COLLATION_OID,
     113                 :             :     F_NAMEIN, F_NAMEOUT},
     114                 :             :     {"regproc", REGPROCOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     115                 :             :     F_REGPROCIN, F_REGPROCOUT},
     116                 :             :     {"text", TEXTOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, DEFAULT_COLLATION_OID,
     117                 :             :     F_TEXTIN, F_TEXTOUT},
     118                 :             :     {"jsonb", JSONBOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
     119                 :             :     F_JSONB_IN, F_JSONB_OUT},
     120                 :             :     {"oid", OIDOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     121                 :             :     F_OIDIN, F_OIDOUT},
     122                 :             :     {"pg_lsn", PG_LSNOID, 0, 8, true, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
     123                 :             :     F_PG_LSN_IN, F_PG_LSN_OUT},
     124                 :             :     {"aclitem", ACLITEMOID, 0, 16, false, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
     125                 :             :     F_ACLITEMIN, F_ACLITEMOUT},
     126                 :             :     {"pg_node_tree", PG_NODE_TREEOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, DEFAULT_COLLATION_OID,
     127                 :             :     F_PG_NODE_TREE_IN, F_PG_NODE_TREE_OUT},
     128                 :             :     {"int2vector", INT2VECTOROID, INT2OID, -1, false, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     129                 :             :     F_INT2VECTORIN, F_INT2VECTOROUT},
     130                 :             :     {"oidvector", OIDVECTOROID, OIDOID, -1, false, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     131                 :             :     F_OIDVECTORIN, F_OIDVECTOROUT},
     132                 :             :     {"_int4", INT4ARRAYOID, INT4OID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
     133                 :             :     F_ARRAY_IN, F_ARRAY_OUT},
     134                 :             :     {"_text", TEXTARRAYOID, TEXTOID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, DEFAULT_COLLATION_OID,
     135                 :             :     F_ARRAY_IN, F_ARRAY_OUT},
     136                 :             :     {"_oid", OIDARRAYOID, OIDOID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
     137                 :             :     F_ARRAY_IN, F_ARRAY_OUT},
     138                 :             :     {"_char", CHARARRAYOID, CHAROID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
     139                 :             :     F_ARRAY_IN, F_ARRAY_OUT},
     140                 :             :     {"_aclitem", ACLITEMARRAYOID, ACLITEMOID, -1, false, TYPALIGN_DOUBLE, TYPSTORAGE_EXTENDED, InvalidOid,
     141                 :             :     F_ARRAY_IN, F_ARRAY_OUT}
     142                 :             : };
     143                 :             : 
     144                 :             : static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
     145                 :             : 
     146                 :             : struct typmap
     147                 :             : {                               /* a hack */
     148                 :             :     Oid         am_oid;
     149                 :             :     FormData_pg_type am_typ;
     150                 :             : };
     151                 :             : 
     152                 :             : static List *Typ = NIL;         /* List of struct typmap* */
     153                 :             : static struct typmap *Ap = NULL;
     154                 :             : 
     155                 :             : /*
     156                 :             :  * Basic information about built-in roles.
     157                 :             :  *
     158                 :             :  * Presently, this need only list roles that are mentioned in aclitem arrays
     159                 :             :  * in the catalog .dat files.  We might as well list everything that is in
     160                 :             :  * pg_authid.dat, since there aren't that many.  Like pg_authid.dat, we
     161                 :             :  * represent the bootstrap superuser's name as "POSTGRES", even though it
     162                 :             :  * (probably) won't be that in the finished installation; this means aclitem
     163                 :             :  * entries in .dat files must spell it like that.
     164                 :             :  */
     165                 :             : struct rolinfo
     166                 :             : {
     167                 :             :     const char *rolname;
     168                 :             :     Oid         oid;
     169                 :             : };
     170                 :             : 
     171                 :             : static const struct rolinfo RolInfo[] = {
     172                 :             :     {"POSTGRES", BOOTSTRAP_SUPERUSERID},
     173                 :             :     {"pg_database_owner", ROLE_PG_DATABASE_OWNER},
     174                 :             :     {"pg_read_all_data", ROLE_PG_READ_ALL_DATA},
     175                 :             :     {"pg_write_all_data", ROLE_PG_WRITE_ALL_DATA},
     176                 :             :     {"pg_monitor", ROLE_PG_MONITOR},
     177                 :             :     {"pg_read_all_settings", ROLE_PG_READ_ALL_SETTINGS},
     178                 :             :     {"pg_read_all_stats", ROLE_PG_READ_ALL_STATS},
     179                 :             :     {"pg_stat_scan_tables", ROLE_PG_STAT_SCAN_TABLES},
     180                 :             :     {"pg_read_server_files", ROLE_PG_READ_SERVER_FILES},
     181                 :             :     {"pg_write_server_files", ROLE_PG_WRITE_SERVER_FILES},
     182                 :             :     {"pg_execute_server_program", ROLE_PG_EXECUTE_SERVER_PROGRAM},
     183                 :             :     {"pg_signal_backend", ROLE_PG_SIGNAL_BACKEND},
     184                 :             :     {"pg_checkpoint", ROLE_PG_CHECKPOINT},
     185                 :             :     {"pg_maintain", ROLE_PG_MAINTAIN},
     186                 :             :     {"pg_use_reserved_connections", ROLE_PG_USE_RESERVED_CONNECTIONS},
     187                 :             :     {"pg_create_subscription", ROLE_PG_CREATE_SUBSCRIPTION},
     188                 :             :     {"pg_signal_autovacuum_worker", ROLE_PG_SIGNAL_AUTOVACUUM_WORKER}
     189                 :             : };
     190                 :             : 
     191                 :             : 
     192                 :             : static Datum values[MAXATTR];   /* current row's attribute values */
     193                 :             : static bool Nulls[MAXATTR];
     194                 :             : 
     195                 :             : static MemoryContext nogc = NULL;   /* special no-gc mem context */
     196                 :             : 
     197                 :             : /*
     198                 :             :  *  At bootstrap time, we first declare all the indices to be built, and
     199                 :             :  *  then build them.  The IndexList structure stores enough information
     200                 :             :  *  to allow us to build the indices after they've been declared.
     201                 :             :  */
     202                 :             : 
     203                 :             : typedef struct _IndexList
     204                 :             : {
     205                 :             :     Oid         il_heap;
     206                 :             :     Oid         il_ind;
     207                 :             :     IndexInfo  *il_info;
     208                 :             :     struct _IndexList *il_next;
     209                 :             : } IndexList;
     210                 :             : 
     211                 :             : static IndexList *ILHead = NULL;
     212                 :             : 
     213                 :             : 
     214                 :             : /*
     215                 :             :  * In shared memory checker mode, all we really want to do is create shared
     216                 :             :  * memory and semaphores (just to prove we can do it with the current GUC
     217                 :             :  * settings).  Since, in fact, that was already done by
     218                 :             :  * CreateSharedMemoryAndSemaphores(), we have nothing more to do here.
     219                 :             :  */
     220                 :             : static void
     221                 :         122 : CheckerModeMain(void)
     222                 :             : {
     223                 :         122 :     proc_exit(0);
     224                 :             : }
     225                 :             : 
     226                 :             : /*
     227                 :             :  *   The main entry point for running the backend in bootstrap mode
     228                 :             :  *
     229                 :             :  *   The bootstrap mode is used to initialize the template database.
     230                 :             :  *   The bootstrap backend doesn't speak SQL, but instead expects
     231                 :             :  *   commands in a special bootstrap language.
     232                 :             :  *
     233                 :             :  *   When check_only is true, startup is done only far enough to verify that
     234                 :             :  *   the current configuration, particularly the passed in options pertaining
     235                 :             :  *   to shared memory sizing, options work (or at least do not cause an error
     236                 :             :  *   up to shared memory creation).
     237                 :             :  */
     238                 :             : void
     239                 :         208 : BootstrapModeMain(int argc, char *argv[], bool check_only)
     240                 :             : {
     241                 :             :     int         i;
     242                 :         208 :     char       *progname = argv[0];
     243                 :             :     pg_getopt_ctx optctx;
     244                 :             :     int         flag;
     245                 :         208 :     char       *userDoption = NULL;
     246                 :         208 :     uint32      bootstrap_data_checksum_version = PG_DATA_CHECKSUM_OFF;
     247                 :             :     yyscan_t    scanner;
     248                 :             : 
     249                 :             :     Assert(!IsUnderPostmaster);
     250                 :             : 
     251                 :         208 :     InitStandaloneProcess(argv[0]);
     252                 :             : 
     253                 :             :     /* Set defaults, to be overridden by explicit options below */
     254                 :         208 :     InitializeGUCOptions();
     255                 :             : 
     256                 :             :     /* an initial --boot or --check should be present */
     257                 :             :     Assert(argc > 1
     258                 :             :            && (strcmp(argv[1], "--boot") == 0
     259                 :             :                || strcmp(argv[1], "--check") == 0));
     260                 :         208 :     argv++;
     261                 :         208 :     argc--;
     262                 :             : 
     263                 :         208 :     pg_getopt_start(&optctx, argc, argv, "B:c:d:D:Fkr:X:-:");
     264         [ +  + ]:        1332 :     while ((flag = pg_getopt_next(&optctx)) != -1)
     265                 :             :     {
     266   [ -  -  +  -  :        1148 :         switch (flag)
          -  +  +  -  +  
                      - ]
     267                 :             :         {
     268                 :           0 :             case 'B':
     269                 :           0 :                 SetConfigOption("shared_buffers", optctx.optarg, PGC_POSTMASTER, PGC_S_ARGV);
     270                 :           0 :                 break;
     271                 :           0 :             case '-':
     272                 :             : 
     273                 :             :                 /*
     274                 :             :                  * Error if the user misplaced a special must-be-first option
     275                 :             :                  * for dispatching to a subprogram.  parse_dispatch_option()
     276                 :             :                  * returns DISPATCH_POSTMASTER if it doesn't find a match, so
     277                 :             :                  * error for anything else.
     278                 :             :                  */
     279         [ #  # ]:           0 :                 if (parse_dispatch_option(optctx.optarg) != DISPATCH_POSTMASTER)
     280         [ #  # ]:           0 :                     ereport(ERROR,
     281                 :             :                             (errcode(ERRCODE_SYNTAX_ERROR),
     282                 :             :                              errmsg("--%s must be first argument", optctx.optarg)));
     283                 :             : 
     284                 :             :                 pg_fallthrough;
     285                 :             :             case 'c':
     286                 :             :                 {
     287                 :             :                     char       *name,
     288                 :             :                                *value;
     289                 :             : 
     290                 :         830 :                     ParseLongOption(optctx.optarg, &name, &value);
     291         [ -  + ]:         830 :                     if (!value)
     292                 :             :                     {
     293         [ #  # ]:           0 :                         if (flag == '-')
     294         [ #  # ]:           0 :                             ereport(ERROR,
     295                 :             :                                     (errcode(ERRCODE_SYNTAX_ERROR),
     296                 :             :                                      errmsg("--%s requires a value",
     297                 :             :                                             optctx.optarg)));
     298                 :             :                         else
     299         [ #  # ]:           0 :                             ereport(ERROR,
     300                 :             :                                     (errcode(ERRCODE_SYNTAX_ERROR),
     301                 :             :                                      errmsg("-c %s requires a value",
     302                 :             :                                             optctx.optarg)));
     303                 :             :                     }
     304                 :             : 
     305                 :         830 :                     SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
     306                 :         806 :                     pfree(name);
     307                 :         806 :                     pfree(value);
     308                 :         806 :                     break;
     309                 :             :                 }
     310                 :           0 :             case 'D':
     311                 :           0 :                 userDoption = pstrdup(optctx.optarg);
     312                 :           0 :                 break;
     313                 :           0 :             case 'd':
     314                 :             :                 {
     315                 :             :                     /* Turn on debugging for the bootstrap process. */
     316                 :             :                     char       *debugstr;
     317                 :             : 
     318                 :           0 :                     debugstr = psprintf("debug%s", optctx.optarg);
     319                 :           0 :                     SetConfigOption("log_min_messages", debugstr,
     320                 :             :                                     PGC_POSTMASTER, PGC_S_ARGV);
     321                 :           0 :                     SetConfigOption("client_min_messages", debugstr,
     322                 :             :                                     PGC_POSTMASTER, PGC_S_ARGV);
     323                 :           0 :                     pfree(debugstr);
     324                 :             :                 }
     325                 :           0 :                 break;
     326                 :         208 :             case 'F':
     327                 :         208 :                 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
     328                 :         208 :                 break;
     329                 :          48 :             case 'k':
     330                 :          48 :                 bootstrap_data_checksum_version = PG_DATA_CHECKSUM_VERSION;
     331                 :          48 :                 break;
     332                 :           0 :             case 'r':
     333                 :           0 :                 strlcpy(OutputFileName, optctx.optarg, MAXPGPATH);
     334                 :           0 :                 break;
     335                 :          62 :             case 'X':
     336                 :          62 :                 SetConfigOption("wal_segment_size", optctx.optarg, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
     337                 :          62 :                 break;
     338                 :           0 :             default:
     339                 :           0 :                 write_stderr("Try \"%s --help\" for more information.\n",
     340                 :             :                              progname);
     341                 :           0 :                 proc_exit(1);
     342                 :             :                 break;
     343                 :             :         }
     344                 :             :     }
     345                 :             : 
     346         [ -  + ]:         184 :     if (argc != optctx.optind)
     347                 :             :     {
     348                 :           0 :         write_stderr("%s: invalid command-line arguments\n", progname);
     349                 :           0 :         proc_exit(1);
     350                 :             :     }
     351                 :             : 
     352                 :             :     /* Acquire configuration parameters */
     353         [ -  + ]:         184 :     if (!SelectConfigFiles(userDoption, progname))
     354                 :           0 :         proc_exit(1);
     355                 :             : 
     356                 :             :     /*
     357                 :             :      * Validate we have been given a reasonable-looking DataDir and change
     358                 :             :      * into it
     359                 :             :      */
     360                 :         183 :     checkDataDir();
     361                 :         183 :     ChangeToDataDir();
     362                 :             : 
     363                 :         183 :     CreateDataDirLockFile(false);
     364                 :             : 
     365                 :         183 :     SetProcessingMode(BootstrapProcessing);
     366                 :         183 :     IgnoreSystemIndexes = true;
     367                 :             : 
     368                 :         183 :     RegisterBuiltinShmemCallbacks();
     369                 :             : 
     370                 :         183 :     InitializeMaxBackends();
     371                 :             : 
     372                 :             :     /*
     373                 :             :      * Even though bootstrapping runs in single-process mode, initialize
     374                 :             :      * postmaster child slots array so that --check can detect running out of
     375                 :             :      * shared memory or other resources if max_connections is set too high.
     376                 :             :      */
     377                 :         183 :     InitPostmasterChildSlots();
     378                 :             : 
     379                 :         183 :     InitializeFastPathLocks();
     380                 :             : 
     381                 :         183 :     ShmemCallRequestCallbacks();
     382                 :         183 :     CreateSharedMemoryAndSemaphores();
     383                 :             : 
     384                 :             :     /*
     385                 :             :      * Estimate number of openable files.  This is essential too in --check
     386                 :             :      * mode, because on some platforms semaphores count as open files.
     387                 :             :      */
     388                 :         183 :     set_max_safe_fds();
     389                 :             : 
     390                 :             :     /*
     391                 :             :      * XXX: It might make sense to move this into its own function at some
     392                 :             :      * point. Right now it seems like it'd cause more code duplication than
     393                 :             :      * it's worth.
     394                 :             :      */
     395         [ +  + ]:         183 :     if (check_only)
     396                 :             :     {
     397                 :         122 :         SetProcessingMode(NormalProcessing);
     398                 :         122 :         CheckerModeMain();
     399                 :           0 :         abort();
     400                 :             :     }
     401                 :             : 
     402                 :             :     /*
     403                 :             :      * Do backend-like initialization for bootstrap mode
     404                 :             :      */
     405                 :          61 :     InitProcess();
     406                 :             : 
     407                 :          61 :     BaseInit();
     408                 :             : 
     409                 :          61 :     bootstrap_signals();
     410                 :          61 :     BootStrapXLOG(bootstrap_data_checksum_version);
     411                 :             : 
     412                 :             :     /*
     413                 :             :      * To ensure that src/common/link-canary.c is linked into the backend, we
     414                 :             :      * must call it from somewhere.  Here is as good as anywhere.
     415                 :             :      */
     416         [ -  + ]:          61 :     if (pg_link_canary_is_frontend())
     417         [ #  # ]:           0 :         elog(ERROR, "backend is incorrectly linked to frontend functions");
     418                 :             : 
     419                 :          61 :     InitPostgres(NULL, InvalidOid, NULL, InvalidOid, 0, NULL);
     420                 :             : 
     421                 :             :     /* Initialize stuff for bootstrap-file processing */
     422         [ +  + ]:        2501 :     for (i = 0; i < MAXATTR; i++)
     423                 :             :     {
     424                 :        2440 :         attrtypes[i] = NULL;
     425                 :        2440 :         Nulls[i] = false;
     426                 :             :     }
     427                 :             : 
     428         [ -  + ]:          61 :     if (boot_yylex_init(&scanner) != 0)
     429         [ #  # ]:           0 :         elog(ERROR, "yylex_init() failed: %m");
     430                 :             : 
     431                 :             :     /*
     432                 :             :      * Process bootstrap input.
     433                 :             :      */
     434                 :          61 :     StartTransactionCommand();
     435                 :          61 :     boot_yyparse(scanner);
     436                 :          61 :     CommitTransactionCommand();
     437                 :             : 
     438                 :             :     /*
     439                 :             :      * We should now know about all mapped relations, so it's okay to write
     440                 :             :      * out the initial relation mapping files.
     441                 :             :      */
     442                 :          61 :     RelationMapFinishBootstrap();
     443                 :             : 
     444                 :             :     /* Clean up and exit */
     445                 :          61 :     cleanup();
     446                 :          61 :     proc_exit(0);
     447                 :             : }
     448                 :             : 
     449                 :             : 
     450                 :             : /* ----------------------------------------------------------------
     451                 :             :  *                      misc functions
     452                 :             :  * ----------------------------------------------------------------
     453                 :             :  */
     454                 :             : 
     455                 :             : /*
     456                 :             :  * Set up signal handling for a bootstrap process
     457                 :             :  */
     458                 :             : static void
     459                 :          61 : bootstrap_signals(void)
     460                 :             : {
     461                 :             :     Assert(!IsUnderPostmaster);
     462                 :             : 
     463                 :             :     /*
     464                 :             :      * We don't actually need any non-default signal handling in bootstrap
     465                 :             :      * mode; "curl up and die" is a sufficient response for all these cases.
     466                 :             :      * Let's set that handling explicitly, as documentation if nothing else.
     467                 :             :      */
     468                 :          61 :     pqsignal(SIGHUP, PG_SIG_DFL);
     469                 :          61 :     pqsignal(SIGINT, PG_SIG_DFL);
     470                 :          61 :     pqsignal(SIGTERM, PG_SIG_DFL);
     471                 :          61 :     pqsignal(SIGQUIT, PG_SIG_DFL);
     472                 :          61 : }
     473                 :             : 
     474                 :             : /* ----------------------------------------------------------------
     475                 :             :  *              MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
     476                 :             :  * ----------------------------------------------------------------
     477                 :             :  */
     478                 :             : 
     479                 :             : /* ----------------
     480                 :             :  *      boot_openrel
     481                 :             :  *
     482                 :             :  * Execute BKI OPEN command.
     483                 :             :  * ----------------
     484                 :             :  */
     485                 :             : void
     486                 :        3660 : boot_openrel(char *relname)
     487                 :             : {
     488                 :             :     int         i;
     489                 :             : 
     490         [ -  + ]:        3660 :     if (strlen(relname) >= NAMEDATALEN)
     491                 :           0 :         relname[NAMEDATALEN - 1] = '\0';
     492                 :             : 
     493                 :             :     /*
     494                 :             :      * pg_type must be filled before any OPEN command is executed, hence we
     495                 :             :      * can now populate Typ if we haven't yet.
     496                 :             :      */
     497         [ -  + ]:        3660 :     if (Typ == NIL)
     498                 :           0 :         populate_typ_list();
     499                 :             : 
     500         [ -  + ]:        3660 :     if (boot_reldesc != NULL)
     501                 :           0 :         closerel(NULL);
     502                 :             : 
     503         [ -  + ]:        3660 :     elog(DEBUG4, "open relation %s, attrsize %d",
     504                 :             :          relname, (int) ATTRIBUTE_FIXED_PART_SIZE);
     505                 :             : 
     506                 :        3660 :     boot_reldesc = table_openrv(makeRangeVar(NULL, relname, -1), NoLock);
     507                 :        3660 :     numattr = RelationGetNumberOfAttributes(boot_reldesc);
     508         [ +  + ]:       33855 :     for (i = 0; i < numattr; i++)
     509                 :             :     {
     510         [ -  + ]:       30195 :         if (attrtypes[i] == NULL)
     511                 :           0 :             attrtypes[i] = AllocateAttribute();
     512                 :       30195 :         memmove(attrtypes[i],
     513                 :       30195 :                 TupleDescAttr(boot_reldesc->rd_att, i),
     514                 :             :                 ATTRIBUTE_FIXED_PART_SIZE);
     515                 :             : 
     516                 :             :         {
     517                 :       30195 :             Form_pg_attribute at = attrtypes[i];
     518                 :             : 
     519         [ -  + ]:       30195 :             elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
     520                 :             :                  i, NameStr(at->attname), at->attlen, at->attnum,
     521                 :             :                  at->atttypid);
     522                 :             :         }
     523                 :             :     }
     524                 :        3660 : }
     525                 :             : 
     526                 :             : /* ----------------
     527                 :             :  *      closerel
     528                 :             :  * ----------------
     529                 :             :  */
     530                 :             : void
     531                 :        3904 : closerel(char *relname)
     532                 :             : {
     533         [ +  - ]:        3904 :     if (relname)
     534                 :             :     {
     535         [ +  - ]:        3904 :         if (boot_reldesc)
     536                 :             :         {
     537         [ -  + ]:        3904 :             if (strcmp(RelationGetRelationName(boot_reldesc), relname) != 0)
     538         [ #  # ]:           0 :                 elog(ERROR, "close of %s when %s was expected",
     539                 :             :                      relname, RelationGetRelationName(boot_reldesc));
     540                 :             :         }
     541                 :             :         else
     542         [ #  # ]:           0 :             elog(ERROR, "close of %s before any relation was opened",
     543                 :             :                  relname);
     544                 :             :     }
     545                 :             : 
     546         [ -  + ]:        3904 :     if (boot_reldesc == NULL)
     547         [ #  # ]:           0 :         elog(ERROR, "no open relation to close");
     548                 :             :     else
     549                 :             :     {
     550         [ -  + ]:        3904 :         elog(DEBUG4, "close relation %s",
     551                 :             :              RelationGetRelationName(boot_reldesc));
     552                 :        3904 :         table_close(boot_reldesc, NoLock);
     553                 :        3904 :         boot_reldesc = NULL;
     554                 :             :     }
     555                 :        3904 : }
     556                 :             : 
     557                 :             : 
     558                 :             : 
     559                 :             : /* ----------------
     560                 :             :  * DEFINEATTR()
     561                 :             :  *
     562                 :             :  * define a <field,type> pair
     563                 :             :  * if there are n fields in a relation to be created, this routine
     564                 :             :  * will be called n times
     565                 :             :  * ----------------
     566                 :             :  */
     567                 :             : void
     568                 :       37637 : DefineAttr(char *name, char *type, int attnum, int nullness)
     569                 :             : {
     570                 :             :     Oid         typeoid;
     571                 :             : 
     572         [ -  + ]:       37637 :     if (boot_reldesc != NULL)
     573                 :             :     {
     574         [ #  # ]:           0 :         elog(WARNING, "no open relations allowed with CREATE command");
     575                 :           0 :         closerel(NULL);
     576                 :             :     }
     577                 :             : 
     578         [ +  + ]:       37637 :     if (attrtypes[attnum] == NULL)
     579                 :        2074 :         attrtypes[attnum] = AllocateAttribute();
     580   [ +  -  -  +  :       37637 :     MemSet(attrtypes[attnum], 0, ATTRIBUTE_FIXED_PART_SIZE);
          -  -  -  -  -  
                      - ]
     581                 :             : 
     582                 :       37637 :     namestrcpy(&attrtypes[attnum]->attname, name);
     583         [ -  + ]:       37637 :     elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
     584                 :       37637 :     attrtypes[attnum]->attnum = attnum + 1;
     585                 :             : 
     586                 :       37637 :     typeoid = gettype(type);
     587                 :             : 
     588         [ +  + ]:       37637 :     if (Typ != NIL)
     589                 :             :     {
     590                 :       32330 :         attrtypes[attnum]->atttypid = Ap->am_oid;
     591                 :       32330 :         attrtypes[attnum]->attlen = Ap->am_typ.typlen;
     592                 :       32330 :         attrtypes[attnum]->attbyval = Ap->am_typ.typbyval;
     593                 :       32330 :         attrtypes[attnum]->attalign = Ap->am_typ.typalign;
     594                 :       32330 :         attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
     595                 :       32330 :         attrtypes[attnum]->attcompression = InvalidCompressionMethod;
     596                 :       32330 :         attrtypes[attnum]->attcollation = Ap->am_typ.typcollation;
     597                 :             :         /* if an array type, assume 1-dimensional attribute */
     598   [ +  +  +  + ]:       32330 :         if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
     599                 :        2867 :             attrtypes[attnum]->attndims = 1;
     600                 :             :         else
     601                 :       29463 :             attrtypes[attnum]->attndims = 0;
     602                 :             :     }
     603                 :             :     else
     604                 :             :     {
     605                 :        5307 :         attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
     606                 :        5307 :         attrtypes[attnum]->attlen = TypInfo[typeoid].len;
     607                 :        5307 :         attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
     608                 :        5307 :         attrtypes[attnum]->attalign = TypInfo[typeoid].align;
     609                 :        5307 :         attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
     610                 :        5307 :         attrtypes[attnum]->attcompression = InvalidCompressionMethod;
     611                 :        5307 :         attrtypes[attnum]->attcollation = TypInfo[typeoid].collation;
     612                 :             :         /* if an array type, assume 1-dimensional attribute */
     613         [ +  + ]:        5307 :         if (TypInfo[typeoid].elem != InvalidOid &&
     614         [ +  + ]:         854 :             attrtypes[attnum]->attlen < 0)
     615                 :         671 :             attrtypes[attnum]->attndims = 1;
     616                 :             :         else
     617                 :        4636 :             attrtypes[attnum]->attndims = 0;
     618                 :             :     }
     619                 :             : 
     620                 :             :     /*
     621                 :             :      * If a system catalog column is collation-aware, force it to use C
     622                 :             :      * collation, so that its behavior is independent of the database's
     623                 :             :      * collation.  This is essential to allow template0 to be cloned with a
     624                 :             :      * different database collation.
     625                 :             :      */
     626         [ +  + ]:       37637 :     if (OidIsValid(attrtypes[attnum]->attcollation))
     627                 :        6161 :         attrtypes[attnum]->attcollation = C_COLLATION_OID;
     628                 :             : 
     629                 :       37637 :     attrtypes[attnum]->atttypmod = -1;
     630                 :       37637 :     attrtypes[attnum]->attislocal = true;
     631                 :             : 
     632         [ +  + ]:       37637 :     if (nullness == BOOTCOL_NULL_FORCE_NOT_NULL)
     633                 :             :     {
     634                 :        2135 :         attrtypes[attnum]->attnotnull = true;
     635                 :             :     }
     636         [ +  + ]:       35502 :     else if (nullness == BOOTCOL_NULL_FORCE_NULL)
     637                 :             :     {
     638                 :         244 :         attrtypes[attnum]->attnotnull = false;
     639                 :             :     }
     640                 :             :     else
     641                 :             :     {
     642                 :             :         Assert(nullness == BOOTCOL_NULL_AUTO);
     643                 :             : 
     644                 :             :         /*
     645                 :             :          * Mark as "not null" if type is fixed-width and prior columns are
     646                 :             :          * likewise fixed-width and not-null.  This corresponds to case where
     647                 :             :          * column can be accessed directly via C struct declaration.
     648                 :             :          */
     649         [ +  + ]:       35258 :         if (attrtypes[attnum]->attlen > 0)
     650                 :             :         {
     651                 :             :             int         i;
     652                 :             : 
     653                 :             :             /* check earlier attributes */
     654         [ +  + ]:      217709 :             for (i = 0; i < attnum; i++)
     655                 :             :             {
     656         [ +  + ]:      187697 :                 if (attrtypes[i]->attlen <= 0 ||
     657         [ +  - ]:      187514 :                     !attrtypes[i]->attnotnull)
     658                 :             :                     break;
     659                 :             :             }
     660         [ +  + ]:       30195 :             if (i == attnum)
     661                 :       30012 :                 attrtypes[attnum]->attnotnull = true;
     662                 :             :         }
     663                 :             :     }
     664                 :       37637 : }
     665                 :             : 
     666                 :             : 
     667                 :             : /* ----------------
     668                 :             :  *      InsertOneTuple
     669                 :             :  *
     670                 :             :  * If objectid is not zero, it is a specific OID to assign to the tuple.
     671                 :             :  * Otherwise, an OID will be assigned (if necessary) by heap_insert.
     672                 :             :  * ----------------
     673                 :             :  */
     674                 :             : void
     675                 :      663009 : InsertOneTuple(void)
     676                 :             : {
     677                 :             :     HeapTuple   tuple;
     678                 :             :     TupleDesc   tupDesc;
     679                 :             :     int         i;
     680                 :             : 
     681         [ -  + ]:      663009 :     elog(DEBUG4, "inserting row with %d columns", numattr);
     682                 :             : 
     683                 :      663009 :     tupDesc = CreateTupleDesc(numattr, attrtypes);
     684                 :      663009 :     tuple = heap_form_tuple(tupDesc, values, Nulls);
     685                 :      663009 :     pfree(tupDesc);             /* just free's tupDesc, not the attrtypes */
     686                 :             : 
     687                 :      663009 :     simple_heap_insert(boot_reldesc, tuple);
     688                 :      663009 :     heap_freetuple(tuple);
     689         [ -  + ]:      663009 :     elog(DEBUG4, "row inserted");
     690                 :             : 
     691                 :             :     /*
     692                 :             :      * Reset null markers for next tuple
     693                 :             :      */
     694         [ +  + ]:    10546656 :     for (i = 0; i < numattr; i++)
     695                 :     9883647 :         Nulls[i] = false;
     696                 :      663009 : }
     697                 :             : 
     698                 :             : /* ----------------
     699                 :             :  *      InsertOneValue
     700                 :             :  * ----------------
     701                 :             :  */
     702                 :             : void
     703                 :     7933732 : InsertOneValue(const char *value, int i)
     704                 :             : {
     705                 :             :     Form_pg_attribute attr;
     706                 :             :     Oid         typoid;
     707                 :             :     int16       typlen;
     708                 :             :     bool        typbyval;
     709                 :             :     char        typalign;
     710                 :             :     char        typdelim;
     711                 :             :     Oid         typioparam;
     712                 :             :     Oid         typinput;
     713                 :             :     Oid         typoutput;
     714                 :             :     Oid         typcollation;
     715                 :             : 
     716                 :             :     Assert(i >= 0 && i < MAXATTR);
     717                 :             : 
     718         [ -  + ]:     7933732 :     elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
     719                 :             : 
     720                 :     7933732 :     attr = TupleDescAttr(RelationGetDescr(boot_reldesc), i);
     721                 :     7933732 :     typoid = attr->atttypid;
     722                 :             : 
     723                 :     7933732 :     boot_get_type_io_data(typoid,
     724                 :             :                           &typlen, &typbyval, &typalign,
     725                 :             :                           &typdelim, &typioparam,
     726                 :             :                           &typinput, &typoutput,
     727                 :             :                           &typcollation);
     728                 :             : 
     729                 :             :     /*
     730                 :             :      * pg_node_tree values can't be inserted normally (pg_node_tree_in would
     731                 :             :      * just error out), so provide special cases for such columns that we
     732                 :             :      * would like to fill during bootstrap.
     733                 :             :      */
     734         [ +  + ]:     7933732 :     if (typoid == PG_NODE_TREEOID)
     735                 :             :     {
     736                 :             :         /* pg_proc.proargdefaults */
     737   [ +  -  +  - ]:        2562 :         if (RelationGetRelid(boot_reldesc) == ProcedureRelationId &&
     738                 :             :             i == Anum_pg_proc_proargdefaults - 1)
     739                 :        2562 :             InsertOneProargdefaultsValue(value);
     740                 :             :         else                    /* maybe other cases later */
     741         [ #  # ]:           0 :             elog(ERROR, "can't handle pg_node_tree input for %s.%s",
     742                 :             :                  RelationGetRelationName(boot_reldesc),
     743                 :             :                  NameStr(attr->attname));
     744                 :             :     }
     745                 :             :     else
     746                 :             :     {
     747                 :             :         /* Normal case */
     748                 :     7931170 :         values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
     749                 :             :     }
     750                 :             : 
     751                 :             :     /*
     752                 :             :      * We use ereport not elog here so that parameters aren't evaluated unless
     753                 :             :      * the message is going to be printed, which generally it isn't
     754                 :             :      */
     755         [ -  + ]:     7933732 :     ereport(DEBUG4,
     756                 :             :             (errmsg_internal("inserted -> %s",
     757                 :             :                              OidOutputFunctionCall(typoutput, values[i]))));
     758                 :     7933732 : }
     759                 :             : 
     760                 :             : /* ----------------
     761                 :             :  *      InsertOneProargdefaultsValue
     762                 :             :  *
     763                 :             :  * In general, proargdefaults can be a list of any expressions, but
     764                 :             :  * for bootstrap we only support a list of Const nodes.  The input
     765                 :             :  * has the form of a text array, and we feed non-null elements to the
     766                 :             :  * typinput functions for the appropriate parameters.
     767                 :             :  * ----------------
     768                 :             :  */
     769                 :             : static void
     770                 :        2562 : InsertOneProargdefaultsValue(const char *value)
     771                 :             : {
     772                 :             :     int         pronargs;
     773                 :             :     oidvector  *proargtypes;
     774                 :             :     Datum       arrayval;
     775                 :             :     Datum      *array_datums;
     776                 :             :     bool       *array_nulls;
     777                 :             :     int         array_count;
     778                 :             :     List       *proargdefaults;
     779                 :             :     char       *nodestring;
     780                 :             : 
     781                 :             :     /* The pg_proc columns we need to use must have been filled already */
     782                 :             :     StaticAssertDecl(Anum_pg_proc_pronargs < Anum_pg_proc_proargdefaults,
     783                 :             :                      "pronargs must come before proargdefaults");
     784                 :             :     StaticAssertDecl(Anum_pg_proc_pronargdefaults < Anum_pg_proc_proargdefaults,
     785                 :             :                      "pronargdefaults must come before proargdefaults");
     786                 :             :     StaticAssertDecl(Anum_pg_proc_proargtypes < Anum_pg_proc_proargdefaults,
     787                 :             :                      "proargtypes must come before proargdefaults");
     788         [ -  + ]:        2562 :     if (Nulls[Anum_pg_proc_pronargs - 1])
     789         [ #  # ]:           0 :         elog(ERROR, "pronargs must not be null");
     790         [ -  + ]:        2562 :     if (Nulls[Anum_pg_proc_proargtypes - 1])
     791         [ #  # ]:           0 :         elog(ERROR, "proargtypes must not be null");
     792                 :        2562 :     pronargs = DatumGetInt16(values[Anum_pg_proc_pronargs - 1]);
     793                 :        2562 :     proargtypes = DatumGetPointer(values[Anum_pg_proc_proargtypes - 1]);
     794                 :             :     Assert(pronargs == proargtypes->dim1);
     795                 :             : 
     796                 :             :     /* Parse the input string as an array value, then deconstruct to Datums */
     797                 :        2562 :     arrayval = OidFunctionCall3(F_ARRAY_IN,
     798                 :             :                                 CStringGetDatum(value),
     799                 :             :                                 ObjectIdGetDatum(CSTRINGOID),
     800                 :             :                                 Int32GetDatum(-1));
     801                 :        2562 :     deconstruct_array_builtin(DatumGetArrayTypeP(arrayval), CSTRINGOID,
     802                 :             :                               &array_datums, &array_nulls, &array_count);
     803                 :             : 
     804                 :             :     /* The values should correspond to the last N argtypes */
     805         [ -  + ]:        2562 :     if (array_count > pronargs)
     806         [ #  # ]:           0 :         elog(ERROR, "too many proargdefaults entries");
     807                 :             : 
     808                 :             :     /* Build the List of Const nodes */
     809                 :        2562 :     proargdefaults = NIL;
     810         [ +  + ]:        6832 :     for (int i = 0; i < array_count; i++)
     811                 :             :     {
     812                 :        4270 :         Oid         argtype = proargtypes->values[pronargs - array_count + i];
     813                 :             :         int16       typlen;
     814                 :             :         bool        typbyval;
     815                 :             :         char        typalign;
     816                 :             :         char        typdelim;
     817                 :             :         Oid         typioparam;
     818                 :             :         Oid         typinput;
     819                 :             :         Oid         typoutput;
     820                 :             :         Oid         typcollation;
     821                 :             :         Datum       defval;
     822                 :             :         bool        defnull;
     823                 :             :         Const      *defConst;
     824                 :             : 
     825                 :        4270 :         boot_get_type_io_data(argtype,
     826                 :             :                               &typlen, &typbyval, &typalign,
     827                 :             :                               &typdelim, &typioparam,
     828                 :             :                               &typinput, &typoutput,
     829                 :             :                               &typcollation);
     830                 :             : 
     831                 :        4270 :         defnull = array_nulls[i];
     832         [ +  + ]:        4270 :         if (defnull)
     833                 :         122 :             defval = (Datum) 0;
     834                 :             :         else
     835                 :        4148 :             defval = OidInputFunctionCall(typinput,
     836                 :        4148 :                                           DatumGetCString(array_datums[i]),
     837                 :             :                                           typioparam, -1);
     838                 :             : 
     839                 :        4270 :         defConst = makeConst(argtype,
     840                 :             :                              -1,    /* never any typmod */
     841                 :             :                              typcollation,
     842                 :             :                              typlen,
     843                 :             :                              defval,
     844                 :             :                              defnull,
     845                 :             :                              typbyval);
     846                 :        4270 :         proargdefaults = lappend(proargdefaults, defConst);
     847                 :             :     }
     848                 :             : 
     849                 :             :     /*
     850                 :             :      * Flatten the List to a node-tree string, then convert to a text datum,
     851                 :             :      * which is the storage representation of pg_node_tree.
     852                 :             :      */
     853                 :        2562 :     nodestring = nodeToString(proargdefaults);
     854                 :        2562 :     values[Anum_pg_proc_proargdefaults - 1] = CStringGetTextDatum(nodestring);
     855                 :        2562 :     Nulls[Anum_pg_proc_proargdefaults - 1] = false;
     856                 :             : 
     857                 :             :     /*
     858                 :             :      * Hack: fill in pronargdefaults with the right value.  This is surely
     859                 :             :      * ugly, but it beats making the programmer do it.
     860                 :             :      */
     861                 :        2562 :     values[Anum_pg_proc_pronargdefaults - 1] = Int16GetDatum(array_count);
     862                 :        2562 :     Nulls[Anum_pg_proc_pronargdefaults - 1] = false;
     863                 :        2562 : }
     864                 :             : 
     865                 :             : /* ----------------
     866                 :             :  *      InsertOneNull
     867                 :             :  * ----------------
     868                 :             :  */
     869                 :             : void
     870                 :     1949915 : InsertOneNull(int i)
     871                 :             : {
     872         [ -  + ]:     1949915 :     elog(DEBUG4, "inserting column %d NULL", i);
     873                 :             :     Assert(i >= 0 && i < MAXATTR);
     874         [ -  + ]:     1949915 :     if (TupleDescAttr(boot_reldesc->rd_att, i)->attnotnull)
     875         [ #  # ]:           0 :         elog(ERROR,
     876                 :             :              "NULL value specified for not-null column \"%s\" of relation \"%s\"",
     877                 :             :              NameStr(TupleDescAttr(boot_reldesc->rd_att, i)->attname),
     878                 :             :              RelationGetRelationName(boot_reldesc));
     879                 :     1949915 :     values[i] = PointerGetDatum(NULL);
     880                 :     1949915 :     Nulls[i] = true;
     881                 :     1949915 : }
     882                 :             : 
     883                 :             : /* ----------------
     884                 :             :  *      cleanup
     885                 :             :  * ----------------
     886                 :             :  */
     887                 :             : static void
     888                 :          61 : cleanup(void)
     889                 :             : {
     890         [ -  + ]:          61 :     if (boot_reldesc != NULL)
     891                 :           0 :         closerel(NULL);
     892                 :          61 : }
     893                 :             : 
     894                 :             : /* ----------------
     895                 :             :  *      populate_typ_list
     896                 :             :  *
     897                 :             :  * Load the Typ list by reading pg_type.
     898                 :             :  * ----------------
     899                 :             :  */
     900                 :             : static void
     901                 :         122 : populate_typ_list(void)
     902                 :             : {
     903                 :             :     Relation    rel;
     904                 :             :     TableScanDesc scan;
     905                 :             :     HeapTuple   tup;
     906                 :             :     MemoryContext old;
     907                 :             : 
     908                 :             :     Assert(Typ == NIL);
     909                 :             : 
     910                 :         122 :     rel = table_open(TypeRelationId, NoLock);
     911                 :         122 :     scan = table_beginscan_catalog(rel, 0, NULL);
     912                 :         122 :     old = MemoryContextSwitchTo(TopMemoryContext);
     913         [ +  + ]:       26108 :     while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
     914                 :             :     {
     915                 :       25986 :         Form_pg_type typForm = (Form_pg_type) GETSTRUCT(tup);
     916                 :             :         struct typmap *newtyp;
     917                 :             : 
     918                 :       25986 :         newtyp = palloc_object(struct typmap);
     919                 :       25986 :         Typ = lappend(Typ, newtyp);
     920                 :             : 
     921                 :       25986 :         newtyp->am_oid = typForm->oid;
     922                 :       25986 :         memcpy(&newtyp->am_typ, typForm, sizeof(newtyp->am_typ));
     923                 :             :     }
     924                 :         122 :     MemoryContextSwitchTo(old);
     925                 :         122 :     table_endscan(scan);
     926                 :         122 :     table_close(rel, NoLock);
     927                 :         122 : }
     928                 :             : 
     929                 :             : /* ----------------
     930                 :             :  *      gettype
     931                 :             :  *
     932                 :             :  * NB: this is really ugly; it will return an integer index into TypInfo[],
     933                 :             :  * and not an OID at all, until the first reference to a type not known in
     934                 :             :  * TypInfo[].  At that point it will read and cache pg_type in Typ,
     935                 :             :  * and subsequently return a real OID (and set the global pointer Ap to
     936                 :             :  * point at the found row in Typ).  So caller must check whether Typ is
     937                 :             :  * still NIL to determine what the return value is!
     938                 :             :  * ----------------
     939                 :             :  */
     940                 :             : static Oid
     941                 :       37698 : gettype(char *type)
     942                 :             : {
     943         [ +  + ]:       37698 :     if (Typ != NIL)
     944                 :             :     {
     945                 :             :         ListCell   *lc;
     946                 :             : 
     947   [ +  -  +  +  :      600606 :         foreach(lc, Typ)
                   +  + ]
     948                 :             :         {
     949                 :      600545 :             struct typmap *app = lfirst(lc);
     950                 :             : 
     951         [ +  + ]:      600545 :             if (strncmp(NameStr(app->am_typ.typname), type, NAMEDATALEN) == 0)
     952                 :             :             {
     953                 :       32269 :                 Ap = app;
     954                 :       32269 :                 return app->am_oid;
     955                 :             :             }
     956                 :             :         }
     957                 :             : 
     958                 :             :         /*
     959                 :             :          * The type wasn't known; reload the pg_type contents and check again
     960                 :             :          * to handle composite types, added since last populating the list.
     961                 :             :          */
     962                 :             : 
     963                 :          61 :         list_free_deep(Typ);
     964                 :          61 :         Typ = NIL;
     965                 :          61 :         populate_typ_list();
     966                 :             : 
     967                 :             :         /*
     968                 :             :          * Calling gettype would result in infinite recursion for types
     969                 :             :          * missing in pg_type, so just repeat the lookup.
     970                 :             :          */
     971   [ +  -  +  -  :       13847 :         foreach(lc, Typ)
                   +  - ]
     972                 :             :         {
     973                 :       13847 :             struct typmap *app = lfirst(lc);
     974                 :             : 
     975         [ +  + ]:       13847 :             if (strncmp(NameStr(app->am_typ.typname), type, NAMEDATALEN) == 0)
     976                 :             :             {
     977                 :          61 :                 Ap = app;
     978                 :          61 :                 return app->am_oid;
     979                 :             :             }
     980                 :             :         }
     981                 :             :     }
     982                 :             :     else
     983                 :             :     {
     984                 :             :         int         i;
     985                 :             : 
     986         [ +  + ]:       52643 :         for (i = 0; i < n_types; i++)
     987                 :             :         {
     988         [ +  + ]:       52582 :             if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
     989                 :        5307 :                 return i;
     990                 :             :         }
     991                 :             :         /* Not in TypInfo, so we'd better be able to read pg_type now */
     992         [ -  + ]:          61 :         elog(DEBUG4, "external type: %s", type);
     993                 :          61 :         populate_typ_list();
     994                 :          61 :         return gettype(type);
     995                 :             :     }
     996         [ #  # ]:           0 :     elog(ERROR, "unrecognized type \"%s\"", type);
     997                 :             :     /* not reached, here to make compiler happy */
     998                 :             :     return 0;
     999                 :             : }
    1000                 :             : 
    1001                 :             : /* ----------------
    1002                 :             :  *      boot_get_type_io_data
    1003                 :             :  *
    1004                 :             :  * Obtain type I/O information at bootstrap time.  This intentionally has
    1005                 :             :  * an API very close to that of lsyscache.c's get_type_io_data, except that
    1006                 :             :  * we only support obtaining the typinput and typoutput routines, not
    1007                 :             :  * the binary I/O routines, and we also return the type's collation.
    1008                 :             :  * This is exported so that array_in and array_out can be made to work
    1009                 :             :  * during early bootstrap.
    1010                 :             :  * ----------------
    1011                 :             :  */
    1012                 :             : void
    1013                 :     7977896 : boot_get_type_io_data(Oid typid,
    1014                 :             :                       int16 *typlen,
    1015                 :             :                       bool *typbyval,
    1016                 :             :                       char *typalign,
    1017                 :             :                       char *typdelim,
    1018                 :             :                       Oid *typioparam,
    1019                 :             :                       Oid *typinput,
    1020                 :             :                       Oid *typoutput,
    1021                 :             :                       Oid *typcollation)
    1022                 :             : {
    1023         [ +  + ]:     7977896 :     if (Typ != NIL)
    1024                 :             :     {
    1025                 :             :         /* We have the boot-time contents of pg_type, so use it */
    1026                 :     3134374 :         struct typmap *ap = NULL;
    1027                 :             :         ListCell   *lc;
    1028                 :             : 
    1029   [ +  -  +  -  :    28294350 :         foreach(lc, Typ)
                   +  - ]
    1030                 :             :         {
    1031                 :    28294350 :             ap = lfirst(lc);
    1032         [ +  + ]:    28294350 :             if (ap->am_oid == typid)
    1033                 :     3134374 :                 break;
    1034                 :             :         }
    1035                 :             : 
    1036   [ +  -  -  + ]:     3134374 :         if (!ap || ap->am_oid != typid)
    1037         [ #  # ]:           0 :             elog(ERROR, "type OID %u not found in Typ list", typid);
    1038                 :             : 
    1039                 :     3134374 :         *typlen = ap->am_typ.typlen;
    1040                 :     3134374 :         *typbyval = ap->am_typ.typbyval;
    1041                 :     3134374 :         *typalign = ap->am_typ.typalign;
    1042                 :     3134374 :         *typdelim = ap->am_typ.typdelim;
    1043                 :             : 
    1044                 :             :         /* XXX this logic must match getTypeIOParam() */
    1045         [ +  + ]:     3134374 :         if (OidIsValid(ap->am_typ.typelem))
    1046                 :       87108 :             *typioparam = ap->am_typ.typelem;
    1047                 :             :         else
    1048                 :     3047266 :             *typioparam = typid;
    1049                 :             : 
    1050                 :     3134374 :         *typinput = ap->am_typ.typinput;
    1051                 :     3134374 :         *typoutput = ap->am_typ.typoutput;
    1052                 :             : 
    1053                 :     3134374 :         *typcollation = ap->am_typ.typcollation;
    1054                 :             :     }
    1055                 :             :     else
    1056                 :             :     {
    1057                 :             :         /* We don't have pg_type yet, so use the hard-wired TypInfo array */
    1058                 :             :         int         typeindex;
    1059                 :             : 
    1060         [ +  - ]:    41025794 :         for (typeindex = 0; typeindex < n_types; typeindex++)
    1061                 :             :         {
    1062         [ +  + ]:    41025794 :             if (TypInfo[typeindex].oid == typid)
    1063                 :     4843522 :                 break;
    1064                 :             :         }
    1065         [ -  + ]:     4843522 :         if (typeindex >= n_types)
    1066         [ #  # ]:           0 :             elog(ERROR, "type OID %u not found in TypInfo", typid);
    1067                 :             : 
    1068                 :     4843522 :         *typlen = TypInfo[typeindex].len;
    1069                 :     4843522 :         *typbyval = TypInfo[typeindex].byval;
    1070                 :     4843522 :         *typalign = TypInfo[typeindex].align;
    1071                 :             :         /* We assume typdelim is ',' for all boot-time types */
    1072                 :     4843522 :         *typdelim = ',';
    1073                 :             : 
    1074                 :             :         /* XXX this logic must match getTypeIOParam() */
    1075         [ +  + ]:     4843522 :         if (OidIsValid(TypInfo[typeindex].elem))
    1076                 :      469151 :             *typioparam = TypInfo[typeindex].elem;
    1077                 :             :         else
    1078                 :     4374371 :             *typioparam = typid;
    1079                 :             : 
    1080                 :     4843522 :         *typinput = TypInfo[typeindex].inproc;
    1081                 :     4843522 :         *typoutput = TypInfo[typeindex].outproc;
    1082                 :             : 
    1083                 :     4843522 :         *typcollation = TypInfo[typeindex].collation;
    1084                 :             :     }
    1085                 :     7977896 : }
    1086                 :             : 
    1087                 :             : /* ----------------
    1088                 :             :  *      boot_get_role_oid
    1089                 :             :  *
    1090                 :             :  * Look up a role name at bootstrap time.  This is equivalent to
    1091                 :             :  * get_role_oid(rolname, true): return the role OID or InvalidOid if
    1092                 :             :  * not found.  We only need to cope with built-in role names.
    1093                 :             :  * ----------------
    1094                 :             :  */
    1095                 :             : Oid
    1096                 :        5246 : boot_get_role_oid(const char *rolname)
    1097                 :             : {
    1098         [ +  - ]:        9760 :     for (size_t i = 0; i < lengthof(RolInfo); i++)
    1099                 :             :     {
    1100         [ +  + ]:        9760 :         if (strcmp(RolInfo[i].rolname, rolname) == 0)
    1101                 :        5246 :             return RolInfo[i].oid;
    1102                 :             :     }
    1103                 :           0 :     return InvalidOid;
    1104                 :             : }
    1105                 :             : 
    1106                 :             : /* ----------------
    1107                 :             :  *      AllocateAttribute
    1108                 :             :  *
    1109                 :             :  * Note: bootstrap never sets any per-column ACLs, so we only need
    1110                 :             :  * ATTRIBUTE_FIXED_PART_SIZE space per attribute.
    1111                 :             :  * ----------------
    1112                 :             :  */
    1113                 :             : static Form_pg_attribute
    1114                 :        2074 : AllocateAttribute(void)
    1115                 :             : {
    1116                 :        2074 :     return (Form_pg_attribute)
    1117                 :        2074 :         MemoryContextAllocZero(TopMemoryContext, ATTRIBUTE_FIXED_PART_SIZE);
    1118                 :             : }
    1119                 :             : 
    1120                 :             : /*
    1121                 :             :  *  index_register() -- record an index that has been set up for building
    1122                 :             :  *                      later.
    1123                 :             :  *
    1124                 :             :  *      At bootstrap time, we define a bunch of indexes on system catalogs.
    1125                 :             :  *      We postpone actually building the indexes until just before we're
    1126                 :             :  *      finished with initialization, however.  This is because the indexes
    1127                 :             :  *      themselves have catalog entries, and those have to be included in the
    1128                 :             :  *      indexes on those catalogs.  Doing it in two phases is the simplest
    1129                 :             :  *      way of making sure the indexes have the right contents at the end.
    1130                 :             :  */
    1131                 :             : void
    1132                 :        9760 : index_register(Oid heap,
    1133                 :             :                Oid ind,
    1134                 :             :                const IndexInfo *indexInfo)
    1135                 :             : {
    1136                 :             :     IndexList  *newind;
    1137                 :             :     MemoryContext oldcxt;
    1138                 :             : 
    1139                 :             :     /*
    1140                 :             :      * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
    1141                 :             :      * bootstrap time.  we'll declare the indexes now, but want to create them
    1142                 :             :      * later.
    1143                 :             :      */
    1144                 :             : 
    1145         [ +  + ]:        9760 :     if (nogc == NULL)
    1146                 :          61 :         nogc = AllocSetContextCreate(NULL,
    1147                 :             :                                      "BootstrapNoGC",
    1148                 :             :                                      ALLOCSET_DEFAULT_SIZES);
    1149                 :             : 
    1150                 :        9760 :     oldcxt = MemoryContextSwitchTo(nogc);
    1151                 :             : 
    1152                 :        9760 :     newind = palloc_object(IndexList);
    1153                 :        9760 :     newind->il_heap = heap;
    1154                 :        9760 :     newind->il_ind = ind;
    1155                 :        9760 :     newind->il_info = palloc_object(IndexInfo);
    1156                 :             : 
    1157                 :        9760 :     memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
    1158                 :             :     /* expressions will likely be null, but may as well copy it */
    1159                 :       19520 :     newind->il_info->ii_Expressions =
    1160                 :        9760 :         copyObject(indexInfo->ii_Expressions);
    1161                 :        9760 :     newind->il_info->ii_ExpressionsState = NIL;
    1162                 :             :     /* predicate will likely be null, but may as well copy it */
    1163                 :       19520 :     newind->il_info->ii_Predicate =
    1164                 :        9760 :         copyObject(indexInfo->ii_Predicate);
    1165                 :        9760 :     newind->il_info->ii_PredicateState = NULL;
    1166                 :             :     /* no exclusion constraints at bootstrap time, so no need to copy */
    1167                 :             :     Assert(indexInfo->ii_ExclusionOps == NULL);
    1168                 :             :     Assert(indexInfo->ii_ExclusionProcs == NULL);
    1169                 :             :     Assert(indexInfo->ii_ExclusionStrats == NULL);
    1170                 :             : 
    1171                 :        9760 :     newind->il_next = ILHead;
    1172                 :        9760 :     ILHead = newind;
    1173                 :             : 
    1174                 :        9760 :     MemoryContextSwitchTo(oldcxt);
    1175                 :        9760 : }
    1176                 :             : 
    1177                 :             : 
    1178                 :             : /*
    1179                 :             :  * build_indices -- fill in all the indexes registered earlier
    1180                 :             :  */
    1181                 :             : void
    1182                 :          61 : build_indices(void)
    1183                 :             : {
    1184         [ +  + ]:        9821 :     for (; ILHead != NULL; ILHead = ILHead->il_next)
    1185                 :             :     {
    1186                 :             :         Relation    heap;
    1187                 :             :         Relation    ind;
    1188                 :             : 
    1189                 :             :         /* need not bother with locks during bootstrap */
    1190                 :        9760 :         heap = table_open(ILHead->il_heap, NoLock);
    1191                 :        9760 :         ind = index_open(ILHead->il_ind, NoLock);
    1192                 :             : 
    1193                 :        9760 :         index_build(heap, ind, ILHead->il_info, false, false, false);
    1194                 :             : 
    1195                 :        9760 :         index_close(ind, NoLock);
    1196                 :        9760 :         table_close(heap, NoLock);
    1197                 :             :     }
    1198                 :          61 : }
        

Generated by: LCOV version 2.0-1