bootparse.y 6.51 KB
Newer Older
1 2 3
%{
/*-------------------------------------------------------------------------
 *
4
 * backendparse.y
5
 *	  yacc parser grammer for the "backend" initialization program.
6
 *
7
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
8
 * Portions Copyright (c) 1994, Regents of the University of California
9 10 11
 *
 *
 * IDENTIFICATION
12
 *	  $Header: /cvsroot/pgsql/src/backend/bootstrap/bootparse.y,v 1.44 2002/04/09 20:35:46 tgl Exp $
13 14 15
 *
 *-------------------------------------------------------------------------
 */
Marc G. Fournier's avatar
Marc G. Fournier committed
16 17 18

#include "postgres.h"

19 20
#include <time.h>
#include <unistd.h>
21

22
#include "access/attnum.h"
23 24 25 26
#include "access/htup.h"
#include "access/itup.h"
#include "access/skey.h"
#include "access/strat.h"
27
#include "access/tupdesc.h"
28 29
#include "access/xact.h"
#include "bootstrap/bootstrap.h"
30
#include "catalog/catalog.h"
31
#include "catalog/heap.h"
32
#include "catalog/pg_am.h"
33
#include "catalog/pg_attribute.h"
Marc G. Fournier's avatar
Marc G. Fournier committed
34
#include "catalog/pg_class.h"
35
#include "catalog/pg_namespace.h"
36
#include "commands/defrem.h"
Bruce Momjian's avatar
Bruce Momjian committed
37
#include "miscadmin.h"
38
#include "nodes/makefuncs.h"
Marc G. Fournier's avatar
Marc G. Fournier committed
39
#include "nodes/nodes.h"
40 41
#include "nodes/parsenodes.h"
#include "nodes/pg_list.h"
42
#include "nodes/primnodes.h"
43
#include "rewrite/prs2lock.h"
Marc G. Fournier's avatar
Marc G. Fournier committed
44
#include "storage/block.h"
45
#include "storage/fd.h"
46
#include "storage/ipc.h"
47 48
#include "storage/itemptr.h"
#include "storage/off.h"
49
#include "storage/smgr.h"
Marc G. Fournier's avatar
Marc G. Fournier committed
50
#include "tcop/dest.h"
51 52
#include "utils/nabstime.h"
#include "utils/rel.h"
53 54


55 56 57 58
static void
do_start()
{
	StartTransactionCommand();
59
	elog(DEBUG3, "start transaction");
60 61
}

62

63 64 65 66
static void
do_end()
{
	CommitTransactionCommand();
67
	elog(DEBUG3, "commit transaction");
68 69 70 71 72 73 74 75 76
	if (isatty(0))
	{
		printf("bootstrap> ");
		fflush(stdout);
	}
}


int num_columns_read = 0;
77 78 79

%}

80 81 82 83 84 85
%union
{
	List		*list;
	IndexElem	*ielem;
	char		*str;
	int			ival;
86
	Oid			oidval;
87 88
}

89 90
%type <list>  boot_index_params
%type <ielem> boot_index_param
91
%type <ival> boot_const boot_ident
92
%type <ival> optbootstrap optwithoutoids boot_tuple boot_tuplelist
93
%type <oidval> optoideq
94 95 96

%token <ival> CONST ID
%token OPEN XCLOSE XCREATE INSERT_TUPLE
97
%token STRING XDEFINE
98
%token XDECLARE INDEX ON USING XBUILD INDICES UNIQUE
99
%token COMMA EQUALS LPAREN RPAREN
100
%token OBJ_ID XBOOTSTRAP XWITHOUT_OIDS NULLVAL
101 102 103 104 105 106 107 108
%start TopLevel

%nonassoc low
%nonassoc high

%%

TopLevel:
109
		  Boot_Queries
110 111
		|
		;
112

113 114 115
Boot_Queries:
		  Boot_Query
		| Boot_Queries Boot_Query
116
		;
117

118 119 120 121 122 123
Boot_Query :
		  Boot_OpenStmt
		| Boot_CloseStmt
		| Boot_CreateStmt
		| Boot_InsertStmt
		| Boot_DeclareIndexStmt
124
		| Boot_DeclareUniqueIndexStmt
125
		| Boot_BuildIndsStmt
126 127
		;

128 129
Boot_OpenStmt:
		  OPEN boot_ident
130
				{
131
					do_start();
132
					boot_openrel(LexIDStr($2));
133
					do_end();
134 135
				}
		;
136

137 138
Boot_CloseStmt:
		  XCLOSE boot_ident %prec low
139
				{
140
					do_start();
141
					closerel(LexIDStr($2));
142
					do_end();
143 144 145
				}
		| XCLOSE %prec high
				{
146
					do_start();
147
					closerel(NULL);
148
					do_end();
149 150
				}
		;
151

152
Boot_CreateStmt:
153
		  XCREATE optbootstrap optwithoutoids boot_ident LPAREN
154
				{
155 156
					do_start();
					numattr = 0;
157 158 159 160 161 162
					if ($2)
						elog(DEBUG3, "creating bootstrap relation %s...",
							 LexIDStr($4));
					else
						elog(DEBUG3, "creating relation %s...",
							 LexIDStr($4));
163
				}
164
		  boot_typelist
