1 /*-------------------------------------------------------------------------
4 * Contains functions which control the execution of the POSTGRES utility
5 * commands. At one time acted as an interface between the Lisp and C
8 * Copyright (c) 1994, Regents of the University of California
12 * $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.55 1999/02/13 23:18:48 momjian Exp $
14 *-------------------------------------------------------------------------
17 #include "access/xact.h"
18 #include "access/heapam.h"
19 #include "catalog/catalog.h"
20 #include "catalog/pg_type.h"
22 #include "commands/async.h"
23 #include "commands/cluster.h"
24 #include "commands/command.h"
25 #include "commands/copy.h"
26 #include "commands/creatinh.h"
27 #include "commands/dbcommands.h"
28 #include "commands/sequence.h"
29 #include "commands/defrem.h"
30 #include "commands/rename.h"
31 #include "commands/view.h"
32 #include "commands/version.h"
33 #include "commands/vacuum.h"
34 #include "commands/recipe.h"
35 #include "commands/explain.h"
36 #include "commands/trigger.h"
37 #include "commands/proclang.h"
38 #include "commands/variable.h"
40 #include "nodes/parsenodes.h"
41 #include "../backend/parser/parse.h"
42 #include "utils/builtins.h"
43 #include "utils/acl.h"
44 #include "utils/palloc.h"
45 #include "rewrite/rewriteRemove.h"
46 #include "rewrite/rewriteDefine.h"
47 #include "tcop/tcopdebug.h"
48 #include "tcop/dest.h"
49 #include "tcop/utility.h"
50 #include "fmgr.h" /* For load_file() */
51 #include "storage/fd.h"
52 #include "utils/ps_status.h"
55 #include "miscadmin.h"
56 #include "utils/acl.h"
57 #include "utils/syscache.h"
60 void DefineUser(CreateUserStmt *stmt);
61 void AlterUser(AlterUserStmt *stmt);
62 void RemoveUser(char *username);
65 * CHECK_IF_ABORTED() is used to avoid doing unnecessary
66 * processing within an aborted transaction block.
69 /* we have to use IF because of the 'break' */
70 #define CHECK_IF_ABORTED() \
73 if (IsAbortedTransactionBlockState()) \
75 elog(NOTICE, "(transaction aborted): %s", \
76 "all queries ignored until end of transaction block"); \
77 commandTag = "*ABORT STATE*"; \
83 * general utility function invoker
87 ProcessUtility(Node *parsetree,
90 char *commandTag = NULL;
95 userName = GetPgUserName();
97 switch (nodeTag(parsetree))
101 * ******************************** transactions ********************************
104 case T_TransactionStmt:
106 TransactionStmt *stmt = (TransactionStmt *) parsetree;
108 switch (stmt->command)
111 PS_SET_STATUS(commandTag = "BEGIN");
113 BeginTransactionBlock();
117 PS_SET_STATUS(commandTag = "END");
118 EndTransactionBlock();
122 PS_SET_STATUS(commandTag = "ABORT");
123 UserAbortTransactionBlock();
130 * ******************************** portal manipulation ********************************
133 case T_ClosePortalStmt:
135 ClosePortalStmt *stmt = (ClosePortalStmt *) parsetree;
137 PS_SET_STATUS(commandTag = "CLOSE");
140 PerformPortalClose(stmt->portalname, dest);
146 FetchStmt *stmt = (FetchStmt *) parsetree;
147 char *portalName = stmt->portalname;
151 PS_SET_STATUS(commandTag = (stmt->ismove) ? "MOVE" : "FETCH");
154 forward = (bool) (stmt->direction == FORWARD);
157 * parser ensures that count is >= 0 and 'fetch ALL' -> 0
160 count = stmt->howMany;
161 PerformPortalFetch(portalName, forward, count, commandTag,
162 (stmt->ismove) ? None : dest); /* /dev/null for MOVE */
167 * ******************************** relation and attribute
168 * manipulation ********************************
172 PS_SET_STATUS(commandTag = "CREATE");
175 DefineRelation((CreateStmt *) parsetree, RELKIND_RELATION);
180 DestroyStmt *stmt = (DestroyStmt *) parsetree;
182 List *args = stmt->relNames;
185 PS_SET_STATUS(commandTag = "DROP");
190 relname = strVal(lfirst(arg));
191 if (IsSystemRelationName(relname))
192 elog(ERROR, "class \"%s\" is a system catalog",
194 rel = heap_openr(relname);
195 if (RelationIsValid(rel))
197 if (stmt->sequence &&
198 rel->rd_rel->relkind != RELKIND_SEQUENCE)
199 elog(ERROR, "Use DROP TABLE to drop table '%s'",
201 if (!(stmt->sequence) &&
202 rel->rd_rel->relkind == RELKIND_SEQUENCE)
203 elog(ERROR, "Use DROP SEQUENCE to drop sequence '%s'",
208 if (!pg_ownercheck(userName, relname, RELNAME))
209 elog(ERROR, "you do not own class \"%s\"",
215 relname = strVal(lfirst(arg));
216 RemoveRelation(relname);
223 CopyStmt *stmt = (CopyStmt *) parsetree;
225 PS_SET_STATUS(commandTag = "COPY");
228 DoCopy(stmt->relname,
231 (bool) (stmt->direction == FROM),
232 (bool) (stmt->filename == NULL),
235 * null filename means copy to/from stdout/stdin, rather
236 * than to/from a file.
245 AddAttrStmt *stmt = (AddAttrStmt *) parsetree;
247 PS_SET_STATUS(commandTag = "ADD");
251 * owner checking done in PerformAddAttribute (now
254 PerformAddAttribute(stmt->relname,
257 (ColumnDef *) stmt->colDef);
266 RenameStmt *stmt = (RenameStmt *) parsetree;
268 PS_SET_STATUS(commandTag = "RENAME");
271 relname = stmt->relname;
272 if (IsSystemRelationName(relname))
273 elog(ERROR, "class \"%s\" is a system catalog",
276 if (!pg_ownercheck(userName, relname, RELNAME))
277 elog(ERROR, "you do not own class \"%s\"",
282 * XXX using len == 3 to tell the difference
283 * between "rename rel to newrel" and
284 * "rename att in rel to newatt" will not
285 * work soon because "rename type/operator/rule"
286 * stuff is being added. - cim 10/24/90
288 * [another piece of amuzing but useless anecdote -- ay]
290 if (stmt->column == NULL)
295 * Note: we also rename the "type" tuple
296 * corresponding to the relation.
299 renamerel(relname, /* old name */
300 stmt->newname); /* new name */
301 TypeRename(relname, /* old name */
302 stmt->newname); /* new name */
310 renameatt(relname, /* relname */
311 stmt->column, /* old att name */
312 stmt->newname, /* new att name */
314 stmt->inh); /* recursive? */
319 case T_ChangeACLStmt:
321 ChangeACLStmt *stmt = (ChangeACLStmt *) parsetree;
326 PS_SET_STATUS(commandTag = "CHANGE");
331 modechg = stmt->modechg;
333 foreach(i, stmt->relNames)
335 relname = strVal(lfirst(i));
336 if (!pg_ownercheck(userName, relname, RELNAME))
337 elog(ERROR, "you do not own class \"%s\"",
341 foreach(i, stmt->relNames)
343 relname = strVal(lfirst(i));
344 ChangeAcl(relname, aip, modechg);
351 * ******************************** object creation /
352 * destruction ********************************
357 DefineStmt *stmt = (DefineStmt *) parsetree;
359 PS_SET_STATUS(commandTag = "CREATE");
362 switch (stmt->defType)
365 DefineOperator(stmt->defname, /* operator name */
366 stmt->definition); /* rest */
369 DefineType(stmt->defname, stmt->definition);
372 DefineAggregate(stmt->defname, /* aggregate name */
373 stmt->definition); /* rest */
379 case T_ViewStmt: /* CREATE VIEW */
381 ViewStmt *stmt = (ViewStmt *) parsetree;
383 PS_SET_STATUS(commandTag = "CREATE");
385 DefineView(stmt->viewname, stmt->query); /* retrieve parsetree */
389 case T_ProcedureStmt: /* CREATE FUNCTION */
390 PS_SET_STATUS(commandTag = "CREATE");
392 CreateFunction((ProcedureStmt *) parsetree, dest); /* everything */
395 case T_IndexStmt: /* CREATE INDEX */
397 IndexStmt *stmt = (IndexStmt *) parsetree;
399 PS_SET_STATUS(commandTag = "CREATE");
401 DefineIndex(stmt->relname, /* relation name */
402 stmt->idxname, /* index name */
403 stmt->accessMethod, /* am name */
404 stmt->indexParams, /* parameters */
408 (Expr *) stmt->whereClause,
413 case T_RuleStmt: /* CREATE RULE */
415 RuleStmt *stmt = (RuleStmt *) parsetree;
419 relname = stmt->object->relname;
420 aclcheck_result = pg_aclcheck(relname, userName, ACL_RU);
421 if (aclcheck_result != ACLCHECK_OK)
422 elog(ERROR, "%s: %s", relname, aclcheck_error_strings[aclcheck_result]);
424 PS_SET_STATUS(commandTag = "CREATE");
426 DefineQueryRewrite(stmt);
430 case T_CreateSeqStmt:
431 PS_SET_STATUS(commandTag = "CREATE");
434 DefineSequence((CreateSeqStmt *) parsetree);
439 ExtendStmt *stmt = (ExtendStmt *) parsetree;
441 PS_SET_STATUS(commandTag = "EXTEND");
444 ExtendIndex(stmt->idxname, /* index name */
445 (Expr *) stmt->whereClause, /* where */
452 RemoveStmt *stmt = (RemoveStmt *) parsetree;
454 PS_SET_STATUS(commandTag = "DROP");
457 switch (stmt->removeType)
460 relname = stmt->name;
461 if (IsSystemRelationName(relname))
462 elog(ERROR, "class \"%s\" is a system catalog index",
465 if (!pg_ownercheck(userName, relname, RELNAME))
466 elog(ERROR, "%s: %s", relname, aclcheck_error_strings[ACLCHECK_NOT_OWNER]);
468 RemoveIndex(relname);
472 char *rulename = stmt->name;
477 relationName = RewriteGetRuleEventRel(rulename);
478 aclcheck_result = pg_aclcheck(relationName, userName, ACL_RU);
479 if (aclcheck_result != ACLCHECK_OK)
480 elog(ERROR, "%s: %s", relationName, aclcheck_error_strings[aclcheck_result]);
482 RemoveRewriteRule(rulename);
487 /* XXX moved to remove.c */
489 RemoveType(stmt->name);
493 char *viewName = stmt->name;
498 ruleName = MakeRetrieveViewRuleName(viewName);
499 relationName = RewriteGetRuleEventRel(ruleName);
500 if (!pg_ownercheck(userName, relationName, RELNAME))
501 elog(ERROR, "%s: %s", relationName, aclcheck_error_strings[ACLCHECK_NOT_OWNER]);
504 RemoveView(viewName);
512 case T_RemoveAggrStmt:
514 RemoveAggrStmt *stmt = (RemoveAggrStmt *) parsetree;
516 PS_SET_STATUS(commandTag = "DROP");
518 RemoveAggregate(stmt->aggname, stmt->aggtype);
522 case T_RemoveFuncStmt:
524 RemoveFuncStmt *stmt = (RemoveFuncStmt *) parsetree;
526 PS_SET_STATUS(commandTag = "DROP");
528 RemoveFunction(stmt->funcname,
534 case T_RemoveOperStmt:
536 RemoveOperStmt *stmt = (RemoveOperStmt *) parsetree;
537 char *type1 = (char *) NULL;
538 char *type2 = (char *) NULL;
540 PS_SET_STATUS(commandTag = "DROP");
543 if (lfirst(stmt->args) != NULL)
544 type1 = strVal(lfirst(stmt->args));
545 if (lsecond(stmt->args) != NULL)
546 type2 = strVal(lsecond(stmt->args));
547 RemoveOperator(stmt->opname, type1, type2);
552 elog(ERROR, "CREATE VERSION is not currently implemented");
557 CreatedbStmt *stmt = (CreatedbStmt *) parsetree;
559 PS_SET_STATUS(commandTag = "CREATEDB");
561 createdb(stmt->dbname, stmt->dbpath, stmt->encoding);
565 case T_DestroydbStmt:
567 DestroydbStmt *stmt = (DestroydbStmt *) parsetree;
569 PS_SET_STATUS(commandTag = "DESTROYDB");
571 destroydb(stmt->dbname);
575 /* Query-level asynchronous notification */
578 NotifyStmt *stmt = (NotifyStmt *) parsetree;
580 PS_SET_STATUS(commandTag = "NOTIFY");
583 Async_Notify(stmt->relname);
589 ListenStmt *stmt = (ListenStmt *) parsetree;
591 PS_SET_STATUS(commandTag = "LISTEN");
594 Async_Listen(stmt->relname, MyProcPid);
600 UnlistenStmt *stmt = (UnlistenStmt *) parsetree;
602 PS_SET_STATUS(commandTag = "UNLISTEN");
605 Async_Unlisten(stmt->relname, MyProcPid);
610 * ******************************** dynamic loader ********************************
615 LoadStmt *stmt = (LoadStmt *) parsetree;
619 PS_SET_STATUS(commandTag = "LOAD");
622 filename = stmt->filename;
625 if ((fp = AllocateFile(filename, "r")) == NULL)
627 if ((fp = AllocateFile(filename, "rb")) == NULL)
629 elog(ERROR, "LOAD: could not open file '%s'", filename);
637 ClusterStmt *stmt = (ClusterStmt *) parsetree;
639 PS_SET_STATUS(commandTag = "CLUSTER");
642 cluster(stmt->relname, stmt->indexname);
647 PS_SET_STATUS(commandTag = "VACUUM");
649 vacuum(((VacuumStmt *) parsetree)->vacrel,
650 ((VacuumStmt *) parsetree)->verbose,
651 ((VacuumStmt *) parsetree)->analyze,
652 ((VacuumStmt *) parsetree)->va_spec);
657 ExplainStmt *stmt = (ExplainStmt *) parsetree;
659 PS_SET_STATUS(commandTag = "EXPLAIN");
662 ExplainQuery(stmt->query, stmt->verbose, dest);
667 * ******************************** Tioga-related statements *******************************
671 RecipeStmt *stmt = (RecipeStmt *) parsetree;
673 PS_SET_STATUS(commandTag = "EXECUTE RECIPE");
680 * ******************************** set variable statements *******************************
682 case T_VariableSetStmt:
684 VariableSetStmt *n = (VariableSetStmt *) parsetree;
686 SetPGVariable(n->name, n->value);
687 PS_SET_STATUS(commandTag = "SET VARIABLE");
691 case T_VariableShowStmt:
693 VariableShowStmt *n = (VariableShowStmt *) parsetree;
695 GetPGVariable(n->name);
696 PS_SET_STATUS(commandTag = "SHOW VARIABLE");
700 case T_VariableResetStmt:
702 VariableResetStmt *n = (VariableResetStmt *) parsetree;
704 ResetPGVariable(n->name);
705 PS_SET_STATUS(commandTag = "RESET VARIABLE");
710 * ******************************** TRIGGER statements *******************************
712 case T_CreateTrigStmt:
713 PS_SET_STATUS(commandTag = "CREATE");
716 CreateTrigger((CreateTrigStmt *) parsetree);
720 PS_SET_STATUS(commandTag = "DROP");
723 DropTrigger((DropTrigStmt *) parsetree);
727 * ************* PROCEDURAL LANGUAGE statements *****************
729 case T_CreatePLangStmt:
730 PS_SET_STATUS(commandTag = "CREATE");
733 CreateProceduralLanguage((CreatePLangStmt *) parsetree);
736 case T_DropPLangStmt:
737 PS_SET_STATUS(commandTag = "DROP");
740 DropProceduralLanguage((DropPLangStmt *) parsetree);
744 * ******************************** USER statements ****
747 case T_CreateUserStmt:
748 PS_SET_STATUS(commandTag = "CREATE USER");
751 DefineUser((CreateUserStmt *) parsetree);
754 case T_AlterUserStmt:
755 PS_SET_STATUS(commandTag = "ALTER USER");
758 AlterUser((AlterUserStmt *) parsetree);
762 PS_SET_STATUS(commandTag = "DROP USER");
765 RemoveUser(((DropUserStmt *) parsetree)->user);
769 PS_SET_STATUS(commandTag = "LOCK TABLE");
772 LockTableCommand((LockStmt *) parsetree);
777 * ******************************** default ********************************
781 elog(ERROR, "ProcessUtility: command #%d unsupported",
787 * tell fe/be or whatever that we're done.
790 EndCommand(commandTag, dest);