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.68 1999/09/29 16:06:11 wieck Exp $
14 *-------------------------------------------------------------------------
18 #include "access/heapam.h"
19 #include "catalog/catalog.h"
20 #include "catalog/pg_type.h"
21 #include "commands/async.h"
22 #include "commands/cluster.h"
23 #include "commands/command.h"
24 #include "commands/copy.h"
25 #include "commands/creatinh.h"
26 #include "commands/dbcommands.h"
27 #include "commands/defrem.h"
28 #include "commands/explain.h"
29 #include "commands/proclang.h"
30 #include "commands/rename.h"
31 #include "commands/sequence.h"
32 #include "commands/trigger.h"
33 #include "commands/vacuum.h"
34 #include "commands/variable.h"
35 #include "commands/view.h"
36 #include "miscadmin.h"
37 #include "rewrite/rewriteDefine.h"
38 #include "rewrite/rewriteRemove.h"
39 #include "tcop/utility.h"
40 #include "utils/acl.h"
41 #include "utils/acl.h"
42 #include "utils/ps_status.h"
43 #include "utils/syscache.h"
45 #include "../backend/parser/parse.h"
47 void DefineUser(CreateUserStmt *stmt, CommandDest);
48 void AlterUser(AlterUserStmt *stmt, CommandDest);
49 void RemoveUser(char *username, CommandDest);
52 * CHECK_IF_ABORTED() is used to avoid doing unnecessary
53 * processing within an aborted transaction block.
56 /* we have to use IF because of the 'break' */
57 #define CHECK_IF_ABORTED() \
60 if (IsAbortedTransactionBlockState()) \
62 elog(NOTICE, "(transaction aborted): %s", \
63 "all queries ignored until end of transaction block"); \
64 commandTag = "*ABORT STATE*"; \
70 * general utility function invoker
74 ProcessUtility(Node *parsetree,
77 char *commandTag = NULL;
82 userName = GetPgUserName();
84 switch (nodeTag(parsetree))
88 * ******************************** transactions ********************************
91 case T_TransactionStmt:
93 TransactionStmt *stmt = (TransactionStmt *) parsetree;
95 switch (stmt->command)
98 PS_SET_STATUS(commandTag = "BEGIN");
100 BeginTransactionBlock();
104 PS_SET_STATUS(commandTag = "END");
105 EndTransactionBlock();
109 PS_SET_STATUS(commandTag = "ABORT");
110 UserAbortTransactionBlock();
117 * ******************************** portal manipulation ********************************
120 case T_ClosePortalStmt:
122 ClosePortalStmt *stmt = (ClosePortalStmt *) parsetree;
124 PS_SET_STATUS(commandTag = "CLOSE");
127 PerformPortalClose(stmt->portalname, dest);
133 FetchStmt *stmt = (FetchStmt *) parsetree;
134 char *portalName = stmt->portalname;
138 PS_SET_STATUS(commandTag = (stmt->ismove) ? "MOVE" : "FETCH");
141 forward = (bool) (stmt->direction == FORWARD);
144 * parser ensures that count is >= 0 and 'fetch ALL' -> 0
147 count = stmt->howMany;
148 PerformPortalFetch(portalName, forward, count, commandTag,
149 (stmt->ismove) ? None : dest); /* /dev/null for MOVE */
154 * ******************************** relation and attribute
155 * manipulation ********************************
159 PS_SET_STATUS(commandTag = "CREATE");
162 DefineRelation((CreateStmt *) parsetree, RELKIND_RELATION);
167 DestroyStmt *stmt = (DestroyStmt *) parsetree;
168 List *args = stmt->relNames;
171 PS_SET_STATUS(commandTag = "DROP");
174 /* check as much as we can before we start dropping ... */
179 relname = strVal(lfirst(arg));
180 if (!allowSystemTableMods && IsSystemRelationName(relname))
181 elog(ERROR, "class \"%s\" is a system catalog",
183 rel = heap_openr(relname, AccessExclusiveLock);
184 if (stmt->sequence &&
185 rel->rd_rel->relkind != RELKIND_SEQUENCE)
186 elog(ERROR, "Use DROP TABLE to drop table '%s'",
188 if (!(stmt->sequence) &&
189 rel->rd_rel->relkind == RELKIND_SEQUENCE)
190 elog(ERROR, "Use DROP SEQUENCE to drop sequence '%s'",
192 /* close rel, but keep lock until end of xact */
193 heap_close(rel, NoLock);
195 if (!pg_ownercheck(userName, relname, RELNAME))
196 elog(ERROR, "you do not own class \"%s\"",
200 /* OK, terminate 'em all */
203 relname = strVal(lfirst(arg));
204 RemoveRelation(relname);
214 PS_SET_STATUS(commandTag = "TRUNCATE");
217 relname = ((TruncateStmt *) parsetree)->relName;
218 if (!allowSystemTableMods && IsSystemRelationName(relname)) {
219 elog(ERROR, "TRUNCATE cannot be used on system tables. '%s' is a system table",
223 rel = heap_openr(relname, AccessExclusiveLock);
224 if (RelationIsValid(rel)) {
225 if (rel->rd_rel->relkind == RELKIND_SEQUENCE) {
226 elog(ERROR, "TRUNCATE cannot be used on sequences. '%s' is a sequence",
229 heap_close(rel, NoLock);
232 if (!pg_ownercheck(userName, relname, RELNAME)) {
233 elog(ERROR, "you do not own class \"%s\"", relname);
236 TruncateRelation(((TruncateStmt *) parsetree)->relName);
243 CopyStmt *stmt = (CopyStmt *) parsetree;
245 PS_SET_STATUS(commandTag = "COPY");
248 DoCopy(stmt->relname,
251 (bool) (stmt->direction == FROM),
252 (bool) (stmt->filename == NULL),
255 * null filename means copy to/from stdout/stdin, rather
256 * than to/from a file.
265 AddAttrStmt *stmt = (AddAttrStmt *) parsetree;
267 PS_SET_STATUS(commandTag = "ADD");
271 * owner checking done in PerformAddAttribute (now
274 PerformAddAttribute(stmt->relname,
277 (ColumnDef *) stmt->colDef);
286 RenameStmt *stmt = (RenameStmt *) parsetree;
288 PS_SET_STATUS(commandTag = "RENAME");
291 relname = stmt->relname;
292 if (!allowSystemTableMods && IsSystemRelationName(relname))
293 elog(ERROR, "class \"%s\" is a system catalog",
296 if (!pg_ownercheck(userName, relname, RELNAME))
297 elog(ERROR, "you do not own class \"%s\"",
302 * XXX using len == 3 to tell the difference
303 * between "rename rel to newrel" and
304 * "rename att in rel to newatt" will not
305 * work soon because "rename type/operator/rule"
306 * stuff is being added. - cim 10/24/90
308 * [another piece of amuzing but useless anecdote -- ay]
310 if (stmt->column == NULL)
315 * Note: we also rename the "type" tuple
316 * corresponding to the relation.
319 renamerel(relname, /* old name */
320 stmt->newname); /* new name */
321 TypeRename(relname, /* old name */
322 stmt->newname); /* new name */
330 renameatt(relname, /* relname */
331 stmt->column, /* old att name */
332 stmt->newname, /* new att name */
334 stmt->inh); /* recursive? */
339 case T_ChangeACLStmt:
341 ChangeACLStmt *stmt = (ChangeACLStmt *) parsetree;
346 PS_SET_STATUS(commandTag = "CHANGE");
351 modechg = stmt->modechg;
353 foreach(i, stmt->relNames)
355 relname = strVal(lfirst(i));
356 if (!pg_ownercheck(userName, relname, RELNAME))
357 elog(ERROR, "you do not own class \"%s\"",
361 foreach(i, stmt->relNames)
363 relname = strVal(lfirst(i));
364 ChangeAcl(relname, aip, modechg);
371 * ******************************** object creation /
372 * destruction ********************************
377 DefineStmt *stmt = (DefineStmt *) parsetree;
379 PS_SET_STATUS(commandTag = "CREATE");
382 switch (stmt->defType)
385 DefineOperator(stmt->defname, /* operator name */
386 stmt->definition); /* rest */
389 DefineType(stmt->defname, stmt->definition);
392 DefineAggregate(stmt->defname, /* aggregate name */
393 stmt->definition); /* rest */
399 case T_ViewStmt: /* CREATE VIEW */
401 ViewStmt *stmt = (ViewStmt *) parsetree;
403 PS_SET_STATUS(commandTag = "CREATE");
405 DefineView(stmt->viewname, stmt->query); /* retrieve parsetree */
409 case T_ProcedureStmt: /* CREATE FUNCTION */
410 PS_SET_STATUS(commandTag = "CREATE");
412 CreateFunction((ProcedureStmt *) parsetree, dest); /* everything */
415 case T_IndexStmt: /* CREATE INDEX */
417 IndexStmt *stmt = (IndexStmt *) parsetree;
419 PS_SET_STATUS(commandTag = "CREATE");
421 DefineIndex(stmt->relname, /* relation name */
422 stmt->idxname, /* index name */
423 stmt->accessMethod, /* am name */
424 stmt->indexParams, /* parameters */
428 (Expr *) stmt->whereClause,
433 case T_RuleStmt: /* CREATE RULE */
435 RuleStmt *stmt = (RuleStmt *) parsetree;
439 relname = stmt->object->relname;
440 aclcheck_result = pg_aclcheck(relname, userName, ACL_RU);
441 if (aclcheck_result != ACLCHECK_OK)
442 elog(ERROR, "%s: %s", relname, aclcheck_error_strings[aclcheck_result]);
444 PS_SET_STATUS(commandTag = "CREATE");
446 DefineQueryRewrite(stmt);
450 case T_CreateSeqStmt:
451 PS_SET_STATUS(commandTag = "CREATE");
454 DefineSequence((CreateSeqStmt *) parsetree);
459 ExtendStmt *stmt = (ExtendStmt *) parsetree;
461 PS_SET_STATUS(commandTag = "EXTEND");
464 ExtendIndex(stmt->idxname, /* index name */
465 (Expr *) stmt->whereClause, /* where */
472 RemoveStmt *stmt = (RemoveStmt *) parsetree;
474 PS_SET_STATUS(commandTag = "DROP");
477 switch (stmt->removeType)
480 relname = stmt->name;
481 if (!allowSystemTableMods && IsSystemRelationName(relname))
482 elog(ERROR, "class \"%s\" is a system catalog index",
485 if (!pg_ownercheck(userName, relname, RELNAME))
486 elog(ERROR, "%s: %s", relname, aclcheck_error_strings[ACLCHECK_NOT_OWNER]);
488 RemoveIndex(relname);
492 char *rulename = stmt->name;
497 relationName = RewriteGetRuleEventRel(rulename);
498 aclcheck_result = pg_aclcheck(relationName, userName, ACL_RU);
499 if (aclcheck_result != ACLCHECK_OK)
500 elog(ERROR, "%s: %s", relationName, aclcheck_error_strings[aclcheck_result]);
502 RemoveRewriteRule(rulename);
507 /* XXX moved to remove.c */
509 RemoveType(stmt->name);
513 char *viewName = stmt->name;
518 ruleName = MakeRetrieveViewRuleName(viewName);
519 relationName = RewriteGetRuleEventRel(ruleName);
520 if (!pg_ownercheck(userName, relationName, RELNAME))
521 elog(ERROR, "%s: %s", relationName, aclcheck_error_strings[ACLCHECK_NOT_OWNER]);
524 RemoveView(viewName);
532 case T_RemoveAggrStmt:
534 RemoveAggrStmt *stmt = (RemoveAggrStmt *) parsetree;
536 PS_SET_STATUS(commandTag = "DROP");
538 RemoveAggregate(stmt->aggname, stmt->aggtype);
542 case T_RemoveFuncStmt:
544 RemoveFuncStmt *stmt = (RemoveFuncStmt *) parsetree;
546 PS_SET_STATUS(commandTag = "DROP");
548 RemoveFunction(stmt->funcname,
554 case T_RemoveOperStmt:
556 RemoveOperStmt *stmt = (RemoveOperStmt *) parsetree;
557 char *type1 = (char *) NULL;
558 char *type2 = (char *) NULL;
560 PS_SET_STATUS(commandTag = "DROP");
563 if (lfirst(stmt->args) != NULL)
564 type1 = strVal(lfirst(stmt->args));
565 if (lsecond(stmt->args) != NULL)
566 type2 = strVal(lsecond(stmt->args));
567 RemoveOperator(stmt->opname, type1, type2);
572 elog(ERROR, "CREATE VERSION is not currently implemented");
577 CreatedbStmt *stmt = (CreatedbStmt *) parsetree;
579 PS_SET_STATUS(commandTag = "CREATEDB");
581 createdb(stmt->dbname, stmt->dbpath, stmt->encoding, dest);
585 case T_DestroydbStmt:
587 DestroydbStmt *stmt = (DestroydbStmt *) parsetree;
589 PS_SET_STATUS(commandTag = "DESTROYDB");
591 destroydb(stmt->dbname, dest);
595 /* Query-level asynchronous notification */
598 NotifyStmt *stmt = (NotifyStmt *) parsetree;
600 PS_SET_STATUS(commandTag = "NOTIFY");
603 Async_Notify(stmt->relname);
609 ListenStmt *stmt = (ListenStmt *) parsetree;
611 PS_SET_STATUS(commandTag = "LISTEN");
614 Async_Listen(stmt->relname, MyProcPid);
620 UnlistenStmt *stmt = (UnlistenStmt *) parsetree;
622 PS_SET_STATUS(commandTag = "UNLISTEN");
625 Async_Unlisten(stmt->relname, MyProcPid);
630 * ******************************** dynamic loader ********************************
635 LoadStmt *stmt = (LoadStmt *) parsetree;
637 PS_SET_STATUS(commandTag = "LOAD");
640 closeAllVfds(); /* probably not necessary... */
641 load_file(stmt->filename);
647 ClusterStmt *stmt = (ClusterStmt *) parsetree;
649 PS_SET_STATUS(commandTag = "CLUSTER");
652 cluster(stmt->relname, stmt->indexname);
657 PS_SET_STATUS(commandTag = "VACUUM");
659 vacuum(((VacuumStmt *) parsetree)->vacrel,
660 ((VacuumStmt *) parsetree)->verbose,
661 ((VacuumStmt *) parsetree)->analyze,
662 ((VacuumStmt *) parsetree)->va_spec);
667 ExplainStmt *stmt = (ExplainStmt *) parsetree;
669 PS_SET_STATUS(commandTag = "EXPLAIN");
672 ExplainQuery(stmt->query, stmt->verbose, dest);
679 * ******************************** Tioga-related statements *******************************
683 RecipeStmt *stmt = (RecipeStmt *) parsetree;
685 PS_SET_STATUS(commandTag = "EXECUTE RECIPE");
693 * ******************************** set variable statements *******************************
695 case T_VariableSetStmt:
697 VariableSetStmt *n = (VariableSetStmt *) parsetree;
699 SetPGVariable(n->name, n->value);
700 PS_SET_STATUS(commandTag = "SET VARIABLE");
704 case T_VariableShowStmt:
706 VariableShowStmt *n = (VariableShowStmt *) parsetree;
708 GetPGVariable(n->name);
709 PS_SET_STATUS(commandTag = "SHOW VARIABLE");
713 case T_VariableResetStmt:
715 VariableResetStmt *n = (VariableResetStmt *) parsetree;
717 ResetPGVariable(n->name);
718 PS_SET_STATUS(commandTag = "RESET VARIABLE");
723 * ******************************** TRIGGER statements *******************************
725 case T_CreateTrigStmt:
726 PS_SET_STATUS(commandTag = "CREATE");
729 CreateTrigger((CreateTrigStmt *) parsetree);
733 PS_SET_STATUS(commandTag = "DROP");
736 DropTrigger((DropTrigStmt *) parsetree);
740 * ************* PROCEDURAL LANGUAGE statements *****************
742 case T_CreatePLangStmt:
743 PS_SET_STATUS(commandTag = "CREATE");
746 CreateProceduralLanguage((CreatePLangStmt *) parsetree);
749 case T_DropPLangStmt:
750 PS_SET_STATUS(commandTag = "DROP");
753 DropProceduralLanguage((DropPLangStmt *) parsetree);
757 * ******************************** USER statements ****
760 case T_CreateUserStmt:
761 PS_SET_STATUS(commandTag = "CREATE USER");
764 DefineUser((CreateUserStmt *) parsetree, dest);
767 case T_AlterUserStmt:
768 PS_SET_STATUS(commandTag = "ALTER USER");
771 AlterUser((AlterUserStmt *) parsetree, dest);
775 PS_SET_STATUS(commandTag = "DROP USER");
778 RemoveUser(((DropUserStmt *) parsetree)->user, dest);
782 PS_SET_STATUS(commandTag = "LOCK TABLE");
785 LockTableCommand((LockStmt *) parsetree);
788 case T_ConstraintsSetStmt:
789 PS_SET_STATUS(commandTag = "SET CONSTRAINTS");
792 DeferredTriggerSetState((ConstraintsSetStmt *) parsetree);
797 * ******************************** default ********************************
801 elog(ERROR, "ProcessUtility: command #%d unsupported",
807 * tell fe/be or whatever that we're done.
810 EndCommand(commandTag, dest);