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

Generated by: LCOV version 1.14