1 /*-------------------------------------------------------------------------
4 * routines to support running postgres in 'bootstrap' mode
5 * bootstrap mode is used to create the initial template database
7 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/bootstrap/bootstrap.c
13 *-------------------------------------------------------------------------
24 #include "access/genam.h"
25 #include "access/heapam.h"
26 #include "access/xact.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 "libpq/pqsignal.h"
32 #include "miscadmin.h"
33 #include "nodes/makefuncs.h"
34 #include "postmaster/bgwriter.h"
35 #include "postmaster/walwriter.h"
36 #include "replication/walreceiver.h"
37 #include "storage/bufmgr.h"
38 #include "storage/ipc.h"
39 #include "storage/proc.h"
40 #include "storage/procsignal.h"
41 #include "tcop/tcopprot.h"
42 #include "utils/builtins.h"
43 #include "utils/fmgroids.h"
44 #include "utils/memutils.h"
45 #include "utils/ps_status.h"
46 #include "utils/rel.h"
47 #include "utils/relmapper.h"
48 #include "utils/tqual.h"
54 #define ALLOC(t, c) ((t *) calloc((unsigned)(c), sizeof(t)))
56 static void CheckerModeMain(void);
57 static void BootstrapModeMain(void);
58 static void bootstrap_signals(void);
59 static void ShutdownAuxiliaryProcess(int code, Datum arg);
60 static Form_pg_attribute AllocateAttribute(void);
61 static Oid gettype(char *type);
62 static void cleanup(void);
69 Relation boot_reldesc; /* current relation descriptor */
71 Form_pg_attribute attrtypes[MAXATTR]; /* points to attribute info */
72 int numattr; /* number of attributes for cur. rel */
76 * Basic information associated with each type. This is used before
77 * pg_type is filled, so it has to cover the datatypes used as column types
78 * in the core "bootstrapped" catalogs.
80 * XXX several of these input/output functions do catalog scans
81 * (e.g., F_REGPROCIN scans pg_proc). this obviously creates some
82 * order dependencies in the catalog creation process.
86 char name[NAMEDATALEN];
98 static const struct typinfo TypInfo[] = {
99 {"bool", BOOLOID, 0, 1, true, 'c', 'p', InvalidOid,
100 F_BOOLIN, F_BOOLOUT},
101 {"bytea", BYTEAOID, 0, -1, false, 'i', 'x', InvalidOid,
102 F_BYTEAIN, F_BYTEAOUT},
103 {"char", CHAROID, 0, 1, true, 'c', 'p', InvalidOid,
104 F_CHARIN, F_CHAROUT},
105 {"int2", INT2OID, 0, 2, true, 's', 'p', InvalidOid,
106 F_INT2IN, F_INT2OUT},
107 {"int4", INT4OID, 0, 4, true, 'i', 'p', InvalidOid,
108 F_INT4IN, F_INT4OUT},
109 {"float4", FLOAT4OID, 0, 4, FLOAT4PASSBYVAL, 'i', 'p', InvalidOid,
110 F_FLOAT4IN, F_FLOAT4OUT},
111 {"name", NAMEOID, CHAROID, NAMEDATALEN, false, 'c', 'p', InvalidOid,
112 F_NAMEIN, F_NAMEOUT},
113 {"regclass", REGCLASSOID, 0, 4, true, 'i', 'p', InvalidOid,
114 F_REGCLASSIN, F_REGCLASSOUT},
115 {"regproc", REGPROCOID, 0, 4, true, 'i', 'p', InvalidOid,
116 F_REGPROCIN, F_REGPROCOUT},
117 {"regtype", REGTYPEOID, 0, 4, true, 'i', 'p', InvalidOid,
118 F_REGTYPEIN, F_REGTYPEOUT},
119 {"text", TEXTOID, 0, -1, false, 'i', 'x', DEFAULT_COLLATION_OID,
120 F_TEXTIN, F_TEXTOUT},
121 {"oid", OIDOID, 0, 4, true, 'i', 'p', InvalidOid,
123 {"tid", TIDOID, 0, 6, false, 's', 'p', InvalidOid,
125 {"xid", XIDOID, 0, 4, true, 'i', 'p', InvalidOid,
127 {"cid", CIDOID, 0, 4, true, 'i', 'p', InvalidOid,
129 {"pg_node_tree", PGNODETREEOID, 0, -1, false, 'i', 'x', DEFAULT_COLLATION_OID,
130 F_PG_NODE_TREE_IN, F_PG_NODE_TREE_OUT},
131 {"int2vector", INT2VECTOROID, INT2OID, -1, false, 'i', 'p', InvalidOid,
132 F_INT2VECTORIN, F_INT2VECTOROUT},
133 {"oidvector", OIDVECTOROID, OIDOID, -1, false, 'i', 'p', InvalidOid,
134 F_OIDVECTORIN, F_OIDVECTOROUT},
135 {"_int4", INT4ARRAYOID, INT4OID, -1, false, 'i', 'x', InvalidOid,
136 F_ARRAY_IN, F_ARRAY_OUT},
137 {"_text", 1009, TEXTOID, -1, false, 'i', 'x', DEFAULT_COLLATION_OID,
138 F_ARRAY_IN, F_ARRAY_OUT},
139 {"_oid", 1028, OIDOID, -1, false, 'i', 'x', InvalidOid,
140 F_ARRAY_IN, F_ARRAY_OUT},
141 {"_char", 1002, CHAROID, -1, false, 'i', 'x', InvalidOid,
142 F_ARRAY_IN, F_ARRAY_OUT},
143 {"_aclitem", 1034, ACLITEMOID, -1, false, 'i', 'x', InvalidOid,
144 F_ARRAY_IN, F_ARRAY_OUT}
147 static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
152 FormData_pg_type am_typ;
155 static struct typmap **Typ = NULL;
156 static struct typmap *Ap = NULL;
158 static Datum values[MAXATTR]; /* current row's attribute values */
159 static bool Nulls[MAXATTR];
161 static MemoryContext nogc = NULL; /* special no-gc mem context */
164 * At bootstrap time, we first declare all the indices to be built, and
165 * then build them. The IndexList structure stores enough information
166 * to allow us to build the indices after they've been declared.
169 typedef struct _IndexList
174 struct _IndexList *il_next;
177 static IndexList *ILHead = NULL;
181 * AuxiliaryProcessMain
183 * The main entry point for auxiliary processes, such as the bgwriter,
184 * walwriter, walreceiver, bootstrapper and the shared memory checker code.
186 * This code is here just because of historical reasons.
189 AuxiliaryProcessMain(int argc, char *argv[])
191 char *progname = argv[0];
193 AuxProcType auxType = CheckerProcess;
194 char *userDoption = NULL;
199 MyProcPid = getpid();
201 MyStartTime = time(NULL);
204 * Fire up essential subsystems: error and memory management
206 * If we are running under the postmaster, this is done already.
208 if (!IsUnderPostmaster)
211 /* Compute paths, if we didn't inherit them from postmaster */
212 if (my_exec_path[0] == '\0')
214 if (find_my_exec(progname, my_exec_path) < 0)
215 elog(FATAL, "%s: could not locate my own executable path",
220 * process command arguments
223 /* Set defaults, to be overriden by explicit options below */
224 if (!IsUnderPostmaster)
225 InitializeGUCOptions();
227 /* Ignore the initial --boot argument, if present */
228 if (argc > 1 && strcmp(argv[1], "--boot") == 0)
234 while ((flag = getopt(argc, argv, "B:c:d:D:Fr:x:-:")) != -1)
239 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
242 userDoption = optarg;
246 /* Turn on debugging for the bootstrap process. */
247 char *debugstr = palloc(strlen("debug") + strlen(optarg) + 1);
249 sprintf(debugstr, "debug%s", optarg);
250 SetConfigOption("log_min_messages", debugstr,
251 PGC_POSTMASTER, PGC_S_ARGV);
252 SetConfigOption("client_min_messages", debugstr,
253 PGC_POSTMASTER, PGC_S_ARGV);
258 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
261 strlcpy(OutputFileName, optarg, MAXPGPATH);
264 auxType = atoi(optarg);
272 ParseLongOption(optarg, &name, &value);
277 (errcode(ERRCODE_SYNTAX_ERROR),
278 errmsg("--%s requires a value",
282 (errcode(ERRCODE_SYNTAX_ERROR),
283 errmsg("-c %s requires a value",
287 SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
294 write_stderr("Try \"%s --help\" for more information.\n",
303 write_stderr("%s: invalid command-line arguments\n", progname);
308 * Identify myself via ps
310 if (IsUnderPostmaster)
317 statmsg = "startup process";
319 case BgWriterProcess:
320 statmsg = "writer process";
322 case WalWriterProcess:
323 statmsg = "wal writer process";
325 case WalReceiverProcess:
326 statmsg = "wal receiver process";
329 statmsg = "??? process";
332 init_ps_display(statmsg, "", "", "");
335 /* Acquire configuration parameters, unless inherited from postmaster */
336 if (!IsUnderPostmaster)
338 if (!SelectConfigFiles(userDoption, progname))
340 /* If timezone is not set, determine what the OS uses */
341 pg_timezone_initialize();
342 /* If timezone_abbreviations is not set, select default */
343 pg_timezone_abbrev_initialize();
346 /* Validate we have been given a reasonable-looking DataDir */
348 ValidatePgVersion(DataDir);
350 /* Change into DataDir (if under postmaster, should be done already) */
351 if (!IsUnderPostmaster)
354 /* If standalone, create lockfile for data directory */
355 if (!IsUnderPostmaster)
356 CreateDataDirLockFile(false);
358 SetProcessingMode(BootstrapProcessing);
359 IgnoreSystemIndexes = true;
364 * When we are an auxiliary process, we aren't going to do the full
365 * InitPostgres pushups, but there are a couple of things that need to get
366 * lit up even in an auxiliary process.
368 if (IsUnderPostmaster)
371 * Create a PGPROC so we can use LWLocks. In the EXEC_BACKEND case,
372 * this was already done by SubPostmasterMain().
375 InitAuxiliaryProcess();
379 * Assign the ProcSignalSlot for an auxiliary process. Since it
380 * doesn't have a BackendId, the slot is statically allocated based on
381 * the auxiliary process type (auxType). Backends use slots indexed
382 * in the range from 1 to MaxBackends (inclusive), so we use
383 * MaxBackends + AuxProcType + 1 as the index of the slot for an
386 * This will need rethinking if we ever want more than one of a
387 * particular auxiliary process type.
389 ProcSignalInit(MaxBackends + auxType + 1);
391 /* finish setting up bufmgr.c */
392 InitBufferPoolBackend();
394 /* register a shutdown callback for LWLock cleanup */
395 on_shmem_exit(ShutdownAuxiliaryProcess, 0);
401 SetProcessingMode(NormalProcessing);
406 /* don't set signals, they're useless here */
408 proc_exit(1); /* should never return */
410 case BootstrapProcess:
414 proc_exit(1); /* should never return */
417 /* don't set signals, startup process has its own agenda */
418 StartupProcessMain();
419 proc_exit(1); /* should never return */
421 case BgWriterProcess:
422 /* don't set signals, bgwriter has its own agenda */
423 BackgroundWriterMain();
424 proc_exit(1); /* should never return */
426 case WalWriterProcess:
427 /* don't set signals, walwriter has its own agenda */
430 proc_exit(1); /* should never return */
432 case WalReceiverProcess:
433 /* don't set signals, walreceiver has its own agenda */
435 proc_exit(1); /* should never return */
438 elog(PANIC, "unrecognized process type: %d", auxType);
444 * In shared memory checker mode, all we really want to do is create shared
445 * memory and semaphores (just to prove we can do it with the current GUC
446 * settings). Since, in fact, that was already done by BaseInit(),
447 * we have nothing more to do here.
450 CheckerModeMain(void)
456 * The main entry point for running the backend in bootstrap mode
458 * The bootstrap mode is used to initialize the template database.
459 * The bootstrap backend doesn't speak SQL, but instead expects
460 * commands in a special bootstrap language.
463 BootstrapModeMain(void)
467 Assert(!IsUnderPostmaster);
469 SetProcessingMode(BootstrapProcessing);
472 * Do backend-like initialization for bootstrap mode
476 InitPostgres(NULL, InvalidOid, NULL, NULL);
478 /* Initialize stuff for bootstrap-file processing */
479 for (i = 0; i < MAXATTR; i++)
486 * Process bootstrap input.
491 * We should now know about all mapped relations, so it's okay to write
492 * out the initial relation mapping files.
494 RelationMapFinishBootstrap();
496 /* Clean up and exit */
502 /* ----------------------------------------------------------------
504 * ----------------------------------------------------------------
508 * Set up signal handling for a bootstrap process
511 bootstrap_signals(void)
513 if (IsUnderPostmaster)
516 * If possible, make this process a group leader, so that the
517 * postmaster can signal any child processes too.
521 elog(FATAL, "setsid() failed: %m");
525 * Properly accept or ignore signals the postmaster might send us
527 pqsignal(SIGHUP, SIG_IGN);
528 pqsignal(SIGINT, SIG_IGN); /* ignore query-cancel */
529 pqsignal(SIGTERM, die);
530 pqsignal(SIGQUIT, quickdie);
531 pqsignal(SIGALRM, SIG_IGN);
532 pqsignal(SIGPIPE, SIG_IGN);
533 pqsignal(SIGUSR1, SIG_IGN);
534 pqsignal(SIGUSR2, SIG_IGN);
537 * Reset some signals that are accepted by postmaster but not here
539 pqsignal(SIGCHLD, SIG_DFL);
540 pqsignal(SIGTTIN, SIG_DFL);
541 pqsignal(SIGTTOU, SIG_DFL);
542 pqsignal(SIGCONT, SIG_DFL);
543 pqsignal(SIGWINCH, SIG_DFL);
546 * Unblock signals (they were blocked when the postmaster forked us)
548 PG_SETMASK(&UnBlockSig);
552 /* Set up appropriately for interactive use */
553 pqsignal(SIGHUP, die);
554 pqsignal(SIGINT, die);
555 pqsignal(SIGTERM, die);
556 pqsignal(SIGQUIT, die);
561 * Begin shutdown of an auxiliary process. This is approximately the equivalent
562 * of ShutdownPostgres() in postinit.c. We can't run transactions in an
563 * auxiliary process, so most of the work of AbortTransaction() is not needed,
564 * but we do need to make sure we've released any LWLocks we are holding.
565 * (This is only critical during an error exit.)
568 ShutdownAuxiliaryProcess(int code, Datum arg)
573 /* ----------------------------------------------------------------
574 * MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
575 * ----------------------------------------------------------------
583 boot_openrel(char *relname)
591 if (strlen(relname) >= NAMEDATALEN)
592 relname[NAMEDATALEN - 1] = '\0';
596 /* We can now load the pg_type data */
597 rel = heap_open(TypeRelationId, NoLock);
598 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
600 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
603 app = Typ = ALLOC(struct typmap *, i + 1);
605 *app++ = ALLOC(struct typmap, 1);
607 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
609 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
611 (*app)->am_oid = HeapTupleGetOid(tup);
612 memcpy((char *) &(*app)->am_typ,
613 (char *) GETSTRUCT(tup),
614 sizeof((*app)->am_typ));
618 heap_close(rel, NoLock);
621 if (boot_reldesc != NULL)
624 elog(DEBUG4, "open relation %s, attrsize %d",
625 relname, (int) ATTRIBUTE_FIXED_PART_SIZE);
627 boot_reldesc = heap_openrv(makeRangeVar(NULL, relname, -1), NoLock);
628 numattr = boot_reldesc->rd_rel->relnatts;
629 for (i = 0; i < numattr; i++)
631 if (attrtypes[i] == NULL)
632 attrtypes[i] = AllocateAttribute();
633 memmove((char *) attrtypes[i],
634 (char *) boot_reldesc->rd_att->attrs[i],
635 ATTRIBUTE_FIXED_PART_SIZE);
638 Form_pg_attribute at = attrtypes[i];
640 elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
641 i, NameStr(at->attname), at->attlen, at->attnum,
658 if (strcmp(RelationGetRelationName(boot_reldesc), name) != 0)
659 elog(ERROR, "close of %s when %s was expected",
660 name, RelationGetRelationName(boot_reldesc));
663 elog(ERROR, "close of %s before any relation was opened",
667 if (boot_reldesc == NULL)
668 elog(ERROR, "no open relation to close");
671 elog(DEBUG4, "close relation %s",
672 RelationGetRelationName(boot_reldesc));
673 heap_close(boot_reldesc, NoLock);
683 * define a <field,type> pair
684 * if there are n fields in a relation to be created, this routine
685 * will be called n times
689 DefineAttr(char *name, char *type, int attnum)
693 if (boot_reldesc != NULL)
695 elog(WARNING, "no open relations allowed with CREATE command");
699 if (attrtypes[attnum] == NULL)
700 attrtypes[attnum] = AllocateAttribute();
701 MemSet(attrtypes[attnum], 0, ATTRIBUTE_FIXED_PART_SIZE);
703 namestrcpy(&attrtypes[attnum]->attname, name);
704 elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
705 attrtypes[attnum]->attnum = attnum + 1; /* fillatt */
707 typeoid = gettype(type);
711 attrtypes[attnum]->atttypid = Ap->am_oid;
712 attrtypes[attnum]->attlen = Ap->am_typ.typlen;
713 attrtypes[attnum]->attbyval = Ap->am_typ.typbyval;
714 attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
715 attrtypes[attnum]->attalign = Ap->am_typ.typalign;
716 attrtypes[attnum]->attcollation = Ap->am_typ.typcollation;
717 /* if an array type, assume 1-dimensional attribute */
718 if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
719 attrtypes[attnum]->attndims = 1;
721 attrtypes[attnum]->attndims = 0;
725 attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
726 attrtypes[attnum]->attlen = TypInfo[typeoid].len;
727 attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
728 attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
729 attrtypes[attnum]->attalign = TypInfo[typeoid].align;
730 attrtypes[attnum]->attcollation = TypInfo[typeoid].collation;
731 /* if an array type, assume 1-dimensional attribute */
732 if (TypInfo[typeoid].elem != InvalidOid &&
733 attrtypes[attnum]->attlen < 0)
734 attrtypes[attnum]->attndims = 1;
736 attrtypes[attnum]->attndims = 0;
739 attrtypes[attnum]->attstattarget = -1;
740 attrtypes[attnum]->attcacheoff = -1;
741 attrtypes[attnum]->atttypmod = -1;
742 attrtypes[attnum]->attislocal = true;
745 * Mark as "not null" if type is fixed-width and prior columns are too.
746 * This corresponds to case where column can be accessed directly via C
747 * struct declaration.
749 * oidvector and int2vector are also treated as not-nullable, even though
750 * they are no longer fixed-width.
752 #define MARKNOTNULL(att) \
753 ((att)->attlen > 0 || \
754 (att)->atttypid == OIDVECTOROID || \
755 (att)->atttypid == INT2VECTOROID)
757 if (MARKNOTNULL(attrtypes[attnum]))
761 for (i = 0; i < attnum; i++)
763 if (!MARKNOTNULL(attrtypes[i]))
767 attrtypes[attnum]->attnotnull = true;
775 * If objectid is not zero, it is a specific OID to assign to the tuple.
776 * Otherwise, an OID will be assigned (if necessary) by heap_insert.
780 InsertOneTuple(Oid objectid)
786 elog(DEBUG4, "inserting row oid %u, %d columns", objectid, numattr);
788 tupDesc = CreateTupleDesc(numattr,
789 RelationGetForm(boot_reldesc)->relhasoids,
791 tuple = heap_form_tuple(tupDesc, values, Nulls);
792 if (objectid != (Oid) 0)
793 HeapTupleSetOid(tuple, objectid);
794 pfree(tupDesc); /* just free's tupDesc, not the attrtypes */
796 simple_heap_insert(boot_reldesc, tuple);
797 heap_freetuple(tuple);
798 elog(DEBUG4, "row inserted");
801 * Reset null markers for next tuple
803 for (i = 0; i < numattr; i++)
812 InsertOneValue(char *value, int i)
824 AssertArg(i >= 0 || i < MAXATTR);
826 elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
828 typoid = boot_reldesc->rd_att->attrs[i]->atttypid;
830 boot_get_type_io_data(typoid,
831 &typlen, &typbyval, &typalign,
832 &typdelim, &typioparam,
833 &typinput, &typoutput);
835 values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
836 prt = OidOutputFunctionCall(typoutput, values[i]);
837 elog(DEBUG4, "inserted -> %s", prt);
848 elog(DEBUG4, "inserting column %d NULL", i);
849 Assert(i >= 0 || i < MAXATTR);
850 values[i] = PointerGetDatum(NULL);
861 if (boot_reldesc != NULL)
868 * NB: this is really ugly; it will return an integer index into TypInfo[],
869 * and not an OID at all, until the first reference to a type not known in
870 * TypInfo[]. At that point it will read and cache pg_type in the Typ array,
871 * and subsequently return a real OID (and set the global pointer Ap to
872 * point at the found row in Typ). So caller must check whether Typ is
873 * still NULL to determine what the return value is!
887 for (app = Typ; *app != NULL; app++)
889 if (strncmp(NameStr((*app)->am_typ.typname), type, NAMEDATALEN) == 0)
892 return (*app)->am_oid;
898 for (i = 0; i < n_types; i++)
900 if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
903 elog(DEBUG4, "external type: %s", type);
904 rel = heap_open(TypeRelationId, NoLock);
905 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
907 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
910 app = Typ = ALLOC(struct typmap *, i + 1);
912 *app++ = ALLOC(struct typmap, 1);
914 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
916 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
918 (*app)->am_oid = HeapTupleGetOid(tup);
919 memmove((char *) &(*app++)->am_typ,
920 (char *) GETSTRUCT(tup),
921 sizeof((*app)->am_typ));
924 heap_close(rel, NoLock);
925 return gettype(type);
927 elog(ERROR, "unrecognized type \"%s\"", type);
928 /* not reached, here to make compiler happy */
933 * boot_get_type_io_data
935 * Obtain type I/O information at bootstrap time. This intentionally has
936 * almost the same API as lsyscache.c's get_type_io_data, except that
937 * we only support obtaining the typinput and typoutput routines, not
938 * the binary I/O routines. It is exported so that array_in and array_out
939 * can be made to work during early bootstrap.
943 boot_get_type_io_data(Oid typid,
954 /* We have the boot-time contents of pg_type, so use it */
959 while (*app && (*app)->am_oid != typid)
963 elog(ERROR, "type OID %u not found in Typ list", typid);
965 *typlen = ap->am_typ.typlen;
966 *typbyval = ap->am_typ.typbyval;
967 *typalign = ap->am_typ.typalign;
968 *typdelim = ap->am_typ.typdelim;
970 /* XXX this logic must match getTypeIOParam() */
971 if (OidIsValid(ap->am_typ.typelem))
972 *typioparam = ap->am_typ.typelem;
976 *typinput = ap->am_typ.typinput;
977 *typoutput = ap->am_typ.typoutput;
981 /* We don't have pg_type yet, so use the hard-wired TypInfo array */
984 for (typeindex = 0; typeindex < n_types; typeindex++)
986 if (TypInfo[typeindex].oid == typid)
989 if (typeindex >= n_types)
990 elog(ERROR, "type OID %u not found in TypInfo", typid);
992 *typlen = TypInfo[typeindex].len;
993 *typbyval = TypInfo[typeindex].byval;
994 *typalign = TypInfo[typeindex].align;
995 /* We assume typdelim is ',' for all boot-time types */
998 /* XXX this logic must match getTypeIOParam() */
999 if (OidIsValid(TypInfo[typeindex].elem))
1000 *typioparam = TypInfo[typeindex].elem;
1002 *typioparam = typid;
1004 *typinput = TypInfo[typeindex].inproc;
1005 *typoutput = TypInfo[typeindex].outproc;
1012 * Note: bootstrap never sets any per-column ACLs, so we only need
1013 * ATTRIBUTE_FIXED_PART_SIZE space per attribute.
1016 static Form_pg_attribute
1017 AllocateAttribute(void)
1019 Form_pg_attribute attribute = (Form_pg_attribute) malloc(ATTRIBUTE_FIXED_PART_SIZE);
1021 if (!PointerIsValid(attribute))
1022 elog(FATAL, "out of memory");
1023 MemSet(attribute, 0, ATTRIBUTE_FIXED_PART_SIZE);
1030 * XXX arrays of "basetype" are always "_basetype".
1031 * this is an evil hack inherited from rel. 3.1.
1032 * XXX array dimension is thrown away because we
1033 * don't support fixed-dimension arrays. again,
1034 * sickness from 3.1.
1036 * the string passed in must have a '[' character in it
1038 * the string returned is a pointer to static storage and should NOT
1039 * be freed by the CALLER.
1043 MapArrayTypeName(char *s)
1047 static char newStr[NAMEDATALEN]; /* array type names < NAMEDATALEN long */
1049 if (s == NULL || s[0] == '\0')
1054 for (i = 0; i < NAMEDATALEN - 1 && s[i] != '['; i++, j++)
1064 * index_register() -- record an index that has been set up for building
1067 * At bootstrap time, we define a bunch of indexes on system catalogs.
1068 * We postpone actually building the indexes until just before we're
1069 * finished with initialization, however. This is because the indexes
1070 * themselves have catalog entries, and those have to be included in the
1071 * indexes on those catalogs. Doing it in two phases is the simplest
1072 * way of making sure the indexes have the right contents at the end.
1075 index_register(Oid heap,
1077 IndexInfo *indexInfo)
1080 MemoryContext oldcxt;
1083 * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
1084 * bootstrap time. we'll declare the indexes now, but want to create them
1089 nogc = AllocSetContextCreate(NULL,
1091 ALLOCSET_DEFAULT_MINSIZE,
1092 ALLOCSET_DEFAULT_INITSIZE,
1093 ALLOCSET_DEFAULT_MAXSIZE);
1095 oldcxt = MemoryContextSwitchTo(nogc);
1097 newind = (IndexList *) palloc(sizeof(IndexList));
1098 newind->il_heap = heap;
1099 newind->il_ind = ind;
1100 newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo));
1102 memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
1103 /* expressions will likely be null, but may as well copy it */
1104 newind->il_info->ii_Expressions = (List *)
1105 copyObject(indexInfo->ii_Expressions);
1106 newind->il_info->ii_ExpressionsState = NIL;
1107 /* predicate will likely be null, but may as well copy it */
1108 newind->il_info->ii_Predicate = (List *)
1109 copyObject(indexInfo->ii_Predicate);
1110 newind->il_info->ii_PredicateState = NIL;
1111 /* no exclusion constraints at bootstrap time, so no need to copy */
1112 Assert(indexInfo->ii_ExclusionOps == NULL);
1113 Assert(indexInfo->ii_ExclusionProcs == NULL);
1114 Assert(indexInfo->ii_ExclusionStrats == NULL);
1116 newind->il_next = ILHead;
1119 MemoryContextSwitchTo(oldcxt);
1124 * build_indices -- fill in all the indexes registered earlier
1129 for (; ILHead != NULL; ILHead = ILHead->il_next)
1134 /* need not bother with locks during bootstrap */
1135 heap = heap_open(ILHead->il_heap, NoLock);
1136 ind = index_open(ILHead->il_ind, NoLock);
1138 index_build(heap, ind, ILHead->il_info, false, false);
1140 index_close(ind, NoLock);
1141 heap_close(heap, NoLock);