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.78 2000/01/14 22:11:35 petere 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/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 "rewrite/rewriteDefine.h"
41 #include "rewrite/rewriteRemove.h"
42 #include "tcop/utility.h"
43 #include "utils/acl.h"
44 #include "utils/ps_status.h"
45 #include "utils/syscache.h"
49 * CHECK_IF_ABORTED() is used to avoid doing unnecessary
50 * processing within an aborted transaction block.
53 /* we have to use IF because of the 'break' */
54 #define CHECK_IF_ABORTED() \
57 if (IsAbortedTransactionBlockState()) \
59 elog(NOTICE, "(transaction aborted): %s", \
60 "all queries ignored until end of transaction block"); \
61 commandTag = "*ABORT STATE*"; \
67 * general utility function invoker
71 ProcessUtility(Node *parsetree,
74 char *commandTag = NULL;
79 userName = GetPgUserName();
81 switch (nodeTag(parsetree))
85 * ******************************** transactions ********************************
88 case T_TransactionStmt:
90 TransactionStmt *stmt = (TransactionStmt *) parsetree;
92 switch (stmt->command)
95 PS_SET_STATUS(commandTag = "BEGIN");
97 BeginTransactionBlock();
101 PS_SET_STATUS(commandTag = "END");
102 EndTransactionBlock();
106 PS_SET_STATUS(commandTag = "ABORT");
107 UserAbortTransactionBlock();
114 * ******************************** portal manipulation ********************************
117 case T_ClosePortalStmt:
119 ClosePortalStmt *stmt = (ClosePortalStmt *) parsetree;
121 PS_SET_STATUS(commandTag = "CLOSE");
124 PerformPortalClose(stmt->portalname, dest);
130 FetchStmt *stmt = (FetchStmt *) parsetree;
131 char *portalName = stmt->portalname;
135 PS_SET_STATUS(commandTag = (stmt->ismove) ? "MOVE" : "FETCH");
138 forward = (bool) (stmt->direction == FORWARD);
141 * parser ensures that count is >= 0 and 'fetch ALL' -> 0
144 count = stmt->howMany;
145 PerformPortalFetch(portalName, forward, count, commandTag,
146 (stmt->ismove) ? None : dest); /* /dev/null for MOVE */
151 * ******************************** relation and attribute
152 * manipulation ********************************
156 PS_SET_STATUS(commandTag = "CREATE");
159 DefineRelation((CreateStmt *) parsetree, RELKIND_RELATION);
164 DropStmt *stmt = (DropStmt *) parsetree;
165 List *args = stmt->relNames;
168 PS_SET_STATUS(commandTag = "DROP");
171 /* check as much as we can before we start dropping ... */
176 relname = strVal(lfirst(arg));
177 if (!allowSystemTableMods && IsSystemRelationName(relname))
178 elog(ERROR, "class \"%s\" is a system catalog",
180 rel = heap_openr(relname, AccessExclusiveLock);
181 if (stmt->sequence &&
182 rel->rd_rel->relkind != RELKIND_SEQUENCE)
183 elog(ERROR, "Use DROP TABLE to drop table '%s'",
185 if (!(stmt->sequence) &&
186 rel->rd_rel->relkind == RELKIND_SEQUENCE)
187 elog(ERROR, "Use DROP SEQUENCE to drop sequence '%s'",
189 /* close rel, but keep lock until end of xact */
190 heap_close(rel, NoLock);
192 if (!pg_ownercheck(userName, relname, RELNAME))
193 elog(ERROR, "you do not own class \"%s\"",
197 /* OK, terminate 'em all */
200 relname = strVal(lfirst(arg));
201 RemoveRelation(relname);
210 PS_SET_STATUS(commandTag = "TRUNCATE");
213 relname = ((TruncateStmt *) parsetree)->relName;
214 if (!allowSystemTableMods && IsSystemRelationName(relname))
215 elog(ERROR, "TRUNCATE cannot be used on system tables. '%s' is a system table",
218 /* Grab exclusive lock in preparation for truncate... */
219 rel = heap_openr(relname, AccessExclusiveLock);
220 if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
221 elog(ERROR, "TRUNCATE cannot be used on sequences. '%s' is a sequence",
223 heap_close(rel, NoLock);
226 if (!pg_ownercheck(userName, relname, RELNAME))
227 elog(ERROR, "you do not own class \"%s\"", relname);
229 TruncateRelation(relname);
236 CommentStmt *statement;
238 statement = ((CommentStmt *) parsetree);
240 PS_SET_STATUS(commandTag = "COMMENT");
242 CommentObject(statement->objtype, statement->objname,
243 statement->objproperty, statement->objlist,
252 CopyStmt *stmt = (CopyStmt *) parsetree;
254 PS_SET_STATUS(commandTag = "COPY");
257 DoCopy(stmt->relname,
260 (bool) (stmt->direction == FROM),
261 (bool) (stmt->filename == NULL),
264 * null filename means copy to/from stdout/stdin, rather
265 * than to/from a file.
275 AddAttrStmt *stmt = (AddAttrStmt *) parsetree;
277 PS_SET_STATUS(commandTag = "ADD");
281 * owner checking done in PerformAddAttribute (now
284 PerformAddAttribute(stmt->relname,
287 (ColumnDef *) stmt->colDef);
296 RenameStmt *stmt = (RenameStmt *) parsetree;
298 PS_SET_STATUS(commandTag = "RENAME");
301 relname = stmt->relname;
302 if (!allowSystemTableMods && IsSystemRelationName(relname))
303 elog(ERROR, "class \"%s\" is a system catalog",
306 if (!pg_ownercheck(userName, relname, RELNAME))
307 elog(ERROR, "you do not own class \"%s\"",
312 * XXX using len == 3 to tell the difference
313 * between "rename rel to newrel" and
314 * "rename att in rel to newatt" will not
315 * work soon because "rename type/operator/rule"
316 * stuff is being added. - cim 10/24/90
318 * [another piece of amuzing but useless anecdote -- ay]
320 if (stmt->column == NULL)
325 * Note: we also rename the "type" tuple
326 * corresponding to the relation.
329 renamerel(relname, /* old name */
330 stmt->newname); /* new name */
331 TypeRename(relname, /* old name */
332 stmt->newname); /* new name */
340 renameatt(relname, /* relname */
341 stmt->column, /* old att name */
342 stmt->newname, /* new att name */
344 stmt->inh); /* recursive? */
349 case T_ChangeACLStmt:
351 ChangeACLStmt *stmt = (ChangeACLStmt *) parsetree;
356 PS_SET_STATUS(commandTag = "CHANGE");
361 modechg = stmt->modechg;
363 foreach(i, stmt->relNames)
365 relname = strVal(lfirst(i));
366 if (!pg_ownercheck(userName, relname, RELNAME))
367 elog(ERROR, "you do not own class \"%s\"",
371 foreach(i, stmt->relNames)
373 relname = strVal(lfirst(i));
374 ChangeAcl(relname, aip, modechg);
381 * ******************************** object creation /
382 * destruction ********************************
387 DefineStmt *stmt = (DefineStmt *) parsetree;
389 PS_SET_STATUS(commandTag = "CREATE");
392 switch (stmt->defType)
395 DefineOperator(stmt->defname, /* operator name */
396 stmt->definition); /* rest */
399 DefineType(stmt->defname, stmt->definition);
402 DefineAggregate(stmt->defname, /* aggregate name */
403 stmt->definition); /* rest */
409 case T_ViewStmt: /* CREATE VIEW */
411 ViewStmt *stmt = (ViewStmt *) parsetree;
413 PS_SET_STATUS(commandTag = "CREATE");
415 DefineView(stmt->viewname, stmt->query); /* retrieve parsetree */
419 case T_ProcedureStmt: /* CREATE FUNCTION */
420 PS_SET_STATUS(commandTag = "CREATE");
422 CreateFunction((ProcedureStmt *) parsetree, dest); /* everything */
425 case T_IndexStmt: /* CREATE INDEX */
427 IndexStmt *stmt = (IndexStmt *) parsetree;
429 PS_SET_STATUS(commandTag = "CREATE");
431 DefineIndex(stmt->relname, /* relation name */
432 stmt->idxname, /* index name */
433 stmt->accessMethod, /* am name */
434 stmt->indexParams, /* parameters */
438 (Expr *) stmt->whereClause,
443 case T_RuleStmt: /* CREATE RULE */
445 RuleStmt *stmt = (RuleStmt *) parsetree;
449 relname = stmt->object->relname;
450 aclcheck_result = pg_aclcheck(relname, userName, ACL_RU);
451 if (aclcheck_result != ACLCHECK_OK)
452 elog(ERROR, "%s: %s", relname, aclcheck_error_strings[aclcheck_result]);
454 PS_SET_STATUS(commandTag = "CREATE");
456 DefineQueryRewrite(stmt);
460 case T_CreateSeqStmt:
461 PS_SET_STATUS(commandTag = "CREATE");
464 DefineSequence((CreateSeqStmt *) parsetree);
469 ExtendStmt *stmt = (ExtendStmt *) parsetree;
471 PS_SET_STATUS(commandTag = "EXTEND");
474 ExtendIndex(stmt->idxname, /* index name */
475 (Expr *) stmt->whereClause, /* where */
482 RemoveStmt *stmt = (RemoveStmt *) parsetree;
484 PS_SET_STATUS(commandTag = "DROP");
487 switch (stmt->removeType)
490 relname = stmt->name;
491 if (!allowSystemTableMods && IsSystemRelationName(relname))
492 elog(ERROR, "class \"%s\" is a system catalog index",
495 if (!pg_ownercheck(userName, relname, RELNAME))
496 elog(ERROR, "%s: %s", relname, aclcheck_error_strings[ACLCHECK_NOT_OWNER]);
498 RemoveIndex(relname);
502 char *rulename = stmt->name;
507 relationName = RewriteGetRuleEventRel(rulename);
508 aclcheck_result = pg_aclcheck(relationName, userName, ACL_RU);
509 if (aclcheck_result != ACLCHECK_OK)
510 elog(ERROR, "%s: %s", relationName, aclcheck_error_strings[aclcheck_result]);
512 RemoveRewriteRule(rulename);
517 /* XXX moved to remove.c */
519 RemoveType(stmt->name);
523 char *viewName = stmt->name;
528 ruleName = MakeRetrieveViewRuleName(viewName);
529 relationName = RewriteGetRuleEventRel(ruleName);
530 if (!pg_ownercheck(userName, relationName, RELNAME))
531 elog(ERROR, "%s: %s", relationName, aclcheck_error_strings[ACLCHECK_NOT_OWNER]);
534 RemoveView(viewName);
542 case T_RemoveAggrStmt:
544 RemoveAggrStmt *stmt = (RemoveAggrStmt *) parsetree;
546 PS_SET_STATUS(commandTag = "DROP");
548 RemoveAggregate(stmt->aggname, stmt->aggtype);
552 case T_RemoveFuncStmt:
554 RemoveFuncStmt *stmt = (RemoveFuncStmt *) parsetree;
556 PS_SET_STATUS(commandTag = "DROP");
558 RemoveFunction(stmt->funcname,
564 case T_RemoveOperStmt:
566 RemoveOperStmt *stmt = (RemoveOperStmt *) parsetree;
567 char *type1 = (char *) NULL;
568 char *type2 = (char *) NULL;
570 PS_SET_STATUS(commandTag = "DROP");
573 if (lfirst(stmt->args) != NULL)
574 type1 = strVal(lfirst(stmt->args));
575 if (lsecond(stmt->args) != NULL)
576 type2 = strVal(lsecond(stmt->args));
577 RemoveOperator(stmt->opname, type1, type2);
582 elog(ERROR, "CREATE VERSION is not currently implemented");
587 CreatedbStmt *stmt = (CreatedbStmt *) parsetree;
589 PS_SET_STATUS(commandTag = "CREATE DATABASE");
591 createdb(stmt->dbname, stmt->dbpath, stmt->encoding);
597 DropdbStmt *stmt = (DropdbStmt *) parsetree;
599 PS_SET_STATUS(commandTag = "DROP DATABASE");
601 dropdb(stmt->dbname);
605 /* Query-level asynchronous notification */
608 NotifyStmt *stmt = (NotifyStmt *) parsetree;
610 PS_SET_STATUS(commandTag = "NOTIFY");
613 Async_Notify(stmt->relname);
619 ListenStmt *stmt = (ListenStmt *) parsetree;
621 PS_SET_STATUS(commandTag = "LISTEN");
624 Async_Listen(stmt->relname, MyProcPid);
630 UnlistenStmt *stmt = (UnlistenStmt *) parsetree;
632 PS_SET_STATUS(commandTag = "UNLISTEN");
635 Async_Unlisten(stmt->relname, MyProcPid);
640 * ******************************** dynamic loader ********************************
645 LoadStmt *stmt = (LoadStmt *) parsetree;
647 PS_SET_STATUS(commandTag = "LOAD");
650 closeAllVfds(); /* probably not necessary... */
651 load_file(stmt->filename);
657 ClusterStmt *stmt = (ClusterStmt *) parsetree;
659 PS_SET_STATUS(commandTag = "CLUSTER");
662 cluster(stmt->relname, stmt->indexname);
667 PS_SET_STATUS(commandTag = "VACUUM");
669 vacuum(((VacuumStmt *) parsetree)->vacrel,
670 ((VacuumStmt *) parsetree)->verbose,
671 ((VacuumStmt *) parsetree)->analyze,
672 ((VacuumStmt *) parsetree)->va_spec);
677 ExplainStmt *stmt = (ExplainStmt *) parsetree;
679 PS_SET_STATUS(commandTag = "EXPLAIN");
682 ExplainQuery(stmt->query, stmt->verbose, dest);
689 * ******************************** Tioga-related statements *******************************
693 RecipeStmt *stmt = (RecipeStmt *) parsetree;
695 PS_SET_STATUS(commandTag = "EXECUTE RECIPE");
703 * ******************************** set variable statements *******************************
705 case T_VariableSetStmt:
707 VariableSetStmt *n = (VariableSetStmt *) parsetree;
709 SetPGVariable(n->name, n->value);
710 PS_SET_STATUS(commandTag = "SET VARIABLE");
714 case T_VariableShowStmt:
716 VariableShowStmt *n = (VariableShowStmt *) parsetree;
718 GetPGVariable(n->name);
719 PS_SET_STATUS(commandTag = "SHOW VARIABLE");
723 case T_VariableResetStmt:
725 VariableResetStmt *n = (VariableResetStmt *) parsetree;
727 ResetPGVariable(n->name);
728 PS_SET_STATUS(commandTag = "RESET VARIABLE");
733 * ******************************** TRIGGER statements *******************************
735 case T_CreateTrigStmt:
736 PS_SET_STATUS(commandTag = "CREATE");
739 CreateTrigger((CreateTrigStmt *) parsetree);
743 PS_SET_STATUS(commandTag = "DROP");
746 DropTrigger((DropTrigStmt *) parsetree);
750 * ************* PROCEDURAL LANGUAGE statements *****************
752 case T_CreatePLangStmt:
753 PS_SET_STATUS(commandTag = "CREATE");
756 CreateProceduralLanguage((CreatePLangStmt *) parsetree);
759 case T_DropPLangStmt:
760 PS_SET_STATUS(commandTag = "DROP");
763 DropProceduralLanguage((DropPLangStmt *) parsetree);
767 * ******************************** USER statements ****
770 case T_CreateUserStmt:
771 PS_SET_STATUS(commandTag = "CREATE USER");
774 CreateUser((CreateUserStmt *) parsetree);
777 case T_AlterUserStmt:
778 PS_SET_STATUS(commandTag = "ALTER USER");
781 AlterUser((AlterUserStmt *) parsetree);
785 PS_SET_STATUS(commandTag = "DROP USER");
788 DropUser((DropUserStmt *) parsetree);
792 PS_SET_STATUS(commandTag = "LOCK TABLE");
795 LockTableCommand((LockStmt *) parsetree);
798 case T_ConstraintsSetStmt:
799 PS_SET_STATUS(commandTag = "SET CONSTRAINTS");
802 DeferredTriggerSetState((ConstraintsSetStmt *) parsetree);
805 case T_CreateGroupStmt:
806 PS_SET_STATUS(commandTag = "CREATE GROUP");
809 CreateGroup((CreateGroupStmt *) parsetree);
812 case T_AlterGroupStmt:
813 PS_SET_STATUS(commandTag = "ALTER GROUP");
816 AlterGroup((AlterGroupStmt *) parsetree, "ALTER GROUP");
819 case T_DropGroupStmt:
820 PS_SET_STATUS(commandTag = "DROP GROUP");
823 DropGroup((DropGroupStmt *) parsetree);
827 * ******************************** default ********************************
831 elog(ERROR, "ProcessUtility: command #%d unsupported",
837 * tell fe/be or whatever that we're done.
840 EndCommand(commandTag, dest);