]> granicus.if.org Git - postgresql/blob - src/backend/bootstrap/bootstrap.c
Move Trigger and TriggerDesc structs out of rel.h into a new reltrigger.h
[postgresql] / 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/rel.h"
47 #include "utils/relmapper.h"
48 #include "utils/tqual.h"
49
50 extern int      optind;
51 extern char *optarg;
52
53
54 #define ALLOC(t, c)             ((t *) calloc((unsigned)(c), sizeof(t)))
55
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);
63
64 /* ----------------
65  *              global variables
66  * ----------------
67  */
68
69 Relation        boot_reldesc;           /* current relation descriptor */
70
71 Form_pg_attribute attrtypes[MAXATTR];   /* points to attribute info */
72 int                     numattr;                        /* number of attributes for cur. rel */
73
74
75 /*
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.
79  *
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.
83  */
84 struct typinfo
85 {
86         char            name[NAMEDATALEN];
87         Oid                     oid;
88         Oid                     elem;
89         int16           len;
90         bool            byval;
91         char            align;
92         char            storage;
93         Oid                     collation;
94         Oid                     inproc;
95         Oid                     outproc;
96 };
97
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,
122         F_OIDIN, F_OIDOUT},
123         {"tid", TIDOID, 0, 6, false, 's', 'p', InvalidOid,
124         F_TIDIN, F_TIDOUT},
125         {"xid", XIDOID, 0, 4, true, 'i', 'p', InvalidOid,
126         F_XIDIN, F_XIDOUT},
127         {"cid", CIDOID, 0, 4, true, 'i', 'p', InvalidOid,
128         F_CIDIN, F_CIDOUT},
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}
145 };
146
147 static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
148
149 struct typmap
150 {                                                               /* a hack */
151         Oid                     am_oid;
152         FormData_pg_type am_typ;
153 };
154
155 static struct typmap **Typ = NULL;
156 static struct typmap *Ap = NULL;
157
158 static Datum values[MAXATTR];   /* current row's attribute values */
159 static bool Nulls[MAXATTR];
160
161 static MemoryContext nogc = NULL;               /* special no-gc mem context */
162
163 /*
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.
167  */
168
169 typedef struct _IndexList
170 {
171         Oid                     il_heap;
172         Oid                     il_ind;
173         IndexInfo  *il_info;
174         struct _IndexList *il_next;
175 } IndexList;
176
177 static IndexList *ILHead = NULL;
178
179
180 /*
181  *       AuxiliaryProcessMain
182  *
183  *       The main entry point for auxiliary processes, such as the bgwriter,
184  *       walwriter, walreceiver, bootstrapper and the shared memory checker code.
185  *
186  *       This code is here just because of historical reasons.
187  */
188 void
189 AuxiliaryProcessMain(int argc, char *argv[])
190 {
191         char       *progname = argv[0];
192         int                     flag;
193         AuxProcType auxType = CheckerProcess;
194         char       *userDoption = NULL;
195
196         /*
197          * initialize globals
198          */
199         MyProcPid = getpid();
200
201         MyStartTime = time(NULL);
202
203         /*
204          * Fire up essential subsystems: error and memory management
205          *
206          * If we are running under the postmaster, this is done already.
207          */
208         if (!IsUnderPostmaster)
209                 MemoryContextInit();
210
211         /* Compute paths, if we didn't inherit them from postmaster */
212         if (my_exec_path[0] == '\0')
213         {
214                 if (find_my_exec(progname, my_exec_path) < 0)
215                         elog(FATAL, "%s: could not locate my own executable path",
216                                  progname);
217         }
218
219         /*
220          * process command arguments
221          */
222
223         /* Set defaults, to be overriden by explicit options below */
224         if (!IsUnderPostmaster)
225                 InitializeGUCOptions();
226
227         /* Ignore the initial --boot argument, if present */
228         if (argc > 1 && strcmp(argv[1], "--boot") == 0)
229         {
230                 argv++;
231                 argc--;
232         }
233
234         while ((flag = getopt(argc, argv, "B:c:d:D:Fr:x:-:")) != -1)
235         {
236                 switch (flag)
237                 {
238                         case 'B':
239                                 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
240                                 break;
241                         case 'D':
242                                 userDoption = optarg;
243                                 break;
244                         case 'd':
245                                 {
246                                         /* Turn on debugging for the bootstrap process. */
247                                         char       *debugstr = palloc(strlen("debug") + strlen(optarg) + 1);
248
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);
254                                         pfree(debugstr);
255                                 }
256                                 break;
257                         case 'F':
258                                 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
259                                 break;
260                         case 'r':
261                                 strlcpy(OutputFileName, optarg, MAXPGPATH);
262                                 break;
263                         case 'x':
264                                 auxType = atoi(optarg);
265                                 break;
266                         case 'c':
267                         case '-':
268                                 {
269                                         char       *name,
270                                                            *value;
271
272                                         ParseLongOption(optarg, &name, &value);
273                                         if (!value)
274                                         {
275                                                 if (flag == '-')
276                                                         ereport(ERROR,
277                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
278                                                                          errmsg("--%s requires a value",
279                                                                                         optarg)));
280                                                 else
281                                                         ereport(ERROR,
282                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
283                                                                          errmsg("-c %s requires a value",
284                                                                                         optarg)));
285                                         }
286
287                                         SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
288                                         free(name);
289                                         if (value)
290                                                 free(value);
291                                         break;
292                                 }
293                         default:
294                                 write_stderr("Try \"%s --help\" for more information.\n",
295                                                          progname);
296                                 proc_exit(1);
297                                 break;
298                 }
299         }
300
301         if (argc != optind)
302         {
303                 write_stderr("%s: invalid command-line arguments\n", progname);
304                 proc_exit(1);
305         }
306
307         /*
308          * Identify myself via ps
309          */
310         if (IsUnderPostmaster)
311         {
312                 const char *statmsg;
313
314                 switch (auxType)
315                 {
316                         case StartupProcess:
317                                 statmsg = "startup process";
318                                 break;
319                         case BgWriterProcess:
320                                 statmsg = "writer process";
321                                 break;
322                         case WalWriterProcess:
323                                 statmsg = "wal writer process";
324                                 break;
325                         case WalReceiverProcess:
326                                 statmsg = "wal receiver process";
327                                 break;
328                         default:
329                                 statmsg = "??? process";
330                                 break;
331                 }
332                 init_ps_display(statmsg, "", "", "");
333         }
334
335         /* Acquire configuration parameters, unless inherited from postmaster */
336         if (!IsUnderPostmaster)
337         {
338                 if (!SelectConfigFiles(userDoption, progname))
339                         proc_exit(1);
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();
344         }
345
346         /* Validate we have been given a reasonable-looking DataDir */
347         Assert(DataDir);
348         ValidatePgVersion(DataDir);
349
350         /* Change into DataDir (if under postmaster, should be done already) */
351         if (!IsUnderPostmaster)
352                 ChangeToDataDir();
353
354         /* If standalone, create lockfile for data directory */
355         if (!IsUnderPostmaster)
356                 CreateDataDirLockFile(false);
357
358         SetProcessingMode(BootstrapProcessing);
359         IgnoreSystemIndexes = true;
360
361         BaseInit();
362
363         /*
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.
367          */
368         if (IsUnderPostmaster)
369         {
370                 /*
371                  * Create a PGPROC so we can use LWLocks.  In the EXEC_BACKEND case,
372                  * this was already done by SubPostmasterMain().
373                  */
374 #ifndef EXEC_BACKEND
375                 InitAuxiliaryProcess();
376 #endif
377
378                 /*
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
384                  * auxiliary process.
385                  *
386                  * This will need rethinking if we ever want more than one of a
387                  * particular auxiliary process type.
388                  */
389                 ProcSignalInit(MaxBackends + auxType + 1);
390
391                 /* finish setting up bufmgr.c */
392                 InitBufferPoolBackend();
393
394                 /* register a shutdown callback for LWLock cleanup */
395                 on_shmem_exit(ShutdownAuxiliaryProcess, 0);
396         }
397
398         /*
399          * XLOG operations
400          */
401         SetProcessingMode(NormalProcessing);
402
403         switch (auxType)
404         {
405                 case CheckerProcess:
406                         /* don't set signals, they're useless here */
407                         CheckerModeMain();
408                         proc_exit(1);           /* should never return */
409
410                 case BootstrapProcess:
411                         bootstrap_signals();
412                         BootStrapXLOG();
413                         BootstrapModeMain();
414                         proc_exit(1);           /* should never return */
415
416                 case StartupProcess:
417                         /* don't set signals, startup process has its own agenda */
418                         StartupProcessMain();
419                         proc_exit(1);           /* should never return */
420
421                 case BgWriterProcess:
422                         /* don't set signals, bgwriter has its own agenda */
423                         BackgroundWriterMain();
424                         proc_exit(1);           /* should never return */
425
426                 case WalWriterProcess:
427                         /* don't set signals, walwriter has its own agenda */
428                         InitXLOGAccess();
429                         WalWriterMain();
430                         proc_exit(1);           /* should never return */
431
432                 case WalReceiverProcess:
433                         /* don't set signals, walreceiver has its own agenda */
434                         WalReceiverMain();
435                         proc_exit(1);           /* should never return */
436
437                 default:
438                         elog(PANIC, "unrecognized process type: %d", auxType);
439                         proc_exit(1);
440         }
441 }
442
443 /*
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.
448  */
449 static void
450 CheckerModeMain(void)
451 {
452         proc_exit(0);
453 }
454
455 /*
456  *       The main entry point for running the backend in bootstrap mode
457  *
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.
461  */
462 static void
463 BootstrapModeMain(void)
464 {
465         int                     i;
466
467         Assert(!IsUnderPostmaster);
468
469         SetProcessingMode(BootstrapProcessing);
470
471         /*
472          * Do backend-like initialization for bootstrap mode
473          */
474         InitProcess();
475
476         InitPostgres(NULL, InvalidOid, NULL, NULL);
477
478         /* Initialize stuff for bootstrap-file processing */
479         for (i = 0; i < MAXATTR; i++)
480         {
481                 attrtypes[i] = NULL;
482                 Nulls[i] = false;
483         }
484
485         /*
486          * Process bootstrap input.
487          */
488         boot_yyparse();
489
490         /*
491          * We should now know about all mapped relations, so it's okay to write
492          * out the initial relation mapping files.
493          */
494         RelationMapFinishBootstrap();
495
496         /* Clean up and exit */
497         cleanup();
498         proc_exit(0);
499 }
500
501
502 /* ----------------------------------------------------------------
503  *                                              misc functions
504  * ----------------------------------------------------------------
505  */
506
507 /*
508  * Set up signal handling for a bootstrap process
509  */
510 static void
511 bootstrap_signals(void)
512 {
513         if (IsUnderPostmaster)
514         {
515                 /*
516                  * If possible, make this process a group leader, so that the
517                  * postmaster can signal any child processes too.
518                  */
519 #ifdef HAVE_SETSID
520                 if (setsid() < 0)
521                         elog(FATAL, "setsid() failed: %m");
522 #endif
523
524                 /*
525                  * Properly accept or ignore signals the postmaster might send us
526                  */
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);
535
536                 /*
537                  * Reset some signals that are accepted by postmaster but not here
538                  */
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);
544
545                 /*
546                  * Unblock signals (they were blocked when the postmaster forked us)
547                  */
548                 PG_SETMASK(&UnBlockSig);
549         }
550         else
551         {
552                 /* Set up appropriately for interactive use */
553                 pqsignal(SIGHUP, die);
554                 pqsignal(SIGINT, die);
555                 pqsignal(SIGTERM, die);
556                 pqsignal(SIGQUIT, die);
557         }
558 }
559
560 /*
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.)
566  */
567 static void
568 ShutdownAuxiliaryProcess(int code, Datum arg)
569 {
570         LWLockReleaseAll();
571 }
572
573 /* ----------------------------------------------------------------
574  *                              MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
575  * ----------------------------------------------------------------
576  */
577
578 /* ----------------
579  *              boot_openrel
580  * ----------------
581  */
582 void
583 boot_openrel(char *relname)
584 {
585         int                     i;
586         struct typmap **app;
587         Relation        rel;
588         HeapScanDesc scan;
589         HeapTuple       tup;
590
591         if (strlen(relname) >= NAMEDATALEN)
592                 relname[NAMEDATALEN - 1] = '\0';
593
594         if (Typ == NULL)
595         {
596                 /* We can now load the pg_type data */
597                 rel = heap_open(TypeRelationId, NoLock);
598                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
599                 i = 0;
600                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
601                         ++i;
602                 heap_endscan(scan);
603                 app = Typ = ALLOC(struct typmap *, i + 1);
604                 while (i-- > 0)
605                         *app++ = ALLOC(struct typmap, 1);
606                 *app = NULL;
607                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
608                 app = Typ;
609                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
610                 {
611                         (*app)->am_oid = HeapTupleGetOid(tup);
612                         memcpy((char *) &(*app)->am_typ,
613                                    (char *) GETSTRUCT(tup),
614                                    sizeof((*app)->am_typ));
615                         app++;
616                 }
617                 heap_endscan(scan);
618                 heap_close(rel, NoLock);
619         }
620
621         if (boot_reldesc != NULL)
622                 closerel(NULL);
623
624         elog(DEBUG4, "open relation %s, attrsize %d",
625                  relname, (int) ATTRIBUTE_FIXED_PART_SIZE);
626
627         boot_reldesc = heap_openrv(makeRangeVar(NULL, relname, -1), NoLock);
628         numattr = boot_reldesc->rd_rel->relnatts;
629         for (i = 0; i < numattr; i++)
630         {
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);
636
637                 {
638                         Form_pg_attribute at = attrtypes[i];
639
640                         elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
641                                  i, NameStr(at->attname), at->attlen, at->attnum,
642                                  at->atttypid);
643                 }
644         }
645 }
646
647 /* ----------------
648  *              closerel
649  * ----------------
650  */
651 void
652 closerel(char *name)
653 {
654         if (name)
655         {
656                 if (boot_reldesc)
657                 {
658                         if (strcmp(RelationGetRelationName(boot_reldesc), name) != 0)
659                                 elog(ERROR, "close of %s when %s was expected",
660                                          name, RelationGetRelationName(boot_reldesc));
661                 }
662                 else
663                         elog(ERROR, "close of %s before any relation was opened",
664                                  name);
665         }
666
667         if (boot_reldesc == NULL)
668                 elog(ERROR, "no open relation to close");
669         else
670         {
671                 elog(DEBUG4, "close relation %s",
672                          RelationGetRelationName(boot_reldesc));
673                 heap_close(boot_reldesc, NoLock);
674                 boot_reldesc = NULL;
675         }
676 }
677
678
679
680 /* ----------------
681  * DEFINEATTR()
682  *
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
686  * ----------------
687  */
688 void
689 DefineAttr(char *name, char *type, int attnum)
690 {
691         Oid                     typeoid;
692
693         if (boot_reldesc != NULL)
694         {
695                 elog(WARNING, "no open relations allowed with CREATE command");
696                 closerel(NULL);
697         }
698
699         if (attrtypes[attnum] == NULL)
700                 attrtypes[attnum] = AllocateAttribute();
701         MemSet(attrtypes[attnum], 0, ATTRIBUTE_FIXED_PART_SIZE);
702
703         namestrcpy(&attrtypes[attnum]->attname, name);
704         elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
705         attrtypes[attnum]->attnum = attnum + 1;         /* fillatt */
706
707         typeoid = gettype(type);
708
709         if (Typ != NULL)
710         {
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;
720                 else
721                         attrtypes[attnum]->attndims = 0;
722         }
723         else
724         {
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;
735                 else
736                         attrtypes[attnum]->attndims = 0;
737         }
738
739         attrtypes[attnum]->attstattarget = -1;
740         attrtypes[attnum]->attcacheoff = -1;
741         attrtypes[attnum]->atttypmod = -1;
742         attrtypes[attnum]->attislocal = true;
743
744         /*
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.
748          *
749          * oidvector and int2vector are also treated as not-nullable, even though
750          * they are no longer fixed-width.
751          */
752 #define MARKNOTNULL(att) \
753         ((att)->attlen > 0 || \
754          (att)->atttypid == OIDVECTOROID || \
755          (att)->atttypid == INT2VECTOROID)
756
757         if (MARKNOTNULL(attrtypes[attnum]))
758         {
759                 int                     i;
760
761                 for (i = 0; i < attnum; i++)
762                 {
763                         if (!MARKNOTNULL(attrtypes[i]))
764                                 break;
765                 }
766                 if (i == attnum)
767                         attrtypes[attnum]->attnotnull = true;
768         }
769 }
770
771
772 /* ----------------
773  *              InsertOneTuple
774  *
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.
777  * ----------------
778  */
779 void
780 InsertOneTuple(Oid objectid)
781 {
782         HeapTuple       tuple;
783         TupleDesc       tupDesc;
784         int                     i;
785
786         elog(DEBUG4, "inserting row oid %u, %d columns", objectid, numattr);
787
788         tupDesc = CreateTupleDesc(numattr,
789                                                           RelationGetForm(boot_reldesc)->relhasoids,
790                                                           attrtypes);
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 */
795
796         simple_heap_insert(boot_reldesc, tuple);
797         heap_freetuple(tuple);
798         elog(DEBUG4, "row inserted");
799
800         /*
801          * Reset null markers for next tuple
802          */
803         for (i = 0; i < numattr; i++)
804                 Nulls[i] = false;
805 }
806
807 /* ----------------
808  *              InsertOneValue
809  * ----------------
810  */
811 void
812 InsertOneValue(char *value, int i)
813 {
814         Oid                     typoid;
815         int16           typlen;
816         bool            typbyval;
817         char            typalign;
818         char            typdelim;
819         Oid                     typioparam;
820         Oid                     typinput;
821         Oid                     typoutput;
822         char       *prt;
823
824         AssertArg(i >= 0 || i < MAXATTR);
825
826         elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
827
828         typoid = boot_reldesc->rd_att->attrs[i]->atttypid;
829
830         boot_get_type_io_data(typoid,
831                                                   &typlen, &typbyval, &typalign,
832                                                   &typdelim, &typioparam,
833                                                   &typinput, &typoutput);
834
835         values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
836         prt = OidOutputFunctionCall(typoutput, values[i]);
837         elog(DEBUG4, "inserted -> %s", prt);
838         pfree(prt);
839 }
840
841 /* ----------------
842  *              InsertOneNull
843  * ----------------
844  */
845 void
846 InsertOneNull(int i)
847 {
848         elog(DEBUG4, "inserting column %d NULL", i);
849         Assert(i >= 0 || i < MAXATTR);
850         values[i] = PointerGetDatum(NULL);
851         Nulls[i] = true;
852 }
853
854 /* ----------------
855  *              cleanup
856  * ----------------
857  */
858 static void
859 cleanup(void)
860 {
861         if (boot_reldesc != NULL)
862                 closerel(NULL);
863 }
864
865 /* ----------------
866  *              gettype
867  *
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!
874  * ----------------
875  */
876 static Oid
877 gettype(char *type)
878 {
879         int                     i;
880         Relation        rel;
881         HeapScanDesc scan;
882         HeapTuple       tup;
883         struct typmap **app;
884
885         if (Typ != NULL)
886         {
887                 for (app = Typ; *app != NULL; app++)
888                 {
889                         if (strncmp(NameStr((*app)->am_typ.typname), type, NAMEDATALEN) == 0)
890                         {
891                                 Ap = *app;
892                                 return (*app)->am_oid;
893                         }
894                 }
895         }
896         else
897         {
898                 for (i = 0; i < n_types; i++)
899                 {
900                         if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
901                                 return i;
902                 }
903                 elog(DEBUG4, "external type: %s", type);
904                 rel = heap_open(TypeRelationId, NoLock);
905                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
906                 i = 0;
907                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
908                         ++i;
909                 heap_endscan(scan);
910                 app = Typ = ALLOC(struct typmap *, i + 1);
911                 while (i-- > 0)
912                         *app++ = ALLOC(struct typmap, 1);
913                 *app = NULL;
914                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
915                 app = Typ;
916                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
917                 {
918                         (*app)->am_oid = HeapTupleGetOid(tup);
919                         memmove((char *) &(*app++)->am_typ,
920                                         (char *) GETSTRUCT(tup),
921                                         sizeof((*app)->am_typ));
922                 }
923                 heap_endscan(scan);
924                 heap_close(rel, NoLock);
925                 return gettype(type);
926         }
927         elog(ERROR, "unrecognized type \"%s\"", type);
928         /* not reached, here to make compiler happy */
929         return 0;
930 }
931
932 /* ----------------
933  *              boot_get_type_io_data
934  *
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.
940  * ----------------
941  */
942 void
943 boot_get_type_io_data(Oid typid,
944                                           int16 *typlen,
945                                           bool *typbyval,
946                                           char *typalign,
947                                           char *typdelim,
948                                           Oid *typioparam,
949                                           Oid *typinput,
950                                           Oid *typoutput)
951 {
952         if (Typ != NULL)
953         {
954                 /* We have the boot-time contents of pg_type, so use it */
955                 struct typmap **app;
956                 struct typmap *ap;
957
958                 app = Typ;
959                 while (*app && (*app)->am_oid != typid)
960                         ++app;
961                 ap = *app;
962                 if (ap == NULL)
963                         elog(ERROR, "type OID %u not found in Typ list", typid);
964
965                 *typlen = ap->am_typ.typlen;
966                 *typbyval = ap->am_typ.typbyval;
967                 *typalign = ap->am_typ.typalign;
968                 *typdelim = ap->am_typ.typdelim;
969
970                 /* XXX this logic must match getTypeIOParam() */
971                 if (OidIsValid(ap->am_typ.typelem))
972                         *typioparam = ap->am_typ.typelem;
973                 else
974                         *typioparam = typid;
975
976                 *typinput = ap->am_typ.typinput;
977                 *typoutput = ap->am_typ.typoutput;
978         }
979         else
980         {
981                 /* We don't have pg_type yet, so use the hard-wired TypInfo array */
982                 int                     typeindex;
983
984                 for (typeindex = 0; typeindex < n_types; typeindex++)
985                 {
986                         if (TypInfo[typeindex].oid == typid)
987                                 break;
988                 }
989                 if (typeindex >= n_types)
990                         elog(ERROR, "type OID %u not found in TypInfo", typid);
991
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 */
996                 *typdelim = ',';
997
998                 /* XXX this logic must match getTypeIOParam() */
999                 if (OidIsValid(TypInfo[typeindex].elem))
1000                         *typioparam = TypInfo[typeindex].elem;
1001                 else
1002                         *typioparam = typid;
1003
1004                 *typinput = TypInfo[typeindex].inproc;
1005                 *typoutput = TypInfo[typeindex].outproc;
1006         }
1007 }
1008
1009 /* ----------------
1010  *              AllocateAttribute
1011  *
1012  * Note: bootstrap never sets any per-column ACLs, so we only need
1013  * ATTRIBUTE_FIXED_PART_SIZE space per attribute.
1014  * ----------------
1015  */
1016 static Form_pg_attribute
1017 AllocateAttribute(void)
1018 {
1019         Form_pg_attribute attribute = (Form_pg_attribute) malloc(ATTRIBUTE_FIXED_PART_SIZE);
1020
1021         if (!PointerIsValid(attribute))
1022                 elog(FATAL, "out of memory");
1023         MemSet(attribute, 0, ATTRIBUTE_FIXED_PART_SIZE);
1024
1025         return attribute;
1026 }
1027
1028 /* ----------------
1029  *              MapArrayTypeName
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.
1035  *
1036  * the string passed in must have a '[' character in it
1037  *
1038  * the string returned is a pointer to static storage and should NOT
1039  * be freed by the CALLER.
1040  * ----------------
1041  */
1042 char *
1043 MapArrayTypeName(char *s)
1044 {
1045         int                     i,
1046                                 j;
1047         static char newStr[NAMEDATALEN];        /* array type names < NAMEDATALEN long */
1048
1049         if (s == NULL || s[0] == '\0')
1050                 return s;
1051
1052         j = 1;
1053         newStr[0] = '_';
1054         for (i = 0; i < NAMEDATALEN - 1 && s[i] != '['; i++, j++)
1055                 newStr[j] = s[i];
1056
1057         newStr[j] = '\0';
1058
1059         return newStr;
1060 }
1061
1062
1063 /*
1064  *      index_register() -- record an index that has been set up for building
1065  *                                              later.
1066  *
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.
1073  */
1074 void
1075 index_register(Oid heap,
1076                            Oid ind,
1077                            IndexInfo *indexInfo)
1078 {
1079         IndexList  *newind;
1080         MemoryContext oldcxt;
1081
1082         /*
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
1085          * later.
1086          */
1087
1088         if (nogc == NULL)
1089                 nogc = AllocSetContextCreate(NULL,
1090                                                                          "BootstrapNoGC",
1091                                                                          ALLOCSET_DEFAULT_MINSIZE,
1092                                                                          ALLOCSET_DEFAULT_INITSIZE,
1093                                                                          ALLOCSET_DEFAULT_MAXSIZE);
1094
1095         oldcxt = MemoryContextSwitchTo(nogc);
1096
1097         newind = (IndexList *) palloc(sizeof(IndexList));
1098         newind->il_heap = heap;
1099         newind->il_ind = ind;
1100         newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo));
1101
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);
1115
1116         newind->il_next = ILHead;
1117         ILHead = newind;
1118
1119         MemoryContextSwitchTo(oldcxt);
1120 }
1121
1122
1123 /*
1124  * build_indices -- fill in all the indexes registered earlier
1125  */
1126 void
1127 build_indices(void)
1128 {
1129         for (; ILHead != NULL; ILHead = ILHead->il_next)
1130         {
1131                 Relation        heap;
1132                 Relation        ind;
1133
1134                 /* need not bother with locks during bootstrap */
1135                 heap = heap_open(ILHead->il_heap, NoLock);
1136                 ind = index_open(ILHead->il_ind, NoLock);
1137
1138                 index_build(heap, ind, ILHead->il_info, false, false);
1139
1140                 index_close(ind, NoLock);
1141                 heap_close(heap, NoLock);
1142         }
1143 }