OSDN Git Service

fc093ccc4b9aa339f0fa093e1d6d24cb40ff2699
[pg-rex/syncrep.git] / src / backend / bootstrap / bootstrap.c
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-2011, 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 <time.h>
18 #include <unistd.h>
19 #include <signal.h>
20 #ifdef HAVE_GETOPT_H
21 #include <getopt.h>
22 #endif
23
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/relmapper.h"
47 #include "utils/tqual.h"
48
49 extern int      optind;
50 extern char *optarg;
51
52
53 #define ALLOC(t, c)             ((t *) calloc((unsigned)(c), sizeof(t)))
54
55 static void CheckerModeMain(void);
56 static void BootstrapModeMain(void);
57 static void bootstrap_signals(void);
58 static void ShutdownAuxiliaryProcess(int code, Datum arg);
59 static Form_pg_attribute AllocateAttribute(void);
60 static Oid      gettype(char *type);
61 static void cleanup(void);
62
63 /* ----------------
64  *              global variables
65  * ----------------
66  */
67
68 Relation        boot_reldesc;           /* current relation descriptor */
69
70 Form_pg_attribute attrtypes[MAXATTR];   /* points to attribute info */
71 int                     numattr;                        /* number of attributes for cur. rel */
72
73
74 /*
75  * Basic information associated with each type.  This is used before
76  * pg_type is filled, so it has to cover the datatypes used as column types
77  * in the core "bootstrapped" catalogs.
78  *
79  *              XXX several of these input/output functions do catalog scans
80  *                      (e.g., F_REGPROCIN scans pg_proc).      this obviously creates some
81  *                      order dependencies in the catalog creation process.
82  */
83 struct typinfo
84 {
85         char            name[NAMEDATALEN];
86         Oid                     oid;
87         Oid                     elem;
88         int16           len;
89         bool            byval;
90         char            align;
91         char            storage;
92         Oid                     collation;
93         Oid                     inproc;
94         Oid                     outproc;
95 };
96
97 static const struct typinfo TypInfo[] = {
98         {"bool", BOOLOID, 0, 1, true, 'c', 'p', InvalidOid,
99         F_BOOLIN, F_BOOLOUT},
100         {"bytea", BYTEAOID, 0, -1, false, 'i', 'x', InvalidOid,
101         F_BYTEAIN, F_BYTEAOUT},
102         {"char", CHAROID, 0, 1, true, 'c', 'p', InvalidOid,
103         F_CHARIN, F_CHAROUT},
104         {"int2", INT2OID, 0, 2, true, 's', 'p', InvalidOid,
105         F_INT2IN, F_INT2OUT},
106         {"int4", INT4OID, 0, 4, true, 'i', 'p', InvalidOid,
107         F_INT4IN, F_INT4OUT},
108         {"float4", FLOAT4OID, 0, 4, FLOAT4PASSBYVAL, 'i', 'p', InvalidOid,
109         F_FLOAT4IN, F_FLOAT4OUT},
110         {"name", NAMEOID, CHAROID, NAMEDATALEN, false, 'c', 'p', InvalidOid,
111         F_NAMEIN, F_NAMEOUT},
112         {"regclass", REGCLASSOID, 0, 4, true, 'i', 'p', InvalidOid,
113         F_REGCLASSIN, F_REGCLASSOUT},
114         {"regproc", REGPROCOID, 0, 4, true, 'i', 'p', InvalidOid,
115         F_REGPROCIN, F_REGPROCOUT},
116         {"regtype", REGTYPEOID, 0, 4, true, 'i', 'p', InvalidOid,
117         F_REGTYPEIN, F_REGTYPEOUT},
118         {"text", TEXTOID, 0, -1, false, 'i', 'x', DEFAULT_COLLATION_OID,
119         F_TEXTIN, F_TEXTOUT},
120         {"oid", OIDOID, 0, 4, true, 'i', 'p', InvalidOid,
121         F_OIDIN, F_OIDOUT},
122         {"tid", TIDOID, 0, 6, false, 's', 'p', InvalidOid,
123         F_TIDIN, F_TIDOUT},
124         {"xid", XIDOID, 0, 4, true, 'i', 'p', InvalidOid,
125         F_XIDIN, F_XIDOUT},
126         {"cid", CIDOID, 0, 4, true, 'i', 'p', InvalidOid,
127         F_CIDIN, F_CIDOUT},
128         {"pg_node_tree", PGNODETREEOID, 0, -1, false, 'i', 'x', DEFAULT_COLLATION_OID,
129         F_PG_NODE_TREE_IN, F_PG_NODE_TREE_OUT},
130         {"int2vector", INT2VECTOROID, INT2OID, -1, false, 'i', 'p', InvalidOid,
131         F_INT2VECTORIN, F_INT2VECTOROUT},
132         {"oidvector", OIDVECTOROID, OIDOID, -1, false, 'i', 'p', InvalidOid,
133         F_OIDVECTORIN, F_OIDVECTOROUT},
134         {"_int4", INT4ARRAYOID, INT4OID, -1, false, 'i', 'x', InvalidOid,
135         F_ARRAY_IN, F_ARRAY_OUT},
136         {"_text", 1009, TEXTOID, -1, false, 'i', 'x', DEFAULT_COLLATION_OID,
137         F_ARRAY_IN, F_ARRAY_OUT},
138         {"_oid", 1028, OIDOID, -1, false, 'i', 'x', InvalidOid,
139         F_ARRAY_IN, F_ARRAY_OUT},
140         {"_char", 1002, CHAROID, -1, false, 'i', 'x', InvalidOid,
141         F_ARRAY_IN, F_ARRAY_OUT},
142         {"_aclitem", 1034, ACLITEMOID, -1, false, 'i', 'x', InvalidOid,
143         F_ARRAY_IN, F_ARRAY_OUT}
144 };
145
146 static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
147
148 struct typmap
149 {                                                               /* a hack */
150         Oid                     am_oid;
151         FormData_pg_type am_typ;
152 };
153
154 static struct typmap **Typ = NULL;
155 static struct typmap *Ap = NULL;
156
157 static Datum values[MAXATTR];   /* current row's attribute values */
158 static bool Nulls[MAXATTR];
159
160 static MemoryContext nogc = NULL;               /* special no-gc mem context */
161
162 /*
163  *      At bootstrap time, we first declare all the indices to be built, and
164  *      then build them.  The IndexList structure stores enough information
165  *      to allow us to build the indices after they've been declared.
166  */
167
168 typedef struct _IndexList
169 {
170         Oid                     il_heap;
171         Oid                     il_ind;
172         IndexInfo  *il_info;
173         struct _IndexList *il_next;
174 } IndexList;
175
176 static IndexList *ILHead = NULL;
177
178
179 /*
180  *       AuxiliaryProcessMain
181  *
182  *       The main entry point for auxiliary processes, such as the bgwriter,
183  *       walwriter, walreceiver, bootstrapper and the shared memory checker code.
184  *
185  *       This code is here just because of historical reasons.
186  */
187 void
188 AuxiliaryProcessMain(int argc, char *argv[])
189 {
190         char       *progname = argv[0];
191         int                     flag;
192         AuxProcType auxType = CheckerProcess;
193         char       *userDoption = NULL;
194
195         /*
196          * initialize globals
197          */
198         MyProcPid = getpid();
199
200         MyStartTime = time(NULL);
201
202         /*
203          * Fire up essential subsystems: error and memory management
204          *
205          * If we are running under the postmaster, this is done already.
206          */
207         if (!IsUnderPostmaster)
208                 MemoryContextInit();
209
210         /* Compute paths, if we didn't inherit them from postmaster */
211         if (my_exec_path[0] == '\0')
212         {
213                 if (find_my_exec(progname, my_exec_path) < 0)
214                         elog(FATAL, "%s: could not locate my own executable path",
215                                  progname);
216         }
217
218         /*
219          * process command arguments
220          */
221
222         /* Set defaults, to be overriden by explicit options below */
223         if (!IsUnderPostmaster)
224                 InitializeGUCOptions();
225
226         /* Ignore the initial --boot argument, if present */
227         if (argc > 1 && strcmp(argv[1], "--boot") == 0)
228         {
229                 argv++;
230                 argc--;
231         }
232
233         while ((flag = getopt(argc, argv, "B:c:d:D:Fr:x:-:")) != -1)
234         {
235                 switch (flag)
236                 {
237                         case 'B':
238                                 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
239                                 break;
240                         case 'D':
241                                 userDoption = optarg;
242                                 break;
243                         case 'd':
244                                 {
245                                         /* Turn on debugging for the bootstrap process. */
246                                         char       *debugstr = palloc(strlen("debug") + strlen(optarg) + 1);
247
248                                         sprintf(debugstr, "debug%s", optarg);
249                                         SetConfigOption("log_min_messages", debugstr,
250                                                                         PGC_POSTMASTER, PGC_S_ARGV);
251                                         SetConfigOption("client_min_messages", debugstr,
252                                                                         PGC_POSTMASTER, PGC_S_ARGV);
253                                         pfree(debugstr);
254                                 }
255                                 break;
256                         case 'F':
257                                 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
258                                 break;
259                         case 'r':
260                                 strlcpy(OutputFileName, optarg, MAXPGPATH);
261                                 break;
262                         case 'x':
263                                 auxType = atoi(optarg);
264                                 break;
265                         case 'c':
266                         case '-':
267                                 {
268                                         char       *name,
269                                                            *value;
270
271                                         ParseLongOption(optarg, &name, &value);
272                                         if (!value)
273                                         {
274                                                 if (flag == '-')
275                                                         ereport(ERROR,
276                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
277                                                                          errmsg("--%s requires a value",
278                                                                                         optarg)));
279                                                 else
280                                                         ereport(ERROR,
281                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
282                                                                          errmsg("-c %s requires a value",
283                                                                                         optarg)));
284                                         }
285
286                                         SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
287                                         free(name);
288                                         if (value)
289                                                 free(value);
290                                         break;
291                                 }
292                         default:
293                                 write_stderr("Try \"%s --help\" for more information.\n",
294                                                          progname);
295                                 proc_exit(1);
296                                 break;
297                 }
298         }
299
300         if (argc != optind)
301         {
302                 write_stderr("%s: invalid command-line arguments\n", progname);
303                 proc_exit(1);
304         }
305
306         /*
307          * Identify myself via ps
308          */
309         if (IsUnderPostmaster)
310         {
311                 const char *statmsg;
312
313                 switch (auxType)
314                 {
315                         case StartupProcess:
316                                 statmsg = "startup process";
317                                 break;
318                         case BgWriterProcess:
319                                 statmsg = "writer process";
320                                 break;
321                         case WalWriterProcess:
322                                 statmsg = "wal writer process";
323                                 break;
324                         case WalReceiverProcess:
325                                 statmsg = "wal receiver process";
326                                 break;
327                         default:
328                                 statmsg = "??? process";
329                                 break;
330                 }
331                 init_ps_display(statmsg, "", "", "");
332         }
333
334         /* Acquire configuration parameters, unless inherited from postmaster */
335         if (!IsUnderPostmaster)
336         {
337                 if (!SelectConfigFiles(userDoption, progname))
338                         proc_exit(1);
339                 /* If timezone is not set, determine what the OS uses */
340                 pg_timezone_initialize();
341                 /* If timezone_abbreviations is not set, select default */
342                 pg_timezone_abbrev_initialize();
343         }
344
345         /* Validate we have been given a reasonable-looking DataDir */
346         Assert(DataDir);
347         ValidatePgVersion(DataDir);
348
349         /* Change into DataDir (if under postmaster, should be done already) */
350         if (!IsUnderPostmaster)
351                 ChangeToDataDir();
352
353         /* If standalone, create lockfile for data directory */
354         if (!IsUnderPostmaster)
355                 CreateDataDirLockFile(false);
356
357         SetProcessingMode(BootstrapProcessing);
358         IgnoreSystemIndexes = true;
359
360         BaseInit();
361
362         /*
363          * When we are an auxiliary process, we aren't going to do the full
364          * InitPostgres pushups, but there are a couple of things that need to get
365          * lit up even in an auxiliary process.
366          */
367         if (IsUnderPostmaster)
368         {
369                 /*
370                  * Create a PGPROC so we can use LWLocks.  In the EXEC_BACKEND case,
371                  * this was already done by SubPostmasterMain().
372                  */
373 #ifndef EXEC_BACKEND
374                 InitAuxiliaryProcess();
375 #endif
376
377                 /*
378                  * Assign the ProcSignalSlot for an auxiliary process.  Since it
379                  * doesn't have a BackendId, the slot is statically allocated based on
380                  * the auxiliary process type (auxType).  Backends use slots indexed
381                  * in the range from 1 to MaxBackends (inclusive), so we use
382                  * MaxBackends + AuxProcType + 1 as the index of the slot for an
383                  * auxiliary process.
384                  *
385                  * This will need rethinking if we ever want more than one of a
386                  * particular auxiliary process type.
387                  */
388                 ProcSignalInit(MaxBackends + auxType + 1);
389
390                 /* finish setting up bufmgr.c */
391                 InitBufferPoolBackend();
392
393                 /* register a shutdown callback for LWLock cleanup */
394                 on_shmem_exit(ShutdownAuxiliaryProcess, 0);
395         }
396
397         /*
398          * XLOG operations
399          */
400         SetProcessingMode(NormalProcessing);
401
402         switch (auxType)
403         {
404                 case CheckerProcess:
405                         /* don't set signals, they're useless here */
406                         CheckerModeMain();
407                         proc_exit(1);           /* should never return */
408
409                 case BootstrapProcess:
410                         bootstrap_signals();
411                         BootStrapXLOG();
412                         BootstrapModeMain();
413                         proc_exit(1);           /* should never return */
414
415                 case StartupProcess:
416                         /* don't set signals, startup process has its own agenda */
417                         StartupProcessMain();
418                         proc_exit(1);           /* should never return */
419
420                 case BgWriterProcess:
421                         /* don't set signals, bgwriter has its own agenda */
422                         BackgroundWriterMain();
423                         proc_exit(1);           /* should never return */
424
425                 case WalWriterProcess:
426                         /* don't set signals, walwriter has its own agenda */
427                         InitXLOGAccess();
428                         WalWriterMain();
429                         proc_exit(1);           /* should never return */
430
431                 case WalReceiverProcess:
432                         /* don't set signals, walreceiver has its own agenda */
433                         WalReceiverMain();
434                         proc_exit(1);           /* should never return */
435
436                 default:
437                         elog(PANIC, "unrecognized process type: %d", auxType);
438                         proc_exit(1);
439         }
440 }
441
442 /*
443  * In shared memory checker mode, all we really want to do is create shared
444  * memory and semaphores (just to prove we can do it with the current GUC
445  * settings).  Since, in fact, that was already done by BaseInit(),
446  * we have nothing more to do here.
447  */
448 static void
449 CheckerModeMain(void)
450 {
451         proc_exit(0);
452 }
453
454 /*
455  *       The main entry point for running the backend in bootstrap mode
456  *
457  *       The bootstrap mode is used to initialize the template database.
458  *       The bootstrap backend doesn't speak SQL, but instead expects
459  *       commands in a special bootstrap language.
460  */
461 static void
462 BootstrapModeMain(void)
463 {
464         int                     i;
465
466         Assert(!IsUnderPostmaster);
467
468         SetProcessingMode(BootstrapProcessing);
469
470         /*
471          * Do backend-like initialization for bootstrap mode
472          */
473         InitProcess();
474
475         InitPostgres(NULL, InvalidOid, NULL, NULL);
476
477         /* Initialize stuff for bootstrap-file processing */
478         for (i = 0; i < MAXATTR; i++)
479         {
480                 attrtypes[i] = NULL;
481                 Nulls[i] = false;
482         }
483
484         /*
485          * Process bootstrap input.
486          */
487         boot_yyparse();
488
489         /*
490          * We should now know about all mapped relations, so it's okay to write
491          * out the initial relation mapping files.
492          */
493         RelationMapFinishBootstrap();
494
495         /* Clean up and exit */
496         cleanup();
497         proc_exit(0);
498 }
499
500
501 /* ----------------------------------------------------------------
502  *                                              misc functions
503  * ----------------------------------------------------------------
504  */
505
506 /*
507  * Set up signal handling for a bootstrap process
508  */
509 static void
510 bootstrap_signals(void)
511 {
512         if (IsUnderPostmaster)
513         {
514                 /*
515                  * If possible, make this process a group leader, so that the
516                  * postmaster can signal any child processes too.
517                  */
518 #ifdef HAVE_SETSID
519                 if (setsid() < 0)
520                         elog(FATAL, "setsid() failed: %m");
521 #endif
522
523                 /*
524                  * Properly accept or ignore signals the postmaster might send us
525                  */
526                 pqsignal(SIGHUP, SIG_IGN);
527                 pqsignal(SIGINT, SIG_IGN);              /* ignore query-cancel */
528                 pqsignal(SIGTERM, die);
529                 pqsignal(SIGQUIT, quickdie);
530                 pqsignal(SIGALRM, SIG_IGN);
531                 pqsignal(SIGPIPE, SIG_IGN);
532                 pqsignal(SIGUSR1, SIG_IGN);
533                 pqsignal(SIGUSR2, SIG_IGN);
534
535                 /*
536                  * Reset some signals that are accepted by postmaster but not here
537                  */
538                 pqsignal(SIGCHLD, SIG_DFL);
539                 pqsignal(SIGTTIN, SIG_DFL);
540                 pqsignal(SIGTTOU, SIG_DFL);
541                 pqsignal(SIGCONT, SIG_DFL);
542                 pqsignal(SIGWINCH, SIG_DFL);
543
544                 /*
545                  * Unblock signals (they were blocked when the postmaster forked us)
546                  */
547                 PG_SETMASK(&UnBlockSig);
548         }
549         else
550         {
551                 /* Set up appropriately for interactive use */
552                 pqsignal(SIGHUP, die);
553                 pqsignal(SIGINT, die);
554                 pqsignal(SIGTERM, die);
555                 pqsignal(SIGQUIT, die);
556         }
557 }
558
559 /*
560  * Begin shutdown of an auxiliary process.      This is approximately the equivalent
561  * of ShutdownPostgres() in postinit.c.  We can't run transactions in an
562  * auxiliary process, so most of the work of AbortTransaction() is not needed,
563  * but we do need to make sure we've released any LWLocks we are holding.
564  * (This is only critical during an error exit.)
565  */
566 static void
567 ShutdownAuxiliaryProcess(int code, Datum arg)
568 {
569         LWLockReleaseAll();
570 }
571
572 /* ----------------------------------------------------------------
573  *                              MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
574  * ----------------------------------------------------------------
575  */
576
577 /* ----------------
578  *              boot_openrel
579  * ----------------
580  */
581 void
582 boot_openrel(char *relname)
583 {
584         int                     i;
585         struct typmap **app;
586         Relation        rel;
587         HeapScanDesc scan;
588         HeapTuple       tup;
589
590         if (strlen(relname) >= NAMEDATALEN)
591                 relname[NAMEDATALEN - 1] = '\0';
592
593         if (Typ == NULL)
594         {
595                 /* We can now load the pg_type data */
596                 rel = heap_open(TypeRelationId, NoLock);
597                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
598                 i = 0;
599                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
600                         ++i;
601                 heap_endscan(scan);
602                 app = Typ = ALLOC(struct typmap *, i + 1);
603                 while (i-- > 0)
604                         *app++ = ALLOC(struct typmap, 1);
605                 *app = NULL;
606                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
607                 app = Typ;
608                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
609                 {
610                         (*app)->am_oid = HeapTupleGetOid(tup);
611                         memcpy((char *) &(*app)->am_typ,
612                                    (char *) GETSTRUCT(tup),
613                                    sizeof((*app)->am_typ));
614                         app++;
615                 }
616                 heap_endscan(scan);
617                 heap_close(rel, NoLock);
618         }
619
620         if (boot_reldesc != NULL)
621                 closerel(NULL);
622
623         elog(DEBUG4, "open relation %s, attrsize %d",
624                  relname, (int) ATTRIBUTE_FIXED_PART_SIZE);
625
626         boot_reldesc = heap_openrv(makeRangeVar(NULL, relname, -1), NoLock);
627         numattr = boot_reldesc->rd_rel->relnatts;
628         for (i = 0; i < numattr; i++)
629         {
630                 if (attrtypes[i] == NULL)
631                         attrtypes[i] = AllocateAttribute();
632                 memmove((char *) attrtypes[i],
633                                 (char *) boot_reldesc->rd_att->attrs[i],
634                                 ATTRIBUTE_FIXED_PART_SIZE);
635
636                 {
637                         Form_pg_attribute at = attrtypes[i];
638
639                         elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
640                                  i, NameStr(at->attname), at->attlen, at->attnum,
641                                  at->atttypid);
642                 }
643         }
644 }
645
646 /* ----------------
647  *              closerel
648  * ----------------
649  */
650 void
651 closerel(char *name)
652 {
653         if (name)
654         {
655                 if (boot_reldesc)
656                 {
657                         if (strcmp(RelationGetRelationName(boot_reldesc), name) != 0)
658                                 elog(ERROR, "close of %s when %s was expected",
659                                          name, RelationGetRelationName(boot_reldesc));
660                 }
661                 else
662                         elog(ERROR, "close of %s before any relation was opened",
663                                  name);
664         }
665
666         if (boot_reldesc == NULL)
667                 elog(ERROR, "no open relation to close");
668         else
669         {
670                 elog(DEBUG4, "close relation %s",
671                          RelationGetRelationName(boot_reldesc));
672                 heap_close(boot_reldesc, NoLock);
673                 boot_reldesc = NULL;
674         }
675 }
676
677
678
679 /* ----------------
680  * DEFINEATTR()
681  *
682  * define a <field,type> pair
683  * if there are n fields in a relation to be created, this routine
684  * will be called n times
685  * ----------------
686  */
687 void
688 DefineAttr(char *name, char *type, int attnum)
689 {
690         Oid                     typeoid;
691
692         if (boot_reldesc != NULL)
693         {
694                 elog(WARNING, "no open relations allowed with CREATE command");
695                 closerel(NULL);
696         }
697
698         if (attrtypes[attnum] == NULL)
699                 attrtypes[attnum] = AllocateAttribute();
700         MemSet(attrtypes[attnum], 0, ATTRIBUTE_FIXED_PART_SIZE);
701
702         namestrcpy(&attrtypes[attnum]->attname, name);
703         elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
704         attrtypes[attnum]->attnum = attnum + 1;         /* fillatt */
705
706         typeoid = gettype(type);
707
708         if (Typ != NULL)
709         {
710                 attrtypes[attnum]->atttypid = Ap->am_oid;
711                 attrtypes[attnum]->attlen = Ap->am_typ.typlen;
712                 attrtypes[attnum]->attbyval = Ap->am_typ.typbyval;
713                 attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
714                 attrtypes[attnum]->attalign = Ap->am_typ.typalign;
715                 attrtypes[attnum]->attcollation = Ap->am_typ.typcollation;
716                 /* if an array type, assume 1-dimensional attribute */
717                 if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
718                         attrtypes[attnum]->attndims = 1;
719                 else
720                         attrtypes[attnum]->attndims = 0;
721         }
722         else
723         {
724                 attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
725                 attrtypes[attnum]->attlen = TypInfo[typeoid].len;
726                 attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
727                 attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
728                 attrtypes[attnum]->attalign = TypInfo[typeoid].align;
729                 attrtypes[attnum]->attcollation = TypInfo[typeoid].collation;
730                 /* if an array type, assume 1-dimensional attribute */
731                 if (TypInfo[typeoid].elem != InvalidOid &&
732                         attrtypes[attnum]->attlen < 0)
733                         attrtypes[attnum]->attndims = 1;
734                 else
735                         attrtypes[attnum]->attndims = 0;
736         }
737
738         attrtypes[attnum]->attstattarget = -1;
739         attrtypes[attnum]->attcacheoff = -1;
740         attrtypes[attnum]->atttypmod = -1;
741         attrtypes[attnum]->attislocal = true;
742
743         /*
744          * Mark as "not null" if type is fixed-width and prior columns are too.
745          * This corresponds to case where column can be accessed directly via C
746          * struct declaration.
747          *
748          * oidvector and int2vector are also treated as not-nullable, even though
749          * they are no longer fixed-width.
750          */
751 #define MARKNOTNULL(att) \
752         ((att)->attlen > 0 || \
753          (att)->atttypid == OIDVECTOROID || \
754          (att)->atttypid == INT2VECTOROID)
755
756         if (MARKNOTNULL(attrtypes[attnum]))
757         {
758                 int                     i;
759
760                 for (i = 0; i < attnum; i++)
761                 {
762                         if (!MARKNOTNULL(attrtypes[i]))
763                                 break;
764                 }
765                 if (i == attnum)
766                         attrtypes[attnum]->attnotnull = true;
767         }
768 }
769
770
771 /* ----------------
772  *              InsertOneTuple
773  *
774  * If objectid is not zero, it is a specific OID to assign to the tuple.
775  * Otherwise, an OID will be assigned (if necessary) by heap_insert.
776  * ----------------
777  */
778 void
779 InsertOneTuple(Oid objectid)
780 {
781         HeapTuple       tuple;
782         TupleDesc       tupDesc;
783         int                     i;
784
785         elog(DEBUG4, "inserting row oid %u, %d columns", objectid, numattr);
786
787         tupDesc = CreateTupleDesc(numattr,
788                                                           RelationGetForm(boot_reldesc)->relhasoids,
789                                                           attrtypes);
790         tuple = heap_form_tuple(tupDesc, values, Nulls);
791         if (objectid != (Oid) 0)
792                 HeapTupleSetOid(tuple, objectid);
793         pfree(tupDesc);                         /* just free's tupDesc, not the attrtypes */
794
795         simple_heap_insert(boot_reldesc, tuple);
796         heap_freetuple(tuple);
797         elog(DEBUG4, "row inserted");
798
799         /*
800          * Reset null markers for next tuple
801          */
802         for (i = 0; i < numattr; i++)
803                 Nulls[i] = false;
804 }
805
806 /* ----------------
807  *              InsertOneValue
808  * ----------------
809  */
810 void
811 InsertOneValue(char *value, int i)
812 {
813         Oid                     typoid;
814         int16           typlen;
815         bool            typbyval;
816         char            typalign;
817         char            typdelim;
818         Oid                     typioparam;
819         Oid                     typinput;
820         Oid                     typoutput;
821         char       *prt;
822
823         AssertArg(i >= 0 || i < MAXATTR);
824
825         elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
826
827         typoid = boot_reldesc->rd_att->attrs[i]->atttypid;
828
829         boot_get_type_io_data(typoid,
830                                                   &typlen, &typbyval, &typalign,
831                                                   &typdelim, &typioparam,
832                                                   &typinput, &typoutput);
833
834         values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
835         prt = OidOutputFunctionCall(typoutput, values[i]);
836         elog(DEBUG4, "inserted -> %s", prt);
837         pfree(prt);
838 }
839
840 /* ----------------
841  *              InsertOneNull
842  * ----------------
843  */
844 void
845 InsertOneNull(int i)
846 {
847         elog(DEBUG4, "inserting column %d NULL", i);
848         Assert(i >= 0 || i < MAXATTR);
849         values[i] = PointerGetDatum(NULL);
850         Nulls[i] = true;
851 }
852
853 /* ----------------
854  *              cleanup
855  * ----------------
856  */
857 static void
858 cleanup(void)
859 {
860         if (boot_reldesc != NULL)
861                 closerel(NULL);
862 }
863
864 /* ----------------
865  *              gettype
866  *
867  * NB: this is really ugly; it will return an integer index into TypInfo[],
868  * and not an OID at all, until the first reference to a type not known in
869  * TypInfo[].  At that point it will read and cache pg_type in the Typ array,
870  * and subsequently return a real OID (and set the global pointer Ap to
871  * point at the found row in Typ).      So caller must check whether Typ is
872  * still NULL to determine what the return value is!
873  * ----------------
874  */
875 static Oid
876 gettype(char *type)
877 {
878         int                     i;
879         Relation        rel;
880         HeapScanDesc scan;
881         HeapTuple       tup;
882         struct typmap **app;
883
884         if (Typ != NULL)
885         {
886                 for (app = Typ; *app != NULL; app++)
887                 {
888                         if (strncmp(NameStr((*app)->am_typ.typname), type, NAMEDATALEN) == 0)
889                         {
890                                 Ap = *app;
891                                 return (*app)->am_oid;
892                         }
893                 }
894         }
895         else
896         {
897                 for (i = 0; i < n_types; i++)
898                 {
899                         if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
900                                 return i;
901                 }
902                 elog(DEBUG4, "external type: %s", type);
903                 rel = heap_open(TypeRelationId, NoLock);
904                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
905                 i = 0;
906                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
907                         ++i;
908                 heap_endscan(scan);
909                 app = Typ = ALLOC(struct typmap *, i + 1);
910                 while (i-- > 0)
911                         *app++ = ALLOC(struct typmap, 1);
912                 *app = NULL;
913                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
914                 app = Typ;
915                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
916                 {
917                         (*app)->am_oid = HeapTupleGetOid(tup);
918                         memmove((char *) &(*app++)->am_typ,
919                                         (char *) GETSTRUCT(tup),
920                                         sizeof((*app)->am_typ));
921                 }
922                 heap_endscan(scan);
923                 heap_close(rel, NoLock);
924                 return gettype(type);
925         }
926         elog(ERROR, "unrecognized type \"%s\"", type);
927         /* not reached, here to make compiler happy */
928         return 0;
929 }
930
931 /* ----------------
932  *              boot_get_type_io_data
933  *
934  * Obtain type I/O information at bootstrap time.  This intentionally has
935  * almost the same API as lsyscache.c's get_type_io_data, except that
936  * we only support obtaining the typinput and typoutput routines, not
937  * the binary I/O routines.  It is exported so that array_in and array_out
938  * can be made to work during early bootstrap.
939  * ----------------
940  */
941 void
942 boot_get_type_io_data(Oid typid,
943                                           int16 *typlen,
944                                           bool *typbyval,
945                                           char *typalign,
946                                           char *typdelim,
947                                           Oid *typioparam,
948                                           Oid *typinput,
949                                           Oid *typoutput)
950 {
951         if (Typ != NULL)
952         {
953                 /* We have the boot-time contents of pg_type, so use it */
954                 struct typmap **app;
955                 struct typmap *ap;
956
957                 app = Typ;
958                 while (*app && (*app)->am_oid != typid)
959                         ++app;
960                 ap = *app;
961                 if (ap == NULL)
962                         elog(ERROR, "type OID %u not found in Typ list", typid);
963
964                 *typlen = ap->am_typ.typlen;
965                 *typbyval = ap->am_typ.typbyval;
966                 *typalign = ap->am_typ.typalign;
967                 *typdelim = ap->am_typ.typdelim;
968
969                 /* XXX this logic must match getTypeIOParam() */
970                 if (OidIsValid(ap->am_typ.typelem))
971                         *typioparam = ap->am_typ.typelem;
972                 else
973                         *typioparam = typid;
974
975                 *typinput = ap->am_typ.typinput;
976                 *typoutput = ap->am_typ.typoutput;
977         }
978         else
979         {
980                 /* We don't have pg_type yet, so use the hard-wired TypInfo array */
981                 int                     typeindex;
982
983                 for (typeindex = 0; typeindex < n_types; typeindex++)
984                 {
985                         if (TypInfo[typeindex].oid == typid)
986                                 break;
987                 }
988                 if (typeindex >= n_types)
989                         elog(ERROR, "type OID %u not found in TypInfo", typid);
990
991                 *typlen = TypInfo[typeindex].len;
992                 *typbyval = TypInfo[typeindex].byval;
993                 *typalign = TypInfo[typeindex].align;
994                 /* We assume typdelim is ',' for all boot-time types */
995                 *typdelim = ',';
996
997                 /* XXX this logic must match getTypeIOParam() */
998                 if (OidIsValid(TypInfo[typeindex].elem))
999                         *typioparam = TypInfo[typeindex].elem;
1000                 else
1001                         *typioparam = typid;
1002
1003                 *typinput = TypInfo[typeindex].inproc;
1004                 *typoutput = TypInfo[typeindex].outproc;
1005         }
1006 }
1007
1008 /* ----------------
1009  *              AllocateAttribute
1010  *
1011  * Note: bootstrap never sets any per-column ACLs, so we only need
1012  * ATTRIBUTE_FIXED_PART_SIZE space per attribute.
1013  * ----------------
1014  */
1015 static Form_pg_attribute
1016 AllocateAttribute(void)
1017 {
1018         Form_pg_attribute attribute = (Form_pg_attribute) malloc(ATTRIBUTE_FIXED_PART_SIZE);
1019
1020         if (!PointerIsValid(attribute))
1021                 elog(FATAL, "out of memory");
1022         MemSet(attribute, 0, ATTRIBUTE_FIXED_PART_SIZE);
1023
1024         return attribute;
1025 }
1026
1027 /* ----------------
1028  *              MapArrayTypeName
1029  * XXX arrays of "basetype" are always "_basetype".
1030  *         this is an evil hack inherited from rel. 3.1.
1031  * XXX array dimension is thrown away because we
1032  *         don't support fixed-dimension arrays.  again,
1033  *         sickness from 3.1.
1034  *
1035  * the string passed in must have a '[' character in it
1036  *
1037  * the string returned is a pointer to static storage and should NOT
1038  * be freed by the CALLER.
1039  * ----------------
1040  */
1041 char *
1042 MapArrayTypeName(char *s)
1043 {
1044         int                     i,
1045                                 j;
1046         static char newStr[NAMEDATALEN];        /* array type names < NAMEDATALEN long */
1047
1048         if (s == NULL || s[0] == '\0')
1049                 return s;
1050
1051         j = 1;
1052         newStr[0] = '_';
1053         for (i = 0; i < NAMEDATALEN - 1 && s[i] != '['; i++, j++)
1054                 newStr[j] = s[i];
1055
1056         newStr[j] = '\0';
1057
1058         return newStr;
1059 }
1060
1061
1062 /*
1063  *      index_register() -- record an index that has been set up for building
1064  *                                              later.
1065  *
1066  *              At bootstrap time, we define a bunch of indexes on system catalogs.
1067  *              We postpone actually building the indexes until just before we're
1068  *              finished with initialization, however.  This is because the indexes
1069  *              themselves have catalog entries, and those have to be included in the
1070  *              indexes on those catalogs.      Doing it in two phases is the simplest
1071  *              way of making sure the indexes have the right contents at the end.
1072  */
1073 void
1074 index_register(Oid heap,
1075                            Oid ind,
1076                            IndexInfo *indexInfo)
1077 {
1078         IndexList  *newind;
1079         MemoryContext oldcxt;
1080
1081         /*
1082          * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
1083          * bootstrap time.      we'll declare the indexes now, but want to create them
1084          * later.
1085          */
1086
1087         if (nogc == NULL)
1088                 nogc = AllocSetContextCreate(NULL,
1089                                                                          "BootstrapNoGC",
1090                                                                          ALLOCSET_DEFAULT_MINSIZE,
1091                                                                          ALLOCSET_DEFAULT_INITSIZE,
1092                                                                          ALLOCSET_DEFAULT_MAXSIZE);
1093
1094         oldcxt = MemoryContextSwitchTo(nogc);
1095
1096         newind = (IndexList *) palloc(sizeof(IndexList));
1097         newind->il_heap = heap;
1098         newind->il_ind = ind;
1099         newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo));
1100
1101         memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
1102         /* expressions will likely be null, but may as well copy it */
1103         newind->il_info->ii_Expressions = (List *)
1104                 copyObject(indexInfo->ii_Expressions);
1105         newind->il_info->ii_ExpressionsState = NIL;
1106         /* predicate will likely be null, but may as well copy it */
1107         newind->il_info->ii_Predicate = (List *)
1108                 copyObject(indexInfo->ii_Predicate);
1109         newind->il_info->ii_PredicateState = NIL;
1110         /* no exclusion constraints at bootstrap time, so no need to copy */
1111         Assert(indexInfo->ii_ExclusionOps == NULL);
1112         Assert(indexInfo->ii_ExclusionProcs == NULL);
1113         Assert(indexInfo->ii_ExclusionStrats == NULL);
1114
1115         newind->il_next = ILHead;
1116         ILHead = newind;
1117
1118         MemoryContextSwitchTo(oldcxt);
1119 }
1120
1121
1122 /*
1123  * build_indices -- fill in all the indexes registered earlier
1124  */
1125 void
1126 build_indices(void)
1127 {
1128         for (; ILHead != NULL; ILHead = ILHead->il_next)
1129         {
1130                 Relation        heap;
1131                 Relation        ind;
1132
1133                 /* need not bother with locks during bootstrap */
1134                 heap = heap_open(ILHead->il_heap, NoLock);
1135                 ind = index_open(ILHead->il_ind, NoLock);
1136
1137                 index_build(heap, ind, ILHead->il_info, false, false);
1138
1139                 index_close(ind, NoLock);
1140                 heap_close(heap, NoLock);
1141         }
1142 }