LCOV - code coverage report
Current view: top level - src/backend/bootstrap - bootstrap.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 257 298 86.2 %
Date: 2021-12-05 02:08:31 Functions: 16 16 100.0 %
Legend: Lines: hit not hit

          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-2021, 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 "access/xlog_internal.h"
      27             : #include "bootstrap/bootstrap.h"
      28             : #include "catalog/index.h"
      29             : #include "catalog/pg_collation.h"
      30             : #include "catalog/pg_type.h"
      31             : #include "common/link-canary.h"
      32             : #include "libpq/pqsignal.h"
      33             : #include "miscadmin.h"
      34             : #include "nodes/makefuncs.h"
      35             : #include "pg_getopt.h"
      36             : #include "storage/bufmgr.h"
      37             : #include "storage/bufpage.h"
      38             : #include "storage/condition_variable.h"
      39             : #include "storage/ipc.h"
      40             : #include "storage/proc.h"
      41             : #include "tcop/tcopprot.h"
      42             : #include "utils/builtins.h"
      43             : #include "utils/fmgroids.h"
      44             : #include "utils/memutils.h"
      45             : #include "utils/rel.h"
      46             : #include "utils/relmapper.h"
      47             : 
      48             : uint32      bootstrap_data_checksum_version = 0;    /* No checksum */
      49             : 
      50             : 
      51             : static void CheckerModeMain(void);
      52             : static void bootstrap_signals(void);
      53             : static Form_pg_attribute AllocateAttribute(void);
      54             : static void populate_typ_list(void);
      55             : static Oid  gettype(char *type);
      56             : static void cleanup(void);
      57             : 
      58             : /* ----------------
      59             :  *      global variables
      60             :  * ----------------
      61             :  */
      62             : 
      63             : Relation    boot_reldesc;       /* current relation descriptor */
      64             : 
      65             : Form_pg_attribute attrtypes[MAXATTR];   /* points to attribute info */
      66             : int         numattr;            /* number of attributes for cur. rel */
      67             : 
      68             : 
      69             : /*
      70             :  * Basic information associated with each type.  This is used before
      71             :  * pg_type is filled, so it has to cover the datatypes used as column types
      72             :  * in the core "bootstrapped" catalogs.
      73             :  *
      74             :  *      XXX several of these input/output functions do catalog scans
      75             :  *          (e.g., F_REGPROCIN scans pg_proc).  this obviously creates some
      76             :  *          order dependencies in the catalog creation process.
      77             :  */
      78             : struct typinfo
      79             : {
      80             :     char        name[NAMEDATALEN];
      81             :     Oid         oid;
      82             :     Oid         elem;
      83             :     int16       len;
      84             :     bool        byval;
      85             :     char        align;
      86             :     char        storage;
      87             :     Oid         collation;
      88             :     Oid         inproc;
      89             :     Oid         outproc;
      90             : };
      91             : 
      92             : static const struct typinfo TypInfo[] = {
      93             :     {"bool", BOOLOID, 0, 1, true, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
      94             :     F_BOOLIN, F_BOOLOUT},
      95             :     {"bytea", BYTEAOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
      96             :     F_BYTEAIN, F_BYTEAOUT},
      97             :     {"char", CHAROID, 0, 1, true, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
      98             :     F_CHARIN, F_CHAROUT},
      99             :     {"int2", INT2OID, 0, 2, true, TYPALIGN_SHORT, TYPSTORAGE_PLAIN, InvalidOid,
     100             :     F_INT2IN, F_INT2OUT},
     101             :     {"int4", INT4OID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     102             :     F_INT4IN, F_INT4OUT},
     103             :     {"float4", FLOAT4OID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     104             :     F_FLOAT4IN, F_FLOAT4OUT},
     105             :     {"name", NAMEOID, CHAROID, NAMEDATALEN, false, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, C_COLLATION_OID,
     106             :     F_NAMEIN, F_NAMEOUT},
     107             :     {"regclass", REGCLASSOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     108             :     F_REGCLASSIN, F_REGCLASSOUT},
     109             :     {"regproc", REGPROCOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     110             :     F_REGPROCIN, F_REGPROCOUT},
     111             :     {"regtype", REGTYPEOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     112             :     F_REGTYPEIN, F_REGTYPEOUT},
     113             :     {"regrole", REGROLEOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     114             :     F_REGROLEIN, F_REGROLEOUT},
     115             :     {"regnamespace", REGNAMESPACEOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     116             :     F_REGNAMESPACEIN, F_REGNAMESPACEOUT},
     117             :     {"text", TEXTOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, DEFAULT_COLLATION_OID,
     118             :     F_TEXTIN, F_TEXTOUT},
     119             :     {"oid", OIDOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     120             :     F_OIDIN, F_OIDOUT},
     121             :     {"tid", TIDOID, 0, 6, false, TYPALIGN_SHORT, TYPSTORAGE_PLAIN, InvalidOid,
     122             :     F_TIDIN, F_TIDOUT},
     123             :     {"xid", XIDOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     124             :     F_XIDIN, F_XIDOUT},
     125             :     {"cid", CIDOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     126             :     F_CIDIN, F_CIDOUT},
     127             :     {"pg_node_tree", PG_NODE_TREEOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, DEFAULT_COLLATION_OID,
     128             :     F_PG_NODE_TREE_IN, F_PG_NODE_TREE_OUT},
     129             :     {"int2vector", INT2VECTOROID, INT2OID, -1, false, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     130             :     F_INT2VECTORIN, F_INT2VECTOROUT},
     131             :     {"oidvector", OIDVECTOROID, OIDOID, -1, false, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
     132             :     F_OIDVECTORIN, F_OIDVECTOROUT},
     133             :     {"_int4", INT4ARRAYOID, INT4OID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
     134             :     F_ARRAY_IN, F_ARRAY_OUT},
     135             :     {"_text", 1009, TEXTOID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, DEFAULT_COLLATION_OID,
     136             :     F_ARRAY_IN, F_ARRAY_OUT},
     137             :     {"_oid", 1028, OIDOID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
     138             :     F_ARRAY_IN, F_ARRAY_OUT},
     139             :     {"_char", 1002, CHAROID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
     140             :     F_ARRAY_IN, F_ARRAY_OUT},
     141             :     {"_aclitem", 1034, ACLITEMOID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
     142             :     F_ARRAY_IN, F_ARRAY_OUT}
     143             : };
     144             : 
     145             : static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
     146             : 
     147             : struct typmap
     148             : {                               /* a hack */
     149             :     Oid         am_oid;
     150             :     FormData_pg_type am_typ;
     151             : };
     152             : 
     153             : static List *Typ = NIL;         /* List of struct typmap* */
     154             : static struct typmap *Ap = NULL;
     155             : 
     156             : static Datum values[MAXATTR];   /* current row's attribute values */
     157             : static bool Nulls[MAXATTR];
     158             : 
     159             : static MemoryContext nogc = NULL;   /* special no-gc mem context */
     160             : 
     161             : /*
     162             :  *  At bootstrap time, we first declare all the indices to be built, and
     163             :  *  then build them.  The IndexList structure stores enough information
     164             :  *  to allow us to build the indices after they've been declared.
     165             :  */
     166             : 
     167             : typedef struct _IndexList
     168             : {
     169             :     Oid         il_heap;
     170             :     Oid         il_ind;
     171             :     IndexInfo  *il_info;
     172             :     struct _IndexList *il_next;
     173             : } IndexList;
     174             : 
     175             : static IndexList *ILHead = NULL;
     176             : 
     177             : 
     178             : /*
     179             :  * In shared memory checker mode, all we really want to do is create shared
     180             :  * memory and semaphores (just to prove we can do it with the current GUC
     181             :  * settings).  Since, in fact, that was already done by
     182             :  * CreateSharedMemoryAndSemaphores(), we have nothing more to do here.
     183             :  */
     184             : static void
     185         964 : CheckerModeMain(void)
     186             : {
     187         964 :     proc_exit(0);
     188             : }
     189             : 
     190             : /*
     191             :  *   The main entry point for running the backend in bootstrap mode
     192             :  *
     193             :  *   The bootstrap mode is used to initialize the template database.
     194             :  *   The bootstrap backend doesn't speak SQL, but instead expects
     195             :  *   commands in a special bootstrap language.
     196             :  *
     197             :  *   When check_only is true, startup is done only far enough to verify that
     198             :  *   the current configuration, particularly the passed in options pertaining
     199             :  *   to shared memory sizing, options work (or at least do not cause an error
     200             :  *   up to shared memory creation).
     201             :  */
     202             : void
     203        1446 : BootstrapModeMain(int argc, char *argv[], bool check_only)
     204             : {
     205             :     int         i;
     206        1446 :     char       *progname = argv[0];
     207             :     int         flag;
     208        1446 :     char       *userDoption = NULL;
     209             : 
     210             :     Assert(!IsUnderPostmaster);
     211             : 
     212        1446 :     InitStandaloneProcess(argv[0]);
     213             : 
     214             :     /* Set defaults, to be overridden by explicit options below */
     215        1446 :     InitializeGUCOptions();
     216             : 
     217             :     /* an initial --boot or --check should be present */
     218             :     Assert(argc > 1
     219             :            && (strcmp(argv[1], "--boot") == 0
     220             :                || strcmp(argv[1], "--check") == 0));
     221        1446 :     argv++;
     222        1446 :     argc--;
     223             : 
     224        6268 :     while ((flag = getopt(argc, argv, "B:c:d:D:Fkr:X:-:")) != -1)
     225             :     {
     226        4822 :         switch (flag)
     227             :         {
     228           0 :             case 'B':
     229           0 :                 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
     230           0 :                 break;
     231           0 :             case 'D':
     232           0 :                 userDoption = pstrdup(optarg);
     233           0 :                 break;
     234           0 :             case 'd':
     235             :                 {
     236             :                     /* Turn on debugging for the bootstrap process. */
     237             :                     char       *debugstr;
     238             : 
     239           0 :                     debugstr = psprintf("debug%s", optarg);
     240           0 :                     SetConfigOption("log_min_messages", debugstr,
     241             :                                     PGC_POSTMASTER, PGC_S_ARGV);
     242           0 :                     SetConfigOption("client_min_messages", debugstr,
     243             :                                     PGC_POSTMASTER, PGC_S_ARGV);
     244           0 :                     pfree(debugstr);
     245             :                 }
     246           0 :                 break;
     247        1446 :             case 'F':
     248        1446 :                 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
     249        1446 :                 break;
     250           2 :             case 'k':
     251           2 :                 bootstrap_data_checksum_version = PG_DATA_CHECKSUM_VERSION;
     252           2 :                 break;
     253           0 :             case 'r':
     254           0 :                 strlcpy(OutputFileName, optarg, MAXPGPATH);
     255           0 :                 break;
     256         482 :             case 'X':
     257             :                 {
     258         482 :                     int         WalSegSz = strtoul(optarg, NULL, 0);
     259             : 
     260         482 :                     if (!IsValidWalSegSize(WalSegSz))
     261           0 :                         ereport(ERROR,
     262             :                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     263             :                                  errmsg("-X requires a power of two value between 1 MB and 1 GB")));
     264         482 :                     SetConfigOption("wal_segment_size", optarg, PGC_INTERNAL,
     265             :                                     PGC_S_OVERRIDE);
     266             :                 }
     267         482 :                 break;
     268        2892 :             case 'c':
     269             :             case '-':
     270             :                 {
     271             :                     char       *name,
     272             :                                *value;
     273             : 
     274        2892 :                     ParseLongOption(optarg, &name, &value);
     275        2892 :                     if (!value)
     276             :                     {
     277           0 :                         if (flag == '-')
     278           0 :                             ereport(ERROR,
     279             :                                     (errcode(ERRCODE_SYNTAX_ERROR),
     280             :                                      errmsg("--%s requires a value",
     281             :                                             optarg)));
     282             :                         else
     283           0 :                             ereport(ERROR,
     284             :                                     (errcode(ERRCODE_SYNTAX_ERROR),
     285             :                                      errmsg("-c %s requires a value",
     286             :                                             optarg)));
     287             :                     }
     288             : 
     289        2892 :                     SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
     290        2892 :                     free(name);
     291        2892 :                     if (value)
     292        2892 :                         free(value);
     293        2892 :                     break;
     294             :                 }
     295           0 :             default:
     296           0 :                 write_stderr("Try \"%s --help\" for more information.\n",
     297             :                              progname);
     298           0 :                 proc_exit(1);
     299             :                 break;
     300             :         }
     301             :     }
     302             : 
     303        1446 :     if (argc != optind)
     304             :     {
     305           0 :         write_stderr("%s: invalid command-line arguments\n", progname);
     306           0 :         proc_exit(1);
     307             :     }
     308             : 
     309             :     /* Acquire configuration parameters */
     310        1446 :     if (!SelectConfigFiles(userDoption, progname))
     311           0 :         proc_exit(1);
     312             : 
     313             :     /*
     314             :      * Validate we have been given a reasonable-looking DataDir and change
     315             :      * into it
     316             :      */
     317        1446 :     checkDataDir();
     318        1446 :     ChangeToDataDir();
     319             : 
     320        1446 :     CreateDataDirLockFile(false);
     321             : 
     322        1446 :     SetProcessingMode(BootstrapProcessing);
     323        1446 :     IgnoreSystemIndexes = true;
     324             : 
     325        1446 :     InitializeMaxBackends();
     326             : 
     327        1446 :     CreateSharedMemoryAndSemaphores();
     328             : 
     329             :     /*
     330             :      * XXX: It might make sense to move this into its own function at some
     331             :      * point. Right now it seems like it'd cause more code duplication than
     332             :      * it's worth.
     333             :      */
     334        1446 :     if (check_only)
     335             :     {
     336         964 :         SetProcessingMode(NormalProcessing);
     337         964 :         CheckerModeMain();
     338           0 :         abort();
     339             :     }
     340             : 
     341             :     /*
     342             :      * Do backend-like initialization for bootstrap mode
     343             :      */
     344         482 :     InitProcess();
     345             : 
     346         482 :     BaseInit();
     347             : 
     348         482 :     bootstrap_signals();
     349         482 :     BootStrapXLOG();
     350             : 
     351             :     /*
     352             :      * To ensure that src/common/link-canary.c is linked into the backend, we
     353             :      * must call it from somewhere.  Here is as good as anywhere.
     354             :      */
     355         482 :     if (pg_link_canary_is_frontend())
     356           0 :         elog(ERROR, "backend is incorrectly linked to frontend functions");
     357             : 
     358         482 :     InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL, false);
     359             : 
     360             :     /* Initialize stuff for bootstrap-file processing */
     361       19762 :     for (i = 0; i < MAXATTR; i++)
     362             :     {
     363       19280 :         attrtypes[i] = NULL;
     364       19280 :         Nulls[i] = false;
     365             :     }
     366             : 
     367             :     /*
     368             :      * Process bootstrap input.
     369             :      */
     370         482 :     StartTransactionCommand();
     371         482 :     boot_yyparse();
     372         482 :     CommitTransactionCommand();
     373             : 
     374             :     /*
     375             :      * We should now know about all mapped relations, so it's okay to write
     376             :      * out the initial relation mapping files.
     377             :      */
     378         482 :     RelationMapFinishBootstrap();
     379             : 
     380             :     /* Clean up and exit */
     381         482 :     cleanup();
     382         482 :     proc_exit(0);
     383             : }
     384             : 
     385             : 
     386             : /* ----------------------------------------------------------------
     387             :  *                      misc functions
     388             :  * ----------------------------------------------------------------
     389             :  */
     390             : 
     391             : /*
     392             :  * Set up signal handling for a bootstrap process
     393             :  */
     394             : static void
     395         482 : bootstrap_signals(void)
     396             : {
     397             :     Assert(!IsUnderPostmaster);
     398             : 
     399             :     /*
     400             :      * We don't actually need any non-default signal handling in bootstrap
     401             :      * mode; "curl up and die" is a sufficient response for all these cases.
     402             :      * Let's set that handling explicitly, as documentation if nothing else.
     403             :      */
     404         482 :     pqsignal(SIGHUP, SIG_DFL);
     405         482 :     pqsignal(SIGINT, SIG_DFL);
     406         482 :     pqsignal(SIGTERM, SIG_DFL);
     407         482 :     pqsignal(SIGQUIT, SIG_DFL);
     408         482 : }
     409             : 
     410             : /* ----------------------------------------------------------------
     411             :  *              MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
     412             :  * ----------------------------------------------------------------
     413             :  */
     414             : 
     415             : /* ----------------
     416             :  *      boot_openrel
     417             :  *
     418             :  * Execute BKI OPEN command.
     419             :  * ----------------
     420             :  */
     421             : void
     422       28438 : boot_openrel(char *relname)
     423             : {
     424             :     int         i;
     425             : 
     426       28438 :     if (strlen(relname) >= NAMEDATALEN)
     427           0 :         relname[NAMEDATALEN - 1] = '\0';
     428             : 
     429             :     /*
     430             :      * pg_type must be filled before any OPEN command is executed, hence we
     431             :      * can now populate Typ if we haven't yet.
     432             :      */
     433       28438 :     if (Typ == NIL)
     434           0 :         populate_typ_list();
     435             : 
     436       28438 :     if (boot_reldesc != NULL)
     437           0 :         closerel(NULL);
     438             : 
     439       28438 :     elog(DEBUG4, "open relation %s, attrsize %d",
     440             :          relname, (int) ATTRIBUTE_FIXED_PART_SIZE);
     441             : 
     442       28438 :     boot_reldesc = table_openrv(makeRangeVar(NULL, relname, -1), NoLock);
     443       28438 :     numattr = RelationGetNumberOfAttributes(boot_reldesc);
     444      247266 :     for (i = 0; i < numattr; i++)
     445             :     {
     446      218828 :         if (attrtypes[i] == NULL)
     447           0 :             attrtypes[i] = AllocateAttribute();
     448      218828 :         memmove((char *) attrtypes[i],
     449      218828 :                 (char *) TupleDescAttr(boot_reldesc->rd_att, i),
     450             :                 ATTRIBUTE_FIXED_PART_SIZE);
     451             : 
     452             :         {
     453      218828 :             Form_pg_attribute at = attrtypes[i];
     454             : 
     455      218828 :             elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
     456             :                  i, NameStr(at->attname), at->attlen, at->attnum,
     457             :                  at->atttypid);
     458             :         }
     459             :     }
     460       28438 : }
     461             : 
     462             : /* ----------------
     463             :  *      closerel
     464             :  * ----------------
     465             :  */
     466             : void
     467       30366 : closerel(char *name)
     468             : {
     469       30366 :     if (name)
     470             :     {
     471       30366 :         if (boot_reldesc)
     472             :         {
     473       30366 :             if (strcmp(RelationGetRelationName(boot_reldesc), name) != 0)
     474           0 :                 elog(ERROR, "close of %s when %s was expected",
     475             :                      name, RelationGetRelationName(boot_reldesc));
     476             :         }
     477             :         else
     478           0 :             elog(ERROR, "close of %s before any relation was opened",
     479             :                  name);
     480             :     }
     481             : 
     482       30366 :     if (boot_reldesc == NULL)
     483           0 :         elog(ERROR, "no open relation to close");
     484             :     else
     485             :     {
     486       30366 :         elog(DEBUG4, "close relation %s",
     487             :              RelationGetRelationName(boot_reldesc));
     488       30366 :         table_close(boot_reldesc, NoLock);
     489       30366 :         boot_reldesc = NULL;
     490             :     }
     491       30366 : }
     492             : 
     493             : 
     494             : 
     495             : /* ----------------
     496             :  * DEFINEATTR()
     497             :  *
     498             :  * define a <field,type> pair
     499             :  * if there are n fields in a relation to be created, this routine
     500             :  * will be called n times
     501             :  * ----------------
     502             :  */
     503             : void
     504      277150 : DefineAttr(char *name, char *type, int attnum, int nullness)
     505             : {
     506             :     Oid         typeoid;
     507             : 
     508      277150 :     if (boot_reldesc != NULL)
     509             :     {
     510           0 :         elog(WARNING, "no open relations allowed with CREATE command");
     511           0 :         closerel(NULL);
     512             :     }
     513             : 
     514      277150 :     if (attrtypes[attnum] == NULL)
     515       15906 :         attrtypes[attnum] = AllocateAttribute();
     516     4157250 :     MemSet(attrtypes[attnum], 0, ATTRIBUTE_FIXED_PART_SIZE);
     517             : 
     518      277150 :     namestrcpy(&attrtypes[attnum]->attname, name);
     519      277150 :     elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
     520      277150 :     attrtypes[attnum]->attnum = attnum + 1;
     521             : 
     522      277150 :     typeoid = gettype(type);
     523             : 
     524      277150 :     if (Typ != NIL)
     525             :     {
     526      235216 :         attrtypes[attnum]->atttypid = Ap->am_oid;
     527      235216 :         attrtypes[attnum]->attlen = Ap->am_typ.typlen;
     528      235216 :         attrtypes[attnum]->attbyval = Ap->am_typ.typbyval;
     529      235216 :         attrtypes[attnum]->attalign = Ap->am_typ.typalign;
     530      235216 :         attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
     531      235216 :         attrtypes[attnum]->attcompression = InvalidCompressionMethod;
     532      235216 :         attrtypes[attnum]->attcollation = Ap->am_typ.typcollation;
     533             :         /* if an array type, assume 1-dimensional attribute */
     534      235216 :         if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
     535       21208 :             attrtypes[attnum]->attndims = 1;
     536             :         else
     537      214008 :             attrtypes[attnum]->attndims = 0;
     538             :     }
     539             :     else
     540             :     {
     541       41934 :         attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
     542       41934 :         attrtypes[attnum]->attlen = TypInfo[typeoid].len;
     543       41934 :         attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
     544       41934 :         attrtypes[attnum]->attalign = TypInfo[typeoid].align;
     545       41934 :         attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
     546       41934 :         attrtypes[attnum]->attcompression = InvalidCompressionMethod;
     547       41934 :         attrtypes[attnum]->attcollation = TypInfo[typeoid].collation;
     548             :         /* if an array type, assume 1-dimensional attribute */
     549       41934 :         if (TypInfo[typeoid].elem != InvalidOid &&
     550        6748 :             attrtypes[attnum]->attlen < 0)
     551        5302 :             attrtypes[attnum]->attndims = 1;
     552             :         else
     553       36632 :             attrtypes[attnum]->attndims = 0;
     554             :     }
     555             : 
     556             :     /*
     557             :      * If a system catalog column is collation-aware, force it to use C
     558             :      * collation, so that its behavior is independent of the database's
     559             :      * collation.  This is essential to allow template0 to be cloned with a
     560             :      * different database collation.
     561             :      */
     562      277150 :     if (OidIsValid(attrtypes[attnum]->attcollation))
     563       43862 :         attrtypes[attnum]->attcollation = C_COLLATION_OID;
     564             : 
     565      277150 :     attrtypes[attnum]->attstattarget = -1;
     566      277150 :     attrtypes[attnum]->attcacheoff = -1;
     567      277150 :     attrtypes[attnum]->atttypmod = -1;
     568      277150 :     attrtypes[attnum]->attislocal = true;
     569             : 
     570      277150 :     if (nullness == BOOTCOL_NULL_FORCE_NOT_NULL)
     571             :     {
     572       14942 :         attrtypes[attnum]->attnotnull = true;
     573             :     }
     574      262208 :     else if (nullness == BOOTCOL_NULL_FORCE_NULL)
     575             :     {
     576         964 :         attrtypes[attnum]->attnotnull = false;
     577             :     }
     578             :     else
     579             :     {
     580             :         Assert(nullness == BOOTCOL_NULL_AUTO);
     581             : 
     582             :         /*
     583             :          * Mark as "not null" if type is fixed-width and prior columns are
     584             :          * likewise fixed-width and not-null.  This corresponds to case where
     585             :          * column can be accessed directly via C struct declaration.
     586             :          */
     587      261244 :         if (attrtypes[attnum]->attlen > 0)
     588             :         {
     589             :             int         i;
     590             : 
     591             :             /* check earlier attributes */
     592     1593010 :             for (i = 0; i < attnum; i++)
     593             :             {
     594     1366952 :                 if (attrtypes[i]->attlen <= 0 ||
     595     1365506 :                     !attrtypes[i]->attnotnull)
     596             :                     break;
     597             :             }
     598      227504 :             if (i == attnum)
     599      226058 :                 attrtypes[attnum]->attnotnull = true;
     600             :         }
     601             :     }
     602      277150 : }
     603             : 
     604             : 
     605             : /* ----------------
     606             :  *      InsertOneTuple
     607             :  *
     608             :  * If objectid is not zero, it is a specific OID to assign to the tuple.
     609             :  * Otherwise, an OID will be assigned (if necessary) by heap_insert.
     610             :  * ----------------
     611             :  */
     612             : void
     613     5002196 : InsertOneTuple(void)
     614             : {
     615             :     HeapTuple   tuple;
     616             :     TupleDesc   tupDesc;
     617             :     int         i;
     618             : 
     619     5002196 :     elog(DEBUG4, "inserting row with %d columns", numattr);
     620             : 
     621     5002196 :     tupDesc = CreateTupleDesc(numattr, attrtypes);
     622     5002196 :     tuple = heap_form_tuple(tupDesc, values, Nulls);
     623     5002196 :     pfree(tupDesc);             /* just free's tupDesc, not the attrtypes */
     624             : 
     625     5002196 :     simple_heap_insert(boot_reldesc, tuple);
     626     5002196 :     heap_freetuple(tuple);
     627     5002196 :     elog(DEBUG4, "row inserted");
     628             : 
     629             :     /*
     630             :      * Reset null markers for next tuple
     631             :      */
     632    78995462 :     for (i = 0; i < numattr; i++)
     633    73993266 :         Nulls[i] = false;
     634     5002196 : }
     635             : 
     636             : /* ----------------
     637             :  *      InsertOneValue
     638             :  * ----------------
     639             :  */
     640             : void
     641    59566042 : InsertOneValue(char *value, int i)
     642             : {
     643             :     Oid         typoid;
     644             :     int16       typlen;
     645             :     bool        typbyval;
     646             :     char        typalign;
     647             :     char        typdelim;
     648             :     Oid         typioparam;
     649             :     Oid         typinput;
     650             :     Oid         typoutput;
     651             : 
     652             :     AssertArg(i >= 0 && i < MAXATTR);
     653             : 
     654    59566042 :     elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
     655             : 
     656    59566042 :     typoid = TupleDescAttr(boot_reldesc->rd_att, i)->atttypid;
     657             : 
     658    59566042 :     boot_get_type_io_data(typoid,
     659             :                           &typlen, &typbyval, &typalign,
     660             :                           &typdelim, &typioparam,
     661             :                           &typinput, &typoutput);
     662             : 
     663    59566042 :     values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
     664             : 
     665             :     /*
     666             :      * We use ereport not elog here so that parameters aren't evaluated unless
     667             :      * the message is going to be printed, which generally it isn't
     668             :      */
     669    59566042 :     ereport(DEBUG4,
     670             :             (errmsg_internal("inserted -> %s",
     671             :                              OidOutputFunctionCall(typoutput, values[i]))));
     672    59566042 : }
     673             : 
     674             : /* ----------------
     675             :  *      InsertOneNull
     676             :  * ----------------
     677             :  */
     678             : void
     679    14427224 : InsertOneNull(int i)
     680             : {
     681    14427224 :     elog(DEBUG4, "inserting column %d NULL", i);
     682             :     Assert(i >= 0 && i < MAXATTR);
     683    14427224 :     if (TupleDescAttr(boot_reldesc->rd_att, i)->attnotnull)
     684           0 :         elog(ERROR,
     685             :              "NULL value specified for not-null column \"%s\" of relation \"%s\"",
     686             :              NameStr(TupleDescAttr(boot_reldesc->rd_att, i)->attname),
     687             :              RelationGetRelationName(boot_reldesc));
     688    14427224 :     values[i] = PointerGetDatum(NULL);
     689    14427224 :     Nulls[i] = true;
     690    14427224 : }
     691             : 
     692             : /* ----------------
     693             :  *      cleanup
     694             :  * ----------------
     695             :  */
     696             : static void
     697         482 : cleanup(void)
     698             : {
     699         482 :     if (boot_reldesc != NULL)
     700           0 :         closerel(NULL);
     701         482 : }
     702             : 
     703             : /* ----------------
     704             :  *      populate_typ_list
     705             :  *
     706             :  * Load the Typ list by reading pg_type.
     707             :  * ----------------
     708             :  */
     709             : static void
     710         964 : populate_typ_list(void)
     711             : {
     712             :     Relation    rel;
     713             :     TableScanDesc scan;
     714             :     HeapTuple   tup;
     715             :     MemoryContext old;
     716             : 
     717             :     Assert(Typ == NIL);
     718             : 
     719         964 :     rel = table_open(TypeRelationId, NoLock);
     720         964 :     scan = table_beginscan_catalog(rel, 0, NULL);
     721         964 :     old = MemoryContextSwitchTo(TopMemoryContext);
     722      202440 :     while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
     723             :     {
     724      201476 :         Form_pg_type typForm = (Form_pg_type) GETSTRUCT(tup);
     725             :         struct typmap *newtyp;
     726             : 
     727      201476 :         newtyp = (struct typmap *) palloc(sizeof(struct typmap));
     728      201476 :         Typ = lappend(Typ, newtyp);
     729             : 
     730      201476 :         newtyp->am_oid = typForm->oid;
     731      201476 :         memcpy(&newtyp->am_typ, typForm, sizeof(newtyp->am_typ));
     732             :     }
     733         964 :     MemoryContextSwitchTo(old);
     734         964 :     table_endscan(scan);
     735         964 :     table_close(rel, NoLock);
     736         964 : }
     737             : 
     738             : /* ----------------
     739             :  *      gettype
     740             :  *
     741             :  * NB: this is really ugly; it will return an integer index into TypInfo[],
     742             :  * and not an OID at all, until the first reference to a type not known in
     743             :  * TypInfo[].  At that point it will read and cache pg_type in Typ,
     744             :  * and subsequently return a real OID (and set the global pointer Ap to
     745             :  * point at the found row in Typ).  So caller must check whether Typ is
     746             :  * still NIL to determine what the return value is!
     747             :  * ----------------
     748             :  */
     749             : static Oid
     750      277632 : gettype(char *type)
     751             : {
     752      277632 :     if (Typ != NIL)
     753             :     {
     754             :         ListCell   *lc;
     755             : 
     756     4416566 :         foreach(lc, Typ)
     757             :         {
     758     4416084 :             struct typmap *app = lfirst(lc);
     759             : 
     760     4416084 :             if (strncmp(NameStr(app->am_typ.typname), type, NAMEDATALEN) == 0)
     761             :             {
     762      234734 :                 Ap = app;
     763      234734 :                 return app->am_oid;
     764             :             }
     765             :         }
     766             : 
     767             :         /*
     768             :          * The type wasn't known; reload the pg_type contents and check again
     769             :          * to handle composite types, added since last populating the list.
     770             :          */
     771             : 
     772         482 :         list_free_deep(Typ);
     773         482 :         Typ = NIL;
     774         482 :         populate_typ_list();
     775             : 
     776             :         /*
     777             :          * Calling gettype would result in infinite recursion for types
     778             :          * missing in pg_type, so just repeat the lookup.
     779             :          */
     780      107486 :         foreach(lc, Typ)
     781             :         {
     782      107486 :             struct typmap *app = lfirst(lc);
     783             : 
     784      107486 :             if (strncmp(NameStr(app->am_typ.typname), type, NAMEDATALEN) == 0)
     785             :             {
     786         482 :                 Ap = app;
     787         482 :                 return app->am_oid;
     788             :             }
     789             :         }
     790             :     }
     791             :     else
     792             :     {
     793             :         int         i;
     794             : 
     795      401024 :         for (i = 0; i < n_types; i++)
     796             :         {
     797      400542 :             if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
     798       41934 :                 return i;
     799             :         }
     800             :         /* Not in TypInfo, so we'd better be able to read pg_type now */
     801         482 :         elog(DEBUG4, "external type: %s", type);
     802         482 :         populate_typ_list();
     803         482 :         return gettype(type);
     804             :     }
     805           0 :     elog(ERROR, "unrecognized type \"%s\"", type);
     806             :     /* not reached, here to make compiler happy */
     807             :     return 0;
     808             : }
     809             : 
     810             : /* ----------------
     811             :  *      boot_get_type_io_data
     812             :  *
     813             :  * Obtain type I/O information at bootstrap time.  This intentionally has
     814             :  * almost the same API as lsyscache.c's get_type_io_data, except that
     815             :  * we only support obtaining the typinput and typoutput routines, not
     816             :  * the binary I/O routines.  It is exported so that array_in and array_out
     817             :  * can be made to work during early bootstrap.
     818             :  * ----------------
     819             :  */
     820             : void
     821    59741972 : boot_get_type_io_data(Oid typid,
     822             :                       int16 *typlen,
     823             :                       bool *typbyval,
     824             :                       char *typalign,
     825             :                       char *typdelim,
     826             :                       Oid *typioparam,
     827             :                       Oid *typinput,
     828             :                       Oid *typoutput)
     829             : {
     830    59741972 :     if (Typ != NIL)
     831             :     {
     832             :         /* We have the boot-time contents of pg_type, so use it */
     833    24129884 :         struct typmap *ap = NULL;
     834             :         ListCell   *lc;
     835             : 
     836   213929916 :         foreach(lc, Typ)
     837             :         {
     838   213929916 :             ap = lfirst(lc);
     839   213929916 :             if (ap->am_oid == typid)
     840    24129884 :                 break;
     841             :         }
     842             : 
     843    24129884 :         if (!ap || ap->am_oid != typid)
     844           0 :             elog(ERROR, "type OID %u not found in Typ list", typid);
     845             : 
     846    24129884 :         *typlen = ap->am_typ.typlen;
     847    24129884 :         *typbyval = ap->am_typ.typbyval;
     848    24129884 :         *typalign = ap->am_typ.typalign;
     849    24129884 :         *typdelim = ap->am_typ.typdelim;
     850             : 
     851             :         /* XXX this logic must match getTypeIOParam() */
     852    24129884 :         if (OidIsValid(ap->am_typ.typelem))
     853      699382 :             *typioparam = ap->am_typ.typelem;
     854             :         else
     855    23430502 :             *typioparam = typid;
     856             : 
     857    24129884 :         *typinput = ap->am_typ.typinput;
     858    24129884 :         *typoutput = ap->am_typ.typoutput;
     859             :     }
     860             :     else
     861             :     {
     862             :         /* We don't have pg_type yet, so use the hard-wired TypInfo array */
     863             :         int         typeindex;
     864             : 
     865   284252270 :         for (typeindex = 0; typeindex < n_types; typeindex++)
     866             :         {
     867   284252270 :             if (TypInfo[typeindex].oid == typid)
     868    35612088 :                 break;
     869             :         }
     870    35612088 :         if (typeindex >= n_types)
     871           0 :             elog(ERROR, "type OID %u not found in TypInfo", typid);
     872             : 
     873    35612088 :         *typlen = TypInfo[typeindex].len;
     874    35612088 :         *typbyval = TypInfo[typeindex].byval;
     875    35612088 :         *typalign = TypInfo[typeindex].align;
     876             :         /* We assume typdelim is ',' for all boot-time types */
     877    35612088 :         *typdelim = ',';
     878             : 
     879             :         /* XXX this logic must match getTypeIOParam() */
     880    35612088 :         if (OidIsValid(TypInfo[typeindex].elem))
     881     3366288 :             *typioparam = TypInfo[typeindex].elem;
     882             :         else
     883    32245800 :             *typioparam = typid;
     884             : 
     885    35612088 :         *typinput = TypInfo[typeindex].inproc;
     886    35612088 :         *typoutput = TypInfo[typeindex].outproc;
     887             :     }
     888    59741972 : }
     889             : 
     890             : /* ----------------
     891             :  *      AllocateAttribute
     892             :  *
     893             :  * Note: bootstrap never sets any per-column ACLs, so we only need
     894             :  * ATTRIBUTE_FIXED_PART_SIZE space per attribute.
     895             :  * ----------------
     896             :  */
     897             : static Form_pg_attribute
     898       15906 : AllocateAttribute(void)
     899             : {
     900       15906 :     return (Form_pg_attribute)
     901       15906 :         MemoryContextAllocZero(TopMemoryContext, ATTRIBUTE_FIXED_PART_SIZE);
     902             : }
     903             : 
     904             : /*
     905             :  *  index_register() -- record an index that has been set up for building
     906             :  *                      later.
     907             :  *
     908             :  *      At bootstrap time, we define a bunch of indexes on system catalogs.
     909             :  *      We postpone actually building the indexes until just before we're
     910             :  *      finished with initialization, however.  This is because the indexes
     911             :  *      themselves have catalog entries, and those have to be included in the
     912             :  *      indexes on those catalogs.  Doing it in two phases is the simplest
     913             :  *      way of making sure the indexes have the right contents at the end.
     914             :  */
     915             : void
     916       73746 : index_register(Oid heap,
     917             :                Oid ind,
     918             :                IndexInfo *indexInfo)
     919             : {
     920             :     IndexList  *newind;
     921             :     MemoryContext oldcxt;
     922             : 
     923             :     /*
     924             :      * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
     925             :      * bootstrap time.  we'll declare the indexes now, but want to create them
     926             :      * later.
     927             :      */
     928             : 
     929       73746 :     if (nogc == NULL)
     930         482 :         nogc = AllocSetContextCreate(NULL,
     931             :                                      "BootstrapNoGC",
     932             :                                      ALLOCSET_DEFAULT_SIZES);
     933             : 
     934       73746 :     oldcxt = MemoryContextSwitchTo(nogc);
     935             : 
     936       73746 :     newind = (IndexList *) palloc(sizeof(IndexList));
     937       73746 :     newind->il_heap = heap;
     938       73746 :     newind->il_ind = ind;
     939       73746 :     newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo));
     940             : 
     941       73746 :     memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
     942             :     /* expressions will likely be null, but may as well copy it */
     943      147492 :     newind->il_info->ii_Expressions =
     944       73746 :         copyObject(indexInfo->ii_Expressions);
     945       73746 :     newind->il_info->ii_ExpressionsState = NIL;
     946             :     /* predicate will likely be null, but may as well copy it */
     947      147492 :     newind->il_info->ii_Predicate =
     948       73746 :         copyObject(indexInfo->ii_Predicate);
     949       73746 :     newind->il_info->ii_PredicateState = NULL;
     950             :     /* no exclusion constraints at bootstrap time, so no need to copy */
     951             :     Assert(indexInfo->ii_ExclusionOps == NULL);
     952             :     Assert(indexInfo->ii_ExclusionProcs == NULL);
     953             :     Assert(indexInfo->ii_ExclusionStrats == NULL);
     954             : 
     955       73746 :     newind->il_next = ILHead;
     956       73746 :     ILHead = newind;
     957             : 
     958       73746 :     MemoryContextSwitchTo(oldcxt);
     959       73746 : }
     960             : 
     961             : 
     962             : /*
     963             :  * build_indices -- fill in all the indexes registered earlier
     964             :  */
     965             : void
     966         482 : build_indices(void)
     967             : {
     968       74228 :     for (; ILHead != NULL; ILHead = ILHead->il_next)
     969             :     {
     970             :         Relation    heap;
     971             :         Relation    ind;
     972             : 
     973             :         /* need not bother with locks during bootstrap */
     974       73746 :         heap = table_open(ILHead->il_heap, NoLock);
     975       73746 :         ind = index_open(ILHead->il_ind, NoLock);
     976             : 
     977       73746 :         index_build(heap, ind, ILHead->il_info, false, false);
     978             : 
     979       73746 :         index_close(ind, NoLock);
     980       73746 :         table_close(heap, NoLock);
     981             :     }
     982         482 : }

Generated by: LCOV version 1.14