165
				{
166
					do_end();
167 168 169
				}
		  RPAREN
				{
170
					do_start();
171 172 173 174 175 176 177 178

					if ($2)
					{
						extern Relation reldesc;
						TupleDesc tupdesc;

						if (reldesc)
						{
179
							elog(DEBUG3, "create bootstrap: warning, open relation exists, closing first");
180 181
							closerel(NULL);
						}
182

183
						tupdesc = CreateTupleDesc(numattr, attrtypes);
184 185 186
						reldesc = heap_create(LexIDStr($4),
											  PG_CATALOG_NAMESPACE,
											  tupdesc,
187
											  true, true);
188
						reldesc->rd_rel->relhasoids = ! ($3);
189
						elog(DEBUG3, "bootstrap relation created");
190 191 192 193 194 195 196
					}
					else
					{
						Oid id;
						TupleDesc tupdesc;

						tupdesc = CreateTupleDesc(numattr,attrtypes);
197
						id = heap_create_with_catalog(LexIDStr($4),
198
													  PG_CATALOG_NAMESPACE,
199 200
													  tupdesc,
													  RELKIND_RELATION,
201
													  ! ($3),
202
													  true);
203
						elog(DEBUG3, "relation created with oid %u", id);
204
					}
205
					do_end();
206 207
				}
		;
208

209
Boot_InsertStmt:
210 211
		  INSERT_TUPLE optoideq
				{
212
					do_start();
213 214 215 216
					if ($2)
						elog(DEBUG3, "inserting row with oid %u...", $2);
					else
						elog(DEBUG3, "inserting row...");
217
					num_columns_read = 0;
218
				}
219
		  LPAREN  boot_tuplelist RPAREN
220
				{
221 222 223
					if (num_columns_read != numattr)
						elog(ERROR, "incorrect number of columns in row (expected %d, got %d)",
							 numattr, num_columns_read);
224 225
					if (reldesc == (Relation)NULL)
					{
226
						elog(ERROR, "relation not open");
227 228
						err_out();
					}
229
					InsertOneTuple($2);
230
					do_end();
231 232
				}
		;
233

234 235
Boot_DeclareIndexStmt:
		  XDECLARE INDEX boot_ident ON boot_ident USING boot_ident LPAREN boot_index_params RPAREN
236
				{
237
					do_start();
238

239
					DefineIndex(makeRangeVar(NULL, LexIDStr($5)),
240 241
								LexIDStr($3),
								LexIDStr($7),
242
								$9, false, false, NULL, NIL);
243
					do_end();
244 245
				}
		;
246

247 248 249
Boot_DeclareUniqueIndexStmt:
		  XDECLARE UNIQUE INDEX boot_ident ON boot_ident USING boot_ident LPAREN boot_index_params RPAREN
				{
250
					do_start();
251

252
					DefineIndex(makeRangeVar(NULL, LexIDStr($6)),
253 254
								LexIDStr($4),
								LexIDStr($8),
255
								$10, true, false, NULL, NIL);
256
					do_end();
257 258 259
				}
		;

260
Boot_BuildIndsStmt:
261
		  XBUILD INDICES		{ build_indices(); }
262
		;
263

264

265
boot_index_params:
266
		boot_index_params COMMA boot_index_param	{ $$ = lappend($1, $3); }
267
		| boot_index_param							{ $$ = makeList1($1); }
268
		;
269

270 271
boot_index_param:
		boot_ident boot_ident
272 273 274
				{
					IndexElem *n = makeNode(IndexElem);
					n->name = LexIDStr($1);
275
					n->funcname = n->args = NIL; /* no func indexes */
276
					n->class = LexIDStr($2);
277 278
					$$ = n;
				}
279
		;
280 281

optbootstrap:
282 283 284
			XBOOTSTRAP	{ $$ = 1; }
		|				{ $$ = 0; }
		;
285

286 287 288 289 290
optwithoutoids:
			XWITHOUT_OIDS	{ $$ = 1; }
		|					{ $$ = 0; }
		;

291 292 293
boot_typelist:
		  boot_type_thing
		| boot_typelist COMMA boot_type_thing
294
		;
295

296 297
boot_type_thing:
		  boot_ident EQUALS boot_ident
298 299
				{
				   if(++numattr > MAXATTR)
300
						elog(FATAL, "too many columns");
301 302 303
				   DefineAttr(LexIDStr($1),LexIDStr($3),numattr-1);
				}
		;
304 305

optoideq:
306 307
			OBJ_ID EQUALS boot_ident { $$ = atol(LexIDStr($3));	}
		|						{ $$ = (Oid) 0;	}
308
		;
309

310 311 312 313
boot_tuplelist:
		   boot_tuple
		|  boot_tuplelist boot_tuple
		|  boot_tuplelist COMMA boot_tuple
314
		;
315

316
boot_tuple:
317 318 319 320
		  boot_ident
			{ InsertOneValue(LexIDStr($1), num_columns_read++); }
		| boot_const
			{ InsertOneValue(LexIDStr($1), num_columns_read++); }
321
		| NULLVAL
322
			{ InsertOneNull(num_columns_read++); }
323 324
		;

325
boot_const :
326 327
		  CONST { $$=yylval.ival; }
		;
328

329
boot_ident :
330 331
		  ID	{ $$=yylval.ival; }
		;
332
%%