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 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
9 * Portions Copyright (c) 1994, Regents of the University of California
13 * $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.95 2000/10/07 00:58:18 tgl Exp $
15 *-------------------------------------------------------------------------
19 #include "access/heapam.h"
20 #include "catalog/catalog.h"
21 #include "commands/async.h"
22 #include "commands/cluster.h"
23 #include "commands/command.h"
24 #include "commands/comment.h"
25 #include "commands/copy.h"
26 #include "commands/creatinh.h"
27 #include "commands/dbcommands.h"
28 #include "commands/defrem.h"
29 #include "commands/explain.h"
30 #include "commands/proclang.h"
31 #include "commands/rename.h"
32 #include "commands/sequence.h"
33 #include "commands/trigger.h"
34 #include "commands/user.h"
35 #include "commands/vacuum.h"
36 #include "commands/variable.h"
37 #include "commands/view.h"
38 #include "miscadmin.h"
39 #include "parser/parse.h"
40 #include "parser/parse_expr.h"
41 #include "rewrite/rewriteDefine.h"
42 #include "rewrite/rewriteRemove.h"
43 #include "tcop/utility.h"
44 #include "utils/acl.h"
45 #include "utils/ps_status.h"
46 #include "utils/syscache.h"
50 * general utility function invoker
54 ProcessUtility(Node *parsetree,
57 char *commandTag = NULL;
61 switch (nodeTag(parsetree))
65 * ******************************** transactions ********************************
68 case T_TransactionStmt:
70 TransactionStmt *stmt = (TransactionStmt *) parsetree;
72 switch (stmt->command)
75 set_ps_display(commandTag = "BEGIN");
76 BeginTransactionBlock();
80 set_ps_display(commandTag = "COMMIT");
81 EndTransactionBlock();
85 set_ps_display(commandTag = "ROLLBACK");
86 UserAbortTransactionBlock();
93 * ******************************** portal manipulation ********************************
96 case T_ClosePortalStmt:
98 ClosePortalStmt *stmt = (ClosePortalStmt *) parsetree;
100 set_ps_display(commandTag = "CLOSE");
102 PerformPortalClose(stmt->portalname, dest);
108 FetchStmt *stmt = (FetchStmt *) parsetree;
109 char *portalName = stmt->portalname;
113 set_ps_display(commandTag = (stmt->ismove) ? "MOVE" : "FETCH");
117 forward = (bool) (stmt->direction == FORWARD);
120 * parser ensures that count is >= 0 and 'fetch ALL' -> 0
123 count = stmt->howMany;
124 PerformPortalFetch(portalName, forward, count, commandTag,
125 (stmt->ismove) ? None : dest); /* /dev/null for MOVE */
130 * ******************************** relation and attribute
131 * manipulation ********************************
135 set_ps_display(commandTag = "CREATE");
137 DefineRelation((CreateStmt *) parsetree, RELKIND_RELATION);
140 * Let AlterTableCreateToastTable decide if this
141 * one needs a secondary relation too.
144 CommandCounterIncrement();
145 AlterTableCreateToastTable(((CreateStmt *)parsetree)->relname,
151 DropStmt *stmt = (DropStmt *) parsetree;
152 List *args = stmt->relNames;
155 set_ps_display(commandTag = "DROP");
157 /* check as much as we can before we start dropping ... */
162 relname = strVal(lfirst(arg));
163 if (!allowSystemTableMods && IsSystemRelationName(relname))
164 elog(ERROR, "class \"%s\" is a system catalog",
166 rel = heap_openr(relname, AccessExclusiveLock);
167 if (stmt->sequence &&
168 rel->rd_rel->relkind != RELKIND_SEQUENCE)
169 elog(ERROR, "Use DROP TABLE to drop table '%s'",
171 if (!(stmt->sequence) &&
172 rel->rd_rel->relkind == RELKIND_SEQUENCE)
173 elog(ERROR, "Use DROP SEQUENCE to drop sequence '%s'",
175 /* close rel, but keep lock until end of xact */
176 heap_close(rel, NoLock);
178 if (!pg_ownercheck(GetUserId(), relname, RELNAME))
179 elog(ERROR, "you do not own class \"%s\"",
183 /* OK, terminate 'em all */
186 relname = strVal(lfirst(arg));
187 RemoveRelation(relname);
196 set_ps_display(commandTag = "TRUNCATE");
198 relname = ((TruncateStmt *) parsetree)->relName;
199 if (!allowSystemTableMods && IsSystemRelationName(relname))
200 elog(ERROR, "TRUNCATE cannot be used on system tables. '%s' is a system table",
203 /* Grab exclusive lock in preparation for truncate... */
204 rel = heap_openr(relname, AccessExclusiveLock);
205 if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
206 elog(ERROR, "TRUNCATE cannot be used on sequences. '%s' is a sequence",
208 if (rel->rd_rel->relkind == RELKIND_VIEW)
209 elog(ERROR, "TRUNCATE cannot be used on views. '%s' is a sequence",
211 heap_close(rel, NoLock);
214 if (!pg_ownercheck(GetUserId(), relname, RELNAME))
215 elog(ERROR, "you do not own class \"%s\"", relname);
217 TruncateRelation(relname);
223 CommentStmt *statement;
225 statement = ((CommentStmt *) parsetree);
227 set_ps_display(commandTag = "COMMENT");
229 CommentObject(statement->objtype, statement->objname,
230 statement->objproperty, statement->objlist,
237 CopyStmt *stmt = (CopyStmt *) parsetree;
239 set_ps_display(commandTag = "COPY");
241 if (stmt->direction != FROM)
244 DoCopy(stmt->relname,
247 (bool) (stmt->direction == FROM),
248 (bool) (stmt->filename == NULL),
251 * null filename means copy to/from stdout/stdin, rather
252 * than to/from a file.
265 RenameStmt *stmt = (RenameStmt *) parsetree;
267 set_ps_display(commandTag = "ALTER");
269 relname = stmt->relname;
270 if (!allowSystemTableMods && IsSystemRelationName(relname))
271 elog(ERROR, "ALTER TABLE: relation \"%s\" is a system catalog",
274 if (!pg_ownercheck(GetUserId(), relname, RELNAME))
275 elog(ERROR, "permission denied");
279 * XXX using len == 3 to tell the difference
280 * between "rename rel to newrel" and
281 * "rename att in rel to newatt" will not
282 * work soon because "rename type/operator/rule"
283 * stuff is being added. - cim 10/24/90
285 * [another piece of amuzing but useless anecdote -- ay]
287 if (stmt->column == NULL)
292 * Note: we also rename the "type" tuple
293 * corresponding to the relation.
296 renamerel(relname, /* old name */
297 stmt->newname); /* new name */
305 renameatt(relname, /* relname */
306 stmt->column, /* old att name */
307 stmt->newname, /* new att name */
308 stmt->inh); /* recursive? */
313 /* various Alter Table forms */
315 case T_AlterTableStmt:
317 AlterTableStmt *stmt = (AlterTableStmt *) parsetree;
319 set_ps_display(commandTag = "ALTER");
322 * Some or all of these functions are recursive to cover
323 * inherited things, so permission checks are done there.
325 switch (stmt->subtype)
327 case 'A': /* ADD COLUMN */
328 AlterTableAddColumn(stmt->relname, stmt->inh, (ColumnDef *) stmt->def);
330 case 'T': /* ALTER COLUMN */
331 AlterTableAlterColumn(stmt->relname, stmt->inh, stmt->name, stmt->def);
333 case 'D': /* ALTER DROP */
334 AlterTableDropColumn(stmt->relname, stmt->inh, stmt->name, stmt->behavior);
336 case 'C': /* ADD CONSTRAINT */
337 AlterTableAddConstraint(stmt->relname, stmt->inh, stmt->def);
339 case 'X': /* DROP CONSTRAINT */
340 AlterTableDropConstraint(stmt->relname, stmt->inh, stmt->name, stmt->behavior);
342 case 'E': /* CREATE TOAST TABLE */
343 AlterTableCreateToastTable(stmt->relname, false);
345 case 'U': /* ALTER OWNER */
346 AlterTableOwner(stmt->relname, stmt->name);
349 elog(ERROR, "T_AlterTableStmt: unknown subtype");
356 case T_ChangeACLStmt:
358 ChangeACLStmt *stmt = (ChangeACLStmt *) parsetree;
360 set_ps_display(commandTag = "CHANGE");
362 ExecuteChangeACLStmt(stmt);
367 * ******************************** object creation /
368 * destruction ********************************
373 DefineStmt *stmt = (DefineStmt *) parsetree;
375 set_ps_display(commandTag = "CREATE");
377 switch (stmt->defType)
380 DefineOperator(stmt->defname, /* operator name */
381 stmt->definition); /* rest */
384 DefineType(stmt->defname, stmt->definition);
387 DefineAggregate(stmt->defname, /* aggregate name */
388 stmt->definition); /* rest */
394 case T_ViewStmt: /* CREATE VIEW */
396 ViewStmt *stmt = (ViewStmt *) parsetree;
398 set_ps_display(commandTag = "CREATE");
400 DefineView(stmt->viewname, stmt->query); /* retrieve parsetree */
404 case T_ProcedureStmt: /* CREATE FUNCTION */
405 set_ps_display(commandTag = "CREATE");
407 CreateFunction((ProcedureStmt *) parsetree, dest); /* everything */
410 case T_IndexStmt: /* CREATE INDEX */
412 IndexStmt *stmt = (IndexStmt *) parsetree;
414 set_ps_display(commandTag = "CREATE");
416 DefineIndex(stmt->relname, /* relation name */
417 stmt->idxname, /* index name */
418 stmt->accessMethod, /* am name */
419 stmt->indexParams, /* parameters */
423 (Expr *) stmt->whereClause,
428 case T_RuleStmt: /* CREATE RULE */
430 RuleStmt *stmt = (RuleStmt *) parsetree;
434 relname = stmt->object->relname;
435 aclcheck_result = pg_aclcheck(relname, GetUserId(), ACL_RU);
436 if (aclcheck_result != ACLCHECK_OK)
437 elog(ERROR, "%s: %s", relname, aclcheck_error_strings[aclcheck_result]);
439 set_ps_display(commandTag = "CREATE");
441 DefineQueryRewrite(stmt);
445 case T_CreateSeqStmt:
446 set_ps_display(commandTag = "CREATE");
448 DefineSequence((CreateSeqStmt *) parsetree);
453 ExtendStmt *stmt = (ExtendStmt *) parsetree;
455 set_ps_display(commandTag = "EXTEND");
457 ExtendIndex(stmt->idxname, /* index name */
458 (Expr *) stmt->whereClause, /* where */
465 RemoveStmt *stmt = (RemoveStmt *) parsetree;
467 set_ps_display(commandTag = "DROP");
469 switch (stmt->removeType)
472 relname = stmt->name;
473 if (!allowSystemTableMods && IsSystemRelationName(relname))
474 elog(ERROR, "class \"%s\" is a system catalog index",
477 if (!pg_ownercheck(GetUserId(), relname, RELNAME))
478 elog(ERROR, "%s: %s", relname, aclcheck_error_strings[ACLCHECK_NOT_OWNER]);
480 RemoveIndex(relname);
484 char *rulename = stmt->name;
489 relationName = RewriteGetRuleEventRel(rulename);
490 aclcheck_result = pg_aclcheck(relationName, GetUserId(), ACL_RU);
491 if (aclcheck_result != ACLCHECK_OK)
492 elog(ERROR, "%s: %s", relationName, aclcheck_error_strings[aclcheck_result]);
494 RemoveRewriteRule(rulename);
499 /* XXX moved to remove.c */
501 RemoveType(stmt->name);
505 char *viewName = stmt->name;
510 ruleName = MakeRetrieveViewRuleName(viewName);
511 relationName = RewriteGetRuleEventRel(ruleName);
512 if (!pg_ownercheck(GetUserId(), relationName, RELNAME))
513 elog(ERROR, "%s: %s", relationName, aclcheck_error_strings[ACLCHECK_NOT_OWNER]);
516 RemoveView(viewName);
524 case T_RemoveAggrStmt:
526 RemoveAggrStmt *stmt = (RemoveAggrStmt *) parsetree;
527 char *typename = (char *) NULL;
529 set_ps_display(commandTag = "DROP");
531 if (stmt->aggtype != NULL)
532 typename = TypeNameToInternalName((TypeName *) stmt->aggtype);
534 RemoveAggregate(stmt->aggname, typename);
538 case T_RemoveFuncStmt:
540 RemoveFuncStmt *stmt = (RemoveFuncStmt *) parsetree;
542 set_ps_display(commandTag = "DROP");
544 RemoveFunction(stmt->funcname, stmt->args);
548 case T_RemoveOperStmt:
550 RemoveOperStmt *stmt = (RemoveOperStmt *) parsetree;
551 TypeName *typenode1 = (TypeName *) lfirst(stmt->args);
552 TypeName *typenode2 = (TypeName *) lsecond(stmt->args);
553 char *typename1 = (char *) NULL;
554 char *typename2 = (char *) NULL;
556 set_ps_display(commandTag = "DROP");
558 if (typenode1 != NULL)
559 typename1 = TypeNameToInternalName(typenode1);
560 if (typenode2 != NULL)
561 typename2 = TypeNameToInternalName(typenode2);
563 RemoveOperator(stmt->opname, typename1, typename2);
568 elog(ERROR, "CREATE VERSION is not currently implemented");
573 CreatedbStmt *stmt = (CreatedbStmt *) parsetree;
575 set_ps_display(commandTag = "CREATE DATABASE");
577 createdb(stmt->dbname, stmt->dbpath, stmt->encoding);
583 DropdbStmt *stmt = (DropdbStmt *) parsetree;
585 set_ps_display(commandTag = "DROP DATABASE");
587 dropdb(stmt->dbname);
591 /* Query-level asynchronous notification */
594 NotifyStmt *stmt = (NotifyStmt *) parsetree;
596 set_ps_display(commandTag = "NOTIFY");
598 Async_Notify(stmt->relname);
604 ListenStmt *stmt = (ListenStmt *) parsetree;
606 set_ps_display(commandTag = "LISTEN");
608 Async_Listen(stmt->relname, MyProcPid);
614 UnlistenStmt *stmt = (UnlistenStmt *) parsetree;
616 set_ps_display(commandTag = "UNLISTEN");
618 Async_Unlisten(stmt->relname, MyProcPid);
623 * ******************************** dynamic loader ********************************
628 LoadStmt *stmt = (LoadStmt *) parsetree;
630 set_ps_display(commandTag = "LOAD");
632 closeAllVfds(); /* probably not necessary... */
633 load_file(stmt->filename);
639 ClusterStmt *stmt = (ClusterStmt *) parsetree;
641 set_ps_display(commandTag = "CLUSTER");
643 cluster(stmt->relname, stmt->indexname);
648 set_ps_display(commandTag = "VACUUM");
650 vacuum(((VacuumStmt *) parsetree)->vacrel,
651 ((VacuumStmt *) parsetree)->verbose,
652 ((VacuumStmt *) parsetree)->analyze,
653 ((VacuumStmt *) parsetree)->va_spec);
658 ExplainStmt *stmt = (ExplainStmt *) parsetree;
660 set_ps_display(commandTag = "EXPLAIN");
662 ExplainQuery(stmt->query, stmt->verbose, dest);
669 * ******************************** Tioga-related statements *******************************
673 RecipeStmt *stmt = (RecipeStmt *) parsetree;
675 set_ps_display(commandTag = "EXECUTE RECIPE");
683 * ******************************** set variable statements *******************************
685 case T_VariableSetStmt:
687 VariableSetStmt *n = (VariableSetStmt *) parsetree;
689 SetPGVariable(n->name, n->value);
690 set_ps_display(commandTag = "SET VARIABLE");
694 case T_VariableShowStmt:
696 VariableShowStmt *n = (VariableShowStmt *) parsetree;
698 GetPGVariable(n->name);
699 set_ps_display(commandTag = "SHOW VARIABLE");
703 case T_VariableResetStmt:
705 VariableResetStmt *n = (VariableResetStmt *) parsetree;
707 ResetPGVariable(n->name);
708 set_ps_display(commandTag = "RESET VARIABLE");
713 * ******************************** TRIGGER statements *******************************
715 case T_CreateTrigStmt:
716 set_ps_display(commandTag = "CREATE");
718 CreateTrigger((CreateTrigStmt *) parsetree);
722 set_ps_display(commandTag = "DROP");
724 DropTrigger((DropTrigStmt *) parsetree);
728 * ************* PROCEDURAL LANGUAGE statements *****************
730 case T_CreatePLangStmt:
731 set_ps_display(commandTag = "CREATE");
733 CreateProceduralLanguage((CreatePLangStmt *) parsetree);
736 case T_DropPLangStmt:
737 set_ps_display(commandTag = "DROP");
739 DropProceduralLanguage((DropPLangStmt *) parsetree);
743 * ******************************** USER statements ****
746 case T_CreateUserStmt:
747 set_ps_display(commandTag = "CREATE USER");
749 CreateUser((CreateUserStmt *) parsetree);
752 case T_AlterUserStmt:
753 set_ps_display(commandTag = "ALTER USER");
755 AlterUser((AlterUserStmt *) parsetree);
759 set_ps_display(commandTag = "DROP USER");
761 DropUser((DropUserStmt *) parsetree);
765 set_ps_display(commandTag = "LOCK TABLE");
767 LockTableCommand((LockStmt *) parsetree);
770 case T_ConstraintsSetStmt:
771 set_ps_display(commandTag = "SET CONSTRAINTS");
773 DeferredTriggerSetState((ConstraintsSetStmt *) parsetree);
776 case T_CreateGroupStmt:
777 set_ps_display(commandTag = "CREATE GROUP");
779 CreateGroup((CreateGroupStmt *) parsetree);
782 case T_AlterGroupStmt:
783 set_ps_display(commandTag = "ALTER GROUP");
785 AlterGroup((AlterGroupStmt *) parsetree, "ALTER GROUP");
788 case T_DropGroupStmt:
789 set_ps_display(commandTag = "DROP GROUP");
791 DropGroup((DropGroupStmt *) parsetree);
796 ReindexStmt *stmt = (ReindexStmt *) parsetree;
798 set_ps_display(commandTag = "REINDEX");
800 switch (stmt->reindexType)
803 relname = (char *) stmt->name;
804 if (IsSystemRelationName(relname))
806 if (!allowSystemTableMods && IsSystemRelationName(relname))
807 elog(ERROR, "\"%s\" is a system index. call REINDEX under standalone postgres with -O -P options",
809 if (!IsIgnoringSystemIndexes())
810 elog(ERROR, "\"%s\" is a system index. call REINDEX under standalone postgres with -P -O options",
814 if (!pg_ownercheck(GetUserId(), relname, RELNAME))
815 elog(ERROR, "%s: %s", relname, aclcheck_error_strings[ACLCHECK_NOT_OWNER]);
817 ReindexIndex(relname, stmt->force);
820 relname = (char *) stmt->name;
821 if (IsSystemRelationName(relname))
823 if (!allowSystemTableMods && IsSystemRelationName(relname))
824 elog(ERROR, "\"%s\" is a system table. call REINDEX under standalone postgres with -O -P options",
826 if (!IsIgnoringSystemIndexes())
827 elog(ERROR, "\"%s\" is a system table. call REINDEX under standalone postgres with -P -O options",
832 if (!pg_ownercheck(GetUserId(), relname, RELNAME))
833 elog(ERROR, "%s: %s", relname, aclcheck_error_strings[ACLCHECK_NOT_OWNER]);
835 ReindexTable(relname, stmt->force);
838 relname = (char *) stmt->name;
839 if (!allowSystemTableMods)
840 elog(ERROR, "must be called under standalone postgres with -O -P options");
841 if (!IsIgnoringSystemIndexes())
842 elog(ERROR, "must be called under standalone postgres with -P -O options");
843 ReindexDatabase(relname, stmt->force, false);
851 * ******************************** default ********************************
855 elog(ERROR, "ProcessUtility: command #%d unsupported",
861 * tell fe/be or whatever that we're done.
864 EndCommand(commandTag, dest);