diff options
author | Bruce Momjian <bruce@momjian.us> | 2000-04-12 17:17:23 +0000 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 2000-04-12 17:17:23 +0000 |
commit | 52f77df613cea1803ce86321c37229626d9f213c (patch) | |
tree | bd9ac9f667f295cb65f4c448a5bb5a062d656b27 /src/backend/commands | |
parent | db4518729d85da83eafdacbcebaeb12618517595 (diff) | |
download | postgresql-52f77df613cea1803ce86321c37229626d9f213c.tar.gz postgresql-52f77df613cea1803ce86321c37229626d9f213c.zip |
Ye-old pgindent run. Same 4-space tabs.
Diffstat (limited to 'src/backend/commands')
-rw-r--r-- | src/backend/commands/_deadcode/recipe.c | 6 | ||||
-rw-r--r-- | src/backend/commands/async.c | 17 | ||||
-rw-r--r-- | src/backend/commands/cluster.c | 8 | ||||
-rw-r--r-- | src/backend/commands/command.c | 488 | ||||
-rw-r--r-- | src/backend/commands/comment.c | 991 | ||||
-rw-r--r-- | src/backend/commands/copy.c | 95 | ||||
-rw-r--r-- | src/backend/commands/creatinh.c | 50 | ||||
-rw-r--r-- | src/backend/commands/dbcommands.c | 302 | ||||
-rw-r--r-- | src/backend/commands/define.c | 31 | ||||
-rw-r--r-- | src/backend/commands/explain.c | 16 | ||||
-rw-r--r-- | src/backend/commands/indexcmds.c | 59 | ||||
-rw-r--r-- | src/backend/commands/remove.c | 13 | ||||
-rw-r--r-- | src/backend/commands/rename.c | 43 | ||||
-rw-r--r-- | src/backend/commands/sequence.c | 8 | ||||
-rw-r--r-- | src/backend/commands/trigger.c | 394 | ||||
-rw-r--r-- | src/backend/commands/user.c | 1512 | ||||
-rw-r--r-- | src/backend/commands/vacuum.c | 55 | ||||
-rw-r--r-- | src/backend/commands/variable.c | 31 |
18 files changed, 2144 insertions, 1975 deletions
diff --git a/src/backend/commands/_deadcode/recipe.c b/src/backend/commands/_deadcode/recipe.c index 6fd21c9e57d..a0b87743f5b 100644 --- a/src/backend/commands/_deadcode/recipe.c +++ b/src/backend/commands/_deadcode/recipe.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/_deadcode/Attic/recipe.c,v 1.10 2000/01/26 05:56:17 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/_deadcode/Attic/recipe.c,v 1.11 2000/04/12 17:15:06 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -702,7 +702,7 @@ getParamTypes(TgElement * elem, Oid *typev) if (parameterCount == FUNC_MAX_ARGS) { elog(ERROR, - "getParamTypes: Ingredients cannot take > %d arguments",FUNC_MAX_ARGS); + "getParamTypes: Ingredients cannot take > %d arguments", FUNC_MAX_ARGS); } t = elem->inTypes->val[j]; if (strcmp(t, "opaque") == 0) @@ -810,7 +810,7 @@ tg_parseSubQuery(TgRecipe * r, TgNode * n, TeeInfo * teeInfo) { TgElement *elem; char *funcName; - Oid typev[FUNC_MAX_ARGS], /* eight arguments maximum */ + Oid typev[FUNC_MAX_ARGS], /* eight arguments maximum */ relid; int i, parameterCount; diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c index e7cf7ac6748..f6079885fa7 100644 --- a/src/backend/commands/async.c +++ b/src/backend/commands/async.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/async.c,v 1.58 2000/01/26 05:56:12 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/async.c,v 1.59 2000/04/12 17:14:57 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -155,12 +155,13 @@ Async_Notify(char *relname) /* no point in making duplicate entries in the list ... */ if (!AsyncExistsPendingNotify(relname)) { + /* * We allocate list memory from the global malloc pool to ensure - * that it will live until we want to use it. This is probably not - * necessary any longer, since we will use it before the end of the - * transaction. DLList only knows how to use malloc() anyway, but we - * could probably palloc() the strings... + * that it will live until we want to use it. This is probably + * not necessary any longer, since we will use it before the end + * of the transaction. DLList only knows how to use malloc() + * anyway, but we could probably palloc() the strings... */ notifyName = strdup(relname); DLAddHead(pendingNotifies, DLNewElem(notifyName)); @@ -466,6 +467,7 @@ AtCommit_Notify() if (listenerPID == MyProcPid) { + /* * Self-notify: no need to bother with table update. * Indeed, we *must not* clear the notification field in @@ -491,6 +493,7 @@ AtCommit_Notify() */ if (kill(listenerPID, SIGUSR2) < 0) { + /* * Get rid of pg_listener entry if it refers to a PID * that no longer exists. Presumably, that backend @@ -514,7 +517,7 @@ AtCommit_Notify() if (RelationGetForm(lRel)->relhasindex) { Relation idescs[Num_pg_listener_indices]; - + CatalogOpenIndices(Num_pg_listener_indices, Name_pg_listener_indices, idescs); CatalogIndexInsert(idescs, Num_pg_listener_indices, lRel, rTuple); CatalogCloseIndices(Num_pg_listener_indices, idescs); @@ -780,7 +783,7 @@ ProcessIncomingNotify(void) if (RelationGetForm(lRel)->relhasindex) { Relation idescs[Num_pg_listener_indices]; - + CatalogOpenIndices(Num_pg_listener_indices, Name_pg_listener_indices, idescs); CatalogIndexInsert(idescs, Num_pg_listener_indices, lRel, rTuple); CatalogCloseIndices(Num_pg_listener_indices, idescs); diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c index 299101337f1..0cbf4a2177b 100644 --- a/src/backend/commands/cluster.c +++ b/src/backend/commands/cluster.c @@ -15,7 +15,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.50 2000/01/26 05:56:13 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.51 2000/04/12 17:14:57 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -104,8 +104,8 @@ cluster(char *oldrelname, char *oldindexname) * Like vacuum, cluster spans transactions, so I'm going to handle it * in the same way: commit and restart transactions where needed. * - * We grab exclusive access to the target rel and index for the - * duration of the initial transaction. + * We grab exclusive access to the target rel and index for the duration + * of the initial transaction. */ OldHeap = heap_openr(oldrelname, AccessExclusiveLock); @@ -115,7 +115,7 @@ cluster(char *oldrelname, char *oldindexname) LockRelation(OldIndex, AccessExclusiveLock); OIDOldIndex = RelationGetRelid(OldIndex); - heap_close(OldHeap, NoLock); /* do NOT give up the locks */ + heap_close(OldHeap, NoLock);/* do NOT give up the locks */ index_close(OldIndex); /* diff --git a/src/backend/commands/command.c b/src/backend/commands/command.c index b75f3840a87..2b9f6a257b1 100644 --- a/src/backend/commands/command.c +++ b/src/backend/commands/command.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.70 2000/03/09 05:00:23 inoue Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.71 2000/04/12 17:14:57 momjian Exp $ * * NOTES * The PortalExecutorHeapMemory crap needs to be eliminated @@ -51,7 +51,7 @@ #include "access/genam.h" #include "optimizer/clauses.h" #include "../parser/parse.h" -#endif /* _DROP_COLUMN_HACK__ */ +#endif /* _DROP_COLUMN_HACK__ */ /* ---------------- * PortalExecutorHeapMemory stuff @@ -262,7 +262,7 @@ PerformPortalClose(char *name, CommandDest dest) } /* ---------------- - * AlterTableAddColumn + * AlterTableAddColumn * (formerly known as PerformAddAttribute) * * adds an additional attribute to a relation @@ -327,8 +327,8 @@ AlterTableAddColumn(const char *relationName, #endif /* - * Grab an exclusive lock on the target table, which we will NOT release - * until end of transaction. + * Grab an exclusive lock on the target table, which we will NOT + * release until end of transaction. */ rel = heap_openr(relationName, AccessExclusiveLock); myrelid = RelationGetRelid(rel); @@ -341,7 +341,7 @@ AlterTableAddColumn(const char *relationName, elog(ERROR, "Can't add a NOT NULL attribute to an existing relation"); if (colDef->raw_default || colDef->cooked_default) - elog(ERROR, "Adding columns with defaults is not implemented."); + elog(ERROR, "Adding columns with defaults is not implemented."); /* @@ -370,7 +370,7 @@ AlterTableAddColumn(const char *relationName, */ foreach(child, children) { - Oid childrelid = lfirsti(child); + Oid childrelid = lfirsti(child); if (childrelid == myrelid) continue; @@ -514,13 +514,13 @@ static void drop_default(Oid relid, int16 attnum); */ void AlterTableAlterColumn(const char *relationName, - bool inh, const char *colName, - Node *newDefault) + bool inh, const char *colName, + Node *newDefault) { - Relation rel; - HeapTuple tuple; - int16 attnum; - Oid myrelid; + Relation rel; + HeapTuple tuple; + int16 attnum; + Oid myrelid; if (!allowSystemTableMods && IsSystemRelationName(relationName)) elog(ERROR, "ALTER TABLE: relation \"%s\" is a system catalog", @@ -530,121 +530,122 @@ AlterTableAlterColumn(const char *relationName, elog(ERROR, "ALTER TABLE: permission denied"); #endif - rel = heap_openr(relationName, AccessExclusiveLock); - myrelid = RelationGetRelid(rel); - heap_close(rel, NoLock); + rel = heap_openr(relationName, AccessExclusiveLock); + myrelid = RelationGetRelid(rel); + heap_close(rel, NoLock); - /* - * Propagate to children if desired - */ + /* + * Propagate to children if desired + */ if (inh) - { - List *child, - *children; - - /* this routine is actually in the planner */ - children = find_all_inheritors(myrelid); - - /* - * find_all_inheritors does the recursive search of the - * inheritance hierarchy, so all we have to do is process all - * of the relids in the list that it returns. - */ - foreach(child, children) + { + List *child, + *children; + + /* this routine is actually in the planner */ + children = find_all_inheritors(myrelid); + + /* + * find_all_inheritors does the recursive search of the + * inheritance hierarchy, so all we have to do is process all of + * the relids in the list that it returns. + */ + foreach(child, children) { - Oid childrelid = lfirsti(child); - - if (childrelid == myrelid) - continue; - rel = heap_open(childrelid, AccessExclusiveLock); - AlterTableAlterColumn(RelationGetRelationName(rel), - false, colName, newDefault); - heap_close(rel, AccessExclusiveLock); - } - } - - /* -= now do the thing on this relation =- */ - - /* reopen the business */ - rel = heap_openr((char *)relationName, AccessExclusiveLock); - - /* - * get the number of the attribute - */ - tuple = SearchSysCacheTuple(ATTNAME, - ObjectIdGetDatum(myrelid), - NameGetDatum(namein((char *)colName)), - 0, 0); - - if (!HeapTupleIsValid(tuple)) - { - heap_close(rel, AccessExclusiveLock); - elog(ERROR, "ALTER TABLE: relation \"%s\" has no column \"%s\"", - relationName, colName); - } - - attnum = ((Form_pg_attribute) GETSTRUCT(tuple))->attnum; - - if (newDefault) /* SET DEFAULT */ - { - List* rawDefaults = NIL; - RawColumnDefault *rawEnt; - - /* Get rid of the old one first */ - drop_default(myrelid, attnum); + Oid childrelid = lfirsti(child); + + if (childrelid == myrelid) + continue; + rel = heap_open(childrelid, AccessExclusiveLock); + AlterTableAlterColumn(RelationGetRelationName(rel), + false, colName, newDefault); + heap_close(rel, AccessExclusiveLock); + } + } + + /* -= now do the thing on this relation =- */ + + /* reopen the business */ + rel = heap_openr((char *) relationName, AccessExclusiveLock); + + /* + * get the number of the attribute + */ + tuple = SearchSysCacheTuple(ATTNAME, + ObjectIdGetDatum(myrelid), + NameGetDatum(namein((char *) colName)), + 0, 0); + + if (!HeapTupleIsValid(tuple)) + { + heap_close(rel, AccessExclusiveLock); + elog(ERROR, "ALTER TABLE: relation \"%s\" has no column \"%s\"", + relationName, colName); + } + + attnum = ((Form_pg_attribute) GETSTRUCT(tuple))->attnum; + + if (newDefault) /* SET DEFAULT */ + { + List *rawDefaults = NIL; + RawColumnDefault *rawEnt; + + /* Get rid of the old one first */ + drop_default(myrelid, attnum); rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault)); rawEnt->attnum = attnum; - rawEnt->raw_default = newDefault; + rawEnt->raw_default = newDefault; rawDefaults = lappend(rawDefaults, rawEnt); - /* - * This function is intended for CREATE TABLE, - * so it processes a _list_ of defaults, but we just do one. - */ - AddRelationRawConstraints(rel, rawDefaults, NIL); - } - - else /* DROP DEFAULT */ - { - Relation attr_rel; - ScanKeyData scankeys[3]; - HeapScanDesc scan; - HeapTuple tuple; - - attr_rel = heap_openr(AttributeRelationName, AccessExclusiveLock); - ScanKeyEntryInitialize(&scankeys[0], 0x0, Anum_pg_attribute_attrelid, F_OIDEQ, - ObjectIdGetDatum(myrelid)); - ScanKeyEntryInitialize(&scankeys[1], 0x0, Anum_pg_attribute_attnum, F_INT2EQ, - Int16GetDatum(attnum)); - ScanKeyEntryInitialize(&scankeys[2], 0x0, Anum_pg_attribute_atthasdef, F_BOOLEQ, - TRUE); - - scan = heap_beginscan(attr_rel, false, SnapshotNow, 3, scankeys); - AssertState(scan!=NULL); - - if (HeapTupleIsValid(tuple = heap_getnext(scan, 0))) - { - HeapTuple newtuple; - Relation irelations[Num_pg_attr_indices]; - - /* update to false */ - newtuple = heap_copytuple(tuple); - ((Form_pg_attribute) GETSTRUCT(newtuple))->atthasdef = FALSE; - heap_update(attr_rel, &tuple->t_self, newtuple, NULL); - - /* keep the system catalog indices current */ - CatalogOpenIndices(Num_pg_attr_indices, Name_pg_attr_indices, irelations); - CatalogIndexInsert(irelations, Num_pg_attr_indices, attr_rel, newtuple); - CatalogCloseIndices(Num_pg_attr_indices, irelations); - - /* get rid of actual default definition */ - drop_default(myrelid, attnum); - } - - heap_endscan(scan); - heap_close(attr_rel, NoLock); - } + /* + * This function is intended for CREATE TABLE, so it processes a + * _list_ of defaults, but we just do one. + */ + AddRelationRawConstraints(rel, rawDefaults, NIL); + } + + else +/* DROP DEFAULT */ + { + Relation attr_rel; + ScanKeyData scankeys[3]; + HeapScanDesc scan; + HeapTuple tuple; + + attr_rel = heap_openr(AttributeRelationName, AccessExclusiveLock); + ScanKeyEntryInitialize(&scankeys[0], 0x0, Anum_pg_attribute_attrelid, F_OIDEQ, + ObjectIdGetDatum(myrelid)); + ScanKeyEntryInitialize(&scankeys[1], 0x0, Anum_pg_attribute_attnum, F_INT2EQ, + Int16GetDatum(attnum)); + ScanKeyEntryInitialize(&scankeys[2], 0x0, Anum_pg_attribute_atthasdef, F_BOOLEQ, + TRUE); + + scan = heap_beginscan(attr_rel, false, SnapshotNow, 3, scankeys); + AssertState(scan != NULL); + + if (HeapTupleIsValid(tuple = heap_getnext(scan, 0))) + { + HeapTuple newtuple; + Relation irelations[Num_pg_attr_indices]; + + /* update to false */ + newtuple = heap_copytuple(tuple); + ((Form_pg_attribute) GETSTRUCT(newtuple))->atthasdef = FALSE; + heap_update(attr_rel, &tuple->t_self, newtuple, NULL); + + /* keep the system catalog indices current */ + CatalogOpenIndices(Num_pg_attr_indices, Name_pg_attr_indices, irelations); + CatalogIndexInsert(irelations, Num_pg_attr_indices, attr_rel, newtuple); + CatalogCloseIndices(Num_pg_attr_indices, irelations); + + /* get rid of actual default definition */ + drop_default(myrelid, attnum); + } + + heap_endscan(scan); + heap_close(attr_rel, NoLock); + } heap_close(rel, NoLock); } @@ -654,33 +655,33 @@ AlterTableAlterColumn(const char *relationName, static void drop_default(Oid relid, int16 attnum) { - ScanKeyData scankeys[2]; - HeapScanDesc scan; - Relation attrdef_rel; - HeapTuple tuple; + ScanKeyData scankeys[2]; + HeapScanDesc scan; + Relation attrdef_rel; + HeapTuple tuple; - attrdef_rel = heap_openr(AttrDefaultRelationName, AccessExclusiveLock); - ScanKeyEntryInitialize(&scankeys[0], 0x0, Anum_pg_attrdef_adrelid, F_OIDEQ, - ObjectIdGetDatum(relid)); - ScanKeyEntryInitialize(&scankeys[1], 0x0, Anum_pg_attrdef_adnum, F_INT2EQ, - Int16GetDatum(attnum)); + attrdef_rel = heap_openr(AttrDefaultRelationName, AccessExclusiveLock); + ScanKeyEntryInitialize(&scankeys[0], 0x0, Anum_pg_attrdef_adrelid, F_OIDEQ, + ObjectIdGetDatum(relid)); + ScanKeyEntryInitialize(&scankeys[1], 0x0, Anum_pg_attrdef_adnum, F_INT2EQ, + Int16GetDatum(attnum)); - scan = heap_beginscan(attrdef_rel, false, SnapshotNow, 2, scankeys); - AssertState(scan!=NULL); + scan = heap_beginscan(attrdef_rel, false, SnapshotNow, 2, scankeys); + AssertState(scan != NULL); - if (HeapTupleIsValid(tuple = heap_getnext(scan, 0))) - heap_delete(attrdef_rel, &tuple->t_self, NULL); + if (HeapTupleIsValid(tuple = heap_getnext(scan, 0))) + heap_delete(attrdef_rel, &tuple->t_self, NULL); - heap_endscan(scan); + heap_endscan(scan); - heap_close(attrdef_rel, NoLock); + heap_close(attrdef_rel, NoLock); } #ifdef _DROP_COLUMN_HACK__ /* * ALTER TABLE DROP COLUMN trial implementation - * + * */ /* @@ -690,17 +691,17 @@ typedef struct SysScanDescData { Relation heap_rel; Relation irel; - HeapScanDesc scan; - IndexScanDesc iscan; - HeapTupleData tuple; + HeapScanDesc scan; + IndexScanDesc iscan; + HeapTupleData tuple; Buffer buffer; -} SysScanDescData, *SysScanDesc; - +} SysScanDescData, *SysScanDesc; + static void * systable_beginscan(Relation rel, const char *indexRelname, int nkeys, ScanKey entry) { - bool hasindex = (rel->rd_rel->relhasindex && !IsIgnoringSystemIndexes()); - SysScanDesc sysscan; + bool hasindex = (rel->rd_rel->relhasindex && !IsIgnoringSystemIndexes()); + SysScanDesc sysscan; sysscan = (SysScanDesc) palloc(sizeof(SysScanDescData)); sysscan->heap_rel = rel; @@ -710,7 +711,7 @@ systable_beginscan(Relation rel, const char *indexRelname, int nkeys, ScanKey en sysscan->buffer = InvalidBuffer; if (hasindex) { - sysscan->irel = index_openr((char *)indexRelname); + sysscan->irel = index_openr((char *) indexRelname); sysscan->iscan = index_beginscan(sysscan->irel, false, nkeys, entry); } else @@ -720,7 +721,7 @@ systable_beginscan(Relation rel, const char *indexRelname, int nkeys, ScanKey en static void systable_endscan(void *scan) { - SysScanDesc sysscan = (SysScanDesc) scan; + SysScanDesc sysscan = (SysScanDesc) scan; if (sysscan->irel) { @@ -736,9 +737,9 @@ systable_endscan(void *scan) static HeapTuple systable_getnext(void *scan) { - SysScanDesc sysscan = (SysScanDesc) scan; + SysScanDesc sysscan = (SysScanDesc) scan; HeapTuple htup = (HeapTuple) NULL; - RetrieveIndexResult indexRes; + RetrieveIndexResult indexRes; if (sysscan->irel) { @@ -774,50 +775,55 @@ find_attribute_walker(Node *node, int attnum) return false; if (IsA(node, Var)) { - Var *var = (Var *) node; + Var *var = (Var *) node; + if (var->varlevelsup == 0 && var->varno == 1 && var->varattno == attnum) return true; } - return expression_tree_walker(node, find_attribute_walker, (void *)attnum); + return expression_tree_walker(node, find_attribute_walker, (void *) attnum); } static bool find_attribute_in_node(Node *node, int attnum) { - return expression_tree_walker(node, find_attribute_walker, (void *)attnum); + return expression_tree_walker(node, find_attribute_walker, (void *) attnum); } + /* * Remove/check references for the column */ static bool RemoveColumnReferences(Oid reloid, int attnum, bool checkonly, HeapTuple reltup) { - Relation indexRelation, rcrel; - ScanKeyData entry; - HeapScanDesc scan; - void *sysscan; - HeapTuple htup, indexTuple; - Form_pg_index index; - Form_pg_relcheck relcheck; - Form_pg_class pgcform = (Form_pg_class) NULL; - int i; + Relation indexRelation, + rcrel; + ScanKeyData entry; + HeapScanDesc scan; + void *sysscan; + HeapTuple htup, + indexTuple; + Form_pg_index index; + Form_pg_relcheck relcheck; + Form_pg_class pgcform = (Form_pg_class) NULL; + int i; bool checkok = true; - + if (!checkonly) - pgcform = (Form_pg_class) GETSTRUCT (reltup); + pgcform = (Form_pg_class) GETSTRUCT(reltup); + /* - * Remove/check constraints here + * Remove/check constraints here */ ScanKeyEntryInitialize(&entry, (bits16) 0x0, Anum_pg_relcheck_rcrelid, - (RegProcedure) F_OIDEQ, ObjectIdGetDatum(reloid)); + (RegProcedure) F_OIDEQ, ObjectIdGetDatum(reloid)); rcrel = heap_openr(RelCheckRelationName, RowExclusiveLock); - sysscan = systable_beginscan(rcrel, RelCheckIndex,1 ,&entry); + sysscan = systable_beginscan(rcrel, RelCheckIndex, 1, &entry); while (HeapTupleIsValid(htup = systable_getnext(sysscan))) { - char *ccbin; - Node *node; + char *ccbin; + Node *node; relcheck = (Form_pg_relcheck) GETSTRUCT(htup); ccbin = textout(&relcheck->rcbin); @@ -843,15 +849,15 @@ RemoveColumnReferences(Oid reloid, int attnum, bool checkonly, HeapTuple reltup) heap_close(rcrel, NoLock); /* - * What to do with triggers/rules/views/procedues ? + * What to do with triggers/rules/views/procedues ? */ /* - * Remove/check indexes + * Remove/check indexes */ indexRelation = heap_openr(IndexRelationName, RowExclusiveLock); ScanKeyEntryInitialize(&entry, 0, Anum_pg_index_indrelid, F_OIDEQ, - ObjectIdGetDatum(reloid)); + ObjectIdGetDatum(reloid)); scan = heap_beginscan(indexRelation, false, SnapshotNow, 1, &entry); while (HeapTupleIsValid(indexTuple = heap_getnext(scan, 0))) { @@ -870,8 +876,8 @@ RemoveColumnReferences(Oid reloid, int attnum, bool checkonly, HeapTuple reltup) else { htup = SearchSysCacheTuple(RELOID, - ObjectIdGetDatum(index->indexrelid), - 0, 0, 0); + ObjectIdGetDatum(index->indexrelid), + 0, 0, 0); RemoveIndex(NameStr(((Form_pg_class) GETSTRUCT(htup))->relname)); } break; @@ -883,33 +889,38 @@ RemoveColumnReferences(Oid reloid, int attnum, bool checkonly, HeapTuple reltup) return checkok; } -#endif /* _DROP_COLUMN_HACK__ */ + +#endif /* _DROP_COLUMN_HACK__ */ /* * ALTER TABLE DROP COLUMN */ void AlterTableDropColumn(const char *relationName, - bool inh, const char *colName, - int behavior) + bool inh, const char *colName, + int behavior) { #ifdef _DROP_COLUMN_HACK__ - Relation rel, attrdesc, adrel; - Oid myrelid, attoid; + Relation rel, + attrdesc, + adrel; + Oid myrelid, + attoid; HeapTuple reltup; - HeapTupleData classtuple; + HeapTupleData classtuple; Buffer buffer; Form_pg_attribute attribute; HeapTuple tup; Relation idescs[Num_pg_attr_indices]; - int attnum; + int attnum; bool hasindex; char dropColname[32]; - void *sysscan; - ScanKeyData scankeys[2]; + void *sysscan; + ScanKeyData scankeys[2]; - if (inh) + if (inh) elog(ERROR, "ALTER TABLE / DROP COLUMN with inherit option is not supported yet"); + /* * permissions checking. this would normally be done in utility.c, * but this particular routine is recursive. @@ -925,25 +936,25 @@ AlterTableDropColumn(const char *relationName, #endif /* - * Grab an exclusive lock on the target table, which we will NOT release - * until end of transaction. + * Grab an exclusive lock on the target table, which we will NOT + * release until end of transaction. */ rel = heap_openr(relationName, AccessExclusiveLock); myrelid = RelationGetRelid(rel); heap_close(rel, NoLock); /* close rel but keep lock! */ /* - * What to do when rel has inheritors ? + * What to do when rel has inheritors ? */ if (length(find_all_inheritors(myrelid)) > 1) elog(ERROR, "ALTER TABLE: cannot drop a column on table that is inherited from"); /* - * lock the pg_class tuple for update + * lock the pg_class tuple for update */ reltup = SearchSysCacheTuple(RELNAME, PointerGetDatum(relationName), - 0, 0, 0); + 0, 0, 0); if (!HeapTupleIsValid(reltup)) elog(ERROR, "ALTER TABLE: relation \"%s\" not found", @@ -976,19 +987,20 @@ AlterTableDropColumn(const char *relationName, * Get the target pg_attribute tuple */ tup = SearchSysCacheTupleCopy(ATTNAME, - ObjectIdGetDatum(reltup->t_data->t_oid), - PointerGetDatum(colName), 0, 0); + ObjectIdGetDatum(reltup->t_data->t_oid), + PointerGetDatum(colName), 0, 0); if (!HeapTupleIsValid(tup)) elog(ERROR, "ALTER TABLE: column name \"%s\" doesn't exist in table \"%s\"", - colName, relationName); + colName, relationName); attribute = (Form_pg_attribute) GETSTRUCT(tup); if (attribute->attnum <= 0) elog(ERROR, "ALTER TABLE: column name \"%s\" was already dropped", colName); attnum = attribute->attnum; attoid = tup->t_data->t_oid; + /* - * Check constraints/indices etc here + * Check constraints/indices etc here */ if (behavior != CASCADE) { @@ -997,7 +1009,7 @@ AlterTableDropColumn(const char *relationName, } /* - * change the target pg_attribute tuple + * change the target pg_attribute tuple */ sprintf(dropColname, "*already Dropped*%d", attnum); namestrcpy(&(attribute->attname), dropColname); @@ -1009,7 +1021,7 @@ AlterTableDropColumn(const char *relationName, { CatalogOpenIndices(Num_pg_attr_indices, Name_pg_attr_indices, idescs); CatalogIndexInsert(idescs, Num_pg_attr_indices, - attrdesc, tup); + attrdesc, tup); CatalogCloseIndices(Num_pg_attr_indices, idescs); } heap_close(attrdesc, NoLock); @@ -1020,15 +1032,17 @@ AlterTableDropColumn(const char *relationName, /* delete attrdef */ adrel = heap_openr(AttrDefaultRelationName, RowExclusiveLock); ScanKeyEntryInitialize(&scankeys[0], 0x0, Anum_pg_attrdef_adrelid, - F_OIDEQ, ObjectIdGetDatum(myrelid)); - /* Oops pg_attrdef doesn't have (adrelid,adnum) index - ScanKeyEntryInitialize(&scankeys[1], 0x0, Anum_pg_attrdef_adnum, - F_INT2EQ, Int16GetDatum(attnum)); - sysscan = systable_beginscan(adrel, AttrDefaultIndex, 2, scankeys); - */ + F_OIDEQ, ObjectIdGetDatum(myrelid)); + + /* + * Oops pg_attrdef doesn't have (adrelid,adnum) index + * ScanKeyEntryInitialize(&scankeys[1], 0x0, Anum_pg_attrdef_adnum, + * F_INT2EQ, Int16GetDatum(attnum)); sysscan = + * systable_beginscan(adrel, AttrDefaultIndex, 2, scankeys); + */ sysscan = systable_beginscan(adrel, AttrDefaultIndex, 1, scankeys); while (HeapTupleIsValid(tup = systable_getnext(sysscan))) - { + { if (((Form_pg_attrdef) GETSTRUCT(tup))->adnum == attnum) { heap_delete(adrel, &tup->t_self, NULL); @@ -1037,8 +1051,9 @@ AlterTableDropColumn(const char *relationName, } systable_endscan(sysscan); heap_close(adrel, NoLock); + /* - * Remove objects which reference this column + * Remove objects which reference this column */ if (behavior == CASCADE) { @@ -1055,8 +1070,8 @@ AlterTableDropColumn(const char *relationName, heap_freetuple(reltup); heap_close(rel, NoLock); #else - elog(ERROR, "ALTER TABLE / DROP COLUMN is not implemented"); -#endif /* _DROP_COLUMN_HACK__ */ + elog(ERROR, "ALTER TABLE / DROP COLUMN is not implemented"); +#endif /* _DROP_COLUMN_HACK__ */ } @@ -1066,76 +1081,80 @@ AlterTableDropColumn(const char *relationName, */ void AlterTableAddConstraint(const char *relationName, - bool inh, Node *newConstraint) + bool inh, Node *newConstraint) { - if (newConstraint == NULL) + if (newConstraint == NULL) elog(ERROR, "ALTER TABLE / ADD CONSTRAINT passed invalid constraint."); - switch (nodeTag(newConstraint)) + switch (nodeTag(newConstraint)) { case T_Constraint: elog(ERROR, "ALTER TABLE / ADD CONSTRAINT is not implemented"); case T_FkConstraint: { - FkConstraint *fkconstraint=(FkConstraint *)newConstraint; - Relation rel; + FkConstraint *fkconstraint = (FkConstraint *) newConstraint; + Relation rel; HeapScanDesc scan; - HeapTuple tuple; - Trigger trig; - List *list; - int count; + HeapTuple tuple; + Trigger trig; + List *list; + int count; - /* + /* * Grab an exclusive lock on the pk table, so that someone - * doesn't delete rows out from under us. + * doesn't delete rows out from under us. */ rel = heap_openr(fkconstraint->pktable_name, AccessExclusiveLock); heap_close(rel, NoLock); - /* - * Grab an exclusive lock on the fk table, and then scan through - * each tuple, calling the RI_FKey_Match_Ins (insert trigger) - * as if that tuple had just been inserted. If any of those - * fail, it should elog(ERROR) and that's that. + /* + * Grab an exclusive lock on the fk table, and then scan + * through each tuple, calling the RI_FKey_Match_Ins + * (insert trigger) as if that tuple had just been + * inserted. If any of those fail, it should elog(ERROR) + * and that's that. */ rel = heap_openr(relationName, AccessExclusiveLock); trig.tgoid = 0; trig.tgname = "<unknown>"; - trig.tgfoid = 0; + trig.tgfoid = 0; trig.tgtype = 0; trig.tgenabled = TRUE; trig.tgisconstraint = TRUE; trig.tginitdeferred = FALSE; trig.tgdeferrable = FALSE; - trig.tgargs = (char **)palloc( - sizeof(char *) * (4 + length(fkconstraint->fk_attrs) - + length(fkconstraint->pk_attrs))); - + trig.tgargs = (char **) palloc( + sizeof(char *) * (4 + length(fkconstraint->fk_attrs) + + length(fkconstraint->pk_attrs))); + trig.tgargs[0] = "<unnamed>"; - trig.tgargs[1] = (char *)relationName; + trig.tgargs[1] = (char *) relationName; trig.tgargs[2] = fkconstraint->pktable_name; trig.tgargs[3] = fkconstraint->match_type; count = 4; - foreach (list, fkconstraint->fk_attrs) + foreach(list, fkconstraint->fk_attrs) { - Ident *fk_at = lfirst(list); + Ident *fk_at = lfirst(list); + trig.tgargs[count++] = fk_at->name; } - foreach (list, fkconstraint->pk_attrs) + foreach(list, fkconstraint->pk_attrs) { - Ident *pk_at = lfirst(list); + Ident *pk_at = lfirst(list); + trig.tgargs[count++] = pk_at->name; } trig.tgnargs = count; scan = heap_beginscan(rel, false, SnapshotNow, 0, NULL); - AssertState(scan!=NULL); + AssertState(scan != NULL); while (HeapTupleIsValid(tuple = heap_getnext(scan, 0))) { TriggerData newtrigdata; + newtrigdata.tg_event = TRIGGER_EVENT_INSERT | TRIGGER_EVENT_ROW; newtrigdata.tg_relation = rel; newtrigdata.tg_trigtuple = tuple; @@ -1149,7 +1168,8 @@ AlterTableAddConstraint(const char *relationName, /* Make a call to the check function */ } heap_endscan(scan); - heap_close(rel, NoLock); /* close rel but keep lock! */ + heap_close(rel, NoLock); /* close rel but keep + * lock! */ pfree(trig.tgargs); } @@ -1166,10 +1186,10 @@ AlterTableAddConstraint(const char *relationName, */ void AlterTableDropConstraint(const char *relationName, - bool inh, const char *constrName, - int behavior) + bool inh, const char *constrName, + int behavior) { - elog(ERROR, "ALTER TABLE / DROP CONSTRAINT is not implemented"); + elog(ERROR, "ALTER TABLE / DROP CONSTRAINT is not implemented"); } @@ -1186,7 +1206,7 @@ LockTableCommand(LockStmt *lockstmt) int aclresult; rel = heap_openr(lockstmt->relname, NoLock); - if (! RelationIsValid(rel)) + if (!RelationIsValid(rel)) elog(ERROR, "Relation '%s' does not exist", lockstmt->relname); if (lockstmt->mode == AccessShareLock) diff --git a/src/backend/commands/comment.c b/src/backend/commands/comment.c index 649025b4a13..3fe7b6f6347 100644 --- a/src/backend/commands/comment.c +++ b/src/backend/commands/comment.c @@ -67,44 +67,47 @@ static void CommentTrigger(char *trigger, char *relation, char *comments); *------------------------------------------------------------------ */ -void CommentObject(int objtype, char *objname, char *objproperty, - List *objlist, char *comment) { - - switch (objtype) { - case (INDEX): - case (SEQUENCE): - case (TABLE): - case (VIEW): - CommentRelation(objtype, objname, comment); - break; - case (COLUMN): - CommentAttribute(objname, objproperty, comment); - break; - case (DATABASE): - CommentDatabase(objname, comment); - break; - case (RULE): - CommentRewrite(objname, comment); - break; - case (TYPE_P): - CommentType(objname, comment); - break; - case (AGGREGATE): - CommentAggregate(objname, objproperty, comment); - break; - case (FUNCTION): - CommentProc(objname, objlist, comment); - break; - case (OPERATOR): - CommentOperator(objname, objlist, comment); - break; - case (TRIGGER): - CommentTrigger(objname, objproperty, comment); - break; - default: - elog(ERROR, "An attempt was made to comment on a unknown type: %i", - objtype); - } +void +CommentObject(int objtype, char *objname, char *objproperty, + List *objlist, char *comment) +{ + + switch (objtype) + { + case (INDEX): + case (SEQUENCE): + case (TABLE): + case (VIEW): + CommentRelation(objtype, objname, comment); + break; + case (COLUMN): + CommentAttribute(objname, objproperty, comment); + break; + case (DATABASE): + CommentDatabase(objname, comment); + break; + case (RULE): + CommentRewrite(objname, comment); + break; + case (TYPE_P): + CommentType(objname, comment); + break; + case (AGGREGATE): + CommentAggregate(objname, objproperty, comment); + break; + case (FUNCTION): + CommentProc(objname, objlist, comment); + break; + case (OPERATOR): + CommentOperator(objname, objlist, comment); + break; + case (TRIGGER): + CommentTrigger(objname, objproperty, comment); + break; + default: + elog(ERROR, "An attempt was made to comment on a unknown type: %i", + objtype); + } } @@ -120,87 +123,99 @@ void CommentObject(int objtype, char *objname, char *objproperty, *------------------------------------------------------------------ */ -void CreateComments(Oid oid, char *comment) { - - Relation description; - TupleDesc tupDesc; - HeapScanDesc scan; - ScanKeyData entry; - HeapTuple desctuple = NULL, searchtuple; - Datum values[Natts_pg_description]; - char nulls[Natts_pg_description]; - char replaces[Natts_pg_description]; - bool modified = false; - int i; - - /*** Open pg_description, form a new tuple, if necessary ***/ - - description = heap_openr(DescriptionRelationName, RowExclusiveLock); - tupDesc = description->rd_att; - if ((comment != NULL) && (strlen(comment) > 0)) { - for (i = 0; i < Natts_pg_description; i++) { - nulls[i] = ' '; - replaces[i] = 'r'; - values[i] = (Datum) NULL; - } - i = 0; - values[i++] = ObjectIdGetDatum(oid); - values[i++] = (Datum) fmgr(F_TEXTIN, comment); - } - - /*** Now, open pg_description and attempt to find the old tuple ***/ - - ScanKeyEntryInitialize(&entry, 0x0, Anum_pg_description_objoid, F_OIDEQ, - ObjectIdGetDatum(oid)); - scan = heap_beginscan(description, false, SnapshotNow, 1, &entry); - searchtuple = heap_getnext(scan, 0); - - /*** If a previous tuple exists, either delete or prep replacement ***/ - - if (HeapTupleIsValid(searchtuple)) { - - /*** If the comment is blank, call heap_delete, else heap_update ***/ - - if ((comment == NULL) || (strlen(comment) == 0)) { - heap_delete(description, &searchtuple->t_self, NULL); - } else { - desctuple = heap_modifytuple(searchtuple, description, values, - nulls, replaces); - heap_update(description, &searchtuple->t_self, desctuple, NULL); - modified = TRUE; - } - - } else { - - /*** Only if comment is non-blank do we form a new tuple ***/ - - if ((comment != NULL) && (strlen(comment) > 0)) { - desctuple = heap_formtuple(tupDesc, values, nulls); - heap_insert(description, desctuple); - modified = TRUE; +void +CreateComments(Oid oid, char *comment) +{ + + Relation description; + TupleDesc tupDesc; + HeapScanDesc scan; + ScanKeyData entry; + HeapTuple desctuple = NULL, + searchtuple; + Datum values[Natts_pg_description]; + char nulls[Natts_pg_description]; + char replaces[Natts_pg_description]; + bool modified = false; + int i; + + /*** Open pg_description, form a new tuple, if necessary ***/ + + description = heap_openr(DescriptionRelationName, RowExclusiveLock); + tupDesc = description->rd_att; + if ((comment != NULL) && (strlen(comment) > 0)) + { + for (i = 0; i < Natts_pg_description; i++) + { + nulls[i] = ' '; + replaces[i] = 'r'; + values[i] = (Datum) NULL; + } + i = 0; + values[i++] = ObjectIdGetDatum(oid); + values[i++] = (Datum) fmgr(F_TEXTIN, comment); + } + + /*** Now, open pg_description and attempt to find the old tuple ***/ + + ScanKeyEntryInitialize(&entry, 0x0, Anum_pg_description_objoid, F_OIDEQ, + ObjectIdGetDatum(oid)); + scan = heap_beginscan(description, false, SnapshotNow, 1, &entry); + searchtuple = heap_getnext(scan, 0); + + /*** If a previous tuple exists, either delete or prep replacement ***/ + + if (HeapTupleIsValid(searchtuple)) + { + + /*** If the comment is blank, call heap_delete, else heap_update ***/ + + if ((comment == NULL) || (strlen(comment) == 0)) + heap_delete(description, &searchtuple->t_self, NULL); + else + { + desctuple = heap_modifytuple(searchtuple, description, values, + nulls, replaces); + heap_update(description, &searchtuple->t_self, desctuple, NULL); + modified = TRUE; + } + } + else + { + + /*** Only if comment is non-blank do we form a new tuple ***/ + + if ((comment != NULL) && (strlen(comment) > 0)) + { + desctuple = heap_formtuple(tupDesc, values, nulls); + heap_insert(description, desctuple); + modified = TRUE; + } - } + } - /*** Complete the scan, update indices, if necessary ***/ + /*** Complete the scan, update indices, if necessary ***/ - heap_endscan(scan); + heap_endscan(scan); - if (modified) { - if (RelationGetForm(description)->relhasindex) { - Relation idescs[Num_pg_description_indices]; + if (modified) + { + if (RelationGetForm(description)->relhasindex) + { + Relation idescs[Num_pg_description_indices]; - CatalogOpenIndices(Num_pg_description_indices, - Name_pg_description_indices, idescs); - CatalogIndexInsert(idescs, Num_pg_description_indices, description, - desctuple); - CatalogCloseIndices(Num_pg_description_indices, idescs); - } - heap_freetuple(desctuple); + CatalogOpenIndices(Num_pg_description_indices, + Name_pg_description_indices, idescs); + CatalogIndexInsert(idescs, Num_pg_description_indices, description, + desctuple); + CatalogCloseIndices(Num_pg_description_indices, idescs); + } + heap_freetuple(desctuple); - } + } - heap_close(description, RowExclusiveLock); + heap_close(description, RowExclusiveLock); } @@ -214,34 +229,35 @@ void CreateComments(Oid oid, char *comment) { *------------------------------------------------------------------ */ -void DeleteComments(Oid oid) { +void +DeleteComments(Oid oid) +{ - Relation description; - TupleDesc tupDesc; - ScanKeyData entry; - HeapScanDesc scan; - HeapTuple searchtuple; + Relation description; + TupleDesc tupDesc; + ScanKeyData entry; + HeapScanDesc scan; + HeapTuple searchtuple; - description = heap_openr(DescriptionRelationName, RowExclusiveLock); - tupDesc = description->rd_att; + description = heap_openr(DescriptionRelationName, RowExclusiveLock); + tupDesc = description->rd_att; - /*** Now, open pg_description and attempt to find the old tuple ***/ + /*** Now, open pg_description and attempt to find the old tuple ***/ - ScanKeyEntryInitialize(&entry, 0x0, Anum_pg_description_objoid, F_OIDEQ, - ObjectIdGetDatum(oid)); - scan = heap_beginscan(description, false, SnapshotNow, 1, &entry); - searchtuple = heap_getnext(scan, 0); + ScanKeyEntryInitialize(&entry, 0x0, Anum_pg_description_objoid, F_OIDEQ, + ObjectIdGetDatum(oid)); + scan = heap_beginscan(description, false, SnapshotNow, 1, &entry); + searchtuple = heap_getnext(scan, 0); - /*** If a previous tuple exists, delete it ***/ + /*** If a previous tuple exists, delete it ***/ - if (HeapTupleIsValid(searchtuple)) { - heap_delete(description, &searchtuple->t_self, NULL); - } + if (HeapTupleIsValid(searchtuple)) + heap_delete(description, &searchtuple->t_self, NULL); - /*** Complete the scan, update indices, if necessary ***/ + /*** Complete the scan, update indices, if necessary ***/ - heap_endscan(scan); - heap_close(description, RowExclusiveLock); + heap_endscan(scan); + heap_close(description, RowExclusiveLock); } @@ -256,60 +272,57 @@ void DeleteComments(Oid oid) { *------------------------------------------------------------------ */ -static void CommentRelation(int reltype, char *relname, char *comment) { - - HeapTuple reltuple; - Oid oid; - char relkind; - - /*** First, check object security ***/ - - #ifndef NO_SECURITY - if (!pg_ownercheck(GetPgUserName(), relname, RELNAME)) { - elog(ERROR, "you are not permitted to comment on class '%s'", relname); - } - #endif - - /*** Now, attempt to find the oid in the cached version of pg_class ***/ - - reltuple = SearchSysCacheTuple(RELNAME, PointerGetDatum(relname), - 0, 0, 0); - if (!HeapTupleIsValid(reltuple)) { - elog(ERROR, "relation '%s' does not exist", relname); - } - - oid = reltuple->t_data->t_oid; - - /*** Next, verify that the relation type matches the intent ***/ - - relkind = ((Form_pg_class) GETSTRUCT(reltuple))->relkind; - - switch (reltype) { - case (INDEX): - if (relkind != 'i') { - elog(ERROR, "relation '%s' is not an index", relname); - } - break; - case (TABLE): - if (relkind != 'r') { - elog(ERROR, "relation '%s' is not a table", relname); - } - break; - case (VIEW): - if (relkind != 'r') { - elog(ERROR, "relation '%s' is not a view", relname); - } - break; - case (SEQUENCE): - if (relkind != 'S') { - elog(ERROR, "relation '%s' is not a sequence", relname); - } - break; - } - - /*** Create the comments using the tuple's oid ***/ - - CreateComments(oid, comment); +static void +CommentRelation(int reltype, char *relname, char *comment) +{ + + HeapTuple reltuple; + Oid oid; + char relkind; + + /*** First, check object security ***/ + +#ifndef NO_SECURITY + if (!pg_ownercheck(GetPgUserName(), relname, RELNAME)) + elog(ERROR, "you are not permitted to comment on class '%s'", relname); +#endif + + /*** Now, attempt to find the oid in the cached version of pg_class ***/ + + reltuple = SearchSysCacheTuple(RELNAME, PointerGetDatum(relname), + 0, 0, 0); + if (!HeapTupleIsValid(reltuple)) + elog(ERROR, "relation '%s' does not exist", relname); + + oid = reltuple->t_data->t_oid; + + /*** Next, verify that the relation type matches the intent ***/ + + relkind = ((Form_pg_class) GETSTRUCT(reltuple))->relkind; + + switch (reltype) + { + case (INDEX): + if (relkind != 'i') + elog(ERROR, "relation '%s' is not an index", relname); + break; + case (TABLE): + if (relkind != 'r') + elog(ERROR, "relation '%s' is not a table", relname); + break; + case (VIEW): + if (relkind != 'r') + elog(ERROR, "relation '%s' is not a view", relname); + break; + case (SEQUENCE): + if (relkind != 'S') + elog(ERROR, "relation '%s' is not a sequence", relname); + break; + } + + /*** Create the comments using the tuple's oid ***/ + + CreateComments(oid, comment); } @@ -320,43 +333,45 @@ static void CommentRelation(int reltype, char *relname, char *comment) { * such as a table's column. The routine will check security * restrictions and then attempt to fetch the oid of the associated * attribute. If successful, a comment is added/dropped, else an - * elog() exception is thrown. The parameters are the relation + * elog() exception is thrown. The parameters are the relation * and attribute names, and the comments *------------------------------------------------------------------ */ -static void CommentAttribute(char *relname, char *attrname, char *comment) { +static void +CommentAttribute(char *relname, char *attrname, char *comment) +{ - Relation relation; - HeapTuple attrtuple; - Oid oid; + Relation relation; + HeapTuple attrtuple; + Oid oid; - /*** First, check object security ***/ + /*** First, check object security ***/ - #ifndef NO_SECURITY - if (!pg_ownercheck(GetPgUserName(), relname, RELNAME)) { - elog(ERROR, "you are not permitted to comment on class '%s\'", relname); - } - #endif +#ifndef NO_SECURITY + if (!pg_ownercheck(GetPgUserName(), relname, RELNAME)) + elog(ERROR, "you are not permitted to comment on class '%s\'", relname); +#endif - /*** Now, fetch the attribute oid from the system cache ***/ + /*** Now, fetch the attribute oid from the system cache ***/ - relation = heap_openr(relname, AccessShareLock); - attrtuple = SearchSysCacheTuple(ATTNAME, ObjectIdGetDatum(relation->rd_id), - PointerGetDatum(attrname), 0, 0); - if (!HeapTupleIsValid(attrtuple)) { - elog(ERROR, "'%s' is not an attribute of class '%s'", - attrname, relname); - } - oid = attrtuple->t_data->t_oid; + relation = heap_openr(relname, AccessShareLock); + attrtuple = SearchSysCacheTuple(ATTNAME, ObjectIdGetDatum(relation->rd_id), + PointerGetDatum(attrname), 0, 0); + if (!HeapTupleIsValid(attrtuple)) + { + elog(ERROR, "'%s' is not an attribute of class '%s'", + attrname, relname); + } + oid = attrtuple->t_data->t_oid; - /*** Call CreateComments() to create/drop the comments ***/ + /*** Call CreateComments() to create/drop the comments ***/ - CreateComments(oid, comment); + CreateComments(oid, comment); - /*** Now, close the heap relation and return ***/ + /*** Now, close the heap relation and return ***/ - heap_close(relation, AccessShareLock); + heap_close(relation, AccessShareLock); } @@ -371,61 +386,64 @@ static void CommentAttribute(char *relname, char *attrname, char *comment) { *------------------------------------------------------------------ */ -static void CommentDatabase(char *database, char *comment) { +static void +CommentDatabase(char *database, char *comment) +{ - Relation pg_database; - HeapTuple dbtuple, usertuple; - ScanKeyData entry; - HeapScanDesc scan; - Oid oid; - bool superuser; - int4 dba, userid; - char *username; + Relation pg_database; + HeapTuple dbtuple, + usertuple; + ScanKeyData entry; + HeapScanDesc scan; + Oid oid; + bool superuser; + int4 dba, + userid; + char *username; - /*** First find the tuple in pg_database for the database ***/ + /*** First find the tuple in pg_database for the database ***/ - pg_database = heap_openr(DatabaseRelationName, AccessShareLock); - ScanKeyEntryInitialize(&entry, 0, Anum_pg_database_datname, - F_NAMEEQ, NameGetDatum(database)); - scan = heap_beginscan(pg_database, 0, SnapshotNow, 1, &entry); - dbtuple = heap_getnext(scan, 0); + pg_database = heap_openr(DatabaseRelationName, AccessShareLock); + ScanKeyEntryInitialize(&entry, 0, Anum_pg_database_datname, + F_NAMEEQ, NameGetDatum(database)); + scan = heap_beginscan(pg_database, 0, SnapshotNow, 1, &entry); + dbtuple = heap_getnext(scan, 0); - /*** Validate database exists, and fetch the dba id and oid ***/ + /*** Validate database exists, and fetch the dba id and oid ***/ - if (!HeapTupleIsValid(dbtuple)) { - elog(ERROR, "database '%s' does not exist", database); - } - dba = ((Form_pg_database) GETSTRUCT(dbtuple))->datdba; - oid = dbtuple->t_data->t_oid; + if (!HeapTupleIsValid(dbtuple)) + elog(ERROR, "database '%s' does not exist", database); + dba = ((Form_pg_database) GETSTRUCT(dbtuple))->datdba; + oid = dbtuple->t_data->t_oid; - /*** Now, fetch user information ***/ + /*** Now, fetch user information ***/ - username = GetPgUserName(); - usertuple = SearchSysCacheTuple(SHADOWNAME, PointerGetDatum(username), - 0, 0, 0); - if (!HeapTupleIsValid(usertuple)) { - elog(ERROR, "current user '%s' does not exist", username); - } - userid = ((Form_pg_shadow) GETSTRUCT(usertuple))->usesysid; - superuser = ((Form_pg_shadow) GETSTRUCT(usertuple))->usesuper; + username = GetPgUserName(); + usertuple = SearchSysCacheTuple(SHADOWNAME, PointerGetDatum(username), + 0, 0, 0); + if (!HeapTupleIsValid(usertuple)) + elog(ERROR, "current user '%s' does not exist", username); + userid = ((Form_pg_shadow) GETSTRUCT(usertuple))->usesysid; + superuser = ((Form_pg_shadow) GETSTRUCT(usertuple))->usesuper; - /*** Allow if the userid matches the database dba or is a superuser ***/ + /*** Allow if the userid matches the database dba or is a superuser ***/ - #ifndef NO_SECURITY - if (!(superuser || (userid == dba))) { - elog(ERROR, "you are not permitted to comment on database '%s'", - database); - } - #endif +#ifndef NO_SECURITY + if (!(superuser || (userid == dba))) + { + elog(ERROR, "you are not permitted to comment on database '%s'", + database); + } +#endif - /*** Create the comments with the pg_database oid ***/ + /*** Create the comments with the pg_database oid ***/ - CreateComments(oid, comment); + CreateComments(oid, comment); - /*** Complete the scan and close any opened relations ***/ + /*** Complete the scan and close any opened relations ***/ - heap_endscan(scan); - heap_close(pg_database, AccessShareLock); + heap_endscan(scan); + heap_close(pg_database, AccessShareLock); } @@ -439,38 +457,41 @@ static void CommentDatabase(char *database, char *comment) { *------------------------------------------------------------------ */ -static void CommentRewrite(char *rule, char *comment) { +static void +CommentRewrite(char *rule, char *comment) +{ - HeapTuple rewritetuple; - Oid oid; - char *user, *relation; - int aclcheck; + HeapTuple rewritetuple; + Oid oid; + char *user, + *relation; + int aclcheck; - /*** First, validate user ***/ + /*** First, validate user ***/ - #ifndef NO_SECURITY - user = GetPgUserName(); - relation = RewriteGetRuleEventRel(rule); - aclcheck = pg_aclcheck(relation, user, ACL_RU); - if (aclcheck != ACLCHECK_OK) { - elog(ERROR, "you are not permitted to comment on rule '%s'", - rule); - } - #endif +#ifndef NO_SECURITY + user = GetPgUserName(); + relation = RewriteGetRuleEventRel(rule); + aclcheck = pg_aclcheck(relation, user, ACL_RU); + if (aclcheck != ACLCHECK_OK) + { + elog(ERROR, "you are not permitted to comment on rule '%s'", + rule); + } +#endif - /*** Next, find the rule's oid ***/ + /*** Next, find the rule's oid ***/ - rewritetuple = SearchSysCacheTuple(RULENAME, PointerGetDatum(rule), - 0, 0, 0); - if (!HeapTupleIsValid(rewritetuple)) { - elog(ERROR, "rule '%s' does not exist", rule); - } + rewritetuple = SearchSysCacheTuple(RULENAME, PointerGetDatum(rule), + 0, 0, 0); + if (!HeapTupleIsValid(rewritetuple)) + elog(ERROR, "rule '%s' does not exist", rule); - oid = rewritetuple->t_data->t_oid; + oid = rewritetuple->t_data->t_oid; - /*** Call CreateComments() to create/drop the comments ***/ + /*** Call CreateComments() to create/drop the comments ***/ - CreateComments(oid, comment); + CreateComments(oid, comment); } @@ -485,35 +506,37 @@ static void CommentRewrite(char *rule, char *comment) { *------------------------------------------------------------------ */ -static void CommentType(char *type, char *comment) { +static void +CommentType(char *type, char *comment) +{ - HeapTuple typetuple; - Oid oid; - char *user; + HeapTuple typetuple; + Oid oid; + char *user; - /*** First, validate user ***/ + /*** First, validate user ***/ - #ifndef NO_SECURITY - user = GetPgUserName(); - if (!pg_ownercheck(user, type, TYPENAME)) { - elog(ERROR, "you are not permitted to comment on type '%s'", - type); - } - #endif +#ifndef NO_SECURITY + user = GetPgUserName(); + if (!pg_ownercheck(user, type, TYPENAME)) + { + elog(ERROR, "you are not permitted to comment on type '%s'", + type); + } +#endif - /*** Next, find the type's oid ***/ + /*** Next, find the type's oid ***/ - typetuple = SearchSysCacheTuple(TYPENAME, PointerGetDatum(type), - 0, 0, 0); - if (!HeapTupleIsValid(typetuple)) { - elog(ERROR, "type '%s' does not exist", type); - } + typetuple = SearchSysCacheTuple(TYPENAME, PointerGetDatum(type), + 0, 0, 0); + if (!HeapTupleIsValid(typetuple)) + elog(ERROR, "type '%s' does not exist", type); - oid = typetuple->t_data->t_oid; + oid = typetuple->t_data->t_oid; - /*** Call CreateComments() to create/drop the comments ***/ + /*** Call CreateComments() to create/drop the comments ***/ - CreateComments(oid, comment); + CreateComments(oid, comment); } @@ -527,57 +550,66 @@ static void CommentType(char *type, char *comment) { *------------------------------------------------------------------ */ -static void CommentAggregate(char *aggregate, char *argument, char *comment) { - - HeapTuple aggtuple; - Oid baseoid, oid; - bool defined; - char *user; - - /*** First, attempt to determine the base aggregate oid ***/ - - if (argument) { - baseoid = TypeGet(argument, &defined); - if (!OidIsValid(baseoid)) { - elog(ERROR, "aggregate type '%s' does not exist", argument); - } - } else { - baseoid = 0; - } - - /*** Next, validate the user's attempt to comment ***/ - - #ifndef NO_SECURITY - user = GetPgUserName(); - if (!pg_aggr_ownercheck(user, aggregate, baseoid)) { - if (argument) { - elog(ERROR, "you are not permitted to comment on aggregate '%s' %s '%s'", - aggregate, "with type", argument); - } else { - elog(ERROR, "you are not permitted to comment on aggregate '%s'", - aggregate); - } - } - #endif - - /*** Now, attempt to find the actual tuple in pg_aggregate ***/ - - aggtuple = SearchSysCacheTuple(AGGNAME, PointerGetDatum(aggregate), - ObjectIdGetDatum(baseoid), 0, 0); - if (!HeapTupleIsValid(aggtuple)) { - if (argument) { - elog(ERROR, "aggregate type '%s' does not exist for aggregate '%s'", - argument, aggregate); - } else { - elog(ERROR, "aggregate '%s' does not exist", aggregate); - } - } - - oid = aggtuple->t_data->t_oid; - - /*** Call CreateComments() to create/drop the comments ***/ - - CreateComments(oid, comment); +static void +CommentAggregate(char *aggregate, char *argument, char *comment) +{ + + HeapTuple aggtuple; + Oid baseoid, + oid; + bool defined; + char *user; + + /*** First, attempt to determine the base aggregate oid ***/ + + if (argument) + { + baseoid = TypeGet(argument, &defined); + if (!OidIsValid(baseoid)) + elog(ERROR, "aggregate type '%s' does not exist", argument); + } + else + baseoid = 0; + + /*** Next, validate the user's attempt to comment ***/ + +#ifndef NO_SECURITY + user = GetPgUserName(); + if (!pg_aggr_ownercheck(user, aggregate, baseoid)) + { + if (argument) + { + elog(ERROR, "you are not permitted to comment on aggregate '%s' %s '%s'", + aggregate, "with type", argument); + } + else + { + elog(ERROR, "you are not permitted to comment on aggregate '%s'", + aggregate); + } + } +#endif + + /*** Now, attempt to find the actual tuple in pg_aggregate ***/ + + aggtuple = SearchSysCacheTuple(AGGNAME, PointerGetDatum(aggregate), + ObjectIdGetDatum(baseoid), 0, 0); + if (!HeapTupleIsValid(aggtuple)) + { + if (argument) + { + elog(ERROR, "aggregate type '%s' does not exist for aggregate '%s'", + argument, aggregate); + } + else + elog(ERROR, "aggregate '%s' does not exist", aggregate); + } + + oid = aggtuple->t_data->t_oid; + + /*** Call CreateComments() to create/drop the comments ***/ + + CreateComments(oid, comment); } @@ -592,12 +624,17 @@ static void CommentAggregate(char *aggregate, char *argument, char *comment) { *------------------------------------------------------------------ */ -static void CommentProc(char *function, List *arguments, char *comment) +static void +CommentProc(char *function, List *arguments, char *comment) { - HeapTuple argtuple, functuple; - Oid oid, argoids[FUNC_MAX_ARGS]; - char *user, *argument; - int i, argcount; + HeapTuple argtuple, + functuple; + Oid oid, + argoids[FUNC_MAX_ARGS]; + char *user, + *argument; + int i, + argcount; /*** First, initialize function's argument list with their type oids ***/ @@ -606,13 +643,12 @@ static void CommentProc(char *function, List *arguments, char *comment) if (argcount > FUNC_MAX_ARGS) elog(ERROR, "functions cannot have more than %d arguments", FUNC_MAX_ARGS); - for (i = 0; i < argcount; i++) { + for (i = 0; i < argcount; i++) + { argument = strVal(lfirst(arguments)); arguments = lnext(arguments); if (strcmp(argument, "opaque") == 0) - { argoids[i] = 0; - } else { argtuple = SearchSysCacheTuple(TYPENAME, @@ -623,7 +659,7 @@ static void CommentProc(char *function, List *arguments, char *comment) argument); argoids[i] = argtuple->t_data->t_oid; } - } + } /*** Now, validate the user's ability to comment on this function ***/ @@ -663,81 +699,89 @@ static void CommentProc(char *function, List *arguments, char *comment) *------------------------------------------------------------------ */ -static void CommentOperator(char *opername, List *arguments, char *comment) { - - Form_pg_operator data; - HeapTuple optuple; - Oid oid, leftoid = InvalidOid, rightoid = InvalidOid; - bool defined; - char oprtype = 0, *user, *lefttype = NULL, *righttype = NULL; - - /*** Initialize our left and right argument types ***/ - - if (lfirst(arguments) != NULL) { - lefttype = strVal(lfirst(arguments)); - } - if (lsecond(arguments) != NULL) { - righttype = strVal(lsecond(arguments)); - } +static void +CommentOperator(char *opername, List *arguments, char *comment) +{ - /*** Attempt to fetch the left oid, if specified ***/ + Form_pg_operator data; + HeapTuple optuple; + Oid oid, + leftoid = InvalidOid, + rightoid = InvalidOid; + bool defined; + char oprtype = 0, + *user, + *lefttype = NULL, + *righttype = NULL; + + /*** Initialize our left and right argument types ***/ + + if (lfirst(arguments) != NULL) + lefttype = strVal(lfirst(arguments)); + if (lsecond(arguments) != NULL) + righttype = strVal(lsecond(arguments)); + + /*** Attempt to fetch the left oid, if specified ***/ + + if (lefttype != NULL) + { + leftoid = TypeGet(lefttype, &defined); + if (!OidIsValid(leftoid)) + elog(ERROR, "left type '%s' does not exist", lefttype); + } - if (lefttype != NULL) { - leftoid = TypeGet(lefttype, &defined); - if (!OidIsValid(leftoid)) { - elog(ERROR, "left type '%s' does not exist", lefttype); - } - } + /*** Attempt to fetch the right oid, if specified ***/ - /*** Attempt to fetch the right oid, if specified ***/ + if (righttype != NULL) + { + rightoid = TypeGet(righttype, &defined); + if (!OidIsValid(rightoid)) + elog(ERROR, "right type '%s' does not exist", righttype); + } - if (righttype != NULL) { - rightoid = TypeGet(righttype, &defined); - if (!OidIsValid(rightoid)) { - elog(ERROR, "right type '%s' does not exist", righttype); - } - } + /*** Determine operator type ***/ - /*** Determine operator type ***/ + if (OidIsValid(leftoid) && (OidIsValid(rightoid))) + oprtype = 'b'; + else if (OidIsValid(leftoid)) + oprtype = 'l'; + else if (OidIsValid(rightoid)) + oprtype = 'r'; + else + elog(ERROR, "operator '%s' is of an illegal type'", opername); - if (OidIsValid(leftoid) && (OidIsValid(rightoid))) oprtype = 'b'; - else if (OidIsValid(leftoid)) oprtype = 'l'; - else if (OidIsValid(rightoid)) oprtype = 'r'; - else elog(ERROR, "operator '%s' is of an illegal type'", opername); + /*** Attempt to fetch the operator oid ***/ - /*** Attempt to fetch the operator oid ***/ + optuple = SearchSysCacheTupleCopy(OPERNAME, PointerGetDatum(opername), + ObjectIdGetDatum(leftoid), + ObjectIdGetDatum(rightoid), + CharGetDatum(oprtype)); + if (!HeapTupleIsValid(optuple)) + elog(ERROR, "operator '%s' does not exist", opername); - optuple = SearchSysCacheTupleCopy(OPERNAME, PointerGetDatum(opername), - ObjectIdGetDatum(leftoid), - ObjectIdGetDatum(rightoid), - CharGetDatum(oprtype)); - if (!HeapTupleIsValid(optuple)) { - elog(ERROR, "operator '%s' does not exist", opername); - } + oid = optuple->t_data->t_oid; - oid = optuple->t_data->t_oid; + /*** Valid user's ability to comment on this operator ***/ - /*** Valid user's ability to comment on this operator ***/ +#ifndef NO_SECURITY + user = GetPgUserName(); + if (!pg_ownercheck(user, (char *) ObjectIdGetDatum(oid), OPEROID)) + { + elog(ERROR, "you are not permitted to comment on operator '%s'", + opername); + } +#endif - #ifndef NO_SECURITY - user = GetPgUserName(); - if (!pg_ownercheck(user, (char *) ObjectIdGetDatum(oid), OPEROID)) { - elog(ERROR, "you are not permitted to comment on operator '%s'", - opername); - } - #endif + /*** Get the procedure associated with the operator ***/ - /*** Get the procedure associated with the operator ***/ + data = (Form_pg_operator) GETSTRUCT(optuple); + oid = regproctooid(data->oprcode); + if (oid == InvalidOid) + elog(ERROR, "operator '%s' does not have an underlying function", opername); - data = (Form_pg_operator) GETSTRUCT(optuple); - oid = regproctooid(data->oprcode); - if (oid == InvalidOid) { - elog(ERROR, "operator '%s' does not have an underlying function", opername); - } - - /*** Call CreateComments() to create/drop the comments ***/ + /*** Call CreateComments() to create/drop the comments ***/ - CreateComments(oid, comment); + CreateComments(oid, comment); } @@ -752,58 +796,65 @@ static void CommentOperator(char *opername, List *arguments, char *comment) { *------------------------------------------------------------------ */ -static void CommentTrigger(char *trigger, char *relname, char *comment) { - - Form_pg_trigger data; - Relation pg_trigger, relation; - HeapTuple triggertuple; - HeapScanDesc scan; - ScanKeyData entry; - Oid oid = InvalidOid; - char *user; - - /*** First, validate the user's action ***/ - - #ifndef NO_SECURITY - user = GetPgUserName(); - if (!pg_ownercheck(user, relname, RELNAME)) { - elog(ERROR, "you are not permitted to comment on trigger '%s' %s '%s'", - trigger, "defined for relation", relname); - } - #endif - - /*** Now, fetch the trigger oid from pg_trigger ***/ - - relation = heap_openr(relname, AccessShareLock); - pg_trigger = heap_openr(TriggerRelationName, AccessShareLock); - ScanKeyEntryInitialize(&entry, 0, Anum_pg_trigger_tgrelid, - F_OIDEQ, RelationGetRelid(relation)); - scan = heap_beginscan(pg_trigger, 0, SnapshotNow, 1, &entry); - triggertuple = heap_getnext(scan, 0); - while (HeapTupleIsValid(triggertuple)) { - data = (Form_pg_trigger) GETSTRUCT(triggertuple); - if (namestrcmp(&(data->tgname), trigger) == 0) { - oid = triggertuple->t_data->t_oid; - break; - } - triggertuple = heap_getnext(scan, 0); - } - - /*** If no trigger exists for the relation specified, notify user ***/ - - if (oid == InvalidOid) { - elog(ERROR, "trigger '%s' defined for relation '%s' does not exist", - trigger, relname); - } - - /*** Create the comments with the pg_trigger oid ***/ - - CreateComments(oid, comment); - - /*** Complete the scan and close any opened relations ***/ - - heap_endscan(scan); - heap_close(pg_trigger, AccessShareLock); - heap_close(relation, AccessShareLock); +static void +CommentTrigger(char *trigger, char *relname, char *comment) +{ + + Form_pg_trigger data; + Relation pg_trigger, + relation; + HeapTuple triggertuple; + HeapScanDesc scan; + ScanKeyData entry; + Oid oid = InvalidOid; + char *user; + + /*** First, validate the user's action ***/ + +#ifndef NO_SECURITY + user = GetPgUserName(); + if (!pg_ownercheck(user, relname, RELNAME)) + { + elog(ERROR, "you are not permitted to comment on trigger '%s' %s '%s'", + trigger, "defined for relation", relname); + } +#endif + + /*** Now, fetch the trigger oid from pg_trigger ***/ + + relation = heap_openr(relname, AccessShareLock); + pg_trigger = heap_openr(TriggerRelationName, AccessShareLock); + ScanKeyEntryInitialize(&entry, 0, Anum_pg_trigger_tgrelid, + F_OIDEQ, RelationGetRelid(relation)); + scan = heap_beginscan(pg_trigger, 0, SnapshotNow, 1, &entry); + triggertuple = heap_getnext(scan, 0); + while (HeapTupleIsValid(triggertuple)) + { + data = (Form_pg_trigger) GETSTRUCT(triggertuple); + if (namestrcmp(&(data->tgname), trigger) == 0) + { + oid = triggertuple->t_data->t_oid; + break; + } + triggertuple = heap_getnext(scan, 0); + } + + /*** If no trigger exists for the relation specified, notify user ***/ + + if (oid == InvalidOid) + { + elog(ERROR, "trigger '%s' defined for relation '%s' does not exist", + trigger, relname); + } + + /*** Create the comments with the pg_trigger oid ***/ + + CreateComments(oid, comment); + + /*** Complete the scan and close any opened relations ***/ + + heap_endscan(scan); + heap_close(pg_trigger, AccessShareLock); + heap_close(relation, AccessShareLock); } diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 8634ab11dd5..7662b9dc28a 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.103 2000/03/23 21:38:58 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.104 2000/04/12 17:14:58 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -64,8 +64,8 @@ static int CountTuples(Relation relation); * Static communication variables ... pretty grotty, but COPY has * never been reentrant... */ -int lineno = 0; /* used by elog() -- dz */ -static bool fe_eof; +int lineno = 0; /* used by elog() -- dz */ +static bool fe_eof; /* * These static variables are used to avoid incurring overhead for each @@ -76,9 +76,11 @@ static bool fe_eof; * to attribute_buf's data buffer! * encoding, if needed, can be set once at the start of the copy operation. */ -static StringInfoData attribute_buf; +static StringInfoData attribute_buf; + #ifdef MULTIBYTE -static int encoding; +static int encoding; + #endif @@ -113,11 +115,11 @@ CopySendData(void *databuf, int datasize, FILE *fp) fe_eof = true; } else - { + { fwrite(databuf, datasize, 1, fp); - if (ferror(fp)) - elog(ERROR, "CopySendData: %s", strerror(errno)); - } + if (ferror(fp)) + elog(ERROR, "CopySendData: %s", strerror(errno)); + } } static void @@ -194,7 +196,8 @@ CopyPeekChar(FILE *fp) { if (!fp) { - int ch = pq_peekbyte(); + int ch = pq_peekbyte(); + if (ch == EOF) fe_eof = true; return ch; @@ -280,15 +283,15 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe, * Open and lock the relation, using the appropriate lock type. * * Note: AccessExclusive is probably overkill for copying to a relation, - * but that's what the code grabs on the rel's indices. If this lock is - * relaxed then I think the index locks need relaxed also. + * but that's what the code grabs on the rel's indices. If this lock + * is relaxed then I think the index locks need relaxed also. */ rel = heap_openr(relname, (from ? AccessExclusiveLock : AccessShareLock)); result = pg_aclcheck(relname, UserName, required_access); if (result != ACLCHECK_OK) elog(ERROR, "%s: %s", relname, aclcheck_error_strings[result]); - if (!pipe && !superuser()) + if (!pipe && !superuser()) elog(ERROR, "You must have Postgres superuser privilege to do a COPY " "directly to or from a file. Anyone can COPY to stdout or " "from stdin. Psql's \\copy command also works for anyone."); @@ -345,13 +348,13 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe, } else { - mode_t oumask; /* Pre-existing umask value */ + mode_t oumask; /* Pre-existing umask value */ - oumask = umask((mode_t) 022); + oumask = umask((mode_t) 022); if (*filename != '/') elog(ERROR, "Relative path not allowed for server side" - " COPY command."); + " COPY command."); #ifndef __CYGWIN32__ fp = AllocateFile(filename, "w"); @@ -369,9 +372,7 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe, } if (!pipe) - { FreeFile(fp); - } else if (!from) { if (!binary) @@ -382,9 +383,10 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe, pfree(attribute_buf.data); /* - * Close the relation. If reading, we can release the AccessShareLock - * we got; if writing, we should hold the lock until end of transaction - * to ensure that updates will be committed before lock is released. + * Close the relation. If reading, we can release the AccessShareLock + * we got; if writing, we should hold the lock until end of + * transaction to ensure that updates will be committed before lock is + * released. */ heap_close(rel, (from ? NoLock : AccessShareLock)); } @@ -399,9 +401,11 @@ CopyTo(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null_p int32 attr_count, i; + #ifdef _DROP_COLUMN_HACK__ - bool *valid; -#endif /* _DROP_COLUMN_HACK__ */ + bool *valid; + +#endif /* _DROP_COLUMN_HACK__ */ Form_pg_attribute *attr; FmgrInfo *out_functions; Oid out_func_oid; @@ -435,7 +439,7 @@ CopyTo(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null_p typmod = (int32 *) palloc(attr_count * sizeof(int32)); #ifdef _DROP_COLUMN_HACK__ valid = (bool *) palloc(attr_count * sizeof(bool)); -#endif /* _DROP_COLUMN_HACK__ */ +#endif /* _DROP_COLUMN_HACK__ */ for (i = 0; i < attr_count; i++) { #ifdef _DROP_COLUMN_HACK__ @@ -446,7 +450,7 @@ CopyTo(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null_p } else valid[i] = true; -#endif /* _DROP_COLUMN_HACK__ */ +#endif /* _DROP_COLUMN_HACK__ */ out_func_oid = (Oid) GetOutputFunction(attr[i]->atttypid); fmgr_info(out_func_oid, &out_functions[i]); elements[i] = GetTypeElement(attr[i]->atttypid); @@ -493,7 +497,7 @@ CopyTo(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null_p CopySendChar('\n', fp); continue; } -#endif /* _DROP_COLUMN_HACK__ */ +#endif /* _DROP_COLUMN_HACK__ */ if (!isnull) { string = (char *) (*fmgr_faddr(&out_functions[i])) @@ -502,7 +506,7 @@ CopyTo(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null_p pfree(string); } else - CopySendString(null_print, fp); /* null indicator */ + CopySendString(null_print, fp); /* null indicator */ if (i == attr_count - 1) CopySendChar('\n', fp); @@ -723,7 +727,7 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null #ifdef _DROP_COLUMN_HACK__ if (COLUMN_IS_DROPPED(attr[i])) continue; -#endif /* _DROP_COLUMN_HACK__ */ +#endif /* _DROP_COLUMN_HACK__ */ in_func_oid = (Oid) GetInputFunction(attr[i]->atttypid); fmgr_info(in_func_oid, &in_functions[i]); elements[i] = GetTypeElement(attr[i]->atttypid); @@ -756,7 +760,7 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null byval[i] = 'n'; continue; } -#endif /* _DROP_COLUMN_HACK__ */ +#endif /* _DROP_COLUMN_HACK__ */ byval[i] = (bool) IsTypeByVal(attr[i]->atttypid); } @@ -765,7 +769,8 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null while (!done) { - if (QueryCancel) { + if (QueryCancel) + { lineno = 0; CancelQuery(); } @@ -796,7 +801,7 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null nulls[i] = 'n'; continue; } -#endif /* _DROP_COLUMN_HACK__ */ +#endif /* _DROP_COLUMN_HACK__ */ string = CopyReadAttribute(fp, &isnull, delim, &newline, null_print); if (isnull) { @@ -937,7 +942,7 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim, char *null */ slot->val = tuple; /* SetSlotContents(slot, tuple); */ - if (! ExecQual((List *) indexPred[i], econtext, false)) + if (!ExecQual((List *) indexPred[i], econtext, false)) continue; #endif /* OMIT_PARTIAL_INDEX */ } @@ -1189,6 +1194,7 @@ static char * CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_print) { int c; + #ifdef MULTIBYTE int mblen; unsigned char s[2]; @@ -1222,9 +1228,7 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_ break; } if (strchr(delim, c)) - { break; - } if (c == '\\') { c = CopyGetChar(fp); @@ -1272,13 +1276,16 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_ c = val & 0377; } break; - /* This is a special hack to parse `\N' as <backslash-N> - rather then just 'N' to provide compatibility with - the default NULL output. -- pe */ - case 'N': - appendStringInfoCharMacro(&attribute_buf, '\\'); - c = 'N'; - break; + + /* + * This is a special hack to parse `\N' as + * <backslash-N> rather then just 'N' to provide + * compatibility with the default NULL output. -- pe + */ + case 'N': + appendStringInfoCharMacro(&attribute_buf, '\\'); + c = 'N'; + break; case 'b': c = '\b'; break; @@ -1332,8 +1339,8 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_ } #endif - if (strcmp(attribute_buf.data, null_print)==0) - *isnull = true; + if (strcmp(attribute_buf.data, null_print) == 0) + *isnull = true; return attribute_buf.data; @@ -1346,10 +1353,12 @@ CopyAttributeOut(FILE *fp, char *server_string, char *delim) { char *string; char c; + #ifdef MULTIBYTE char *string_start; int mblen; int i; + #endif #ifdef MULTIBYTE diff --git a/src/backend/commands/creatinh.c b/src/backend/commands/creatinh.c index da0fcd479ed..17dbe2cbdff 100644 --- a/src/backend/commands/creatinh.c +++ b/src/backend/commands/creatinh.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.56 2000/01/29 16:58:34 petere Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.57 2000/04/12 17:14:58 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -32,7 +32,7 @@ */ static bool checkAttrExists(const char *attributeName, - const char *attributeType, List *schema); + const char *attributeType, List *schema); static List *MergeAttributes(List *schema, List *supers, List **supconstr); static void StoreCatalogInheritance(Oid relationId, List *supers); @@ -145,14 +145,14 @@ DefineRelation(CreateStmt *stmt, char relkind) StoreCatalogInheritance(relationId, inheritList); /* - * Now add any newly specified column default values - * and CHECK constraints to the new relation. These are passed - * to us in the form of raw parsetrees; we need to transform - * them to executable expression trees before they can be added. - * The most convenient way to do that is to apply the parser's - * transformExpr routine, but transformExpr doesn't work unless - * we have a pre-existing relation. So, the transformation has - * to be postponed to this final step of CREATE TABLE. + * Now add any newly specified column default values and CHECK + * constraints to the new relation. These are passed to us in the + * form of raw parsetrees; we need to transform them to executable + * expression trees before they can be added. The most convenient way + * to do that is to apply the parser's transformExpr routine, but + * transformExpr doesn't work unless we have a pre-existing relation. + * So, the transformation has to be postponed to this final step of + * CREATE TABLE. * * First, scan schema to find new column defaults. */ @@ -181,21 +181,24 @@ DefineRelation(CreateStmt *stmt, char relkind) return; /* - * We must bump the command counter to make the newly-created - * relation tuple visible for opening. + * We must bump the command counter to make the newly-created relation + * tuple visible for opening. */ CommandCounterIncrement(); + /* * Open the new relation. */ rel = heap_openr(relname, AccessExclusiveLock); + /* * Parse and add the defaults/constraints. */ AddRelationRawConstraints(rel, rawDefaults, stmt->constraints); + /* - * Clean up. We keep lock on new relation (although it shouldn't - * be visible to anyone else anyway, until commit). + * Clean up. We keep lock on new relation (although it shouldn't be + * visible to anyone else anyway, until commit). */ heap_close(rel, NoLock); } @@ -220,13 +223,13 @@ RemoveRelation(char *name) /* * TruncateRelation -- - * Removes all the rows from a relation + * Removes all the rows from a relation * * Exceptions: - * BadArg if name is invalid + * BadArg if name is invalid * * Note: - * Rows are removed, indices are truncated and reconstructed. + * Rows are removed, indices are truncated and reconstructed. */ void TruncateRelation(char *name) @@ -284,6 +287,7 @@ MergeAttributes(List *schema, List *supers, List **supconstr) foreach(rest, lnext(entry)) { + /* * check for duplicated names within the new relation */ @@ -352,11 +356,12 @@ MergeAttributes(List *schema, List *supers, List **supconstr) * check validity * */ - if (checkAttrExists(attributeName, attributeType, schema)) - elog(ERROR, "CREATE TABLE: attribute \"%s\" already exists in inherited schema", - attributeName); + if (checkAttrExists(attributeName, attributeType, schema)) + elog(ERROR, "CREATE TABLE: attribute \"%s\" already exists in inherited schema", + attributeName); if (checkAttrExists(attributeName, attributeType, inhSchema)) + /* * this entry already exists */ @@ -499,7 +504,7 @@ StoreCatalogInheritance(Oid relationId, List *supers) if (RelationGetForm(relation)->relhasindex) { Relation idescs[Num_pg_inherits_indices]; - + CatalogOpenIndices(Num_pg_inherits_indices, Name_pg_inherits_indices, idescs); CatalogIndexInsert(idescs, Num_pg_inherits_indices, relation, tuple); CatalogCloseIndices(Num_pg_inherits_indices, idescs); @@ -642,8 +647,9 @@ checkAttrExists(const char *attributeName, const char *attributeType, List *sche { ColumnDef *def = lfirst(s); - if (strcmp(attributeName, def->colname)==0) + if (strcmp(attributeName, def->colname) == 0) { + /* * attribute exists. Make sure the types are the same. */ diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index d4e88ec2dde..d09973f5402 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.52 2000/03/26 18:32:28 petere Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.53 2000/04/12 17:14:58 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -33,9 +33,9 @@ #include "catalog/pg_shadow.h" #include "commands/comment.h" #include "miscadmin.h" -#include "storage/bufmgr.h" /* for DropBuffers */ -#include "storage/fd.h" /* for closeAllVfds */ -#include "storage/sinval.h" /* for DatabaseHasActiveBackends */ +#include "storage/bufmgr.h" /* for DropBuffers */ +#include "storage/fd.h" /* for closeAllVfds */ +#include "storage/sinval.h" /* for DatabaseHasActiveBackends */ #include "utils/builtins.h" #include "utils/elog.h" #include "utils/palloc.h" @@ -45,10 +45,10 @@ /* non-export function prototypes */ static bool -get_user_info(const char *name, int4 *use_sysid, bool *use_super, bool *use_createdb); + get_user_info(const char *name, int4 *use_sysid, bool *use_super, bool *use_createdb); static bool -get_db_info(const char *name, char *dbpath, Oid *dbIdP, int4 *ownerIdP); + get_db_info(const char *name, char *dbpath, Oid *dbIdP, int4 *ownerIdP); @@ -61,99 +61,104 @@ createdb(const char *dbname, const char *dbpath, int encoding) { char buf[2 * MAXPGPATH + 100]; char *loc; - char locbuf[512]; + char locbuf[512]; int4 user_id; - bool use_super, use_createdb; + bool use_super, + use_createdb; Relation pg_database_rel; HeapTuple tuple; - TupleDesc pg_database_dsc; - Datum new_record[Natts_pg_database]; - char new_record_nulls[Natts_pg_database] = { ' ', ' ', ' ', ' ' }; + TupleDesc pg_database_dsc; + Datum new_record[Natts_pg_database]; + char new_record_nulls[Natts_pg_database] = {' ', ' ', ' ', ' '}; - if (!get_user_info(GetPgUserName(), &user_id, &use_super, &use_createdb)) - elog(ERROR, "current user name is invalid"); + if (!get_user_info(GetPgUserName(), &user_id, &use_super, &use_createdb)) + elog(ERROR, "current user name is invalid"); - if (!use_createdb && !use_super) - elog(ERROR, "CREATE DATABASE: permission denied"); + if (!use_createdb && !use_super) + elog(ERROR, "CREATE DATABASE: permission denied"); - if (get_db_info(dbname, NULL, NULL, NULL)) - elog(ERROR, "CREATE DATABASE: database \"%s\" already exists", dbname); + if (get_db_info(dbname, NULL, NULL, NULL)) + elog(ERROR, "CREATE DATABASE: database \"%s\" already exists", dbname); - /* don't call this in a transaction block */ + /* don't call this in a transaction block */ if (IsTransactionBlock()) - elog(ERROR, "CREATE DATABASE: may not be called in a transaction block"); + elog(ERROR, "CREATE DATABASE: may not be called in a transaction block"); /* Generate directory name for the new database */ - if (dbpath == NULL || strcmp(dbpath, dbname)==0) - strcpy(locbuf, dbname); - else - snprintf(locbuf, sizeof(locbuf), "%s/%s", dbpath, dbname); + if (dbpath == NULL || strcmp(dbpath, dbname) == 0) + strcpy(locbuf, dbname); + else + snprintf(locbuf, sizeof(locbuf), "%s/%s", dbpath, dbname); - loc = ExpandDatabasePath(locbuf); + loc = ExpandDatabasePath(locbuf); if (loc == NULL) elog(ERROR, - "The database path '%s' is invalid. " + "The database path '%s' is invalid. " "This may be due to a character that is not allowed or because the chosen " - "path isn't permitted for databases", dbpath); + "path isn't permitted for databases", dbpath); - /* close virtual file descriptors so the kernel has more available for - the system() calls */ + /* + * close virtual file descriptors so the kernel has more available for + * the system() calls + */ closeAllVfds(); - /* - * Insert a new tuple into pg_database - */ + /* + * Insert a new tuple into pg_database + */ pg_database_rel = heap_openr(DatabaseRelationName, AccessExclusiveLock); pg_database_dsc = RelationGetDescr(pg_database_rel); - /* Form tuple */ - new_record[Anum_pg_database_datname-1] = NameGetDatum(namein(dbname)); - new_record[Anum_pg_database_datdba-1] = Int32GetDatum(user_id); - new_record[Anum_pg_database_encoding-1] = Int32GetDatum(encoding); - new_record[Anum_pg_database_datpath-1] = PointerGetDatum(textin(locbuf)); + /* Form tuple */ + new_record[Anum_pg_database_datname - 1] = NameGetDatum(namein(dbname)); + new_record[Anum_pg_database_datdba - 1] = Int32GetDatum(user_id); + new_record[Anum_pg_database_encoding - 1] = Int32GetDatum(encoding); + new_record[Anum_pg_database_datpath - 1] = PointerGetDatum(textin(locbuf)); - tuple = heap_formtuple(pg_database_dsc, new_record, new_record_nulls); + tuple = heap_formtuple(pg_database_dsc, new_record, new_record_nulls); - /* - * Update table - */ - heap_insert(pg_database_rel, tuple); + /* + * Update table + */ + heap_insert(pg_database_rel, tuple); - /* - * Update indexes (there aren't any currently) - */ + /* + * Update indexes (there aren't any currently) + */ #ifdef Num_pg_database_indices - if (RelationGetForm(pg_database_rel)->relhasindex) { - Relation idescs[Num_pg_database_indices]; - - CatalogOpenIndices(Num_pg_database_indices, - Name_pg_database_indices, idescs); - CatalogIndexInsert(idescs, Num_pg_database_indices, pg_database_rel, - tuple); - CatalogCloseIndices(Num_pg_database_indices, idescs); - } + if (RelationGetForm(pg_database_rel)->relhasindex) + { + Relation idescs[Num_pg_database_indices]; + + CatalogOpenIndices(Num_pg_database_indices, + Name_pg_database_indices, idescs); + CatalogIndexInsert(idescs, Num_pg_database_indices, pg_database_rel, + tuple); + CatalogCloseIndices(Num_pg_database_indices, idescs); + } #endif heap_close(pg_database_rel, NoLock); - /* Copy the template database to the new location */ + /* Copy the template database to the new location */ - if (mkdir(loc, S_IRWXU) != 0) { + if (mkdir(loc, S_IRWXU) != 0) elog(ERROR, "CREATE DATABASE: unable to create database directory '%s': %s", loc, strerror(errno)); - } snprintf(buf, sizeof(buf), "cp %s%cbase%ctemplate1%c* '%s'", DataDir, SEP_CHAR, SEP_CHAR, SEP_CHAR, loc); - if (system(buf) != 0) { - int ret; - snprintf(buf, sizeof(buf), "rm -rf '%s'", loc); - ret = system(buf); - if (ret == 0) - elog(ERROR, "CREATE DATABASE: could not initialize database directory"); - else - elog(ERROR, "CREATE DATABASE: Could not initialize database directory. Delete failed as well"); - } + if (system(buf) != 0) + { + int ret; + + snprintf(buf, sizeof(buf), "rm -rf '%s'", loc); + ret = system(buf); + if (ret == 0) + elog(ERROR, "CREATE DATABASE: could not initialize database directory"); + else + elog(ERROR, "CREATE DATABASE: Could not initialize database directory. Delete failed as well"); + } } @@ -165,18 +170,19 @@ createdb(const char *dbname, const char *dbpath, int encoding) void dropdb(const char *dbname) { - int4 user_id, db_owner; - bool use_super; + int4 user_id, + db_owner; + bool use_super; Oid db_id; char *path, dbpath[MAXPGPATH], buf[MAXPGPATH + 100]; Relation pgdbrel; HeapScanDesc pgdbscan; - ScanKeyData key; + ScanKeyData key; HeapTuple tup; - AssertArg(dbname); + AssertArg(dbname); if (strcmp(dbname, "template1") == 0) elog(ERROR, "DROP DATABASE: May not be executed on the template1 database"); @@ -185,46 +191,49 @@ dropdb(const char *dbname) elog(ERROR, "DROP DATABASE: Cannot be executed on the currently open database"); if (IsTransactionBlock()) - elog(ERROR, "DROP DATABASE: May not be called in a transaction block"); + elog(ERROR, "DROP DATABASE: May not be called in a transaction block"); - if (!get_user_info(GetPgUserName(), &user_id, &use_super, NULL)) - elog(ERROR, "Current user name is invalid"); + if (!get_user_info(GetPgUserName(), &user_id, &use_super, NULL)) + elog(ERROR, "Current user name is invalid"); - if (!get_db_info(dbname, dbpath, &db_id, &db_owner)) - elog(ERROR, "DROP DATABASE: Database \"%s\" does not exist", dbname); + if (!get_db_info(dbname, dbpath, &db_id, &db_owner)) + elog(ERROR, "DROP DATABASE: Database \"%s\" does not exist", dbname); - if (user_id != db_owner && !use_super) - elog(ERROR, "DROP DATABASE: Permission denied"); + if (user_id != db_owner && !use_super) + elog(ERROR, "DROP DATABASE: Permission denied"); path = ExpandDatabasePath(dbpath); if (path == NULL) elog(ERROR, - "The database path '%s' is invalid. " + "The database path '%s' is invalid. " "This may be due to a character that is not allowed or because the chosen " - "path isn't permitted for databases", path); + "path isn't permitted for databases", path); - /* close virtual file descriptors so the kernel has more available for - the system() calls */ + /* + * close virtual file descriptors so the kernel has more available for + * the system() calls + */ closeAllVfds(); /* - * Obtain exclusive lock on pg_database. We need this to ensure - * that no new backend starts up in the target database while we - * are deleting it. (Actually, a new backend might still manage to - * start up, because it will read pg_database without any locking - * to discover the database's OID. But it will detect its error - * in ReverifyMyDatabase and shut down before any serious damage - * is done. See postinit.c.) + * Obtain exclusive lock on pg_database. We need this to ensure that + * no new backend starts up in the target database while we are + * deleting it. (Actually, a new backend might still manage to start + * up, because it will read pg_database without any locking to + * discover the database's OID. But it will detect its error in + * ReverifyMyDatabase and shut down before any serious damage is done. + * See postinit.c.) */ pgdbrel = heap_openr(DatabaseRelationName, AccessExclusiveLock); /* * Check for active backends in the target database. */ - if (DatabaseHasActiveBackends(db_id)) { + if (DatabaseHasActiveBackends(db_id)) + { heap_close(pgdbrel, AccessExclusiveLock); elog(ERROR, "DROP DATABASE: Database \"%s\" is being accessed by other users", dbname); - } + } /* * Find the database's tuple by OID (should be unique, we trust). @@ -238,8 +247,11 @@ dropdb(const char *dbname) if (!HeapTupleIsValid(tup)) { heap_close(pgdbrel, AccessExclusiveLock); - /* This error should never come up since the existence of the - database is checked earlier */ + + /* + * This error should never come up since the existence of the + * database is checked earlier + */ elog(ERROR, "DROP DATABASE: Database \"%s\" doesn't exist despite earlier reports to the contrary", dbname); } @@ -270,7 +282,7 @@ dropdb(const char *dbname) */ snprintf(buf, sizeof(buf), "rm -rf '%s'", path); if (system(buf) != 0) - elog(NOTICE, "DROP DATABASE: The database directory '%s' could not be removed", path); + elog(NOTICE, "DROP DATABASE: The database directory '%s' could not be removed", path); } @@ -285,11 +297,11 @@ get_db_info(const char *name, char *dbpath, Oid *dbIdP, int4 *ownerIdP) Relation relation; HeapTuple tuple; ScanKeyData scanKey; - HeapScanDesc scan; + HeapScanDesc scan; - AssertArg(name); + AssertArg(name); - relation = heap_openr(DatabaseRelationName, AccessExclusiveLock/*???*/); + relation = heap_openr(DatabaseRelationName, AccessExclusiveLock /* ??? */ ); ScanKeyEntryInitialize(&scanKey, 0, Anum_pg_database_datname, F_NAMEEQ, NameGetDatum(name)); @@ -302,76 +314,76 @@ get_db_info(const char *name, char *dbpath, Oid *dbIdP, int4 *ownerIdP) if (HeapTupleIsValid(tuple)) { - text *tmptext; - bool isnull; - - /* oid of the database */ - if (dbIdP) - *dbIdP = tuple->t_data->t_oid; - /* uid of the owner */ - if (ownerIdP) - { - *ownerIdP = (int4) heap_getattr(tuple, - Anum_pg_database_datdba, - RelationGetDescr(relation), - &isnull); - if (isnull) - *ownerIdP = -1; /* hopefully no one has that id already ;) */ - } - /* database path (as registered in pg_database) */ - if (dbpath) - { - tmptext = (text *) heap_getattr(tuple, - Anum_pg_database_datpath, - RelationGetDescr(relation), - &isnull); - - if (!isnull) - { - Assert(VARSIZE(tmptext) - VARHDRSZ < MAXPGPATH); - - strncpy(dbpath, VARDATA(tmptext), VARSIZE(tmptext) - VARHDRSZ); - *(dbpath + VARSIZE(tmptext) - VARHDRSZ) = '\0'; - } - else - strcpy(dbpath, ""); - } + text *tmptext; + bool isnull; + + /* oid of the database */ + if (dbIdP) + *dbIdP = tuple->t_data->t_oid; + /* uid of the owner */ + if (ownerIdP) + { + *ownerIdP = (int4) heap_getattr(tuple, + Anum_pg_database_datdba, + RelationGetDescr(relation), + &isnull); + if (isnull) + *ownerIdP = -1; /* hopefully no one has that id already ;) */ + } + /* database path (as registered in pg_database) */ + if (dbpath) + { + tmptext = (text *) heap_getattr(tuple, + Anum_pg_database_datpath, + RelationGetDescr(relation), + &isnull); + + if (!isnull) + { + Assert(VARSIZE(tmptext) - VARHDRSZ < MAXPGPATH); + + strncpy(dbpath, VARDATA(tmptext), VARSIZE(tmptext) - VARHDRSZ); + *(dbpath + VARSIZE(tmptext) - VARHDRSZ) = '\0'; + } + else + strcpy(dbpath, ""); + } } else - { - if (dbIdP) - *dbIdP = InvalidOid; - } + { + if (dbIdP) + *dbIdP = InvalidOid; + } heap_endscan(scan); /* We will keep the lock on the relation until end of transaction. */ heap_close(relation, NoLock); - return HeapTupleIsValid(tuple); + return HeapTupleIsValid(tuple); } static bool -get_user_info(const char * name, int4 *use_sysid, bool *use_super, bool *use_createdb) +get_user_info(const char *name, int4 *use_sysid, bool *use_super, bool *use_createdb) { - HeapTuple utup; + HeapTuple utup; AssertArg(name); utup = SearchSysCacheTuple(SHADOWNAME, PointerGetDatum(name), 0, 0, 0); - if (!HeapTupleIsValid(utup)) - return false; + if (!HeapTupleIsValid(utup)) + return false; - if (use_sysid) - *use_sysid = ((Form_pg_shadow) GETSTRUCT(utup))->usesysid; - if (use_super) - *use_super = ((Form_pg_shadow) GETSTRUCT(utup))->usesuper; - if (use_createdb) - *use_createdb = ((Form_pg_shadow) GETSTRUCT(utup))->usecreatedb; + if (use_sysid) + *use_sysid = ((Form_pg_shadow) GETSTRUCT(utup))->usesysid; + if (use_super) + *use_super = ((Form_pg_shadow) GETSTRUCT(utup))->usesuper; + if (use_createdb) + *use_createdb = ((Form_pg_shadow) GETSTRUCT(utup))->usecreatedb; - return true; + return true; } diff --git a/src/backend/commands/define.c b/src/backend/commands/define.c index 2090076f2e4..c77b467089b 100644 --- a/src/backend/commands/define.c +++ b/src/backend/commands/define.c @@ -10,7 +10,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/define.c,v 1.39 2000/04/07 13:39:24 thomas Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/define.c,v 1.40 2000/04/12 17:14:58 momjian Exp $ * * DESCRIPTION * The "DefineFoo" routines take the parse tree and pick out the @@ -137,12 +137,13 @@ compute_full_attributes(List *parameters, int32 *byte_pct_p, foreach(pl, parameters) { - DefElem *param = (DefElem *) lfirst(pl); + DefElem *param = (DefElem *) lfirst(pl); if (strcasecmp(param->defname, "iscachable") == 0) *canCache_p = true; else if (strcasecmp(param->defname, "trusted") == 0) { + /* * we don't have untrusted functions any more. The 4.2 * implementation is lousy anyway so I took it out. -ay 10/94 @@ -233,12 +234,14 @@ CreateFunction(ProcedureStmt *stmt, CommandDest dest) */ bool returnsSet; + /* The function returns a set of values, as opposed to a singleton. */ bool lanisPL = false; /* - * The following are optional user-supplied attributes of the function. + * The following are optional user-supplied attributes of the + * function. */ int32 byte_pct, perbyte_cpu, @@ -256,7 +259,7 @@ CreateFunction(ProcedureStmt *stmt, CommandDest dest) elog(ERROR, "Only users with Postgres superuser privilege are " "permitted to create a function " - "in the '%s' language. Others may use the 'sql' language " + "in the '%s' language. Others may use the 'sql' language " "or the created procedural languages.", languageName); } @@ -316,17 +319,17 @@ CreateFunction(ProcedureStmt *stmt, CommandDest dest) interpret_AS_clause(languageName, stmt->as, &prosrc_str, &probin_str); /* - * And now that we have all the parameters, and know we're - * permitted to do so, go ahead and create the function. + * And now that we have all the parameters, and know we're permitted + * to do so, go ahead and create the function. */ ProcedureCreate(stmt->funcname, returnsSet, prorettype, languageName, - prosrc_str, /* converted to text later */ - probin_str, /* converted to text later */ + prosrc_str, /* converted to text later */ + probin_str, /* converted to text later */ canCache, - true, /* (obsolete "trusted") */ + true, /* (obsolete "trusted") */ byte_pct, perbyte_cpu, percall_cpu, @@ -378,7 +381,7 @@ DefineOperator(char *oprName, if (!strcasecmp(defel->defname, "leftarg")) { if ((nodeTag(defel->arg) == T_TypeName) - && (((TypeName *)defel->arg)->setof)) + && (((TypeName *) defel->arg)->setof)) elog(ERROR, "setof type not implemented for leftarg"); typeName1 = defGetString(defel); @@ -388,7 +391,7 @@ DefineOperator(char *oprName, else if (!strcasecmp(defel->defname, "rightarg")) { if ((nodeTag(defel->arg) == T_TypeName) - && (((TypeName *)defel->arg)->setof)) + && (((TypeName *) defel->arg)->setof)) elog(ERROR, "setof type not implemented for rightarg"); typeName2 = defGetString(defel); @@ -698,16 +701,16 @@ DefineType(char *typeName, List *parameters) static char * defGetString(DefElem *def) { - char *string; + char *string; if (nodeTag(def->arg) == T_String) string = strVal(def->arg); else if (nodeTag(def->arg) == T_TypeName) - string = ((TypeName *)def->arg)->name; + string = ((TypeName *) def->arg)->name; else string = NULL; #if 0 - elog(ERROR, "Define: \"%s\" = what?", def->defname); + elog(ERROR, "Define: \"%s\" = what?", def->defname); #endif return string; diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index 64dc164a0d5..d51053c20a1 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -5,7 +5,7 @@ * Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1994-5, Regents of the University of California * - * $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.55 2000/03/14 23:06:12 thomas Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.56 2000/04/12 17:14:58 momjian Exp $ * */ @@ -209,7 +209,7 @@ explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es) switch (nodeTag(plan)) { case T_IndexScan: - if (ScanDirectionIsBackward(((IndexScan *)plan)->indxorderdir)) + if (ScanDirectionIsBackward(((IndexScan *) plan)->indxorderdir)) appendStringInfo(str, " Backward"); appendStringInfo(str, " using "); i = 0; @@ -219,7 +219,7 @@ explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es) Assert(relation); appendStringInfo(str, "%s%s", (++i > 1) ? ", " : "", - stringStringInfo(RelationGetRelationName(relation))); + stringStringInfo(RelationGetRelationName(relation))); /* drop relcache refcount from RelationIdGetRelation */ RelationDecrementReferenceCount(relation); } @@ -238,17 +238,17 @@ explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es) || (length(rte->ref->attrs) > 0)) { appendStringInfo(str, " %s", - stringStringInfo(rte->ref->relname)); + stringStringInfo(rte->ref->relname)); if (length(rte->ref->attrs) > 0) { - List *c; - int firstEntry = true; + List *c; + int firstEntry = true; appendStringInfo(str, " ("); - foreach (c, rte->ref->attrs) + foreach(c, rte->ref->attrs) { - if (! firstEntry) + if (!firstEntry) { appendStringInfo(str, ", "); firstEntry = false; diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 93ad0eb63b4..2577e4741b1 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.22 2000/02/25 02:58:48 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.23 2000/04/12 17:14:58 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -34,9 +34,9 @@ #include "parser/parse_func.h" #include "utils/builtins.h" #include "utils/syscache.h" -#include "miscadmin.h" /* ReindexDatabase() */ -#include "utils/portal.h" /* ReindexDatabase() */ -#include "catalog/catalog.h" /* ReindexDatabase() */ +#include "miscadmin.h" /* ReindexDatabase() */ +#include "utils/portal.h" /* ReindexDatabase() */ +#include "catalog/catalog.h" /* ReindexDatabase() */ #define IsFuncIndex(ATTR_LIST) (((IndexElem*)lfirst(ATTR_LIST))->args != NIL) @@ -45,11 +45,11 @@ static void CheckPredicate(List *predList, List *rangeTable, Oid baseRelOid); static void CheckPredExpr(Node *predicate, List *rangeTable, Oid baseRelOid); static void CheckPredClause(Expr *predicate, List *rangeTable, Oid baseRelOid); static void FuncIndexArgs(IndexElem *funcIndex, FuncIndexInfo *funcInfo, - AttrNumber *attNumP, Oid *opOidP, Oid relId); + AttrNumber *attNumP, Oid *opOidP, Oid relId); static void NormIndexAttrs(List *attList, AttrNumber *attNumP, - Oid *opOidP, Oid relId); + Oid *opOidP, Oid relId); static void ProcessAttrTypename(IndexElem *attribute, - Oid defType, int32 defTypmod); + Oid defType, int32 defTypmod); static Oid GetAttrOpClass(IndexElem *attribute, Oid attrType); static char *GetDefaultOpClass(Oid atttypid); @@ -133,7 +133,7 @@ DefineIndex(char *heapRelationName, */ foreach(pl, parameterList) { - DefElem *param = (DefElem *) lfirst(pl); + DefElem *param = (DefElem *) lfirst(pl); if (!strcasecmp(param->defname, "islossy")) lossy = TRUE; @@ -174,7 +174,7 @@ DefineIndex(char *heapRelationName, namestrcpy(&fInfo.funcName, funcIndex->name); attributeNumberA = (AttrNumber *) palloc(nargs * - sizeof attributeNumberA[0]); + sizeof attributeNumberA[0]); classObjectId = (Oid *) palloc(sizeof(Oid)); @@ -192,7 +192,7 @@ DefineIndex(char *heapRelationName, else { attributeNumberA = (AttrNumber *) palloc(numberOfAttributes * - sizeof attributeNumberA[0]); + sizeof attributeNumberA[0]); classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid)); @@ -490,7 +490,7 @@ NormIndexAttrs(List *attList, /* list of IndexElem's */ atttuple = SearchSysCacheTupleCopy(ATTNAME, ObjectIdGetDatum(relId), - PointerGetDatum(attribute->name), + PointerGetDatum(attribute->name), 0, 0); if (!HeapTupleIsValid(atttuple)) elog(ERROR, "DefineIndex: attribute \"%s\" not found", @@ -608,7 +608,7 @@ RemoveIndex(char *name) * ... */ void -ReindexIndex(const char *name, bool force /* currently unused */) +ReindexIndex(const char *name, bool force /* currently unused */ ) { HeapTuple tuple; @@ -667,28 +667,35 @@ ReindexTable(const char *name, bool force) * "ERROR" if table nonexistent. * ... */ -extern Oid MyDatabaseId; +extern Oid MyDatabaseId; void ReindexDatabase(const char *dbname, bool force, bool all) { - Relation relation, relationRelation; - HeapTuple usertuple, dbtuple, tuple; - HeapScanDesc scan; - int4 user_id, db_owner; + Relation relation, + relationRelation; + HeapTuple usertuple, + dbtuple, + tuple; + HeapScanDesc scan; + int4 user_id, + db_owner; bool superuser; - Oid db_id; - char *username; - ScanKeyData scankey; - PortalVariableMemory pmem; - MemoryContext old; - int relcnt, relalc, i, oncealc = 200; - Oid *relids = (Oid *) NULL; + Oid db_id; + char *username; + ScanKeyData scankey; + PortalVariableMemory pmem; + MemoryContext old; + int relcnt, + relalc, + i, + oncealc = 200; + Oid *relids = (Oid *) NULL; AssertArg(dbname); username = GetPgUserName(); usertuple = SearchSysCacheTuple(SHADOWNAME, PointerGetDatum(username), - 0, 0, 0); + 0, 0, 0); if (!HeapTupleIsValid(usertuple)) elog(ERROR, "Current user '%s' is invalid.", username); user_id = ((Form_pg_shadow) GETSTRUCT(usertuple))->usesysid; @@ -696,7 +703,7 @@ ReindexDatabase(const char *dbname, bool force, bool all) relation = heap_openr(DatabaseRelationName, AccessShareLock); ScanKeyEntryInitialize(&scankey, 0, Anum_pg_database_datname, - F_NAMEEQ, NameGetDatum(dbname)); + F_NAMEEQ, NameGetDatum(dbname)); scan = heap_beginscan(relation, 0, SnapshotNow, 1, &scankey); dbtuple = heap_getnext(scan, 0); if (!HeapTupleIsValid(dbtuple)) diff --git a/src/backend/commands/remove.c b/src/backend/commands/remove.c index dfcad83c3a8..45d1d310e78 100644 --- a/src/backend/commands/remove.c +++ b/src/backend/commands/remove.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/Attic/remove.c,v 1.45 2000/01/26 05:56:13 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/Attic/remove.c,v 1.46 2000/04/12 17:14:59 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -156,14 +156,15 @@ SingleOpOperatorRemove(Oid typeOid) { key[0].sk_attno = attnums[i]; scan = heap_beginscan(rel, 0, SnapshotNow, 1, key); - while (HeapTupleIsValid(tup = heap_getnext(scan, 0))) { + while (HeapTupleIsValid(tup = heap_getnext(scan, 0))) + { - /*** This is apparently a routine not in use, but remove ***/ - /*** any comments anyways ***/ + /*** This is apparently a routine not in use, but remove ***/ + /*** any comments anyways ***/ - DeleteComments(tup->t_data->t_oid); + DeleteComments(tup->t_data->t_oid); - heap_delete(rel, &tup->t_self, NULL); + heap_delete(rel, &tup->t_self, NULL); } diff --git a/src/backend/commands/rename.c b/src/backend/commands/rename.c index 454b2d0d93d..6a9c92b1e63 100644 --- a/src/backend/commands/rename.c +++ b/src/backend/commands/rename.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.41 2000/01/26 05:56:13 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/Attic/rename.c,v 1.42 2000/04/12 17:14:59 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -76,12 +76,12 @@ renameatt(char *relname, #endif /* - * Grab an exclusive lock on the target table, which we will NOT release - * until end of transaction. + * Grab an exclusive lock on the target table, which we will NOT + * release until end of transaction. */ targetrelation = heap_openr(relname, AccessExclusiveLock); relid = RelationGetRelid(targetrelation); - heap_close(targetrelation, NoLock); /* close rel but keep lock! */ + heap_close(targetrelation, NoLock); /* close rel but keep lock! */ /* * if the 'recurse' flag is set then we are supposed to rename this @@ -160,11 +160,12 @@ renameatt(char *relname, /* keep system catalog indices current */ { Relation irelations[Num_pg_attr_indices]; + CatalogOpenIndices(Num_pg_attr_indices, Name_pg_attr_indices, irelations); CatalogIndexInsert(irelations, Num_pg_attr_indices, attrelation, oldatttup); CatalogCloseIndices(Num_pg_attr_indices, irelations); } - + heap_freetuple(oldatttup); heap_close(attrelation, RowExclusiveLock); } @@ -194,8 +195,8 @@ renamerel(const char *oldrelname, const char *newrelname) newrelname); /* - * Grab an exclusive lock on the target table, which we will NOT release - * until end of transaction. + * Grab an exclusive lock on the target table, which we will NOT + * release until end of transaction. */ targetrelation = heap_openr(oldrelname, AccessExclusiveLock); @@ -211,14 +212,15 @@ renamerel(const char *oldrelname, const char *newrelname) * they don't exist anyway. So, no warning in that case. * ---------------- */ - if (IsTransactionBlock() && ! targetrelation->rd_myxactonly) + if (IsTransactionBlock() && !targetrelation->rd_myxactonly) elog(NOTICE, "Caution: RENAME TABLE cannot be rolled back, so don't abort now"); /* - * Flush all blocks of the relation out of the buffer pool. We need this - * because the blocks are marked with the relation's name as well as OID. - * If some backend tries to write a dirty buffer with mdblindwrt after - * we've renamed the physical file, we'll be in big trouble. + * Flush all blocks of the relation out of the buffer pool. We need + * this because the blocks are marked with the relation's name as well + * as OID. If some backend tries to write a dirty buffer with + * mdblindwrt after we've renamed the physical file, we'll be in big + * trouble. * * Since we hold the exclusive lock on the relation, we don't have to * worry about more blocks being read in while we finish the rename. @@ -227,8 +229,8 @@ renamerel(const char *oldrelname, const char *newrelname) elog(ERROR, "renamerel: unable to flush relation from buffer pool"); /* - * Make sure smgr and lower levels close the relation's files. - * (Next access to rel will reopen them.) + * Make sure smgr and lower levels close the relation's files. (Next + * access to rel will reopen them.) * * Note: we rely on shared cache invalidation message to make other * backends close and re-open the files. @@ -238,14 +240,15 @@ renamerel(const char *oldrelname, const char *newrelname) /* * Close rel, but keep exclusive lock! * - * Note: we don't do anything about updating the relcache entry; - * we assume it will be flushed by shared cache invalidate. - * XXX is this good enough? What if relation is myxactonly? + * Note: we don't do anything about updating the relcache entry; we + * assume it will be flushed by shared cache invalidate. XXX is this + * good enough? What if relation is myxactonly? */ heap_close(targetrelation, NoLock); /* - * Find relation's pg_class tuple, and make sure newrelname isn't in use. + * Find relation's pg_class tuple, and make sure newrelname isn't in + * use. */ relrelation = heap_openr(RelationRelationName, RowExclusiveLock); @@ -262,8 +265,8 @@ renamerel(const char *oldrelname, const char *newrelname) * Perform physical rename of files. If this fails, we haven't yet * done anything irreversible. * - * XXX smgr.c ought to provide an interface for this; doing it - * directly is bletcherous. + * XXX smgr.c ought to provide an interface for this; doing it directly + * is bletcherous. */ strcpy(oldpath, relpath(oldrelname)); strcpy(newpath, relpath(newrelname)); diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index 23280ec5673..0667297d763 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -410,7 +410,9 @@ init_sequence(char *caller, char *name) if (elm != (SeqTable) NULL) { - /* We are using a seqtable entry left over from a previous xact; + + /* + * We are using a seqtable entry left over from a previous xact; * must check for relid change. */ elm->rel = seqrel; @@ -424,7 +426,9 @@ init_sequence(char *caller, char *name) } else { - /* Time to make a new seqtable entry. These entries live as long + + /* + * Time to make a new seqtable entry. These entries live as long * as the backend does, so we use plain malloc for them. */ elm = (SeqTable) malloc(sizeof(SeqTableData)); diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c index 78df4fd5e8e..9961ce6ff15 100644 --- a/src/backend/commands/trigger.c +++ b/src/backend/commands/trigger.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.62 2000/02/29 12:28:24 wieck Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.63 2000/04/12 17:14:59 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -89,7 +89,7 @@ CreateTrigger(CreateTrigStmt *stmt) rel = heap_openr(stmt->constrrelname, NoLock); if (rel == NULL) elog(ERROR, "table \"%s\" does not exist", - stmt->constrrelname); + stmt->constrrelname); constrrelid = rel->rd_id; heap_close(rel, NoLock); } @@ -182,12 +182,12 @@ CreateTrigger(CreateTrigStmt *stmt) values[Anum_pg_trigger_tgfoid - 1] = ObjectIdGetDatum(tuple->t_data->t_oid); values[Anum_pg_trigger_tgtype - 1] = Int16GetDatum(tgtype); - values[Anum_pg_trigger_tgenabled - 1] = true; - values[Anum_pg_trigger_tgisconstraint - 1] = stmt->isconstraint; - values[Anum_pg_trigger_tgconstrname - 1] = PointerGetDatum(constrname);; - values[Anum_pg_trigger_tgconstrrelid - 1] = constrrelid; - values[Anum_pg_trigger_tgdeferrable - 1] = stmt->deferrable; - values[Anum_pg_trigger_tginitdeferred - 1] = stmt->initdeferred; + values[Anum_pg_trigger_tgenabled - 1] = true; + values[Anum_pg_trigger_tgisconstraint - 1] = stmt->isconstraint; + values[Anum_pg_trigger_tgconstrname - 1] = PointerGetDatum(constrname);; + values[Anum_pg_trigger_tgconstrrelid - 1] = constrrelid; + values[Anum_pg_trigger_tgdeferrable - 1] = stmt->deferrable; + values[Anum_pg_trigger_tginitdeferred - 1] = stmt->initdeferred; if (stmt->args) { @@ -261,10 +261,11 @@ CreateTrigger(CreateTrigStmt *stmt) CatalogCloseIndices(Num_pg_class_indices, ridescs); heap_freetuple(tuple); heap_close(pgrel, RowExclusiveLock); + /* * We used to try to update the rel's relcache entry here, but that's - * fairly pointless since it will happen as a byproduct of the upcoming - * CommandCounterIncrement... + * fairly pointless since it will happen as a byproduct of the + * upcoming CommandCounterIncrement... */ /* Keep lock on target rel until end of xact */ heap_close(rel, NoLock); @@ -301,12 +302,12 @@ DropTrigger(DropTrigStmt *stmt) if (namestrcmp(&(pg_trigger->tgname), stmt->trigname) == 0) { - /*** Delete any comments associated with this trigger ***/ + /*** Delete any comments associated with this trigger ***/ - DeleteComments(tuple->t_data->t_oid); + DeleteComments(tuple->t_data->t_oid); - heap_delete(tgrel, &tuple->t_self, NULL); - tgfound++; + heap_delete(tgrel, &tuple->t_self, NULL); + tgfound++; } else @@ -337,10 +338,11 @@ DropTrigger(DropTrigStmt *stmt) CatalogCloseIndices(Num_pg_class_indices, ridescs); heap_freetuple(tuple); heap_close(pgrel, RowExclusiveLock); + /* * We used to try to update the rel's relcache entry here, but that's - * fairly pointless since it will happen as a byproduct of the upcoming - * CommandCounterIncrement... + * fairly pointless since it will happen as a byproduct of the + * upcoming CommandCounterIncrement... */ /* Keep lock on target rel until end of xact */ heap_close(rel, NoLock); @@ -360,13 +362,14 @@ RelationRemoveTriggers(Relation rel) tgscan = heap_beginscan(tgrel, 0, SnapshotNow, 1, &key); - while (HeapTupleIsValid(tup = heap_getnext(tgscan, 0))) { + while (HeapTupleIsValid(tup = heap_getnext(tgscan, 0))) + { - /*** Delete any comments associated with this trigger ***/ + /*** Delete any comments associated with this trigger ***/ - DeleteComments(tup->t_data->t_oid); + DeleteComments(tup->t_data->t_oid); - heap_delete(tgrel, &tup->t_self, NULL); + heap_delete(tgrel, &tup->t_self, NULL); } @@ -385,14 +388,14 @@ RelationRemoveTriggers(Relation rel) * ---------- */ ScanKeyEntryInitialize(&key, 0, Anum_pg_trigger_tgconstrrelid, - F_OIDEQ, RelationGetRelid(rel)); + F_OIDEQ, RelationGetRelid(rel)); tgscan = heap_beginscan(tgrel, 0, SnapshotNow, 1, &key); while (HeapTupleIsValid(tup = heap_getnext(tgscan, 0))) { - Form_pg_trigger pg_trigger; - Relation refrel; - DropTrigStmt stmt; + Form_pg_trigger pg_trigger; + Relation refrel; + DropTrigStmt stmt; pg_trigger = (Form_pg_trigger) GETSTRUCT(tup); @@ -436,8 +439,8 @@ RelationBuildTriggers(Relation relation) Relation irel = (Relation) NULL; ScanKeyData skey; HeapTupleData tuple; - IndexScanDesc sd = (IndexScanDesc) NULL; - HeapScanDesc tgscan = (HeapScanDesc) NULL; + IndexScanDesc sd = (IndexScanDesc) NULL; + HeapScanDesc tgscan = (HeapScanDesc) NULL; HeapTuple htup; RetrieveIndexResult indexRes; Buffer buffer; @@ -684,13 +687,13 @@ FreeTriggerDesc(TriggerDesc *trigdesc) bool equalTriggerDescs(TriggerDesc *trigdesc1, TriggerDesc *trigdesc2) { - int i, - j; + int i, + j; /* - * We need not examine the "index" data, just the trigger array itself; - * if we have the same triggers with the same types, the derived index - * data should match. + * We need not examine the "index" data, just the trigger array + * itself; if we have the same triggers with the same types, the + * derived index data should match. * * XXX It seems possible that the same triggers could appear in different * orders in the two trigger arrays; do we need to handle that? @@ -703,8 +706,8 @@ equalTriggerDescs(TriggerDesc *trigdesc1, TriggerDesc *trigdesc2) return false; for (i = 0; i < trigdesc1->numtriggers; i++) { - Trigger *trig1 = trigdesc1->triggers + i; - Trigger *trig2 = NULL; + Trigger *trig1 = trigdesc1->triggers + i; + Trigger *trig2 = NULL; /* * We can't assume that the triggers are always read from @@ -1014,31 +1017,31 @@ ltrmark:; * end. * ---------- */ -static GlobalMemory deftrig_gcxt = NULL; -static GlobalMemory deftrig_cxt = NULL; +static GlobalMemory deftrig_gcxt = NULL; +static GlobalMemory deftrig_cxt = NULL; /* ---------- * Global data that tells which triggers are actually in * state IMMEDIATE or DEFERRED. * ---------- */ -static bool deftrig_dfl_all_isset = false; -static bool deftrig_dfl_all_isdeferred = false; -static List *deftrig_dfl_trigstates = NIL; +static bool deftrig_dfl_all_isset = false; +static bool deftrig_dfl_all_isdeferred = false; +static List *deftrig_dfl_trigstates = NIL; -static bool deftrig_all_isset; -static bool deftrig_all_isdeferred; -static List *deftrig_trigstates; +static bool deftrig_all_isset; +static bool deftrig_all_isdeferred; +static List *deftrig_trigstates; /* ---------- * The list of events during the entire transaction. * * XXX This must finally be held in a file because of the huge - * number of events that could occur in the real world. + * number of events that could occur in the real world. * ---------- */ -static int deftrig_n_events; -static List *deftrig_events; +static int deftrig_n_events; +static List *deftrig_events; /* ---------- @@ -1051,9 +1054,9 @@ static List *deftrig_events; static bool deferredTriggerCheckState(Oid tgoid, int32 itemstate) { - MemoryContext oldcxt; - List *sl; - DeferredTriggerStatus trigstate; + MemoryContext oldcxt; + List *sl; + DeferredTriggerStatus trigstate; /* ---------- * Not deferrable triggers (i.e. normal AFTER ROW triggers @@ -1068,7 +1071,7 @@ deferredTriggerCheckState(Oid tgoid, int32 itemstate) * Lookup if we know an individual state for this trigger * ---------- */ - foreach (sl, deftrig_trigstates) + foreach(sl, deftrig_trigstates) { trigstate = (DeferredTriggerStatus) lfirst(sl); if (trigstate->dts_tgoid == tgoid) @@ -1092,10 +1095,10 @@ deferredTriggerCheckState(Oid tgoid, int32 itemstate) oldcxt = MemoryContextSwitchTo((MemoryContext) deftrig_cxt); trigstate = (DeferredTriggerStatus) - palloc(sizeof(DeferredTriggerStatusData)); - trigstate->dts_tgoid = tgoid; - trigstate->dts_tgisdeferred = - ((itemstate & TRIGGER_DEFERRED_INITDEFERRED) != 0); + palloc(sizeof(DeferredTriggerStatusData)); + trigstate->dts_tgoid = tgoid; + trigstate->dts_tgisdeferred = + ((itemstate & TRIGGER_DEFERRED_INITDEFERRED) != 0); deftrig_trigstates = lappend(deftrig_trigstates, trigstate); MemoryContextSwitchTo(oldcxt); @@ -1130,8 +1133,8 @@ deferredTriggerAddEvent(DeferredTriggerEvent event) static DeferredTriggerEvent deferredTriggerGetPreviousEvent(Oid relid, ItemPointer ctid) { - DeferredTriggerEvent previous; - int n; + DeferredTriggerEvent previous; + int n; for (n = deftrig_n_events - 1; n >= 0; n--) { @@ -1143,15 +1146,15 @@ deferredTriggerGetPreviousEvent(Oid relid, ItemPointer ctid) continue; if (ItemPointerGetBlockNumber(ctid) == - ItemPointerGetBlockNumber(&(previous->dte_newctid)) && - ItemPointerGetOffsetNumber(ctid) == - ItemPointerGetOffsetNumber(&(previous->dte_newctid))) + ItemPointerGetBlockNumber(&(previous->dte_newctid)) && + ItemPointerGetOffsetNumber(ctid) == + ItemPointerGetOffsetNumber(&(previous->dte_newctid))) return previous; } elog(ERROR, - "deferredTriggerGetPreviousEvent(): event for tuple %s not found", - tidout(ctid)); + "deferredTriggerGetPreviousEvent(): event for tuple %s not found", + tidout(ctid)); return NULL; } @@ -1166,13 +1169,13 @@ deferredTriggerGetPreviousEvent(Oid relid, ItemPointer ctid) static void deferredTriggerExecute(DeferredTriggerEvent event, int itemno) { - Relation rel; - TriggerData SaveTriggerData; - HeapTupleData oldtuple; - HeapTupleData newtuple; - HeapTuple rettuple; - Buffer oldbuffer; - Buffer newbuffer; + Relation rel; + TriggerData SaveTriggerData; + HeapTupleData oldtuple; + HeapTupleData newtuple; + HeapTuple rettuple; + Buffer oldbuffer; + Buffer newbuffer; /* ---------- * Open the heap and fetch the required OLD and NEW tuples. @@ -1200,31 +1203,31 @@ deferredTriggerExecute(DeferredTriggerEvent event, int itemno) * Setup the trigger information * ---------- */ - SaveTriggerData.tg_event = (event->dte_event & TRIGGER_EVENT_OPMASK) | - TRIGGER_EVENT_ROW; + SaveTriggerData.tg_event = (event->dte_event & TRIGGER_EVENT_OPMASK) | + TRIGGER_EVENT_ROW; SaveTriggerData.tg_relation = rel; switch (event->dte_event & TRIGGER_EVENT_OPMASK) { case TRIGGER_EVENT_INSERT: SaveTriggerData.tg_trigtuple = &newtuple; - SaveTriggerData.tg_newtuple = NULL; - SaveTriggerData.tg_trigger = - rel->trigdesc->tg_after_row[TRIGGER_EVENT_INSERT][itemno]; + SaveTriggerData.tg_newtuple = NULL; + SaveTriggerData.tg_trigger = + rel->trigdesc->tg_after_row[TRIGGER_EVENT_INSERT][itemno]; break; case TRIGGER_EVENT_UPDATE: SaveTriggerData.tg_trigtuple = &oldtuple; - SaveTriggerData.tg_newtuple = &newtuple; - SaveTriggerData.tg_trigger = - rel->trigdesc->tg_after_row[TRIGGER_EVENT_UPDATE][itemno]; + SaveTriggerData.tg_newtuple = &newtuple; + SaveTriggerData.tg_trigger = + rel->trigdesc->tg_after_row[TRIGGER_EVENT_UPDATE][itemno]; break; case TRIGGER_EVENT_DELETE: SaveTriggerData.tg_trigtuple = &oldtuple; - SaveTriggerData.tg_newtuple = NULL; - SaveTriggerData.tg_trigger = - rel->trigdesc->tg_after_row[TRIGGER_EVENT_DELETE][itemno]; + SaveTriggerData.tg_newtuple = NULL; + SaveTriggerData.tg_trigger = + rel->trigdesc->tg_after_row[TRIGGER_EVENT_DELETE][itemno]; break; } @@ -1271,11 +1274,11 @@ deferredTriggerExecute(DeferredTriggerEvent event, int itemno) static void deferredTriggerInvokeEvents(bool immediate_only) { - List *el; - DeferredTriggerEvent event; - int still_deferred_ones; - int eventno = -1; - int i; + List *el; + DeferredTriggerEvent event; + int still_deferred_ones; + int eventno = -1; + int i; /* ---------- * For now we process all events - to speedup transaction blocks @@ -1286,7 +1289,7 @@ deferredTriggerInvokeEvents(bool immediate_only) * SET CONSTRAINTS ... command finishes and calls EndQuery. * ---------- */ - foreach (el, deftrig_events) + foreach(el, deftrig_events) { eventno++; @@ -1315,8 +1318,8 @@ deferredTriggerInvokeEvents(bool immediate_only) * ---------- */ if (immediate_only && deferredTriggerCheckState( - event->dte_item[i].dti_tgoid, - event->dte_item[i].dti_state)) + event->dte_item[i].dti_tgoid, + event->dte_item[i].dti_state)) { still_deferred_ones = true; continue; @@ -1367,34 +1370,34 @@ DeferredTriggerInit(void) void DeferredTriggerBeginXact(void) { - MemoryContext oldcxt; - List *l; - DeferredTriggerStatus dflstat; - DeferredTriggerStatus stat; + MemoryContext oldcxt; + List *l; + DeferredTriggerStatus dflstat; + DeferredTriggerStatus stat; if (deftrig_cxt != NULL) elog(FATAL, - "DeferredTriggerBeginXact() called while inside transaction"); + "DeferredTriggerBeginXact() called while inside transaction"); /* ---------- * Create the per transaction memory context and copy all states * from the per session context to here. * ---------- */ - deftrig_cxt = CreateGlobalMemory("DeferredTriggerXact"); - oldcxt = MemoryContextSwitchTo((MemoryContext)deftrig_cxt); + deftrig_cxt = CreateGlobalMemory("DeferredTriggerXact"); + oldcxt = MemoryContextSwitchTo((MemoryContext) deftrig_cxt); - deftrig_all_isset = deftrig_dfl_all_isset; - deftrig_all_isdeferred = deftrig_dfl_all_isdeferred; + deftrig_all_isset = deftrig_dfl_all_isset; + deftrig_all_isdeferred = deftrig_dfl_all_isdeferred; - deftrig_trigstates = NIL; - foreach (l, deftrig_dfl_trigstates) + deftrig_trigstates = NIL; + foreach(l, deftrig_dfl_trigstates) { dflstat = (DeferredTriggerStatus) lfirst(l); - stat = (DeferredTriggerStatus) - palloc(sizeof(DeferredTriggerStatusData)); + stat = (DeferredTriggerStatus) + palloc(sizeof(DeferredTriggerStatusData)); - stat->dts_tgoid = dflstat->dts_tgoid; + stat->dts_tgoid = dflstat->dts_tgoid; stat->dts_tgisdeferred = dflstat->dts_tgisdeferred; deftrig_trigstates = lappend(deftrig_trigstates, stat); @@ -1402,8 +1405,8 @@ DeferredTriggerBeginXact(void) MemoryContextSwitchTo(oldcxt); - deftrig_n_events = 0; - deftrig_events = NIL; + deftrig_n_events = 0; + deftrig_events = NIL; } @@ -1484,22 +1487,23 @@ DeferredTriggerAbortXact(void) void DeferredTriggerSetState(ConstraintsSetStmt *stmt) { - Relation tgrel; - Relation irel = (Relation) NULL; - List *l; - List *ls; - List *lnext; - List *loid = NIL; - MemoryContext oldcxt; - bool found; - DeferredTriggerStatus state; - bool hasindex; + Relation tgrel; + Relation irel = (Relation) NULL; + List *l; + List *ls; + List *lnext; + List *loid = NIL; + MemoryContext oldcxt; + bool found; + DeferredTriggerStatus state; + bool hasindex; /* ---------- * Handle SET CONSTRAINTS ALL ... * ---------- */ - if (stmt->constraints == NIL) { + if (stmt->constraints == NIL) + { if (!IsTransactionBlock()) { /* ---------- @@ -1527,13 +1531,15 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt) * Set the session ALL state to known. * ---------- */ - deftrig_dfl_all_isset = true; + deftrig_dfl_all_isset = true; deftrig_dfl_all_isdeferred = stmt->deferred; MemoryContextSwitchTo(oldcxt); return; - } else { + } + else + { /* ---------- * ... inside of a transaction block * ---------- @@ -1559,7 +1565,7 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt) * Set the per transaction ALL state to known. * ---------- */ - deftrig_all_isset = true; + deftrig_all_isset = true; deftrig_all_isdeferred = stmt->deferred; MemoryContextSwitchTo(oldcxt); @@ -1578,23 +1584,23 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt) if (hasindex) irel = index_openr(TriggerConstrNameIndex); - foreach (l, stmt->constraints) + foreach(l, stmt->constraints) { - ScanKeyData skey; - HeapTupleData tuple; - IndexScanDesc sd = (IndexScanDesc) NULL; - HeapScanDesc tgscan = (HeapScanDesc) NULL; - HeapTuple htup; - RetrieveIndexResult indexRes; - Buffer buffer; - Form_pg_trigger pg_trigger; - Oid constr_oid; + ScanKeyData skey; + HeapTupleData tuple; + IndexScanDesc sd = (IndexScanDesc) NULL; + HeapScanDesc tgscan = (HeapScanDesc) NULL; + HeapTuple htup; + RetrieveIndexResult indexRes; + Buffer buffer; + Form_pg_trigger pg_trigger; + Oid constr_oid; /* ---------- * Check that only named constraints are set explicitly * ---------- */ - if (strcmp((char *)lfirst(l), "") == 0) + if (strcmp((char *) lfirst(l), "") == 0) elog(ERROR, "unnamed constraints cannot be set explicitly"); /* ---------- @@ -1605,7 +1611,7 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt) (bits16) 0x0, (AttrNumber) 1, (RegProcedure) F_NAMEEQ, - PointerGetDatum((char *)lfirst(l))); + PointerGetDatum((char *) lfirst(l))); if (hasindex) sd = index_beginscan(irel, false, 1, &skey); @@ -1629,9 +1635,7 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt) heap_fetch(tgrel, SnapshotNow, &tuple, &buffer); pfree(indexRes); if (!tuple.t_data) - { continue; - } htup = &tuple; } else @@ -1649,13 +1653,13 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt) */ pg_trigger = (Form_pg_trigger) GETSTRUCT(htup); if (stmt->deferred && !pg_trigger->tgdeferrable && - pg_trigger->tgfoid != F_RI_FKEY_RESTRICT_UPD && - pg_trigger->tgfoid != F_RI_FKEY_RESTRICT_DEL) + pg_trigger->tgfoid != F_RI_FKEY_RESTRICT_UPD && + pg_trigger->tgfoid != F_RI_FKEY_RESTRICT_DEL) elog(ERROR, "Constraint '%s' is not deferrable", - (char *)lfirst(l)); + (char *) lfirst(l)); constr_oid = htup->t_data->t_oid; - loid = lappend(loid, (Node *)constr_oid); + loid = lappend(loid, (Node *) constr_oid); found = true; if (hasindex) @@ -1667,11 +1671,11 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt) * ---------- */ if (!found) - elog(ERROR, "Constraint '%s' does not exist", (char *)lfirst(l)); + elog(ERROR, "Constraint '%s' does not exist", (char *) lfirst(l)); if (hasindex) index_endscan(sd); - else + else heap_endscan(tgscan); } if (hasindex) @@ -1688,10 +1692,10 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt) */ oldcxt = MemoryContextSwitchTo((MemoryContext) deftrig_gcxt); - foreach (l, loid) + foreach(l, loid) { found = false; - foreach (ls, deftrig_dfl_trigstates) + foreach(ls, deftrig_dfl_trigstates) { state = (DeferredTriggerStatus) lfirst(ls); if (state->dts_tgoid == (Oid) lfirst(l)) @@ -1704,19 +1708,21 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt) if (!found) { state = (DeferredTriggerStatus) - palloc(sizeof(DeferredTriggerStatusData)); - state->dts_tgoid = (Oid) lfirst(l); + palloc(sizeof(DeferredTriggerStatusData)); + state->dts_tgoid = (Oid) lfirst(l); state->dts_tgisdeferred = stmt->deferred; deftrig_dfl_trigstates = - lappend(deftrig_dfl_trigstates, state); + lappend(deftrig_dfl_trigstates, state); } } MemoryContextSwitchTo(oldcxt); return; - } else { + } + else + { /* ---------- * Inside of a transaction block set the trigger * states of individual triggers on transaction level. @@ -1724,10 +1730,10 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt) */ oldcxt = MemoryContextSwitchTo((MemoryContext) deftrig_cxt); - foreach (l, loid) + foreach(l, loid) { found = false; - foreach (ls, deftrig_trigstates) + foreach(ls, deftrig_trigstates) { state = (DeferredTriggerStatus) lfirst(ls); if (state->dts_tgoid == (Oid) lfirst(l)) @@ -1740,12 +1746,12 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt) if (!found) { state = (DeferredTriggerStatus) - palloc(sizeof(DeferredTriggerStatusData)); - state->dts_tgoid = (Oid) lfirst(l); + palloc(sizeof(DeferredTriggerStatusData)); + state->dts_tgoid = (Oid) lfirst(l); state->dts_tgisdeferred = stmt->deferred; deftrig_trigstates = - lappend(deftrig_trigstates, state); + lappend(deftrig_trigstates, state); } } @@ -1764,33 +1770,33 @@ DeferredTriggerSetState(ConstraintsSetStmt *stmt) */ void DeferredTriggerSaveEvent(Relation rel, int event, - HeapTuple oldtup, HeapTuple newtup) + HeapTuple oldtup, HeapTuple newtup) { - MemoryContext oldcxt; - DeferredTriggerEvent new_event; - DeferredTriggerEvent prev_event; - int new_size; - int i; - int ntriggers; - Trigger **triggers; - ItemPointerData oldctid; - ItemPointerData newctid; - TriggerData SaveTriggerData; + MemoryContext oldcxt; + DeferredTriggerEvent new_event; + DeferredTriggerEvent prev_event; + int new_size; + int i; + int ntriggers; + Trigger **triggers; + ItemPointerData oldctid; + ItemPointerData newctid; + TriggerData SaveTriggerData; if (deftrig_cxt == NULL) elog(ERROR, - "DeferredTriggerSaveEvent() called outside of transaction"); + "DeferredTriggerSaveEvent() called outside of transaction"); /* ---------- * Check if we're interested in this row at all * ---------- */ if (rel->trigdesc->n_after_row[TRIGGER_EVENT_INSERT] == 0 && - rel->trigdesc->n_after_row[TRIGGER_EVENT_UPDATE] == 0 && - rel->trigdesc->n_after_row[TRIGGER_EVENT_DELETE] == 0 && - rel->trigdesc->n_before_row[TRIGGER_EVENT_INSERT] == 0 && - rel->trigdesc->n_before_row[TRIGGER_EVENT_UPDATE] == 0 && - rel->trigdesc->n_before_row[TRIGGER_EVENT_DELETE] == 0) + rel->trigdesc->n_after_row[TRIGGER_EVENT_UPDATE] == 0 && + rel->trigdesc->n_after_row[TRIGGER_EVENT_DELETE] == 0 && + rel->trigdesc->n_before_row[TRIGGER_EVENT_INSERT] == 0 && + rel->trigdesc->n_before_row[TRIGGER_EVENT_UPDATE] == 0 && + rel->trigdesc->n_before_row[TRIGGER_EVENT_DELETE] == 0) return; /* ---------- @@ -1813,14 +1819,14 @@ DeferredTriggerSaveEvent(Relation rel, int event, oldcxt = MemoryContextSwitchTo((MemoryContext) deftrig_cxt); ntriggers = rel->trigdesc->n_after_row[event]; - triggers = rel->trigdesc->tg_after_row[event]; + triggers = rel->trigdesc->tg_after_row[event]; - new_size = sizeof(DeferredTriggerEventData) + - ntriggers * sizeof(DeferredTriggerEventItem); + new_size = sizeof(DeferredTriggerEventData) + + ntriggers * sizeof(DeferredTriggerEventItem); new_event = (DeferredTriggerEvent) palloc(new_size); - new_event->dte_event = event & TRIGGER_EVENT_OPMASK; - new_event->dte_relid = rel->rd_id; + new_event->dte_event = event & TRIGGER_EVENT_OPMASK; + new_event->dte_relid = rel->rd_id; ItemPointerCopy(&oldctid, &(new_event->dte_oldctid)); ItemPointerCopy(&newctid, &(new_event->dte_newctid)); new_event->dte_n_items = ntriggers; @@ -1830,11 +1836,11 @@ DeferredTriggerSaveEvent(Relation rel, int event, new_event->dte_item[i].dti_tgoid = triggers[i]->tgoid; new_event->dte_item[i].dti_state = ((triggers[i]->tgdeferrable) ? - TRIGGER_DEFERRED_DEFERRABLE : 0) | + TRIGGER_DEFERRED_DEFERRABLE : 0) | ((triggers[i]->tginitdeferred) ? - TRIGGER_DEFERRED_INITDEFERRED : 0) | + TRIGGER_DEFERRED_INITDEFERRED : 0) | ((rel->trigdesc->n_before_row[event] > 0) ? - TRIGGER_DEFERRED_HAS_BEFORE : 0); + TRIGGER_DEFERRED_HAS_BEFORE : 0); } MemoryContextSwitchTo(oldcxt); @@ -1864,8 +1870,8 @@ DeferredTriggerSaveEvent(Relation rel, int event, */ for (i = 0; i < ntriggers; i++) { - bool is_ri_trigger; - bool key_unchanged; + bool is_ri_trigger; + bool key_unchanged; /* ---------- * We are interested in RI_FKEY triggers only. @@ -1888,11 +1894,11 @@ DeferredTriggerSaveEvent(Relation rel, int event, if (!is_ri_trigger) continue; - SaveTriggerData.tg_event = TRIGGER_EVENT_UPDATE; - SaveTriggerData.tg_relation = rel; + SaveTriggerData.tg_event = TRIGGER_EVENT_UPDATE; + SaveTriggerData.tg_relation = rel; SaveTriggerData.tg_trigtuple = oldtup; - SaveTriggerData.tg_newtuple = newtup; - SaveTriggerData.tg_trigger = triggers[i]; + SaveTriggerData.tg_newtuple = newtup; + SaveTriggerData.tg_trigger = triggers[i]; CurrentTriggerData = &SaveTriggerData; key_unchanged = RI_FKey_keyequal_upd(); @@ -1911,7 +1917,7 @@ DeferredTriggerSaveEvent(Relation rel, int event, if (prev_event) { if (prev_event->dte_event & - TRIGGER_DEFERRED_ROW_INSERTED) + TRIGGER_DEFERRED_ROW_INSERTED) { /* ---------- * This is a row inserted during our transaction. @@ -1919,11 +1925,11 @@ DeferredTriggerSaveEvent(Relation rel, int event, * ---------- */ new_event->dte_event |= - TRIGGER_DEFERRED_ROW_INSERTED; + TRIGGER_DEFERRED_ROW_INSERTED; new_event->dte_event |= - TRIGGER_DEFERRED_KEY_CHANGED; + TRIGGER_DEFERRED_KEY_CHANGED; new_event->dte_item[i].dti_state |= - TRIGGER_DEFERRED_KEY_CHANGED; + TRIGGER_DEFERRED_KEY_CHANGED; } else { @@ -1934,12 +1940,12 @@ DeferredTriggerSaveEvent(Relation rel, int event, * ---------- */ if (prev_event->dte_item[i].dti_state & - TRIGGER_DEFERRED_KEY_CHANGED) + TRIGGER_DEFERRED_KEY_CHANGED) { new_event->dte_item[i].dti_state |= - TRIGGER_DEFERRED_KEY_CHANGED; + TRIGGER_DEFERRED_KEY_CHANGED; new_event->dte_event |= - TRIGGER_DEFERRED_KEY_CHANGED; + TRIGGER_DEFERRED_KEY_CHANGED; } } } @@ -1954,16 +1960,16 @@ DeferredTriggerSaveEvent(Relation rel, int event, if (prev_event) { if (prev_event->dte_event & - TRIGGER_DEFERRED_ROW_INSERTED) + TRIGGER_DEFERRED_ROW_INSERTED) elog(ERROR, "triggered data change violation " - "on relation \"%s\"", - nameout(&(rel->rd_rel->relname))); + "on relation \"%s\"", + nameout(&(rel->rd_rel->relname))); if (prev_event->dte_item[i].dti_state & - TRIGGER_DEFERRED_KEY_CHANGED) + TRIGGER_DEFERRED_KEY_CHANGED) elog(ERROR, "triggered data change violation " - "on relation \"%s\"", - nameout(&(rel->rd_rel->relname))); + "on relation \"%s\"", + nameout(&(rel->rd_rel->relname))); } /* ---------- @@ -1972,7 +1978,7 @@ DeferredTriggerSaveEvent(Relation rel, int event, * ---------- */ new_event->dte_item[i].dti_state |= - TRIGGER_DEFERRED_KEY_CHANGED; + TRIGGER_DEFERRED_KEY_CHANGED; new_event->dte_event |= TRIGGER_DEFERRED_KEY_CHANGED; } } @@ -1996,8 +2002,8 @@ DeferredTriggerSaveEvent(Relation rel, int event, prev_event = deferredTriggerGetPreviousEvent(rel->rd_id, &oldctid); if (prev_event->dte_event & TRIGGER_DEFERRED_KEY_CHANGED) elog(ERROR, "triggered data change violation " - "on relation \"%s\"", - nameout(&(rel->rd_rel->relname))); + "on relation \"%s\"", + nameout(&(rel->rd_rel->relname))); break; } @@ -2012,5 +2018,3 @@ DeferredTriggerSaveEvent(Relation rel, int event, return; } - - diff --git a/src/backend/commands/user.c b/src/backend/commands/user.c index 63676e14e48..fe24e15b1fd 100644 --- a/src/backend/commands/user.c +++ b/src/backend/commands/user.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: user.c,v 1.51 2000/03/15 07:02:56 tgl Exp $ + * $Id: user.c,v 1.52 2000/04/12 17:14:59 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -55,11 +55,11 @@ write_password_file(Relation rel) char *filename, *tempname; int bufsize; - FILE *fp; - mode_t oumask; - HeapScanDesc scan; - HeapTuple tuple; - TupleDesc dsc = RelationGetDescr(rel); + FILE *fp; + mode_t oumask; + HeapScanDesc scan; + HeapTuple tuple; + TupleDesc dsc = RelationGetDescr(rel); /* * Create a temporary filename to be renamed later. This prevents the @@ -71,74 +71,85 @@ write_password_file(Relation rel) tempname = (char *) palloc(bufsize); snprintf(tempname, bufsize, "%s.%d", filename, MyProcPid); - oumask = umask((mode_t) 077); - fp = AllocateFile(tempname, "w"); - umask(oumask); - if (fp == NULL) - elog(ERROR, "%s: %s", tempname, strerror(errno)); - - /* read table */ - scan = heap_beginscan(rel, false, SnapshotSelf, 0, NULL); - while (HeapTupleIsValid(tuple = heap_getnext(scan, 0))) - { - Datum datum_n, datum_p, datum_v; - bool null_n, null_p, null_v; + oumask = umask((mode_t) 077); + fp = AllocateFile(tempname, "w"); + umask(oumask); + if (fp == NULL) + elog(ERROR, "%s: %s", tempname, strerror(errno)); + + /* read table */ + scan = heap_beginscan(rel, false, SnapshotSelf, 0, NULL); + while (HeapTupleIsValid(tuple = heap_getnext(scan, 0))) + { + Datum datum_n, + datum_p, + datum_v; + bool null_n, + null_p, + null_v; datum_n = heap_getattr(tuple, Anum_pg_shadow_usename, dsc, &null_n); - if (null_n) - continue; /* don't allow empty users */ + if (null_n) + continue; /* don't allow empty users */ datum_p = heap_getattr(tuple, Anum_pg_shadow_passwd, dsc, &null_p); - /* It could be argued that people having a null password - shouldn't be allowed to connect, because they need - to have a password set up first. If you think assuming - an empty password in that case is better, erase the following line. */ - if (null_p) - continue; + + /* + * It could be argued that people having a null password shouldn't + * be allowed to connect, because they need to have a password set + * up first. If you think assuming an empty password in that case + * is better, erase the following line. + */ + if (null_p) + continue; datum_v = heap_getattr(tuple, Anum_pg_shadow_valuntil, dsc, &null_v); - /* These fake entries are not really necessary. To remove them, the parser - in backend/libpq/crypt.c would need to be adjusted. Initdb might also - need adjustments. */ - fprintf(fp, - "%s" - CRYPT_PWD_FILE_SEPSTR - "0" - CRYPT_PWD_FILE_SEPSTR - "x" - CRYPT_PWD_FILE_SEPSTR - "x" - CRYPT_PWD_FILE_SEPSTR - "x" - CRYPT_PWD_FILE_SEPSTR - "x" - CRYPT_PWD_FILE_SEPSTR - "%s" - CRYPT_PWD_FILE_SEPSTR - "%s\n", - nameout(DatumGetName(datum_n)), - null_p ? "" : textout((text*)datum_p), - null_v ? "\\N" : nabstimeout((AbsoluteTime)datum_v) /* this is how the parser wants it */ - ); - if (ferror(fp)) - elog(ERROR, "%s: %s", tempname, strerror(errno)); - fflush(fp); - } - heap_endscan(scan); - FreeFile(fp); - - /* - * And rename the temp file to its final name, deleting the old pg_pwd. - */ - rename(tempname, filename); - - /* + /* + * These fake entries are not really necessary. To remove them, + * the parser in backend/libpq/crypt.c would need to be adjusted. + * Initdb might also need adjustments. + */ + fprintf(fp, + "%s" + CRYPT_PWD_FILE_SEPSTR + "0" + CRYPT_PWD_FILE_SEPSTR + "x" + CRYPT_PWD_FILE_SEPSTR + "x" + CRYPT_PWD_FILE_SEPSTR + "x" + CRYPT_PWD_FILE_SEPSTR + "x" + CRYPT_PWD_FILE_SEPSTR + "%s" + CRYPT_PWD_FILE_SEPSTR + "%s\n", + nameout(DatumGetName(datum_n)), + null_p ? "" : textout((text *) datum_p), + null_v ? "\\N" : nabstimeout((AbsoluteTime) datum_v) /* this is how the + * parser wants it */ + ); + if (ferror(fp)) + elog(ERROR, "%s: %s", tempname, strerror(errno)); + fflush(fp); + } + heap_endscan(scan); + FreeFile(fp); + + /* + * And rename the temp file to its final name, deleting the old + * pg_pwd. + */ + rename(tempname, filename); + + /* * Create a flag file the postmaster will detect the next time it * tries to authenticate a user. The postmaster will know to reload * the pg_pwd file contents. */ filename = crypt_getpwdreloadfilename(); if (creat(filename, S_IRUSR | S_IWUSR) == -1) - elog(ERROR, "%s: %s", filename, strerror(errno)); + elog(ERROR, "%s: %s", filename, strerror(errno)); pfree((void *) tempname); } @@ -149,16 +160,17 @@ write_password_file(Relation rel) HeapTuple update_pg_pwd(void) { - Relation rel = heap_openr(ShadowRelationName, AccessExclusiveLock); - write_password_file(rel); - heap_close(rel, AccessExclusiveLock); + Relation rel = heap_openr(ShadowRelationName, AccessExclusiveLock); + + write_password_file(rel); + heap_close(rel, AccessExclusiveLock); /* - * This is a trigger, so clean out the information provided by - * the trigger manager. + * This is a trigger, so clean out the information provided by the + * trigger manager. */ CurrentTriggerData = NULL; - return NULL; + return NULL; } @@ -173,36 +185,37 @@ CreateUser(CreateUserStmt *stmt) TupleDesc pg_shadow_dsc; HeapScanDesc scan; HeapTuple tuple; - Datum new_record[Natts_pg_shadow]; - char new_record_nulls[Natts_pg_shadow]; + Datum new_record[Natts_pg_shadow]; + char new_record_nulls[Natts_pg_shadow]; bool user_exists = false, - sysid_exists = false, - havesysid; + sysid_exists = false, + havesysid; int max_id = -1; - List *item; + List *item; - havesysid = stmt->sysid > 0; + havesysid = stmt->sysid > 0; - /* Check some permissions first */ + /* Check some permissions first */ if (stmt->password) CheckPgUserAclNotNull(); - if (!superuser()) - elog(ERROR, "CREATE USER: permission denied"); + if (!superuser()) + elog(ERROR, "CREATE USER: permission denied"); - /* The reason for the following is this: - * If you start a transaction block, create a user, then roll back the - * transaction, the pg_pwd won't get rolled back due to a bug in the - * Unix file system ( :}). Hence this is in the interest of security. - */ + /* + * The reason for the following is this: If you start a transaction + * block, create a user, then roll back the transaction, the pg_pwd + * won't get rolled back due to a bug in the Unix file system ( :}). + * Hence this is in the interest of security. + */ if (IsTransactionBlock()) - elog(ERROR, "CREATE USER: may not be called in a transaction block"); + elog(ERROR, "CREATE USER: may not be called in a transaction block"); /* - * Scan the pg_shadow relation to be certain the user or id doesn't already - * exist. Note we secure exclusive lock, because we also need to be - * sure of what the next usesysid should be, and we need to protect - * our update of the flat password file. + * Scan the pg_shadow relation to be certain the user or id doesn't + * already exist. Note we secure exclusive lock, because we also need + * to be sure of what the next usesysid should be, and we need to + * protect our update of the flat password file. */ pg_shadow_rel = heap_openr(ShadowRelationName, AccessExclusiveLock); pg_shadow_dsc = RelationGetDescr(pg_shadow_rel); @@ -210,103 +223,108 @@ CreateUser(CreateUserStmt *stmt) scan = heap_beginscan(pg_shadow_rel, false, SnapshotNow, 0, NULL); while (!user_exists && !sysid_exists && HeapTupleIsValid(tuple = heap_getnext(scan, 0))) { - Datum datum; - bool null; + Datum datum; + bool null; datum = heap_getattr(tuple, Anum_pg_shadow_usename, pg_shadow_dsc, &null); user_exists = datum && !null && (strcmp((char *) datum, stmt->user) == 0); datum = heap_getattr(tuple, Anum_pg_shadow_usesysid, pg_shadow_dsc, &null); - if (havesysid) /* customized id wanted */ - sysid_exists = datum && !null && ((int)datum == stmt->sysid); - else /* pick 1 + max */ - { - if ((int) datum > max_id) - max_id = (int) datum; - } + if (havesysid) /* customized id wanted */ + sysid_exists = datum && !null && ((int) datum == stmt->sysid); + else +/* pick 1 + max */ + { + if ((int) datum > max_id) + max_id = (int) datum; + } } heap_endscan(scan); if (user_exists || sysid_exists) { heap_close(pg_shadow_rel, AccessExclusiveLock); - if (user_exists) - elog(ERROR, "CREATE USER: user name \"%s\" already exists", stmt->user); - else - elog(ERROR, "CREATE USER: sysid %d is already assigned", stmt->sysid); + if (user_exists) + elog(ERROR, "CREATE USER: user name \"%s\" already exists", stmt->user); + else + elog(ERROR, "CREATE USER: sysid %d is already assigned", stmt->sysid); return; } - /* - * Build a tuple to insert - */ - new_record[Anum_pg_shadow_usename-1] = PointerGetDatum(namein(stmt->user)); /* this truncated properly */ - new_record[Anum_pg_shadow_usesysid-1] = Int32GetDatum(havesysid ? stmt->sysid : max_id + 1); - - AssertState(BoolIsValid(stmt->createdb)); - new_record[Anum_pg_shadow_usecreatedb-1] = (Datum)(stmt->createdb); - new_record[Anum_pg_shadow_usetrace-1] = (Datum)(false); - AssertState(BoolIsValid(stmt->createuser)); - new_record[Anum_pg_shadow_usesuper-1] = (Datum)(stmt->createuser); - /* superuser gets catupd right by default */ - new_record[Anum_pg_shadow_usecatupd-1] = (Datum)(stmt->createuser); - - if (stmt->password) - new_record[Anum_pg_shadow_passwd-1] = PointerGetDatum(textin(stmt->password)); - if (stmt->validUntil) - new_record[Anum_pg_shadow_valuntil-1] = PointerGetDatum(nabstimein(stmt->validUntil)); - - new_record_nulls[Anum_pg_shadow_usename-1] = ' '; - new_record_nulls[Anum_pg_shadow_usesysid-1] = ' '; - - new_record_nulls[Anum_pg_shadow_usecreatedb-1] = ' '; - new_record_nulls[Anum_pg_shadow_usetrace-1] = ' '; - new_record_nulls[Anum_pg_shadow_usesuper-1] = ' '; - new_record_nulls[Anum_pg_shadow_usecatupd-1] = ' '; - - new_record_nulls[Anum_pg_shadow_passwd-1] = stmt->password ? ' ' : 'n'; - new_record_nulls[Anum_pg_shadow_valuntil-1] = stmt->validUntil ? ' ' : 'n'; - - tuple = heap_formtuple(pg_shadow_dsc, new_record, new_record_nulls); - Assert(tuple); - - /* - * Insert a new record in the pg_shadow table - */ - if (heap_insert(pg_shadow_rel, tuple) == InvalidOid) - elog(ERROR, "CREATE USER: heap_insert failed"); - - /* - * Update indexes - */ - if (RelationGetForm(pg_shadow_rel)->relhasindex) { - Relation idescs[Num_pg_shadow_indices]; - - CatalogOpenIndices(Num_pg_shadow_indices, - Name_pg_shadow_indices, idescs); - CatalogIndexInsert(idescs, Num_pg_shadow_indices, pg_shadow_rel, - tuple); - CatalogCloseIndices(Num_pg_shadow_indices, idescs); - } + /* + * Build a tuple to insert + */ + new_record[Anum_pg_shadow_usename - 1] = PointerGetDatum(namein(stmt->user)); /* this truncated + * properly */ + new_record[Anum_pg_shadow_usesysid - 1] = Int32GetDatum(havesysid ? stmt->sysid : max_id + 1); + + AssertState(BoolIsValid(stmt->createdb)); + new_record[Anum_pg_shadow_usecreatedb - 1] = (Datum) (stmt->createdb); + new_record[Anum_pg_shadow_usetrace - 1] = (Datum) (false); + AssertState(BoolIsValid(stmt->createuser)); + new_record[Anum_pg_shadow_usesuper - 1] = (Datum) (stmt->createuser); + /* superuser gets catupd right by default */ + new_record[Anum_pg_shadow_usecatupd - 1] = (Datum) (stmt->createuser); + + if (stmt->password) + new_record[Anum_pg_shadow_passwd - 1] = PointerGetDatum(textin(stmt->password)); + if (stmt->validUntil) + new_record[Anum_pg_shadow_valuntil - 1] = PointerGetDatum(nabstimein(stmt->validUntil)); + + new_record_nulls[Anum_pg_shadow_usename - 1] = ' '; + new_record_nulls[Anum_pg_shadow_usesysid - 1] = ' '; + + new_record_nulls[Anum_pg_shadow_usecreatedb - 1] = ' '; + new_record_nulls[Anum_pg_shadow_usetrace - 1] = ' '; + new_record_nulls[Anum_pg_shadow_usesuper - 1] = ' '; + new_record_nulls[Anum_pg_shadow_usecatupd - 1] = ' '; + + new_record_nulls[Anum_pg_shadow_passwd - 1] = stmt->password ? ' ' : 'n'; + new_record_nulls[Anum_pg_shadow_valuntil - 1] = stmt->validUntil ? ' ' : 'n'; + + tuple = heap_formtuple(pg_shadow_dsc, new_record, new_record_nulls); + Assert(tuple); + + /* + * Insert a new record in the pg_shadow table + */ + if (heap_insert(pg_shadow_rel, tuple) == InvalidOid) + elog(ERROR, "CREATE USER: heap_insert failed"); + + /* + * Update indexes + */ + if (RelationGetForm(pg_shadow_rel)->relhasindex) + { + Relation idescs[Num_pg_shadow_indices]; + + CatalogOpenIndices(Num_pg_shadow_indices, + Name_pg_shadow_indices, idescs); + CatalogIndexInsert(idescs, Num_pg_shadow_indices, pg_shadow_rel, + tuple); + CatalogCloseIndices(Num_pg_shadow_indices, idescs); + } /* * Add the user to the groups specified. We'll just call the below - * AlterGroup for this. + * AlterGroup for this. */ - foreach(item, stmt->groupElts) - { - AlterGroupStmt ags; + foreach(item, stmt->groupElts) + { + AlterGroupStmt ags; - ags.name = strVal(lfirst(item)); /* the group name to add this in */ - ags.action = +1; - ags.listUsers = lcons((void*)makeInteger(havesysid ? stmt->sysid : max_id + 1), NIL); - AlterGroup(&ags, "CREATE USER"); - } + ags.name = strVal(lfirst(item)); /* the group name to add + * this in */ + ags.action = +1; + ags.listUsers = lcons((void *) makeInteger(havesysid ? stmt->sysid : max_id + 1), NIL); + AlterGroup(&ags, "CREATE USER"); + } /* * Write the updated pg_shadow data to the flat password file. */ - write_password_file(pg_shadow_rel); + write_password_file(pg_shadow_rel); + /* * Now we can clean up. */ @@ -321,30 +339,31 @@ CreateUser(CreateUserStmt *stmt) extern void AlterUser(AlterUserStmt *stmt) { - Datum new_record[Natts_pg_shadow]; - char new_record_nulls[Natts_pg_shadow]; + Datum new_record[Natts_pg_shadow]; + char new_record_nulls[Natts_pg_shadow]; Relation pg_shadow_rel; TupleDesc pg_shadow_dsc; - HeapTuple tuple, new_tuple; - bool null; + HeapTuple tuple, + new_tuple; + bool null; if (stmt->password) CheckPgUserAclNotNull(); - /* must be superuser or just want to change your own password */ - if (!superuser() && - !(stmt->createdb==0 && stmt->createuser==0 && !stmt->validUntil - && stmt->password && strcmp(GetPgUserName(), stmt->user)==0)) - elog(ERROR, "ALTER USER: permission denied"); + /* must be superuser or just want to change your own password */ + if (!superuser() && + !(stmt->createdb == 0 && stmt->createuser == 0 && !stmt->validUntil + && stmt->password && strcmp(GetPgUserName(), stmt->user) == 0)) + elog(ERROR, "ALTER USER: permission denied"); - /* see comments in create user */ + /* see comments in create user */ if (IsTransactionBlock()) - elog(ERROR, "ALTER USER: may not be called in a transaction block"); + elog(ERROR, "ALTER USER: may not be called in a transaction block"); /* - * Scan the pg_shadow relation to be certain the user exists. - * Note we secure exclusive lock to protect our update of the - * flat password file. + * Scan the pg_shadow relation to be certain the user exists. Note we + * secure exclusive lock to protect our update of the flat password + * file. */ pg_shadow_rel = heap_openr(ShadowRelationName, AccessExclusiveLock); pg_shadow_dsc = RelationGetDescr(pg_shadow_rel); @@ -358,107 +377,107 @@ AlterUser(AlterUserStmt *stmt) elog(ERROR, "ALTER USER: user \"%s\" does not exist", stmt->user); } - /* - * Build a tuple to update, perusing the information just obtained - */ - new_record[Anum_pg_shadow_usename-1] = PointerGetDatum(namein(stmt->user)); - new_record_nulls[Anum_pg_shadow_usename-1] = ' '; - - /* sysid - leave as is */ - new_record[Anum_pg_shadow_usesysid-1] = heap_getattr(tuple, Anum_pg_shadow_usesysid, pg_shadow_dsc, &null); - new_record_nulls[Anum_pg_shadow_usesysid-1] = null ? 'n' : ' '; - - /* createdb */ - if (stmt->createdb == 0) - { - /* don't change */ - new_record[Anum_pg_shadow_usecreatedb-1] = heap_getattr(tuple, Anum_pg_shadow_usecreatedb, pg_shadow_dsc, &null); - new_record_nulls[Anum_pg_shadow_usecreatedb-1] = null ? 'n' : ' '; - } - else - { - new_record[Anum_pg_shadow_usecreatedb-1] = (Datum)(stmt->createdb > 0 ? true : false); - new_record_nulls[Anum_pg_shadow_usecreatedb-1] = ' '; - } - - /* trace - leave as is */ - new_record[Anum_pg_shadow_usetrace-1] = heap_getattr(tuple, Anum_pg_shadow_usetrace, pg_shadow_dsc, &null); - new_record_nulls[Anum_pg_shadow_usetrace-1] = null ? 'n' : ' '; - - /* createuser (superuser) */ - if (stmt->createuser == 0) - { - /* don't change */ - new_record[Anum_pg_shadow_usesuper-1] = heap_getattr(tuple, Anum_pg_shadow_usesuper, pg_shadow_dsc, &null); - new_record_nulls[Anum_pg_shadow_usesuper-1] = null ? 'n' : ' '; - } - else - { - new_record[Anum_pg_shadow_usesuper-1] = (Datum)(stmt->createuser > 0 ? true : false); - new_record_nulls[Anum_pg_shadow_usesuper-1] = ' '; - } - - /* catupd - set to false if someone's superuser priv is being yanked */ - if (stmt->createuser < 0) - { - new_record[Anum_pg_shadow_usecatupd-1] = (Datum)(false); - new_record_nulls[Anum_pg_shadow_usecatupd-1] = ' '; - } - else - { - /* leave alone */ - new_record[Anum_pg_shadow_usecatupd-1] = heap_getattr(tuple, Anum_pg_shadow_usecatupd, pg_shadow_dsc, &null); - new_record_nulls[Anum_pg_shadow_usecatupd-1] = null ? 'n' : ' '; - } - - /* password */ - if (stmt->password) - { - new_record[Anum_pg_shadow_passwd-1] = PointerGetDatum(textin(stmt->password)); - new_record_nulls[Anum_pg_shadow_passwd-1] = ' '; - } - else - { - /* leave as is */ - new_record[Anum_pg_shadow_passwd-1] = heap_getattr(tuple, Anum_pg_shadow_passwd, pg_shadow_dsc, &null); - new_record_nulls[Anum_pg_shadow_passwd-1] = null ? 'n' : ' '; - } - - /* valid until */ - if (stmt->validUntil) - { - new_record[Anum_pg_shadow_valuntil-1] = PointerGetDatum(nabstimein(stmt->validUntil)); - new_record_nulls[Anum_pg_shadow_valuntil-1] = ' '; - } - else - { - /* leave as is */ - new_record[Anum_pg_shadow_valuntil-1] = heap_getattr(tuple, Anum_pg_shadow_valuntil, pg_shadow_dsc, &null); - new_record_nulls[Anum_pg_shadow_valuntil-1] = null ? 'n' : ' '; - } - - new_tuple = heap_formtuple(pg_shadow_dsc, new_record, new_record_nulls); - Assert(new_tuple); - /* XXX check return value of this? */ - heap_update(pg_shadow_rel, &tuple->t_self, new_tuple, NULL); - - - /* Update indexes */ - if (RelationGetForm(pg_shadow_rel)->relhasindex) - { - Relation idescs[Num_pg_shadow_indices]; - - CatalogOpenIndices(Num_pg_shadow_indices, - Name_pg_shadow_indices, idescs); - CatalogIndexInsert(idescs, Num_pg_shadow_indices, pg_shadow_rel, - tuple); - CatalogCloseIndices(Num_pg_shadow_indices, idescs); - } + /* + * Build a tuple to update, perusing the information just obtained + */ + new_record[Anum_pg_shadow_usename - 1] = PointerGetDatum(namein(stmt->user)); + new_record_nulls[Anum_pg_shadow_usename - 1] = ' '; + + /* sysid - leave as is */ + new_record[Anum_pg_shadow_usesysid - 1] = heap_getattr(tuple, Anum_pg_shadow_usesysid, pg_shadow_dsc, &null); + new_record_nulls[Anum_pg_shadow_usesysid - 1] = null ? 'n' : ' '; + + /* createdb */ + if (stmt->createdb == 0) + { + /* don't change */ + new_record[Anum_pg_shadow_usecreatedb - 1] = heap_getattr(tuple, Anum_pg_shadow_usecreatedb, pg_shadow_dsc, &null); + new_record_nulls[Anum_pg_shadow_usecreatedb - 1] = null ? 'n' : ' '; + } + else + { + new_record[Anum_pg_shadow_usecreatedb - 1] = (Datum) (stmt->createdb > 0 ? true : false); + new_record_nulls[Anum_pg_shadow_usecreatedb - 1] = ' '; + } + + /* trace - leave as is */ + new_record[Anum_pg_shadow_usetrace - 1] = heap_getattr(tuple, Anum_pg_shadow_usetrace, pg_shadow_dsc, &null); + new_record_nulls[Anum_pg_shadow_usetrace - 1] = null ? 'n' : ' '; + + /* createuser (superuser) */ + if (stmt->createuser == 0) + { + /* don't change */ + new_record[Anum_pg_shadow_usesuper - 1] = heap_getattr(tuple, Anum_pg_shadow_usesuper, pg_shadow_dsc, &null); + new_record_nulls[Anum_pg_shadow_usesuper - 1] = null ? 'n' : ' '; + } + else + { + new_record[Anum_pg_shadow_usesuper - 1] = (Datum) (stmt->createuser > 0 ? true : false); + new_record_nulls[Anum_pg_shadow_usesuper - 1] = ' '; + } + + /* catupd - set to false if someone's superuser priv is being yanked */ + if (stmt->createuser < 0) + { + new_record[Anum_pg_shadow_usecatupd - 1] = (Datum) (false); + new_record_nulls[Anum_pg_shadow_usecatupd - 1] = ' '; + } + else + { + /* leave alone */ + new_record[Anum_pg_shadow_usecatupd - 1] = heap_getattr(tuple, Anum_pg_shadow_usecatupd, pg_shadow_dsc, &null); + new_record_nulls[Anum_pg_shadow_usecatupd - 1] = null ? 'n' : ' '; + } + + /* password */ + if (stmt->password) + { + new_record[Anum_pg_shadow_passwd - 1] = PointerGetDatum(textin(stmt->password)); + new_record_nulls[Anum_pg_shadow_passwd - 1] = ' '; + } + else + { + /* leave as is */ + new_record[Anum_pg_shadow_passwd - 1] = heap_getattr(tuple, Anum_pg_shadow_passwd, pg_shadow_dsc, &null); + new_record_nulls[Anum_pg_shadow_passwd - 1] = null ? 'n' : ' '; + } + + /* valid until */ + if (stmt->validUntil) + { + new_record[Anum_pg_shadow_valuntil - 1] = PointerGetDatum(nabstimein(stmt->validUntil)); + new_record_nulls[Anum_pg_shadow_valuntil - 1] = ' '; + } + else + { + /* leave as is */ + new_record[Anum_pg_shadow_valuntil - 1] = heap_getattr(tuple, Anum_pg_shadow_valuntil, pg_shadow_dsc, &null); + new_record_nulls[Anum_pg_shadow_valuntil - 1] = null ? 'n' : ' '; + } + + new_tuple = heap_formtuple(pg_shadow_dsc, new_record, new_record_nulls); + Assert(new_tuple); + /* XXX check return value of this? */ + heap_update(pg_shadow_rel, &tuple->t_self, new_tuple, NULL); + + + /* Update indexes */ + if (RelationGetForm(pg_shadow_rel)->relhasindex) + { + Relation idescs[Num_pg_shadow_indices]; + + CatalogOpenIndices(Num_pg_shadow_indices, + Name_pg_shadow_indices, idescs); + CatalogIndexInsert(idescs, Num_pg_shadow_indices, pg_shadow_rel, + tuple); + CatalogCloseIndices(Num_pg_shadow_indices, idescs); + } /* * Write the updated pg_shadow data to the flat password file. */ - write_password_file(pg_shadow_rel); + write_password_file(pg_shadow_rel); /* * Now we can clean up. @@ -477,13 +496,13 @@ DropUser(DropUserStmt *stmt) { Relation pg_shadow_rel; TupleDesc pg_shadow_dsc; - List *item; + List *item; - if (!superuser()) - elog(ERROR, "DROP USER: permission denied"); + if (!superuser()) + elog(ERROR, "DROP USER: permission denied"); if (IsTransactionBlock()) - elog(ERROR, "DROP USER: may not be called in a transaction block"); + elog(ERROR, "DROP USER: may not be called in a transaction block"); /* * Scan the pg_shadow relation to find the usesysid of the user to be @@ -493,103 +512,105 @@ DropUser(DropUserStmt *stmt) pg_shadow_rel = heap_openr(ShadowRelationName, AccessExclusiveLock); pg_shadow_dsc = RelationGetDescr(pg_shadow_rel); - foreach(item, stmt->users) - { - HeapTuple tuple, - tmp_tuple; - Relation pg_rel; - TupleDesc pg_dsc; - ScanKeyData scankey; - HeapScanDesc scan; - Datum datum; - bool null; - int32 usesysid; - const char *user = strVal(lfirst(item)); - - tuple = SearchSysCacheTuple(SHADOWNAME, - PointerGetDatum(user), - 0, 0, 0); - if (!HeapTupleIsValid(tuple)) - { - heap_close(pg_shadow_rel, AccessExclusiveLock); - elog(ERROR, "DROP USER: user \"%s\" does not exist%s", user, - (length(stmt->users) > 1) ? " (no users removed)" : ""); - } - - usesysid = DatumGetInt32(heap_getattr(tuple, Anum_pg_shadow_usesysid, pg_shadow_dsc, &null)); - - /*------------------- - * Check if user still owns a database. If so, error out. - * - * (It used to be that this function would drop the database automatically. - * This is not only very dangerous for people that don't read the manual, - * it doesn't seem to be the behaviour one would expect either.) - * -- petere 2000/01/14) - *-------------------*/ - pg_rel = heap_openr(DatabaseRelationName, AccessExclusiveLock); - pg_dsc = RelationGetDescr(pg_rel); - - ScanKeyEntryInitialize(&scankey, 0x0, Anum_pg_database_datdba, F_INT4EQ, - Int32GetDatum(usesysid)); - - scan = heap_beginscan(pg_rel, false, SnapshotNow, 1, &scankey); - - if (HeapTupleIsValid(tmp_tuple = heap_getnext(scan, 0))) - { - datum = heap_getattr(tmp_tuple, Anum_pg_database_datname, pg_dsc, &null); - heap_close(pg_shadow_rel, AccessExclusiveLock); - elog(ERROR, "DROP USER: user \"%s\" owns database \"%s\", cannot be removed%s", - user, nameout(DatumGetName(datum)), - (length(stmt->users) > 1) ? " (no users removed)" : "" - ); - } - - heap_endscan(scan); - heap_close(pg_rel, AccessExclusiveLock); - - /* - * Somehow we'd have to check for tables, views, etc. owned by the user - * as well, but those could be spread out over all sorts of databases - * which we don't have access to (easily). - */ - - /* - * Remove the user from the pg_shadow table - */ - heap_delete(pg_shadow_rel, &tuple->t_self, NULL); - - /* - * Remove user from groups - * - * try calling alter group drop user for every group - */ - pg_rel = heap_openr(GroupRelationName, AccessExclusiveLock); - pg_dsc = RelationGetDescr(pg_rel); - scan = heap_beginscan(pg_rel, false, SnapshotNow, 0, NULL); - while (HeapTupleIsValid(tmp_tuple = heap_getnext(scan, 0))) - { - AlterGroupStmt ags; - - datum = heap_getattr(tmp_tuple, Anum_pg_group_groname, pg_dsc, &null); - - ags.name = nameout(DatumGetName(datum)); /* the group name from which to try to drop the user */ - ags.action = -1; - ags.listUsers = lcons((void*)makeInteger(usesysid), NIL); - AlterGroup(&ags, "DROP USER"); - } - heap_endscan(scan); - heap_close(pg_rel, AccessExclusiveLock); - } + foreach(item, stmt->users) + { + HeapTuple tuple, + tmp_tuple; + Relation pg_rel; + TupleDesc pg_dsc; + ScanKeyData scankey; + HeapScanDesc scan; + Datum datum; + bool null; + int32 usesysid; + const char *user = strVal(lfirst(item)); + + tuple = SearchSysCacheTuple(SHADOWNAME, + PointerGetDatum(user), + 0, 0, 0); + if (!HeapTupleIsValid(tuple)) + { + heap_close(pg_shadow_rel, AccessExclusiveLock); + elog(ERROR, "DROP USER: user \"%s\" does not exist%s", user, + (length(stmt->users) > 1) ? " (no users removed)" : ""); + } + + usesysid = DatumGetInt32(heap_getattr(tuple, Anum_pg_shadow_usesysid, pg_shadow_dsc, &null)); + + /*------------------- + * Check if user still owns a database. If so, error out. + * + * (It used to be that this function would drop the database automatically. + * This is not only very dangerous for people that don't read the manual, + * it doesn't seem to be the behaviour one would expect either.) + * -- petere 2000/01/14) + *-------------------*/ + pg_rel = heap_openr(DatabaseRelationName, AccessExclusiveLock); + pg_dsc = RelationGetDescr(pg_rel); + + ScanKeyEntryInitialize(&scankey, 0x0, Anum_pg_database_datdba, F_INT4EQ, + Int32GetDatum(usesysid)); + + scan = heap_beginscan(pg_rel, false, SnapshotNow, 1, &scankey); + + if (HeapTupleIsValid(tmp_tuple = heap_getnext(scan, 0))) + { + datum = heap_getattr(tmp_tuple, Anum_pg_database_datname, pg_dsc, &null); + heap_close(pg_shadow_rel, AccessExclusiveLock); + elog(ERROR, "DROP USER: user \"%s\" owns database \"%s\", cannot be removed%s", + user, nameout(DatumGetName(datum)), + (length(stmt->users) > 1) ? " (no users removed)" : "" + ); + } + + heap_endscan(scan); + heap_close(pg_rel, AccessExclusiveLock); + + /* + * Somehow we'd have to check for tables, views, etc. owned by the + * user as well, but those could be spread out over all sorts of + * databases which we don't have access to (easily). + */ + + /* + * Remove the user from the pg_shadow table + */ + heap_delete(pg_shadow_rel, &tuple->t_self, NULL); + + /* + * Remove user from groups + * + * try calling alter group drop user for every group + */ + pg_rel = heap_openr(GroupRelationName, AccessExclusiveLock); + pg_dsc = RelationGetDescr(pg_rel); + scan = heap_beginscan(pg_rel, false, SnapshotNow, 0, NULL); + while (HeapTupleIsValid(tmp_tuple = heap_getnext(scan, 0))) + { + AlterGroupStmt ags; + + datum = heap_getattr(tmp_tuple, Anum_pg_group_groname, pg_dsc, &null); + + ags.name = nameout(DatumGetName(datum)); /* the group name from + * which to try to drop + * the user */ + ags.action = -1; + ags.listUsers = lcons((void *) makeInteger(usesysid), NIL); + AlterGroup(&ags, "DROP USER"); + } + heap_endscan(scan); + heap_close(pg_rel, AccessExclusiveLock); + } /* * Write the updated pg_shadow data to the flat password file. */ - write_password_file(pg_shadow_rel); + write_password_file(pg_shadow_rel); - /* - * Now we can clean up. - */ - heap_close(pg_shadow_rel, AccessExclusiveLock); + /* + * Now we can clean up. + */ + heap_close(pg_shadow_rel, AccessExclusiveLock); } @@ -609,7 +630,7 @@ CheckPgUserAclNotNull() 0, 0, 0); if (!HeapTupleIsValid(htup)) { - /* BIG problem */ + /* BIG problem */ elog(ERROR, "IsPgUserAclNull: \"%s\" not found", ShadowRelationName); } @@ -617,10 +638,10 @@ CheckPgUserAclNotNull() if (heap_attisnull(htup, Anum_pg_class_relacl)) { elog(ERROR, - "To use passwords, you have to revoke permissions on %s " - "so normal users cannot read the passwords. " - "Try 'REVOKE ALL ON \"%s\" FROM PUBLIC'.", - ShadowRelationName, ShadowRelationName); + "To use passwords, you have to revoke permissions on %s " + "so normal users cannot read the passwords. " + "Try 'REVOKE ALL ON \"%s\" FROM PUBLIC'.", + ShadowRelationName, ShadowRelationName); } return; @@ -637,14 +658,15 @@ CreateGroup(CreateGroupStmt *stmt) Relation pg_group_rel; HeapScanDesc scan; HeapTuple tuple; - TupleDesc pg_group_dsc; - bool group_exists = false, - sysid_exists = false; - int max_id = 0; - Datum new_record[Natts_pg_group]; - char new_record_nulls[Natts_pg_group]; - List *item, *newlist=NULL; - ArrayType *userarray; + TupleDesc pg_group_dsc; + bool group_exists = false, + sysid_exists = false; + int max_id = 0; + Datum new_record[Natts_pg_group]; + char new_record_nulls[Natts_pg_group]; + List *item, + *newlist = NULL; + ArrayType *userarray; /* * Make sure the user can do this. @@ -652,12 +674,12 @@ CreateGroup(CreateGroupStmt *stmt) if (!superuser()) elog(ERROR, "CREATE GROUP: permission denied"); - /* - * There is not real reason for this, but it makes it consistent - * with create user, and it seems like a good idea anyway. - */ + /* + * There is not real reason for this, but it makes it consistent with + * create user, and it seems like a good idea anyway. + */ if (IsTransactionBlock()) - elog(ERROR, "CREATE GROUP: may not be called in a transaction block"); + elog(ERROR, "CREATE GROUP: may not be called in a transaction block"); pg_group_rel = heap_openr(GroupRelationName, AccessExclusiveLock); @@ -666,111 +688,111 @@ CreateGroup(CreateGroupStmt *stmt) scan = heap_beginscan(pg_group_rel, false, SnapshotNow, 0, NULL); while (!group_exists && !sysid_exists && HeapTupleIsValid(tuple = heap_getnext(scan, false))) { - Datum datum; - bool null; + Datum datum; + bool null; datum = heap_getattr(tuple, Anum_pg_group_groname, pg_group_dsc, &null); group_exists = datum && !null && (strcmp((char *) datum, stmt->name) == 0); datum = heap_getattr(tuple, Anum_pg_group_grosysid, pg_group_dsc, &null); - if (stmt->sysid >= 0) /* customized id wanted */ - sysid_exists = datum && !null && ((int)datum == stmt->sysid); - else /* pick 1 + max */ - { - if ((int) datum > max_id) - max_id = (int) datum; - } + if (stmt->sysid >= 0) /* customized id wanted */ + sysid_exists = datum && !null && ((int) datum == stmt->sysid); + else +/* pick 1 + max */ + { + if ((int) datum > max_id) + max_id = (int) datum; + } } heap_endscan(scan); if (group_exists || sysid_exists) { heap_close(pg_group_rel, AccessExclusiveLock); - if (group_exists) - elog(ERROR, "CREATE GROUP: group name \"%s\" already exists", stmt->name); - else - elog(ERROR, "CREATE GROUP: group sysid %d is already assigned", stmt->sysid); + if (group_exists) + elog(ERROR, "CREATE GROUP: group name \"%s\" already exists", stmt->name); + else + elog(ERROR, "CREATE GROUP: group sysid %d is already assigned", stmt->sysid); } - /* - * Translate the given user names to ids - */ - - foreach(item, stmt->initUsers) - { - const char * groupuser = strVal(lfirst(item)); - Value *v; - - tuple = SearchSysCacheTuple(SHADOWNAME, - PointerGetDatum(groupuser), - 0, 0, 0); - if (!HeapTupleIsValid(tuple)) - { - heap_close(pg_group_rel, AccessExclusiveLock); - elog(ERROR, "CREATE GROUP: user \"%s\" does not exist", groupuser); - } - - v = makeInteger(((Form_pg_shadow) GETSTRUCT(tuple))->usesysid); - if (!member(v, newlist)) - newlist = lcons(v, newlist); - } - - /* build an array to insert */ - if (newlist) - { - int i; - - userarray = palloc(ARR_OVERHEAD(1) + length(newlist) * sizeof(int32)); - ARR_SIZE(userarray) = ARR_OVERHEAD(1) + length(newlist) * sizeof(int32); - ARR_FLAGS(userarray) = 0x0; - ARR_NDIM(userarray) = 1; /* one dimensional array */ - ARR_LBOUND(userarray)[0] = 1; /* axis starts at one */ - ARR_DIMS(userarray)[0] = length(newlist); /* axis is this long */ - /* fill the array */ - i = 0; - foreach(item, newlist) - { - ((int*)ARR_DATA_PTR(userarray))[i++] = intVal(lfirst(item)); - } - } - else - userarray = NULL; - - /* - * Form a tuple to insert - */ - if (stmt->sysid >=0) - max_id = stmt->sysid; - else - max_id++; - - new_record[Anum_pg_group_groname-1] = (Datum)(stmt->name); - new_record[Anum_pg_group_grosysid-1] = (Datum)(max_id); - new_record[Anum_pg_group_grolist-1] = (Datum)userarray; - - new_record_nulls[Anum_pg_group_groname-1] = ' '; - new_record_nulls[Anum_pg_group_grosysid-1] = ' '; - new_record_nulls[Anum_pg_group_grolist-1] = userarray ? ' ' : 'n'; - - tuple = heap_formtuple(pg_group_dsc, new_record, new_record_nulls); - - /* - * Insert a new record in the pg_group_table - */ - heap_insert(pg_group_rel, tuple); - - /* - * Update indexes - */ - if (RelationGetForm(pg_group_rel)->relhasindex) { - Relation idescs[Num_pg_group_indices]; - - CatalogOpenIndices(Num_pg_group_indices, - Name_pg_group_indices, idescs); - CatalogIndexInsert(idescs, Num_pg_group_indices, pg_group_rel, - tuple); - CatalogCloseIndices(Num_pg_group_indices, idescs); - } + /* + * Translate the given user names to ids + */ + + foreach(item, stmt->initUsers) + { + const char *groupuser = strVal(lfirst(item)); + Value *v; + + tuple = SearchSysCacheTuple(SHADOWNAME, + PointerGetDatum(groupuser), + 0, 0, 0); + if (!HeapTupleIsValid(tuple)) + { + heap_close(pg_group_rel, AccessExclusiveLock); + elog(ERROR, "CREATE GROUP: user \"%s\" does not exist", groupuser); + } + + v = makeInteger(((Form_pg_shadow) GETSTRUCT(tuple))->usesysid); + if (!member(v, newlist)) + newlist = lcons(v, newlist); + } + + /* build an array to insert */ + if (newlist) + { + int i; + + userarray = palloc(ARR_OVERHEAD(1) + length(newlist) * sizeof(int32)); + ARR_SIZE(userarray) = ARR_OVERHEAD(1) + length(newlist) * sizeof(int32); + ARR_FLAGS(userarray) = 0x0; + ARR_NDIM(userarray) = 1;/* one dimensional array */ + ARR_LBOUND(userarray)[0] = 1; /* axis starts at one */ + ARR_DIMS(userarray)[0] = length(newlist); /* axis is this long */ + /* fill the array */ + i = 0; + foreach(item, newlist) + ((int *) ARR_DATA_PTR(userarray))[i++] = intVal(lfirst(item)); + } + else + userarray = NULL; + + /* + * Form a tuple to insert + */ + if (stmt->sysid >= 0) + max_id = stmt->sysid; + else + max_id++; + + new_record[Anum_pg_group_groname - 1] = (Datum) (stmt->name); + new_record[Anum_pg_group_grosysid - 1] = (Datum) (max_id); + new_record[Anum_pg_group_grolist - 1] = (Datum) userarray; + + new_record_nulls[Anum_pg_group_groname - 1] = ' '; + new_record_nulls[Anum_pg_group_grosysid - 1] = ' '; + new_record_nulls[Anum_pg_group_grolist - 1] = userarray ? ' ' : 'n'; + + tuple = heap_formtuple(pg_group_dsc, new_record, new_record_nulls); + + /* + * Insert a new record in the pg_group_table + */ + heap_insert(pg_group_rel, tuple); + + /* + * Update indexes + */ + if (RelationGetForm(pg_group_rel)->relhasindex) + { + Relation idescs[Num_pg_group_indices]; + + CatalogOpenIndices(Num_pg_group_indices, + Name_pg_group_indices, idescs); + CatalogIndexInsert(idescs, Num_pg_group_indices, pg_group_rel, + tuple); + CatalogCloseIndices(Num_pg_group_indices, idescs); + } heap_close(pg_group_rel, AccessExclusiveLock); } @@ -781,258 +803,272 @@ CreateGroup(CreateGroupStmt *stmt) * ALTER GROUP */ void -AlterGroup(AlterGroupStmt *stmt, const char * tag) +AlterGroup(AlterGroupStmt *stmt, const char *tag) { Relation pg_group_rel; - TupleDesc pg_group_dsc; - HeapTuple group_tuple; + TupleDesc pg_group_dsc; + HeapTuple group_tuple; - /* + /* * Make sure the user can do this. */ if (!superuser()) elog(ERROR, "%s: permission denied", tag); - /* - * There is not real reason for this, but it makes it consistent - * with alter user, and it seems like a good idea anyway. - */ + /* + * There is not real reason for this, but it makes it consistent with + * alter user, and it seems like a good idea anyway. + */ if (IsTransactionBlock()) - elog(ERROR, "%s: may not be called in a transaction block", tag); + elog(ERROR, "%s: may not be called in a transaction block", tag); - pg_group_rel = heap_openr(GroupRelationName, AccessExclusiveLock); - pg_group_dsc = RelationGetDescr(pg_group_rel); + pg_group_rel = heap_openr(GroupRelationName, AccessExclusiveLock); + pg_group_dsc = RelationGetDescr(pg_group_rel); - /* - * Verify that group exists. - * If we find a tuple, will take that the rest of the way and make our - * modifications on it. - */ - if (!HeapTupleIsValid(group_tuple = SearchSysCacheTupleCopy(GRONAME, PointerGetDatum(stmt->name), 0, 0, 0))) + /* + * Verify that group exists. If we find a tuple, will take that the + * rest of the way and make our modifications on it. + */ + if (!HeapTupleIsValid(group_tuple = SearchSysCacheTupleCopy(GRONAME, PointerGetDatum(stmt->name), 0, 0, 0))) { - heap_close(pg_group_rel, AccessExclusiveLock); + heap_close(pg_group_rel, AccessExclusiveLock); elog(ERROR, "%s: group \"%s\" does not exist", tag, stmt->name); } - AssertState(stmt->action == +1 || stmt->action == -1); - /* - * Now decide what to do. - */ - if (stmt->action == +1) /* add users, might also be invoked by create user */ - { - Datum new_record[Natts_pg_group]; - char new_record_nulls[Natts_pg_group] = { ' ', ' ', ' '}; - ArrayType *newarray, *oldarray; - List * newlist = NULL, *item; - HeapTuple tuple; - bool null = false; - Datum datum = heap_getattr(group_tuple, Anum_pg_group_grolist, pg_group_dsc, &null); - int i; - - oldarray = (ArrayType*)datum; - Assert(null || ARR_NDIM(oldarray) == 1); - /* first add the old array to the hitherto empty list */ - if (!null) - for (i = ARR_LBOUND(oldarray)[0]; i < ARR_LBOUND(oldarray)[0] + ARR_DIMS(oldarray)[0]; i++) - { - int index, arrval; - Value *v; - bool valueNull; - index = i; - arrval = DatumGetInt32(array_ref(oldarray, 1, &index, true/*by value*/, - sizeof(int), 0, &valueNull)); - v = makeInteger(arrval); - /* filter out duplicates */ - if (!member(v, newlist)) - newlist = lcons(v, newlist); - } - - /* - * now convert the to be added usernames to sysids and add them - * to the list - */ - foreach(item, stmt->listUsers) - { - Value *v; - - if (strcmp(tag, "ALTER GROUP")==0) - { - /* Get the uid of the proposed user to add. */ - tuple = SearchSysCacheTuple(SHADOWNAME, - PointerGetDatum(strVal(lfirst(item))), - 0, 0, 0); - if (!HeapTupleIsValid(tuple)) - { - heap_close(pg_group_rel, AccessExclusiveLock); - elog(ERROR, "%s: user \"%s\" does not exist", tag, strVal(lfirst(item))); - } - v = makeInteger(((Form_pg_shadow) GETSTRUCT(tuple))->usesysid); - } - else if (strcmp(tag, "CREATE USER")==0) - { - /* in this case we already know the uid and it wouldn't - be in the cache anyway yet */ - v = lfirst(item); - } - else + AssertState(stmt->action == +1 || stmt->action == -1); + + /* + * Now decide what to do. + */ + if (stmt->action == +1) /* add users, might also be invoked by + * create user */ + { + Datum new_record[Natts_pg_group]; + char new_record_nulls[Natts_pg_group] = {' ', ' ', ' '}; + ArrayType *newarray, + *oldarray; + List *newlist = NULL, + *item; + HeapTuple tuple; + bool null = false; + Datum datum = heap_getattr(group_tuple, Anum_pg_group_grolist, pg_group_dsc, &null); + int i; + + oldarray = (ArrayType *) datum; + Assert(null || ARR_NDIM(oldarray) == 1); + /* first add the old array to the hitherto empty list */ + if (!null) + for (i = ARR_LBOUND(oldarray)[0]; i < ARR_LBOUND(oldarray)[0] + ARR_DIMS(oldarray)[0]; i++) + { + int index, + arrval; + Value *v; + bool valueNull; + + index = i; + arrval = DatumGetInt32(array_ref(oldarray, 1, &index, true /* by value */ , + sizeof(int), 0, &valueNull)); + v = makeInteger(arrval); + /* filter out duplicates */ + if (!member(v, newlist)) + newlist = lcons(v, newlist); + } + + /* + * now convert the to be added usernames to sysids and add them to + * the list + */ + foreach(item, stmt->listUsers) + { + Value *v; + + if (strcmp(tag, "ALTER GROUP") == 0) + { + /* Get the uid of the proposed user to add. */ + tuple = SearchSysCacheTuple(SHADOWNAME, + PointerGetDatum(strVal(lfirst(item))), + 0, 0, 0); + if (!HeapTupleIsValid(tuple)) + { + heap_close(pg_group_rel, AccessExclusiveLock); + elog(ERROR, "%s: user \"%s\" does not exist", tag, strVal(lfirst(item))); + } + v = makeInteger(((Form_pg_shadow) GETSTRUCT(tuple))->usesysid); + } + else if (strcmp(tag, "CREATE USER") == 0) { - elog(ERROR, "AlterGroup: unknown tag %s", tag); + + /* + * in this case we already know the uid and it wouldn't be + * in the cache anyway yet + */ + v = lfirst(item); + } + else + { + elog(ERROR, "AlterGroup: unknown tag %s", tag); v = NULL; /* keep compiler quiet */ } - if (!member(v, newlist)) - newlist = lcons(v, newlist); - else - /* we silently assume here that this error will only come up - in a ALTER GROUP statement */ - elog(NOTICE, "%s: user \"%s\" is already in group \"%s\"", tag, strVal(lfirst(item)), stmt->name); - } - - newarray = palloc(ARR_OVERHEAD(1) + length(newlist) * sizeof(int32)); - ARR_SIZE(newarray) = ARR_OVERHEAD(1) + length(newlist) * sizeof(int32); - ARR_FLAGS(newarray) = 0x0; - ARR_NDIM(newarray) = 1; /* one dimensional array */ - ARR_LBOUND(newarray)[0] = 1; /* axis starts at one */ - ARR_DIMS(newarray)[0] = length(newlist); /* axis is this long */ - /* fill the array */ - i = 0; - foreach(item, newlist) - { - ((int*)ARR_DATA_PTR(newarray))[i++] = intVal(lfirst(item)); - } - - /* - * Form a tuple with the new array and write it back. - */ - new_record[Anum_pg_group_groname-1] = (Datum)(stmt->name); - new_record[Anum_pg_group_grosysid-1] = heap_getattr(group_tuple, Anum_pg_group_grosysid, pg_group_dsc, &null); - new_record[Anum_pg_group_grolist-1] = PointerGetDatum(newarray); - - tuple = heap_formtuple(pg_group_dsc, new_record, new_record_nulls); - heap_update(pg_group_rel, &group_tuple->t_self, tuple, NULL); - - /* Update indexes */ - if (RelationGetForm(pg_group_rel)->relhasindex) { - Relation idescs[Num_pg_group_indices]; - - CatalogOpenIndices(Num_pg_group_indices, - Name_pg_group_indices, idescs); - CatalogIndexInsert(idescs, Num_pg_group_indices, pg_group_rel, - tuple); - CatalogCloseIndices(Num_pg_group_indices, idescs); - } - } /* endif alter group add user */ - - else if (stmt->action == -1) /*drop users from group */ - { - Datum datum; - bool null; - bool is_dropuser = strcmp(tag, "DROP USER")==0; - - datum = heap_getattr(group_tuple, Anum_pg_group_grolist, pg_group_dsc, &null); - if (null) - { - if (!is_dropuser) - elog(NOTICE, "ALTER GROUP: group \"%s\" does not have any members", stmt->name); - } - else - { - HeapTuple tuple; - Datum new_record[Natts_pg_group]; - char new_record_nulls[Natts_pg_group] = { ' ', ' ', ' '}; - ArrayType *oldarray, *newarray; - List * newlist = NULL, *item; - int i; - - oldarray = (ArrayType*)datum; - Assert(ARR_NDIM(oldarray) == 1); - /* first add the old array to the hitherto empty list */ - for (i = ARR_LBOUND(oldarray)[0]; i < ARR_LBOUND(oldarray)[0] + ARR_DIMS(oldarray)[0]; i++) - { - int index, arrval; - Value *v; - bool valueNull; - index = i; - arrval = DatumGetInt32(array_ref(oldarray, 1, &index, true/*by value*/, - sizeof(int), 0, &valueNull)); - v = makeInteger(arrval); - /* filter out duplicates */ - if (!member(v, newlist)) - newlist = lcons(v, newlist); - } - - /* - * now convert the to be dropped usernames to sysids and remove - * them from the list - */ - foreach(item, stmt->listUsers) - { - Value *v; - if (!is_dropuser) - { - /* Get the uid of the proposed user to drop. */ - tuple = SearchSysCacheTuple(SHADOWNAME, - PointerGetDatum(strVal(lfirst(item))), - 0, 0, 0); - if (!HeapTupleIsValid(tuple)) - { - heap_close(pg_group_rel, AccessExclusiveLock); - elog(ERROR, "ALTER GROUP: user \"%s\" does not exist", strVal(lfirst(item))); - } - v = makeInteger(((Form_pg_shadow) GETSTRUCT(tuple))->usesysid); - } - else - { - /* for dropuser we already know the uid */ - v = lfirst(item); - } - if (member(v, newlist)) - newlist = LispRemove(v, newlist); - else if (!is_dropuser) - elog(NOTICE, "ALTER GROUP: user \"%s\" is not in group \"%s\"", strVal(lfirst(item)), stmt->name); - } - - newarray = palloc(ARR_OVERHEAD(1) + length(newlist) * sizeof(int32)); - ARR_SIZE(newarray) = ARR_OVERHEAD(1) + length(newlist) * sizeof(int32); - ARR_FLAGS(newarray) = 0x0; - ARR_NDIM(newarray) = 1; /* one dimensional array */ - ARR_LBOUND(newarray)[0] = 1; /* axis starts at one */ - ARR_DIMS(newarray)[0] = length(newlist); /* axis is this long */ - /* fill the array */ - i = 0; - foreach(item, newlist) - { - ((int*)ARR_DATA_PTR(newarray))[i++] = intVal(lfirst(item)); - } - - /* - * Insert the new tuple with the updated user list - */ - new_record[Anum_pg_group_groname-1] = (Datum)(stmt->name); - new_record[Anum_pg_group_grosysid-1] = heap_getattr(group_tuple, Anum_pg_group_grosysid, pg_group_dsc, &null); - new_record[Anum_pg_group_grolist-1] = PointerGetDatum(newarray); - - tuple = heap_formtuple(pg_group_dsc, new_record, new_record_nulls); - heap_update(pg_group_rel, &group_tuple->t_self, tuple, NULL); - - /* Update indexes */ - if (RelationGetForm(pg_group_rel)->relhasindex) { - Relation idescs[Num_pg_group_indices]; - - CatalogOpenIndices(Num_pg_group_indices, - Name_pg_group_indices, idescs); - CatalogIndexInsert(idescs, Num_pg_group_indices, pg_group_rel, - tuple); - CatalogCloseIndices(Num_pg_group_indices, idescs); - } - - } /* endif group not null */ - } /* endif alter group drop user */ - - heap_close(pg_group_rel, AccessExclusiveLock); - - pfree(group_tuple); + if (!member(v, newlist)) + newlist = lcons(v, newlist); + else + + /* + * we silently assume here that this error will only come + * up in a ALTER GROUP statement + */ + elog(NOTICE, "%s: user \"%s\" is already in group \"%s\"", tag, strVal(lfirst(item)), stmt->name); + } + + newarray = palloc(ARR_OVERHEAD(1) + length(newlist) * sizeof(int32)); + ARR_SIZE(newarray) = ARR_OVERHEAD(1) + length(newlist) * sizeof(int32); + ARR_FLAGS(newarray) = 0x0; + ARR_NDIM(newarray) = 1; /* one dimensional array */ + ARR_LBOUND(newarray)[0] = 1; /* axis starts at one */ + ARR_DIMS(newarray)[0] = length(newlist); /* axis is this long */ + /* fill the array */ + i = 0; + foreach(item, newlist) + ((int *) ARR_DATA_PTR(newarray))[i++] = intVal(lfirst(item)); + + /* + * Form a tuple with the new array and write it back. + */ + new_record[Anum_pg_group_groname - 1] = (Datum) (stmt->name); + new_record[Anum_pg_group_grosysid - 1] = heap_getattr(group_tuple, Anum_pg_group_grosysid, pg_group_dsc, &null); + new_record[Anum_pg_group_grolist - 1] = PointerGetDatum(newarray); + + tuple = heap_formtuple(pg_group_dsc, new_record, new_record_nulls); + heap_update(pg_group_rel, &group_tuple->t_self, tuple, NULL); + + /* Update indexes */ + if (RelationGetForm(pg_group_rel)->relhasindex) + { + Relation idescs[Num_pg_group_indices]; + + CatalogOpenIndices(Num_pg_group_indices, + Name_pg_group_indices, idescs); + CatalogIndexInsert(idescs, Num_pg_group_indices, pg_group_rel, + tuple); + CatalogCloseIndices(Num_pg_group_indices, idescs); + } + } /* endif alter group add user */ + + else if (stmt->action == -1)/* drop users from group */ + { + Datum datum; + bool null; + bool is_dropuser = strcmp(tag, "DROP USER") == 0; + + datum = heap_getattr(group_tuple, Anum_pg_group_grolist, pg_group_dsc, &null); + if (null) + { + if (!is_dropuser) + elog(NOTICE, "ALTER GROUP: group \"%s\" does not have any members", stmt->name); + } + else + { + HeapTuple tuple; + Datum new_record[Natts_pg_group]; + char new_record_nulls[Natts_pg_group] = {' ', ' ', ' '}; + ArrayType *oldarray, + *newarray; + List *newlist = NULL, + *item; + int i; + + oldarray = (ArrayType *) datum; + Assert(ARR_NDIM(oldarray) == 1); + /* first add the old array to the hitherto empty list */ + for (i = ARR_LBOUND(oldarray)[0]; i < ARR_LBOUND(oldarray)[0] + ARR_DIMS(oldarray)[0]; i++) + { + int index, + arrval; + Value *v; + bool valueNull; + + index = i; + arrval = DatumGetInt32(array_ref(oldarray, 1, &index, true /* by value */ , + sizeof(int), 0, &valueNull)); + v = makeInteger(arrval); + /* filter out duplicates */ + if (!member(v, newlist)) + newlist = lcons(v, newlist); + } + + /* + * now convert the to be dropped usernames to sysids and + * remove them from the list + */ + foreach(item, stmt->listUsers) + { + Value *v; + + if (!is_dropuser) + { + /* Get the uid of the proposed user to drop. */ + tuple = SearchSysCacheTuple(SHADOWNAME, + PointerGetDatum(strVal(lfirst(item))), + 0, 0, 0); + if (!HeapTupleIsValid(tuple)) + { + heap_close(pg_group_rel, AccessExclusiveLock); + elog(ERROR, "ALTER GROUP: user \"%s\" does not exist", strVal(lfirst(item))); + } + v = makeInteger(((Form_pg_shadow) GETSTRUCT(tuple))->usesysid); + } + else + { + /* for dropuser we already know the uid */ + v = lfirst(item); + } + if (member(v, newlist)) + newlist = LispRemove(v, newlist); + else if (!is_dropuser) + elog(NOTICE, "ALTER GROUP: user \"%s\" is not in group \"%s\"", strVal(lfirst(item)), stmt->name); + } + + newarray = palloc(ARR_OVERHEAD(1) + length(newlist) * sizeof(int32)); + ARR_SIZE(newarray) = ARR_OVERHEAD(1) + length(newlist) * sizeof(int32); + ARR_FLAGS(newarray) = 0x0; + ARR_NDIM(newarray) = 1; /* one dimensional array */ + ARR_LBOUND(newarray)[0] = 1; /* axis starts at one */ + ARR_DIMS(newarray)[0] = length(newlist); /* axis is this long */ + /* fill the array */ + i = 0; + foreach(item, newlist) + ((int *) ARR_DATA_PTR(newarray))[i++] = intVal(lfirst(item)); + + /* + * Insert the new tuple with the updated user list + */ + new_record[Anum_pg_group_groname - 1] = (Datum) (stmt->name); + new_record[Anum_pg_group_grosysid - 1] = heap_getattr(group_tuple, Anum_pg_group_grosysid, pg_group_dsc, &null); + new_record[Anum_pg_group_grolist - 1] = PointerGetDatum(newarray); + + tuple = heap_formtuple(pg_group_dsc, new_record, new_record_nulls); + heap_update(pg_group_rel, &group_tuple->t_self, tuple, NULL); + + /* Update indexes */ + if (RelationGetForm(pg_group_rel)->relhasindex) + { + Relation idescs[Num_pg_group_indices]; + + CatalogOpenIndices(Num_pg_group_indices, + Name_pg_group_indices, idescs); + CatalogIndexInsert(idescs, Num_pg_group_indices, pg_group_rel, + tuple); + CatalogCloseIndices(Num_pg_group_indices, idescs); + } + + } /* endif group not null */ + } /* endif alter group drop user */ + + heap_close(pg_group_rel, AccessExclusiveLock); + + pfree(group_tuple); } @@ -1046,52 +1082,52 @@ DropGroup(DropGroupStmt *stmt) Relation pg_group_rel; HeapScanDesc scan; HeapTuple tuple; - TupleDesc pg_group_dsc; - bool gro_exists = false; + TupleDesc pg_group_dsc; + bool gro_exists = false; - /* + /* * Make sure the user can do this. */ if (!superuser()) elog(ERROR, "DROP GROUP: permission denied"); - /* - * There is not real reason for this, but it makes it consistent - * with drop user, and it seems like a good idea anyway. - */ + /* + * There is not real reason for this, but it makes it consistent with + * drop user, and it seems like a good idea anyway. + */ if (IsTransactionBlock()) - elog(ERROR, "DROP GROUP: may not be called in a transaction block"); + elog(ERROR, "DROP GROUP: may not be called in a transaction block"); - /* - * Scan the pg_group table and delete all matching groups. - */ + /* + * Scan the pg_group table and delete all matching groups. + */ pg_group_rel = heap_openr(GroupRelationName, AccessExclusiveLock); pg_group_dsc = RelationGetDescr(pg_group_rel); scan = heap_beginscan(pg_group_rel, false, SnapshotNow, 0, NULL); while (HeapTupleIsValid(tuple = heap_getnext(scan, false))) { - Datum datum; - bool null; - - datum = heap_getattr(tuple, Anum_pg_group_groname, pg_group_dsc, &null); - if (datum && !null && strcmp((char*)datum, stmt->name)==0) - { - gro_exists = true; - heap_delete(pg_group_rel, &tuple->t_self, NULL); - } + Datum datum; + bool null; + + datum = heap_getattr(tuple, Anum_pg_group_groname, pg_group_dsc, &null); + if (datum && !null && strcmp((char *) datum, stmt->name) == 0) + { + gro_exists = true; + heap_delete(pg_group_rel, &tuple->t_self, NULL); + } } heap_endscan(scan); - /* - * Did we find any? - */ - if (!gro_exists) - { - heap_close(pg_group_rel, AccessExclusiveLock); + /* + * Did we find any? + */ + if (!gro_exists) + { + heap_close(pg_group_rel, AccessExclusiveLock); elog(ERROR, "DROP GROUP: group \"%s\" does not exist", stmt->name); - } + } heap_close(pg_group_rel, AccessExclusiveLock); } diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index 5c300c69398..834438b7986 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.146 2000/04/06 18:12:07 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.147 2000/04/12 17:14:59 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -102,15 +102,17 @@ static char *vc_show_rusage(struct rusage * ru0); /* * This routines handle a special cross-transaction portal. - * However it is automatically closed in case of abort. + * However it is automatically closed in case of abort. */ -void CommonSpecialPortalOpen(void) +void +CommonSpecialPortalOpen(void) { char *pname; if (CommonSpecialPortalInUse) elog(ERROR, "CommonSpecialPortal is in use"); + /* * Create a portal for safe memory across transactions. We need to * palloc the name space for it because our hash function expects the @@ -130,7 +132,8 @@ void CommonSpecialPortalOpen(void) CommonSpecialPortalInUse = true; } -void CommonSpecialPortalClose(void) +void +CommonSpecialPortalClose(void) { /* Clear flag first, to avoid recursion if PortalDrop elog's */ CommonSpecialPortalInUse = false; @@ -141,16 +144,18 @@ void CommonSpecialPortalClose(void) PortalDrop(&vc_portal); } -PortalVariableMemory CommonSpecialPortalGetMemory(void) +PortalVariableMemory +CommonSpecialPortalGetMemory(void) { return PortalGetVariableMemory(vc_portal); } -bool CommonSpecialPortalIsOpen(void) +bool +CommonSpecialPortalIsOpen(void) { return CommonSpecialPortalInUse; -} - +} + void vacuum(char *vacrel, bool verbose, bool analyze, List *va_spec) { @@ -208,9 +213,9 @@ vacuum(char *vacrel, bool verbose, bool analyze, List *va_spec) * Start up the vacuum cleaner. * * NOTE: since this commits the current transaction, the memory holding - * any passed-in parameters gets freed here. We must have already copied - * pass-by-reference parameters to safe storage. Don't make me fix this - * again! + * any passed-in parameters gets freed here. We must have already + * copied pass-by-reference parameters to safe storage. Don't make me + * fix this again! */ vc_init(); @@ -316,11 +321,12 @@ vc_getrels(NameData *VacRelP) if (NameStr(*VacRelP)) { + /* * we could use the cache here, but it is clearer to use scankeys * for both vacuum cases, bjm 2000/01/19 */ - char *nontemp_relname; + char *nontemp_relname; /* We must re-map temp table names bjm 2000-04-06 */ if ((nontemp_relname = @@ -414,7 +420,7 @@ vc_vacone(Oid relid, bool analyze, List *va_cols) int32 nindices, i; VRelStats *vacrelstats; - bool reindex = false; + bool reindex = false; StartTransactionCommand(); @@ -678,7 +684,7 @@ static void vc_scanheap(VRelStats *vacrelstats, Relation onerel, VPageList vacuum_pages, VPageList fraged_pages) { - BlockNumber nblocks, + BlockNumber nblocks, blkno; ItemId itemid; Buffer buf; @@ -1201,7 +1207,7 @@ vc_repair_frag(VRelStats *vacrelstats, Relation onerel, last_vacuum_block = -1; } if (num_fraged_pages > 0 && - fraged_pages->vpl_pagedesc[num_fraged_pages - 1]->vpd_blkno == + fraged_pages->vpl_pagedesc[num_fraged_pages - 1]->vpd_blkno == (BlockNumber) blkno) { /* page is in fraged_pages too; remove it */ @@ -1456,8 +1462,8 @@ vc_repair_frag(VRelStats *vacrelstats, Relation onerel, * we stop shrinking here. I could try to find * real parent row but want not to do it because * of real solution will be implemented anyway, - * latter, and we are too close to 6.5 release. - * - vadim 06/11/99 + * latter, and we are too close to 6.5 release. - + * vadim 06/11/99 */ if (Ptp.t_data->t_xmax != tp.t_data->t_xmin) { @@ -1539,20 +1545,23 @@ vc_repair_frag(VRelStats *vacrelstats, Relation onerel, * to get t_infomask of inserted heap tuple !!! */ ToPage = BufferGetPage(cur_buffer); + /* * If this page was not used before - clean it. * * This path is different from the other callers of * vc_vacpage, because we have already incremented the * vpd's vpd_offsets_used field to account for the - * tuple(s) we expect to move onto the page. Therefore - * vc_vacpage's check for vpd_offsets_used == 0 is wrong. - * But since that's a good debugging check for all other - * callers, we work around it here rather than remove it. + * tuple(s) we expect to move onto the page. Therefore + * vc_vacpage's check for vpd_offsets_used == 0 is + * wrong. But since that's a good debugging check for + * all other callers, we work around it here rather + * than remove it. */ if (!PageIsEmpty(ToPage) && vtmove[ti].cleanVpd) { - int sv_offsets_used = destvpd->vpd_offsets_used; + int sv_offsets_used = destvpd->vpd_offsets_used; + destvpd->vpd_offsets_used = 0; vc_vacpage(ToPage, destvpd); destvpd->vpd_offsets_used = sv_offsets_used; @@ -2267,7 +2276,7 @@ vc_attrstats(Relation onerel, VRelStats *vacrelstats, HeapTuple tuple) #ifdef _DROP_COLUMN_HACK__ if (COLUMN_IS_DROPPED(stats->attr)) continue; -#endif /* _DROP_COLUMN_HACK__ */ +#endif /* _DROP_COLUMN_HACK__ */ value = heap_getattr(tuple, stats->attr->attnum, tupDesc, &isnull); diff --git a/src/backend/commands/variable.c b/src/backend/commands/variable.c index 6432d3c2b8f..367b17aa64c 100644 --- a/src/backend/commands/variable.c +++ b/src/backend/commands/variable.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/variable.c,v 1.33 2000/04/07 13:39:24 thomas Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/variable.c,v 1.34 2000/04/12 17:15:00 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -436,7 +436,7 @@ parse_geqo(char *value) if (strcasecmp(tok, "on") == 0) { - int new_geqo_rels = GEQO_RELS; + int new_geqo_rels = GEQO_RELS; if (val != NULL) { @@ -505,7 +505,7 @@ static bool show_effective_cache_size() { elog(NOTICE, "EFFECTIVE_CACHE_SIZE is %g (%dK pages)", - effective_cache_size, BLCKSZ/1024); + effective_cache_size, BLCKSZ / 1024); return TRUE; } @@ -656,12 +656,12 @@ reset_cpu_operator_cost() * DATE_STYLE * * NOTE: set_default_datestyle() is called during backend startup to check - * if the PGDATESTYLE environment variable is set. We want the env var + * if the PGDATESTYLE environment variable is set. We want the env var * to determine the value that "RESET DateStyle" will reset to! */ /* These get initialized from the "master" values in init/globals.c */ -static int DefaultDateStyle; +static int DefaultDateStyle; static bool DefaultEuroDates; static bool @@ -777,8 +777,9 @@ set_default_datestyle(void) { char *DBDate; - /* Initialize from compile-time defaults in init/globals.c. - * NB: this is a necessary step; consider PGDATESTYLE="DEFAULT". + /* + * Initialize from compile-time defaults in init/globals.c. NB: this + * is a necessary step; consider PGDATESTYLE="DEFAULT". */ DefaultDateStyle = DateStyle; DefaultEuroDates = EuroDates; @@ -788,9 +789,11 @@ set_default_datestyle(void) if (DBDate == NULL) return; - /* Make a modifiable copy --- overwriting the env var doesn't seem + /* + * Make a modifiable copy --- overwriting the env var doesn't seem * like a good idea, even though we currently won't look at it again. - * Note that we cannot use palloc at this early stage of initialization. + * Note that we cannot use palloc at this early stage of + * initialization. */ DBDate = strdup(DBDate); @@ -1041,9 +1044,8 @@ reset_XactIsoLevel() static bool parse_pg_options(char *value) { - if (!superuser()) { + if (!superuser()) elog(ERROR, "Only users with superuser privilege can set pg_options"); - } if (value == NULL) read_pg_options(0); else @@ -1061,9 +1063,8 @@ show_pg_options(void) static bool reset_pg_options(void) { - if (!superuser()) { + if (!superuser()) elog(ERROR, "Only users with superuser privilege can set pg_options"); - } read_pg_options(0); return (TRUE); } @@ -1075,7 +1076,7 @@ reset_pg_options(void) static bool parse_random_seed(char *value) { - double seed = 0; + double seed = 0; if (value == NULL) reset_random_seed(); @@ -1097,7 +1098,7 @@ show_random_seed(void) static bool reset_random_seed(void) { - double seed = 0.5; + double seed = 0.5; setseed(&seed); return (TRUE); |