LCOV - code coverage report
Current view: top level - src/backend/bootstrap - bootparse.y (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 130 157 82.8 %
Date: 2019-11-21 14:06:36 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : %{
       2             : /*-------------------------------------------------------------------------
       3             :  *
       4             :  * bootparse.y
       5             :  *    yacc grammar for the "bootstrap" mode (BKI file format)
       6             :  *
       7             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  *
      11             :  * IDENTIFICATION
      12             :  *    src/backend/bootstrap/bootparse.y
      13             :  *
      14             :  *-------------------------------------------------------------------------
      15             :  */
      16             : 
      17             : #include "postgres.h"
      18             : 
      19             : #include <unistd.h>
      20             : 
      21             : #include "access/attnum.h"
      22             : #include "access/htup.h"
      23             : #include "access/itup.h"
      24             : #include "access/tupdesc.h"
      25             : #include "bootstrap/bootstrap.h"
      26             : #include "catalog/catalog.h"
      27             : #include "catalog/heap.h"
      28             : #include "catalog/namespace.h"
      29             : #include "catalog/pg_am.h"
      30             : #include "catalog/pg_attribute.h"
      31             : #include "catalog/pg_authid.h"
      32             : #include "catalog/pg_class.h"
      33             : #include "catalog/pg_namespace.h"
      34             : #include "catalog/pg_tablespace.h"
      35             : #include "catalog/toasting.h"
      36             : #include "commands/defrem.h"
      37             : #include "miscadmin.h"
      38             : #include "nodes/makefuncs.h"
      39             : #include "nodes/nodes.h"
      40             : #include "nodes/parsenodes.h"
      41             : #include "nodes/pg_list.h"
      42             : #include "nodes/primnodes.h"
      43             : #include "rewrite/prs2lock.h"
      44             : #include "storage/block.h"
      45             : #include "storage/fd.h"
      46             : #include "storage/ipc.h"
      47             : #include "storage/itemptr.h"
      48             : #include "storage/off.h"
      49             : #include "storage/smgr.h"
      50             : #include "tcop/dest.h"
      51             : #include "utils/memutils.h"
      52             : #include "utils/rel.h"
      53             : 
      54             : 
      55             : /*
      56             :  * Bison doesn't allocate anything that needs to live across parser calls,
      57             :  * so we can easily have it use palloc instead of malloc.  This prevents
      58             :  * memory leaks if we error out during parsing.  Note this only works with
      59             :  * bison >= 2.0.  However, in bison 1.875 the default is to use alloca()
      60             :  * if possible, so there's not really much problem anyhow, at least if
      61             :  * you're building with gcc.
      62             :  */
      63             : #define YYMALLOC palloc
      64             : #define YYFREE   pfree
      65             : 
      66             : static MemoryContext per_line_ctx = NULL;
      67             : 
      68             : static void
      69     2060478 : do_start(void)
      70             : {
      71             :     Assert(CurrentMemoryContext == CurTransactionContext);
      72             :     /* First time through, create the per-line working context */
      73     2060478 :     if (per_line_ctx == NULL)
      74         322 :         per_line_ctx = AllocSetContextCreate(CurTransactionContext,
      75             :                                              "bootstrap per-line processing",
      76             :                                              ALLOCSET_DEFAULT_SIZES);
      77     2060478 :     MemoryContextSwitchTo(per_line_ctx);
      78     2060478 : }
      79             : 
      80             : 
      81             : static void
      82     2060478 : do_end(void)
      83             : {
      84             :     /* Reclaim memory allocated while processing this line */
      85     2060478 :     MemoryContextSwitchTo(CurTransactionContext);
      86     2060478 :     MemoryContextReset(per_line_ctx);
      87     2060478 :     CHECK_FOR_INTERRUPTS();     /* allow SIGINT to kill bootstrap run */
      88     2060478 :     if (isatty(0))
      89             :     {
      90           0 :         printf("bootstrap> ");
      91           0 :         fflush(stdout);
      92             :     }
      93     2060478 : }
      94             : 
      95             : 
      96             : static int num_columns_read = 0;
      97             : 
      98             : %}
      99             : 
     100             : %expect 0
     101             : %name-prefix="boot_yy"
     102             : 
     103             : %union
     104             : {
     105             :     List        *list;
     106             :     IndexElem   *ielem;
     107             :     char        *str;
     108             :     const char  *kw;
     109             :     int         ival;
     110             :     Oid         oidval;
     111             : }
     112             : 
     113             : %type <list>  boot_index_params
     114             : %type <ielem> boot_index_param
     115             : %type <str>   boot_ident
     116             : %type <ival>  optbootstrap optsharedrelation boot_column_nullness
     117             : %type <oidval> oidspec optrowtypeoid
     118             : 
     119             : %token <str> ID
     120             : %token COMMA EQUALS LPAREN RPAREN
     121             : /* NULLVAL is a reserved keyword */
     122             : %token NULLVAL
     123             : /* All the rest are unreserved, and should be handled in boot_ident! */
     124             : %token <kw> OPEN XCLOSE XCREATE INSERT_TUPLE
     125             : %token <kw> XDECLARE INDEX ON USING XBUILD INDICES UNIQUE XTOAST
     126             : %token <kw> OBJ_ID XBOOTSTRAP XSHARED_RELATION XROWTYPE_OID
     127             : %token <kw> XFORCE XNOT XNULL
     128             : 
     129             : %start TopLevel
     130             : 
     131             : %%
     132             : 
     133             : TopLevel:
     134             :           Boot_Queries
     135             :         |
     136             :         ;
     137             : 
     138             : Boot_Queries:
     139             :           Boot_Query
     140             :         | Boot_Queries Boot_Query
     141             :         ;
     142             : 
     143             : Boot_Query :
     144             :           Boot_OpenStmt
     145             :         | Boot_CloseStmt
     146             :         | Boot_CreateStmt
     147             :         | Boot_InsertStmt
     148             :         | Boot_DeclareIndexStmt
     149             :         | Boot_DeclareUniqueIndexStmt
     150             :         | Boot_DeclareToastStmt
     151             :         | Boot_BuildIndsStmt
     152             :         ;
     153             : 
     154             : Boot_OpenStmt:
     155             :           OPEN boot_ident
     156             :                 {
     157       18998 :                     do_start();
     158       18998 :                     boot_openrel($2);
     159       18998 :                     do_end();
     160             :                 }
     161             :         ;
     162             : 
     163             : Boot_CloseStmt:
     164             :           XCLOSE boot_ident
     165             :                 {
     166       20286 :                     do_start();
     167       20286 :                     closerel($2);
     168       20286 :                     do_end();
     169             :                 }
     170             :         ;
     171             : 
     172             : Boot_CreateStmt:
     173             :           XCREATE boot_ident oidspec optbootstrap optsharedrelation optrowtypeoid LPAREN
     174             :                 {
     175       20286 :                     do_start();
     176       20286 :                     numattr = 0;
     177       20286 :                     elog(DEBUG4, "creating%s%s relation %s %u",
     178             :                          $4 ? " bootstrap" : "",
     179             :                          $5 ? " shared" : "",
     180             :                          $2,
     181             :                          $3);
     182             :                 }
     183             :           boot_column_list
     184             :                 {
     185       20286 :                     do_end();
     186             :                 }
     187             :           RPAREN
     188             :                 {
     189             :                     TupleDesc tupdesc;
     190             :                     bool    shared_relation;
     191             :                     bool    mapped_relation;
     192             : 
     193       20286 :                     do_start();
     194             : 
     195       20286 :                     tupdesc = CreateTupleDesc(numattr, attrtypes);
     196             : 
     197       20286 :                     shared_relation = $5;
     198             : 
     199             :                     /*
     200             :                      * The catalogs that use the relation mapper are the
     201             :                      * bootstrap catalogs plus the shared catalogs.  If this
     202             :                      * ever gets more complicated, we should invent a BKI
     203             :                      * keyword to mark the mapped catalogs, but for now a
     204             :                      * quick hack seems the most appropriate thing.  Note in
     205             :                      * particular that all "nailed" heap rels (see formrdesc
     206             :                      * in relcache.c) must be mapped.
     207             :                      */
     208       20286 :                     mapped_relation = ($4 || shared_relation);
     209             : 
     210       20286 :                     if ($4)
     211             :                     {
     212             :                         TransactionId relfrozenxid;
     213             :                         MultiXactId relminmxid;
     214             : 
     215        1288 :                         if (boot_reldesc)
     216             :                         {
     217           0 :                             elog(DEBUG4, "create bootstrap: warning, open relation exists, closing first");
     218           0 :                             closerel(NULL);
     219             :                         }
     220             : 
     221        2576 :                         boot_reldesc = heap_create($2,
     222             :                                                    PG_CATALOG_NAMESPACE,
     223             :                                                    shared_relation ? GLOBALTABLESPACE_OID : 0,
     224        1288 :                                                    $3,
     225             :                                                    InvalidOid,
     226             :                                                    HEAP_TABLE_AM_OID,
     227             :                                                    tupdesc,
     228             :                                                    RELKIND_RELATION,
     229             :                                                    RELPERSISTENCE_PERMANENT,
     230             :                                                    shared_relation,
     231             :                                                    mapped_relation,
     232             :                                                    true,
     233             :                                                    &relfrozenxid,
     234             :                                                    &relminmxid);
     235        1288 :                         elog(DEBUG4, "bootstrap relation created");
     236             :                     }
     237             :                     else
     238             :                     {
     239             :                         Oid id;
     240             : 
     241       56994 :                         id = heap_create_with_catalog($2,
     242             :                                                       PG_CATALOG_NAMESPACE,
     243             :                                                       shared_relation ? GLOBALTABLESPACE_OID : 0,
     244       18998 :                                                       $3,
     245       18998 :                                                       $6,
     246             :                                                       InvalidOid,
     247             :                                                       BOOTSTRAP_SUPERUSERID,
     248             :                                                       HEAP_TABLE_AM_OID,
     249             :                                                       tupdesc,
     250             :                                                       NIL,
     251             :                                                       RELKIND_RELATION,
     252             :                                                       RELPERSISTENCE_PERMANENT,
     253             :                                                       shared_relation,
     254             :                                                       mapped_relation,
     255             :                                                       ONCOMMIT_NOOP,
     256             :                                                       (Datum) 0,
     257             :                                                       false,
     258             :                                                       true,
     259             :                                                       false,
     260             :                                                       InvalidOid,
     261             :                                                       NULL);
     262       18998 :                         elog(DEBUG4, "relation created with OID %u", id);
     263             :                     }
     264       20286 :                     do_end();
     265             :                 }
     266             :         ;
     267             : 
     268             : Boot_InsertStmt:
     269             :           INSERT_TUPLE
     270             :                 {
     271     1931356 :                     do_start();
     272     1931356 :                     elog(DEBUG4, "inserting row");
     273     1931356 :                     num_columns_read = 0;
     274             :                 }
     275             :           LPAREN boot_column_val_list RPAREN
     276             :                 {
     277     1931356 :                     if (num_columns_read != numattr)
     278           0 :                         elog(ERROR, "incorrect number of columns in row (expected %d, got %d)",
     279             :                              numattr, num_columns_read);
     280     1931356 :                     if (boot_reldesc == NULL)
     281           0 :                         elog(FATAL, "relation not open");
     282     1931356 :                     InsertOneTuple();
     283     1931356 :                     do_end();
     284             :                 }
     285             :         ;
     286             : 
     287             : Boot_DeclareIndexStmt:
     288             :           XDECLARE INDEX boot_ident oidspec ON boot_ident USING boot_ident LPAREN boot_index_params RPAREN
     289             :                 {
     290        3864 :                     IndexStmt *stmt = makeNode(IndexStmt);
     291             :                     Oid     relationId;
     292             : 
     293        3864 :                     elog(DEBUG4, "creating index \"%s\"", $3);
     294             : 
     295        3864 :                     do_start();
     296             : 
     297        3864 :                     stmt->idxname = $3;
     298        3864 :                     stmt->relation = makeRangeVar(NULL, $6, -1);
     299        3864 :                     stmt->accessMethod = $8;
     300        3864 :                     stmt->tableSpace = NULL;
     301        3864 :                     stmt->indexParams = $10;
     302        3864 :                     stmt->indexIncludingParams = NIL;
     303        3864 :                     stmt->options = NIL;
     304        3864 :                     stmt->whereClause = NULL;
     305        3864 :                     stmt->excludeOpNames = NIL;
     306        3864 :                     stmt->idxcomment = NULL;
     307        3864 :                     stmt->indexOid = InvalidOid;
     308        3864 :                     stmt->oldNode = InvalidOid;
     309        3864 :                     stmt->unique = false;
     310        3864 :                     stmt->primary = false;
     311        3864 :                     stmt->isconstraint = false;
     312        3864 :                     stmt->deferrable = false;
     313        3864 :                     stmt->initdeferred = false;
     314        3864 :                     stmt->transformed = false;
     315        3864 :                     stmt->concurrent = false;
     316        3864 :                     stmt->if_not_exists = false;
     317        3864 :                     stmt->reset_default_tblspc = false;
     318             : 
     319             :                     /* locks and races need not concern us in bootstrap mode */
     320        3864 :                     relationId = RangeVarGetRelid(stmt->relation, NoLock,
     321             :                                                   false);
     322             : 
     323        3864 :                     DefineIndex(relationId,
     324             :                                 stmt,
     325        3864 :                                 $4,
     326             :                                 InvalidOid,
     327             :                                 InvalidOid,
     328             :                                 false,
     329             :                                 false,
     330             :                                 false,
     331             :                                 true, /* skip_build */
     332             :                                 false);
     333        3864 :                     do_end();
     334             :                 }
     335             :         ;
     336             : 
     337             : Boot_DeclareUniqueIndexStmt:
     338             :           XDECLARE UNIQUE INDEX boot_ident oidspec ON boot_ident USING boot_ident LPAREN boot_index_params RPAREN
     339             :                 {
     340       33810 :                     IndexStmt *stmt = makeNode(IndexStmt);
     341             :                     Oid     relationId;
     342             : 
     343       33810 :                     elog(DEBUG4, "creating unique index \"%s\"", $4);
     344             : 
     345       33810 :                     do_start();
     346             : 
     347       33810 :                     stmt->idxname = $4;
     348       33810 :                     stmt->relation = makeRangeVar(NULL, $7, -1);
     349       33810 :                     stmt->accessMethod = $9;
     350       33810 :                     stmt->tableSpace = NULL;
     351       33810 :                     stmt->indexParams = $11;
     352       33810 :                     stmt->indexIncludingParams = NIL;
     353       33810 :                     stmt->options = NIL;
     354       33810 :                     stmt->whereClause = NULL;
     355       33810 :                     stmt->excludeOpNames = NIL;
     356       33810 :                     stmt->idxcomment = NULL;
     357       33810 :                     stmt->indexOid = InvalidOid;
     358       33810 :                     stmt->oldNode = InvalidOid;
     359       33810 :                     stmt->unique = true;
     360       33810 :                     stmt->primary = false;
     361       33810 :                     stmt->isconstraint = false;
     362       33810 :                     stmt->deferrable = false;
     363       33810 :                     stmt->initdeferred = false;
     364       33810 :                     stmt->transformed = false;
     365       33810 :                     stmt->concurrent = false;
     366       33810 :                     stmt->if_not_exists = false;
     367       33810 :                     stmt->reset_default_tblspc = false;
     368             : 
     369             :                     /* locks and races need not concern us in bootstrap mode */
     370       33810 :                     relationId = RangeVarGetRelid(stmt->relation, NoLock,
     371             :                                                   false);
     372             : 
     373       33810 :                     DefineIndex(relationId,
     374             :                                 stmt,
     375       33810 :                                 $5,
     376             :                                 InvalidOid,
     377             :                                 InvalidOid,
     378             :                                 false,
     379             :                                 false,
     380             :                                 false,
     381             :                                 true, /* skip_build */
     382             :                                 false);
     383       33810 :                     do_end();
     384             :                 }
     385             :         ;
     386             : 
     387             : Boot_DeclareToastStmt:
     388             :           XDECLARE XTOAST oidspec oidspec ON boot_ident
     389             :                 {
     390       11270 :                     elog(DEBUG4, "creating toast table for table \"%s\"", $6);
     391             : 
     392       11270 :                     do_start();
     393             : 
     394       11270 :                     BootstrapToastTable($6, $3, $4);
     395       11270 :                     do_end();
     396             :                 }
     397             :         ;
     398             : 
     399             : Boot_BuildIndsStmt:
     400             :           XBUILD INDICES
     401             :                 {
     402         322 :                     do_start();
     403         322 :                     build_indices();
     404         322 :                     do_end();
     405             :                 }
     406             :         ;
     407             : 
     408             : 
     409             : boot_index_params:
     410       24794 :         boot_index_params COMMA boot_index_param    { $$ = lappend($1, $3); }
     411       37674 :         | boot_index_param                          { $$ = list_make1($1); }
     412             :         ;
     413             : 
     414             : boot_index_param:
     415             :         boot_ident boot_ident
     416             :                 {
     417       62468 :                     IndexElem *n = makeNode(IndexElem);
     418       62468 :                     n->name = $1;
     419       62468 :                     n->expr = NULL;
     420       62468 :                     n->indexcolname = NULL;
     421       62468 :                     n->collation = NIL;
     422       62468 :                     n->opclass = list_make1(makeString($2));
     423       62468 :                     n->ordering = SORTBY_DEFAULT;
     424       62468 :                     n->nulls_ordering = SORTBY_NULLS_DEFAULT;
     425       62468 :                     $$ = n;
     426             :                 }
     427             :         ;
     428             : 
     429             : optbootstrap:
     430        1288 :             XBOOTSTRAP  { $$ = 1; }
     431       18998 :         |               { $$ = 0; }
     432             :         ;
     433             : 
     434             : optsharedrelation:
     435        3542 :             XSHARED_RELATION    { $$ = 1; }
     436       16744 :         |                       { $$ = 0; }
     437             :         ;
     438             : 
     439             : optrowtypeoid:
     440        2898 :             XROWTYPE_OID oidspec    { $$ = $2; }
     441       17388 :         |                           { $$ = InvalidOid; }
     442             :         ;
     443             : 
     444             : boot_column_list:
     445             :           boot_column_def
     446             :         | boot_column_list COMMA boot_column_def
     447             :         ;
     448             : 
     449             : boot_column_def:
     450             :           boot_ident EQUALS boot_ident boot_column_nullness
     451             :                 {
     452      182896 :                    if (++numattr > MAXATTR)
     453           0 :                         elog(FATAL, "too many columns");
     454      182896 :                    DefineAttr($1, $3, numattr-1, $4);
     455             :                 }
     456             :         ;
     457             : 
     458             : boot_column_nullness:
     459        7406 :             XFORCE XNOT XNULL   { $$ = BOOTCOL_NULL_FORCE_NOT_NULL; }
     460           0 :         |   XFORCE XNULL        {  $$ = BOOTCOL_NULL_FORCE_NULL; }
     461      175490 :         | { $$ = BOOTCOL_NULL_AUTO; }
     462             :         ;
     463             : 
     464             : oidspec:
     465       83398 :             boot_ident                          { $$ = atooid($1); }
     466             :         ;
     467             : 
     468             : boot_column_val_list:
     469             :            boot_column_val
     470             :         |  boot_column_val_list boot_column_val
     471             :         |  boot_column_val_list COMMA boot_column_val
     472             :         ;
     473             : 
     474             : boot_column_val:
     475             :           boot_ident
     476    31636822 :             { InsertOneValue($1, num_columns_read++); }
     477             :         | NULLVAL
     478     7930860 :             { InsertOneNull(num_columns_read++); }
     479             :         ;
     480             : 
     481             : boot_ident:
     482    32394810 :           ID            { $$ = $1; }
     483           0 :         | OPEN          { $$ = pstrdup($1); }
     484           0 :         | XCLOSE        { $$ = pstrdup($1); }
     485           0 :         | XCREATE       { $$ = pstrdup($1); }
     486           0 :         | INSERT_TUPLE  { $$ = pstrdup($1); }
     487           0 :         | XDECLARE      { $$ = pstrdup($1); }
     488           0 :         | INDEX         { $$ = pstrdup($1); }
     489           0 :         | ON            { $$ = pstrdup($1); }
     490           0 :         | USING         { $$ = pstrdup($1); }
     491           0 :         | XBUILD        { $$ = pstrdup($1); }
     492           0 :         | INDICES       { $$ = pstrdup($1); }
     493           0 :         | UNIQUE        { $$ = pstrdup($1); }
     494           0 :         | XTOAST        { $$ = pstrdup($1); }
     495           0 :         | OBJ_ID        { $$ = pstrdup($1); }
     496           0 :         | XBOOTSTRAP    { $$ = pstrdup($1); }
     497           0 :         | XSHARED_RELATION  { $$ = pstrdup($1); }
     498           0 :         | XROWTYPE_OID  { $$ = pstrdup($1); }
     499           0 :         | XFORCE        { $$ = pstrdup($1); }
     500           0 :         | XNOT          { $$ = pstrdup($1); }
     501           0 :         | XNULL         { $$ = pstrdup($1); }
     502             :         ;
     503             : %%
     504             : 
     505             : #include "bootscanner.c"

Generated by: LCOV version 1.13