1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-07 19:06:32 +03:00

Clean up various to-do items associated with system indexes:

pg_database now has unique indexes on oid and on datname.
pg_shadow now has unique indexes on usename and on usesysid.
pg_am now has unique index on oid.
pg_opclass now has unique index on oid.
pg_amproc now has unique index on amid+amopclaid+amprocnum.
Remove pg_rewrite's unnecessary index on oid, delete unused RULEOID syscache.
Remove index on pg_listener and associated syscache for performance reasons
(caching rows that are certain to change before you need 'em again is
rather pointless).
Change pg_attrdef's nonunique index on adrelid into a unique index on
adrelid+adnum.

Fix various incorrect settings of pg_class.relisshared, make that the
primary reference point for whether a relation is shared or not.
IsSharedSystemRelationName() is now only consulted to initialize relisshared
during initial creation of tables and indexes.  In theory we might now
support shared user relations, though it's not clear how one would get
entries for them into pg_class &etc of multiple databases.

Fix recently reported bug that pg_attribute rows created for an index all have
the same OID.  (Proof that non-unique OID doesn't matter unless it's
actually used to do lookups ;-))

There's no need to treat pg_trigger, pg_attrdef, pg_relcheck as bootstrap
relations.  Convert them into plain system catalogs without hardwired
entries in pg_class and friends.

Unify global.bki and template1.bki into a single init script postgres.bki,
since the alleged distinction between them was misleading and pointless.
Not to mention that it didn't work for setting up indexes on shared
system relations.

Rationalize locking of pg_shadow, pg_group, pg_attrdef (no need to use
AccessExclusiveLock where ExclusiveLock or even RowExclusiveLock will do).
Also, hold locks until transaction commit where necessary.
This commit is contained in:
Tom Lane
2001-06-12 05:55:50 +00:00
parent d2c8358188
commit 1d584f97b9
30 changed files with 471 additions and 647 deletions

View File

@@ -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.77 2001/03/22 03:59:21 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/async.c,v 1.78 2001/06/12 05:55:49 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -80,7 +80,6 @@
#include "access/heapam.h"
#include "catalog/catname.h"
#include "catalog/indexing.h"
#include "catalog/pg_listener.h"
#include "commands/async.h"
#include "lib/dllist.h"
@@ -195,11 +194,12 @@ void
Async_Listen(char *relname, int pid)
{
Relation lRel;
HeapScanDesc scan;
HeapTuple tuple;
Datum values[Natts_pg_listener];
char nulls[Natts_pg_listener];
int i;
TupleDesc tupDesc;
bool alreadyListener = false;
if (Trace_notify)
elog(DEBUG, "Async_Listen: %s", relname);
@@ -207,12 +207,23 @@ Async_Listen(char *relname, int pid)
lRel = heap_openr(ListenerRelationName, AccessExclusiveLock);
/* Detect whether we are already listening on this relname */
if (SearchSysCacheExists(LISTENREL,
Int32GetDatum(pid),
PointerGetDatum(relname),
0, 0))
scan = heap_beginscan(lRel, 0, SnapshotNow, 0, (ScanKey) NULL);
while (HeapTupleIsValid(tuple = heap_getnext(scan, 0)))
{
Form_pg_listener listener = (Form_pg_listener) GETSTRUCT(tuple);
if (listener->listenerpid == pid &&
strncmp(NameStr(listener->relname), relname, NAMEDATALEN) == 0)
{
alreadyListener = true;
/* No need to scan the rest of the table */
break;
}
}
heap_endscan(scan);
if (alreadyListener)
{
/* No need to scan the rest of the table */
heap_close(lRel, AccessExclusiveLock);
elog(NOTICE, "Async_Listen: We are already listening on %s", relname);
return;
@@ -233,9 +244,10 @@ Async_Listen(char *relname, int pid)
values[i++] = (Datum) pid;
values[i++] = (Datum) 0; /* no notifies pending */
tupDesc = lRel->rd_att;
tuple = heap_formtuple(tupDesc, values, nulls);
tuple = heap_formtuple(RelationGetDescr(lRel), values, nulls);
heap_insert(lRel, tuple);
#ifdef NOT_USED /* currently there are no indexes */
if (RelationGetForm(lRel)->relhasindex)
{
Relation idescs[Num_pg_listener_indices];
@@ -244,6 +256,7 @@ Async_Listen(char *relname, int pid)
CatalogIndexInsert(idescs, Num_pg_listener_indices, lRel, tuple);
CatalogCloseIndices(Num_pg_listener_indices, idescs);
}
#endif
heap_freetuple(tuple);
@@ -280,7 +293,8 @@ void
Async_Unlisten(char *relname, int pid)
{
Relation lRel;
HeapTuple lTuple;
HeapScanDesc scan;
HeapTuple tuple;
/* Handle specially the `unlisten "*"' command */
if ((!relname) || (*relname == '\0') || (strcmp(relname, "*") == 0))
@@ -293,16 +307,26 @@ Async_Unlisten(char *relname, int pid)
elog(DEBUG, "Async_Unlisten %s", relname);
lRel = heap_openr(ListenerRelationName, AccessExclusiveLock);
/* Note we assume there can be only one matching tuple. */
lTuple = SearchSysCache(LISTENREL,
Int32GetDatum(pid),
PointerGetDatum(relname),
0, 0);
if (HeapTupleIsValid(lTuple))
scan = heap_beginscan(lRel, 0, SnapshotNow, 0, (ScanKey) NULL);
while (HeapTupleIsValid(tuple = heap_getnext(scan, 0)))
{
simple_heap_delete(lRel, &lTuple->t_self);
ReleaseSysCache(lTuple);
Form_pg_listener listener = (Form_pg_listener) GETSTRUCT(tuple);
if (listener->listenerpid == pid &&
strncmp(NameStr(listener->relname), relname, NAMEDATALEN) == 0)
{
/* Found the matching tuple, delete it */
simple_heap_delete(lRel, &tuple->t_self);
/*
* We assume there can be only one match, so no need
* to scan the rest of the table
*/
break;
}
}
heap_endscan(scan);
heap_close(lRel, AccessExclusiveLock);
/*
@@ -332,7 +356,7 @@ Async_UnlistenAll()
{
Relation lRel;
TupleDesc tdesc;
HeapScanDesc sRel;
HeapScanDesc scan;
HeapTuple lTuple;
ScanKeyData key[1];
@@ -347,12 +371,12 @@ Async_UnlistenAll()
Anum_pg_listener_pid,
F_INT4EQ,
Int32GetDatum(MyProcPid));
sRel = heap_beginscan(lRel, 0, SnapshotNow, 1, key);
scan = heap_beginscan(lRel, 0, SnapshotNow, 1, key);
while (HeapTupleIsValid(lTuple = heap_getnext(sRel, 0)))
while (HeapTupleIsValid(lTuple = heap_getnext(scan, 0)))
simple_heap_delete(lRel, &lTuple->t_self);
heap_endscan(sRel);
heap_endscan(scan);
heap_close(lRel, AccessExclusiveLock);
}
@@ -418,16 +442,12 @@ AtCommit_Notify()
{
Relation lRel;
TupleDesc tdesc;
HeapScanDesc sRel;
HeapScanDesc scan;
HeapTuple lTuple,
rTuple;
Datum d,
value[Natts_pg_listener];
Datum value[Natts_pg_listener];
char repl[Natts_pg_listener],
nulls[Natts_pg_listener];
bool isnull;
char *relname;
int32 listenerPID;
if (!pendingNotifies)
return; /* no NOTIFY statements in this
@@ -446,10 +466,6 @@ AtCommit_Notify()
if (Trace_notify)
elog(DEBUG, "AtCommit_Notify");
lRel = heap_openr(ListenerRelationName, AccessExclusiveLock);
tdesc = RelationGetDescr(lRel);
sRel = heap_beginscan(lRel, 0, SnapshotNow, 0, (ScanKey) NULL);
/* preset data to update notify column to MyProcPid */
nulls[0] = nulls[1] = nulls[2] = ' ';
repl[0] = repl[1] = repl[2] = ' ';
@@ -457,83 +473,81 @@ AtCommit_Notify()
value[0] = value[1] = value[2] = (Datum) 0;
value[Anum_pg_listener_notify - 1] = Int32GetDatum(MyProcPid);
while (HeapTupleIsValid(lTuple = heap_getnext(sRel, 0)))
lRel = heap_openr(ListenerRelationName, AccessExclusiveLock);
tdesc = RelationGetDescr(lRel);
scan = heap_beginscan(lRel, 0, SnapshotNow, 0, (ScanKey) NULL);
while (HeapTupleIsValid(lTuple = heap_getnext(scan, 0)))
{
d = heap_getattr(lTuple, Anum_pg_listener_relname, tdesc, &isnull);
relname = (char *) DatumGetPointer(d);
Form_pg_listener listener = (Form_pg_listener) GETSTRUCT(lTuple);
char *relname = NameStr(listener->relname);
int32 listenerPID = listener->listenerpid;
if (AsyncExistsPendingNotify(relname))
if (! AsyncExistsPendingNotify(relname))
continue;
if (listenerPID == MyProcPid)
{
d = heap_getattr(lTuple, Anum_pg_listener_pid, tdesc, &isnull);
listenerPID = DatumGetInt32(d);
/*
* Self-notify: no need to bother with table update.
* Indeed, we *must not* clear the notification field in
* this path, or we could lose an outside notify, which'd
* be bad for applications that ignore self-notify messages.
*/
if (listenerPID == MyProcPid)
if (Trace_notify)
elog(DEBUG, "AtCommit_Notify: notifying self");
NotifyMyFrontEnd(relname, listenerPID);
}
else
{
if (Trace_notify)
elog(DEBUG, "AtCommit_Notify: notifying pid %d",
listenerPID);
/*
* If someone has already notified this listener, we don't
* bother modifying the table, but we do still send a
* SIGUSR2 signal, just in case that backend missed the
* earlier signal for some reason. It's OK to send the
* signal first, because the other guy can't read
* pg_listener until we unlock it.
*/
if (kill(listenerPID, SIGUSR2) < 0)
{
/*
* Self-notify: no need to bother with table update.
* Indeed, we *must not* clear the notification field in
* this path, or we could lose an outside notify, which'd
* be bad for applications that ignore self-notify
* messages.
* Get rid of pg_listener entry if it refers to a PID
* that no longer exists. Presumably, that backend
* crashed without deleting its pg_listener entries.
* This code used to only delete the entry if
* errno==ESRCH, but as far as I can see we should
* just do it for any failure (certainly at least for
* EPERM too...)
*/
if (Trace_notify)
elog(DEBUG, "AtCommit_Notify: notifying self");
NotifyMyFrontEnd(relname, listenerPID);
simple_heap_delete(lRel, &lTuple->t_self);
}
else
else if (listener->notification == 0)
{
if (Trace_notify)
elog(DEBUG, "AtCommit_Notify: notifying pid %d", listenerPID);
rTuple = heap_modifytuple(lTuple, lRel,
value, nulls, repl);
simple_heap_update(lRel, &lTuple->t_self, rTuple);
/*
* If someone has already notified this listener, we don't
* bother modifying the table, but we do still send a
* SIGUSR2 signal, just in case that backend missed the
* earlier signal for some reason. It's OK to send the
* signal first, because the other guy can't read
* pg_listener until we unlock it.
*/
if (kill(listenerPID, SIGUSR2) < 0)
#ifdef NOT_USED /* currently there are no indexes */
if (RelationGetForm(lRel)->relhasindex)
{
Relation idescs[Num_pg_listener_indices];
/*
* Get rid of pg_listener entry if it refers to a PID
* that no longer exists. Presumably, that backend
* crashed without deleting its pg_listener entries.
* This code used to only delete the entry if
* errno==ESRCH, but as far as I can see we should
* just do it for any failure (certainly at least for
* EPERM too...)
*/
simple_heap_delete(lRel, &lTuple->t_self);
}
else
{
d = heap_getattr(lTuple, Anum_pg_listener_notify,
tdesc, &isnull);
if (DatumGetInt32(d) == 0)
{
rTuple = heap_modifytuple(lTuple, lRel,
value, nulls, repl);
simple_heap_update(lRel, &lTuple->t_self, rTuple);
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);
}
}
CatalogOpenIndices(Num_pg_listener_indices, Name_pg_listener_indices, idescs);
CatalogIndexInsert(idescs, Num_pg_listener_indices, lRel, rTuple);
CatalogCloseIndices(Num_pg_listener_indices, idescs);
}
#endif
}
}
}
heap_endscan(sRel);
heap_endscan(scan);
/*
* We do NOT release the lock on pg_listener here; we need to hold it
@@ -745,16 +759,12 @@ ProcessIncomingNotify(void)
Relation lRel;
TupleDesc tdesc;
ScanKeyData key[1];
HeapScanDesc sRel;
HeapScanDesc scan;
HeapTuple lTuple,
rTuple;
Datum d,
value[Natts_pg_listener];
Datum value[Natts_pg_listener];
char repl[Natts_pg_listener],
nulls[Natts_pg_listener];
bool isnull;
char *relname;
int32 sourcePID;
if (Trace_notify)
elog(DEBUG, "ProcessIncomingNotify");
@@ -773,7 +783,7 @@ ProcessIncomingNotify(void)
Anum_pg_listener_pid,
F_INT4EQ,
Int32GetDatum(MyProcPid));
sRel = heap_beginscan(lRel, 0, SnapshotNow, 1, key);
scan = heap_beginscan(lRel, 0, SnapshotNow, 1, key);
/* Prepare data for rewriting 0 into notification field */
nulls[0] = nulls[1] = nulls[2] = ' ';
@@ -782,14 +792,14 @@ ProcessIncomingNotify(void)
value[0] = value[1] = value[2] = (Datum) 0;
value[Anum_pg_listener_notify - 1] = Int32GetDatum(0);
while (HeapTupleIsValid(lTuple = heap_getnext(sRel, 0)))
while (HeapTupleIsValid(lTuple = heap_getnext(scan, 0)))
{
d = heap_getattr(lTuple, Anum_pg_listener_notify, tdesc, &isnull);
sourcePID = DatumGetInt32(d);
Form_pg_listener listener = (Form_pg_listener) GETSTRUCT(lTuple);
char *relname = NameStr(listener->relname);
int32 sourcePID = listener->notification;
if (sourcePID != 0)
{
d = heap_getattr(lTuple, Anum_pg_listener_relname, tdesc, &isnull);
relname = (char *) DatumGetPointer(d);
/* Notify the frontend */
if (Trace_notify)
@@ -800,6 +810,8 @@ ProcessIncomingNotify(void)
/* Rewrite the tuple with 0 in notification column */
rTuple = heap_modifytuple(lTuple, lRel, value, nulls, repl);
simple_heap_update(lRel, &lTuple->t_self, rTuple);
#ifdef NOT_USED /* currently there are no indexes */
if (RelationGetForm(lRel)->relhasindex)
{
Relation idescs[Num_pg_listener_indices];
@@ -808,9 +820,10 @@ ProcessIncomingNotify(void)
CatalogIndexInsert(idescs, Num_pg_listener_indices, lRel, rTuple);
CatalogCloseIndices(Num_pg_listener_indices, idescs);
}
#endif
}
}
heap_endscan(sRel);
heap_endscan(scan);
/*
* We do NOT release the lock on pg_listener here; we need to hold it
@@ -876,7 +889,7 @@ AsyncExistsPendingNotify(char *relname)
p = DLGetSucc(p))
{
/* Use NAMEDATALEN for relname comparison. DZ - 26-08-1996 */
if (!strncmp((const char *) DLE_VAL(p), relname, NAMEDATALEN))
if (strncmp((const char *) DLE_VAL(p), relname, NAMEDATALEN) == 0)
return 1;
}

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.132 2001/06/07 00:09:28 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.133 2001/06/12 05:55:49 tgl Exp $
*
* NOTES
* The PerformAddAttribute() code, like most of the relation
@@ -411,8 +411,8 @@ AlterTableAddColumn(const char *relationName,
attributeD.attrelid = reltup->t_data->t_oid;
attributeTuple = heap_addheader(Natts_pg_attribute,
sizeof attributeD,
(char *) &attributeD);
ATTRIBUTE_TUPLE_SIZE,
(void *) &attributeD);
attribute = (Form_pg_attribute) GETSTRUCT(attributeTuple);
@@ -481,7 +481,7 @@ AlterTableAddColumn(const char *relationName,
if (hasindex)
CatalogCloseIndices(Num_pg_attr_indices, idescs);
heap_close(attrdesc, RowExclusiveLock);
heap_close(attrdesc, NoLock);
/*
* Update number of attributes in pg_class tuple
@@ -580,17 +580,15 @@ AlterTableAlterColumnDefault(const char *relationName,
PointerGetDatum(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;
ReleaseSysCache(tuple);
if (newDefault) /* SET DEFAULT */
if (newDefault)
{
/* SET DEFAULT */
List *rawDefaults = NIL;
RawColumnDefault *rawEnt;
@@ -608,15 +606,14 @@ AlterTableAlterColumnDefault(const char *relationName,
*/
AddRelationRawConstraints(rel, rawDefaults, NIL);
}
else
/* DROP DEFAULT */
{
/* DROP DEFAULT */
Relation attr_rel;
ScanKeyData scankeys[3];
HeapScanDesc scan;
attr_rel = heap_openr(AttributeRelationName, AccessExclusiveLock);
attr_rel = heap_openr(AttributeRelationName, RowExclusiveLock);
ScanKeyEntryInitialize(&scankeys[0], 0x0,
Anum_pg_attribute_attrelid, F_OIDEQ,
ObjectIdGetDatum(myrelid));
@@ -665,7 +662,7 @@ drop_default(Oid relid, int16 attnum)
Relation attrdef_rel;
HeapTuple tuple;
attrdef_rel = heap_openr(AttrDefaultRelationName, AccessExclusiveLock);
attrdef_rel = heap_openr(AttrDefaultRelationName, RowExclusiveLock);
ScanKeyEntryInitialize(&scankeys[0], 0x0,
Anum_pg_attrdef_adrelid, F_OIDEQ,
ObjectIdGetDatum(relid));
@@ -778,7 +775,7 @@ AlterTableAlterColumnStatistics(const char *relationName,
}
heap_freetuple(tuple);
heap_close(attrelation, RowExclusiveLock);
heap_close(attrelation, NoLock);
}
@@ -1010,8 +1007,7 @@ AlterTableDropColumn(const char *relationName,
{
#ifdef _DROP_COLUMN_HACK__
Relation rel,
attrdesc,
adrel;
attrdesc;
Oid myrelid,
attoid;
HeapTuple reltup;
@@ -1023,8 +1019,6 @@ AlterTableDropColumn(const char *relationName,
int attnum;
bool hasindex;
char dropColname[32];
void *sysscan;
ScanKeyData scankeys[2];
if (inh)
elog(ERROR, "ALTER TABLE / DROP COLUMN with inherit option is not supported yet");
@@ -1136,30 +1130,9 @@ AlterTableDropColumn(const char *relationName,
/* delete comments */
DeleteComments(attoid);
/* 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);
*--------
*/
sysscan = systable_beginscan(adrel, AttrDefaultIndex, 1, scankeys);
while (HeapTupleIsValid(tup = systable_getnext(sysscan)))
{
if (((Form_pg_attrdef) GETSTRUCT(tup))->adnum == attnum)
{
simple_heap_delete(adrel, &tup->t_self);
break;
}
}
systable_endscan(sysscan);
heap_close(adrel, NoLock);
/* delete attrdef */
drop_default(myrelid, attnum);
/*
* Remove objects which reference this column
@@ -1318,10 +1291,7 @@ AlterTableAddConstraint(char *relationName,
heap_endscan(scan);
if (!successful)
{
heap_close(rel, NoLock);
elog(ERROR, "AlterTableAddConstraint: rejected due to CHECK constraint %s", name);
}
/*
* Call AddRelationRawConstraints to do the
@@ -1768,7 +1738,7 @@ AlterTableOwner(const char *relationName, const char *newOwnerName)
* unlock everything and return
*/
heap_freetuple(tuple);
heap_close(class_rel, RowExclusiveLock);
heap_close(class_rel, NoLock);
}

View File

@@ -1,6 +1,7 @@
/*-------------------------------------------------------------------------
*
* dbcommands.c
* Database management commands (create/drop database).
*
*
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
@@ -8,7 +9,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.74 2001/03/22 03:59:22 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.75 2001/06/12 05:55:49 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -25,6 +26,7 @@
#include "catalog/catalog.h"
#include "catalog/pg_database.h"
#include "catalog/pg_shadow.h"
#include "catalog/indexing.h"
#include "commands/comment.h"
#include "commands/dbcommands.h"
#include "miscadmin.h"
@@ -241,9 +243,8 @@ createdb(const char *dbname, const char *dbpath,
heap_insert(pg_database_rel, tuple);
/*
* Update indexes (there aren't any currently)
* Update indexes
*/
#ifdef Num_pg_database_indices
if (RelationGetForm(pg_database_rel)->relhasindex)
{
Relation idescs[Num_pg_database_indices];
@@ -254,7 +255,6 @@ createdb(const char *dbname, const char *dbpath,
tuple);
CatalogCloseIndices(Num_pg_database_indices, idescs);
}
#endif
/* Close pg_database, but keep lock till commit */
heap_close(pg_database_rel, NoLock);
@@ -333,7 +333,7 @@ dropdb(const char *dbname)
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).
* Find the database's tuple by OID (should be unique).
*/
ScanKeyEntryInitialize(&key, 0, ObjectIdAttributeNumber,
F_OIDEQ, ObjectIdGetDatum(db_id));
@@ -343,7 +343,6 @@ dropdb(const char *dbname)
tup = heap_getnext(pgdbscan, 0);
if (!HeapTupleIsValid(tup))
{
/*
* This error should never come up since the existence of the
* database is checked earlier

View File

@@ -6,17 +6,17 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Header: /cvsroot/pgsql/src/backend/commands/user.c,v 1.75 2001/03/22 06:16:12 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/user.c,v 1.76 2001/06/12 05:55:49 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "postgres.h"
#include "access/heapam.h"
#include "catalog/catname.h"
#include "catalog/pg_database.h"
@@ -31,9 +31,9 @@
#include "utils/fmgroids.h"
#include "utils/syscache.h"
static void CheckPgUserAclNotNull(void);
#define SQL_LENGTH 512
/*---------------------------------------------------------------------
* write_password_file / update_pg_pwd
@@ -121,8 +121,7 @@ write_password_file(Relation rel)
"%s"
CRYPT_PWD_FILE_SEPSTR
"%s\n",
DatumGetCString(DirectFunctionCall1(nameout,
NameGetDatum(DatumGetName(datum_n)))),
DatumGetCString(DirectFunctionCall1(nameout, datum_n)),
null_p ? "" :
DatumGetCString(DirectFunctionCall1(textout, datum_p)),
null_v ? "\\N" :
@@ -168,10 +167,16 @@ write_password_file(Relation rel)
Datum
update_pg_pwd(PG_FUNCTION_ARGS)
{
Relation rel = heap_openr(ShadowRelationName, AccessExclusiveLock);
/*
* ExclusiveLock ensures no one modifies pg_shadow while we read it,
* and that only one backend rewrites the flat file at a time. It's
* OK to allow normal reads of pg_shadow in parallel, however.
*/
Relation rel = heap_openr(ShadowRelationName, ExclusiveLock);
write_password_file(rel);
heap_close(rel, AccessExclusiveLock);
/* OK to release lock, since we did not modify the relation */
heap_close(rel, ExclusiveLock);
return PointerGetDatum(NULL);
}
@@ -210,39 +215,41 @@ CreateUser(CreateUserStmt *stmt)
* 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_rel = heap_openr(ShadowRelationName, ExclusiveLock);
pg_shadow_dsc = RelationGetDescr(pg_shadow_rel);
scan = heap_beginscan(pg_shadow_rel, false, SnapshotNow, 0, NULL);
while (!user_exists && !sysid_exists && HeapTupleIsValid(tuple = heap_getnext(scan, 0)))
while (!user_exists && !sysid_exists &&
HeapTupleIsValid(tuple = heap_getnext(scan, 0)))
{
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_usename,
pg_shadow_dsc, &null);
Assert(!null);
user_exists = (strcmp((char *) DatumGetName(datum), stmt->user) == 0);
datum = heap_getattr(tuple, Anum_pg_shadow_usesysid, pg_shadow_dsc, &null);
datum = heap_getattr(tuple, Anum_pg_shadow_usesysid,
pg_shadow_dsc, &null);
Assert(!null);
if (havesysid) /* customized id wanted */
sysid_exists = datum && !null && ((int) datum == stmt->sysid);
sysid_exists = (DatumGetInt32(datum) == stmt->sysid);
else
/* pick 1 + max */
{
if ((int) datum > max_id)
max_id = (int) datum;
/* pick 1 + max */
if (DatumGetInt32(datum) > max_id)
max_id = DatumGetInt32(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);
return;
}
if (user_exists)
elog(ERROR, "CREATE USER: user name \"%s\" already exists",
stmt->user);
if (sysid_exists)
elog(ERROR, "CREATE USER: sysid %d is already assigned",
stmt->sysid);
/*
* Build a tuple to insert
@@ -252,12 +259,12 @@ CreateUser(CreateUserStmt *stmt)
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);
new_record[Anum_pg_shadow_usecreatedb - 1] = BoolGetDatum(stmt->createdb);
new_record[Anum_pg_shadow_usetrace - 1] = BoolGetDatum(false);
AssertState(BoolIsValid(stmt->createuser));
new_record[Anum_pg_shadow_usesuper - 1] = (Datum) (stmt->createuser);
new_record[Anum_pg_shadow_usesuper - 1] = BoolGetDatum(stmt->createuser);
/* superuser gets catupd right by default */
new_record[Anum_pg_shadow_usecatupd - 1] = (Datum) (stmt->createuser);
new_record[Anum_pg_shadow_usecatupd - 1] = BoolGetDatum(stmt->createuser);
if (stmt->password)
new_record[Anum_pg_shadow_passwd - 1] =
@@ -278,13 +285,11 @@ CreateUser(CreateUserStmt *stmt)
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
* Insert new record in the pg_shadow table
*/
if (heap_insert(pg_shadow_rel, tuple) == InvalidOid)
elog(ERROR, "CREATE USER: heap_insert failed");
heap_insert(pg_shadow_rel, tuple);
/*
* Update indexes
@@ -322,9 +327,9 @@ CreateUser(CreateUserStmt *stmt)
write_password_file(pg_shadow_rel);
/*
* Now we can clean up.
* Now we can clean up; but keep lock until commit.
*/
heap_close(pg_shadow_rel, AccessExclusiveLock);
heap_close(pg_shadow_rel, NoLock);
}
@@ -348,8 +353,11 @@ AlterUser(AlterUserStmt *stmt)
/* must be superuser or just want to change your own password */
if (!superuser() &&
!(stmt->createdb == 0 && stmt->createuser == 0 && !stmt->validUntil
&& stmt->password && strcmp(GetUserName(GetUserId()), stmt->user) == 0))
!(stmt->createdb == 0 &&
stmt->createuser == 0 &&
!stmt->validUntil &&
stmt->password &&
strcmp(GetUserName(GetUserId()), stmt->user) == 0))
elog(ERROR, "ALTER USER: permission denied");
/* changes to the flat password file cannot be rolled back */
@@ -361,17 +369,14 @@ AlterUser(AlterUserStmt *stmt)
* secure exclusive lock to protect our update of the flat password
* file.
*/
pg_shadow_rel = heap_openr(ShadowRelationName, AccessExclusiveLock);
pg_shadow_rel = heap_openr(ShadowRelationName, ExclusiveLock);
pg_shadow_dsc = RelationGetDescr(pg_shadow_rel);
tuple = SearchSysCache(SHADOWNAME,
PointerGetDatum(stmt->user),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
{
heap_close(pg_shadow_rel, AccessExclusiveLock);
elog(ERROR, "ALTER USER: user \"%s\" does not exist", stmt->user);
}
/*
* Build a tuple to update, perusing the information just obtained
@@ -483,8 +488,7 @@ AlterUser(AlterUserStmt *stmt)
/*
* Now we can clean up.
*/
heap_close(pg_shadow_rel, AccessExclusiveLock);
heap_close(pg_shadow_rel, NoLock);
}
@@ -510,7 +514,7 @@ DropUser(DropUserStmt *stmt)
* deleted. Note we secure exclusive lock, because we need to protect
* our update of the flat password file.
*/
pg_shadow_rel = heap_openr(ShadowRelationName, AccessExclusiveLock);
pg_shadow_rel = heap_openr(ShadowRelationName, ExclusiveLock);
pg_shadow_dsc = RelationGetDescr(pg_shadow_rel);
foreach(item, stmt->users)
@@ -530,11 +534,8 @@ DropUser(DropUserStmt *stmt)
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));
@@ -546,28 +547,30 @@ DropUser(DropUserStmt *stmt)
* 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_rel = heap_openr(DatabaseRelationName, AccessShareLock);
pg_dsc = RelationGetDescr(pg_rel);
ScanKeyEntryInitialize(&scankey, 0x0, Anum_pg_database_datdba, F_INT4EQ,
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);
char *dbname;
datum = heap_getattr(tmp_tuple, Anum_pg_database_datname,
pg_dsc, &null);
Assert(!null);
dbname = DatumGetCString(DirectFunctionCall1(nameout, datum));
elog(ERROR, "DROP USER: user \"%s\" owns database \"%s\", cannot be removed%s",
user,
DatumGetCString(DirectFunctionCall1(nameout,
NameGetDatum(DatumGetName(datum)))),
(length(stmt->users) > 1) ? " (no users removed)" : ""
);
user, dbname,
(length(stmt->users) > 1) ? " (no users removed)" : "");
}
heap_endscan(scan);
heap_close(pg_rel, AccessExclusiveLock);
heap_close(pg_rel, AccessShareLock);
/*
* Somehow we'd have to check for tables, views, etc. owned by the
@@ -587,7 +590,7 @@ DropUser(DropUserStmt *stmt)
*
* try calling alter group drop user for every group
*/
pg_rel = heap_openr(GroupRelationName, AccessExclusiveLock);
pg_rel = heap_openr(GroupRelationName, ExclusiveLock);
pg_dsc = RelationGetDescr(pg_rel);
scan = heap_beginscan(pg_rel, false, SnapshotNow, 0, NULL);
while (HeapTupleIsValid(tmp_tuple = heap_getnext(scan, 0)))
@@ -602,7 +605,7 @@ DropUser(DropUserStmt *stmt)
AlterGroup(&ags, "DROP USER");
}
heap_endscan(scan);
heap_close(pg_rel, AccessExclusiveLock);
heap_close(pg_rel, ExclusiveLock);
/*
* Advance command counter so that later iterations of this loop
@@ -622,7 +625,7 @@ DropUser(DropUserStmt *stmt)
/*
* Now we can clean up.
*/
heap_close(pg_shadow_rel, AccessExclusiveLock);
heap_close(pg_shadow_rel, NoLock);
}
@@ -681,38 +684,41 @@ CreateGroup(CreateGroupStmt *stmt)
if (!superuser())
elog(ERROR, "CREATE GROUP: permission denied");
pg_group_rel = heap_openr(GroupRelationName, AccessExclusiveLock);
pg_group_rel = heap_openr(GroupRelationName, ExclusiveLock);
pg_group_dsc = RelationGetDescr(pg_group_rel);
scan = heap_beginscan(pg_group_rel, false, SnapshotNow, 0, NULL);
while (!group_exists && !sysid_exists && HeapTupleIsValid(tuple = heap_getnext(scan, false)))
while (!group_exists && !sysid_exists &&
HeapTupleIsValid(tuple = heap_getnext(scan, false)))
{
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_groname,
pg_group_dsc, &null);
Assert(!null);
group_exists = (strcmp((char *) DatumGetName(datum), stmt->name) == 0);
datum = heap_getattr(tuple, Anum_pg_group_grosysid, pg_group_dsc, &null);
datum = heap_getattr(tuple, Anum_pg_group_grosysid,
pg_group_dsc, &null);
Assert(!null);
if (stmt->sysid >= 0) /* customized id wanted */
sysid_exists = datum && !null && ((int) datum == stmt->sysid);
sysid_exists = (DatumGetInt32(datum) == stmt->sysid);
else
/* pick 1 + max */
{
if ((int) datum > max_id)
max_id = (int) datum;
/* pick 1 + max */
if (DatumGetInt32(datum) > max_id)
max_id = DatumGetInt32(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);
if (sysid_exists)
elog(ERROR, "CREATE GROUP: group sysid %d is already assigned",
stmt->sysid);
/*
* Translate the given user names to ids
@@ -790,7 +796,7 @@ CreateGroup(CreateGroupStmt *stmt)
CatalogCloseIndices(Num_pg_group_indices, idescs);
}
heap_close(pg_group_rel, AccessExclusiveLock);
heap_close(pg_group_rel, NoLock);
}
@@ -811,7 +817,7 @@ AlterGroup(AlterGroupStmt *stmt, const char *tag)
if (!superuser())
elog(ERROR, "%s: permission denied", tag);
pg_group_rel = heap_openr(GroupRelationName, AccessExclusiveLock);
pg_group_rel = heap_openr(GroupRelationName, ExclusiveLock);
pg_group_dsc = RelationGetDescr(pg_group_rel);
/*
@@ -1052,7 +1058,7 @@ AlterGroup(AlterGroupStmt *stmt, const char *tag)
ReleaseSysCache(group_tuple);
heap_close(pg_group_rel, AccessExclusiveLock);
heap_close(pg_group_rel, NoLock);
}
@@ -1078,7 +1084,7 @@ DropGroup(DropGroupStmt *stmt)
/*
* Scan the pg_group table and delete all matching groups.
*/
pg_group_rel = heap_openr(GroupRelationName, AccessExclusiveLock);
pg_group_rel = heap_openr(GroupRelationName, ExclusiveLock);
pg_group_dsc = RelationGetDescr(pg_group_rel);
scan = heap_beginscan(pg_group_rel, false, SnapshotNow, 0, NULL);
@@ -1087,8 +1093,9 @@ DropGroup(DropGroupStmt *stmt)
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)
datum = heap_getattr(tuple, Anum_pg_group_groname,
pg_group_dsc, &null);
if (!null && strcmp((char *) DatumGetName(datum), stmt->name) == 0)
{
gro_exists = true;
simple_heap_delete(pg_group_rel, &tuple->t_self);
@@ -1101,10 +1108,7 @@ DropGroup(DropGroupStmt *stmt)
* 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);
heap_close(pg_group_rel, NoLock);
}