1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* heap.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* code to create and destroy POSTGRES heap relations
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2000-11-08 23:10:03 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.151 2000/11/08 22:09:56 tgl Exp $
|
1999-02-02 04:45:56 +01:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* INTERFACE ROUTINES
|
1997-11-28 05:40:40 +01:00
|
|
|
* heap_create() - Create an uncataloged heap relation
|
1997-11-28 18:28:02 +01:00
|
|
|
* heap_create_with_catalog() - Create a cataloged relation
|
1999-12-10 04:56:14 +01:00
|
|
|
* heap_drop_with_catalog() - Removes named relation from catalogs
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* this code taken from access/heap/create.c, which contains
|
1998-08-06 07:13:14 +02:00
|
|
|
* the old heap_create_with_catalog, amcreate, and amdestroy.
|
1997-11-28 05:40:40 +01:00
|
|
|
* those routines will soon call these routines using the function
|
|
|
|
* manager,
|
1997-09-07 07:04:48 +02:00
|
|
|
* just like the poorly named "NewXXX" routines do. The
|
|
|
|
* "New" routines are all going to die soon, once and for all!
|
|
|
|
* -cim 1/13/91
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/heapam.h"
|
1999-09-23 19:03:39 +02:00
|
|
|
#include "access/genam.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "catalog/catalog.h"
|
|
|
|
#include "catalog/catname.h"
|
|
|
|
#include "catalog/heap.h"
|
|
|
|
#include "catalog/index.h"
|
|
|
|
#include "catalog/indexing.h"
|
|
|
|
#include "catalog/pg_attrdef.h"
|
|
|
|
#include "catalog/pg_inherits.h"
|
2000-06-15 05:33:12 +02:00
|
|
|
#include "catalog/pg_index.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "catalog/pg_ipl.h"
|
1999-09-23 19:03:39 +02:00
|
|
|
#include "catalog/pg_proc.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "catalog/pg_relcheck.h"
|
1999-11-28 03:03:04 +01:00
|
|
|
#include "catalog/pg_statistic.h"
|
1999-10-04 04:12:26 +02:00
|
|
|
#include "catalog/pg_type.h"
|
1999-10-26 05:12:39 +02:00
|
|
|
#include "commands/comment.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "commands/trigger.h"
|
2000-01-16 20:57:00 +01:00
|
|
|
#include "miscadmin.h"
|
1999-10-04 01:55:40 +02:00
|
|
|
#include "optimizer/clauses.h"
|
|
|
|
#include "optimizer/planmain.h"
|
|
|
|
#include "optimizer/var.h"
|
2000-02-15 04:38:29 +01:00
|
|
|
#include "nodes/makefuncs.h"
|
1999-10-04 01:55:40 +02:00
|
|
|
#include "parser/parse_clause.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "parser/parse_expr.h"
|
1999-10-04 01:55:40 +02:00
|
|
|
#include "parser/parse_relation.h"
|
1999-10-04 04:12:26 +02:00
|
|
|
#include "parser/parse_target.h"
|
2000-06-15 05:33:12 +02:00
|
|
|
#include "parser/parse_type.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "rewrite/rewriteRemove.h"
|
|
|
|
#include "storage/smgr.h"
|
|
|
|
#include "utils/builtins.h"
|
2000-05-21 01:11:29 +02:00
|
|
|
#include "utils/catcache.h"
|
2000-05-28 19:56:29 +02:00
|
|
|
#include "utils/fmgroids.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "utils/relcache.h"
|
1998-08-19 04:04:17 +02:00
|
|
|
#include "utils/syscache.h"
|
1999-02-02 04:45:56 +01:00
|
|
|
#include "utils/temprel.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
static void AddNewRelationTuple(Relation pg_class_desc,
|
2000-04-12 19:17:23 +02:00
|
|
|
Relation new_rel_desc, Oid new_rel_oid,
|
|
|
|
int natts,
|
|
|
|
char relkind, char *temp_relname);
|
1999-02-02 04:45:56 +01:00
|
|
|
static void DeleteAttributeTuples(Relation rel);
|
|
|
|
static void DeleteRelationTuple(Relation rel);
|
|
|
|
static void DeleteTypeTuple(Relation rel);
|
1997-09-08 04:41:22 +02:00
|
|
|
static void RelationRemoveIndexes(Relation relation);
|
|
|
|
static void RelationRemoveInheritance(Relation relation);
|
1999-02-02 04:45:56 +01:00
|
|
|
static void AddNewRelationType(char *typeName, Oid new_rel_oid);
|
1999-10-04 01:55:40 +02:00
|
|
|
static void StoreAttrDefault(Relation rel, AttrNumber attnum, char *adbin,
|
2000-04-12 19:17:23 +02:00
|
|
|
bool updatePgAttribute);
|
1999-10-04 01:55:40 +02:00
|
|
|
static void StoreRelCheck(Relation rel, char *ccname, char *ccbin);
|
1997-09-08 04:41:22 +02:00
|
|
|
static void StoreConstraints(Relation rel);
|
|
|
|
static void RemoveConstraints(Relation rel);
|
1999-11-28 03:03:04 +01:00
|
|
|
static void RemoveStatistics(Relation rel);
|
1997-08-19 23:40:56 +02:00
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* XXX UGLY HARD CODED BADNESS FOLLOWS XXX
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* these should all be moved to someplace in the lib/catalog
|
|
|
|
* module, if not obliterated first.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note:
|
1997-09-07 07:04:48 +02:00
|
|
|
* Should the executor special case these attributes in the future?
|
|
|
|
* Advantage: consume 1/2 the space in the ATTRIBUTE relation.
|
|
|
|
* Disadvantage: having rules to compute values in these tuples may
|
|
|
|
* be more difficult if not impossible.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
static FormData_pg_attribute a1 = {
|
1998-07-20 21:21:45 +02:00
|
|
|
0xffffffff, {"ctid"}, TIDOID, 0, sizeof(ItemPointerData),
|
1999-12-20 11:40:43 +01:00
|
|
|
SelfItemPointerAttributeNumber, 0, -1, -1, '\0', 'p', '\0', 'i', '\0', '\0'
|
1996-07-09 08:22:35 +02:00
|
|
|
};
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
static FormData_pg_attribute a2 = {
|
1998-07-20 21:21:45 +02:00
|
|
|
0xffffffff, {"oid"}, OIDOID, 0, sizeof(Oid),
|
1999-12-20 11:40:43 +01:00
|
|
|
ObjectIdAttributeNumber, 0, -1, -1, '\001', 'p', '\0', 'i', '\0', '\0'
|
1996-07-09 08:22:35 +02:00
|
|
|
};
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
static FormData_pg_attribute a3 = {
|
1998-07-20 21:21:45 +02:00
|
|
|
0xffffffff, {"xmin"}, XIDOID, 0, sizeof(TransactionId),
|
1999-12-20 11:40:43 +01:00
|
|
|
MinTransactionIdAttributeNumber, 0, -1, -1, '\001', 'p', '\0', 'i', '\0', '\0'
|
1996-07-09 08:22:35 +02:00
|
|
|
};
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
static FormData_pg_attribute a4 = {
|
1998-07-20 21:21:45 +02:00
|
|
|
0xffffffff, {"cmin"}, CIDOID, 0, sizeof(CommandId),
|
1999-12-20 11:40:43 +01:00
|
|
|
MinCommandIdAttributeNumber, 0, -1, -1, '\001', 'p', '\0', 'i', '\0', '\0'
|
1996-07-09 08:22:35 +02:00
|
|
|
};
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
static FormData_pg_attribute a5 = {
|
1998-07-20 21:21:45 +02:00
|
|
|
0xffffffff, {"xmax"}, XIDOID, 0, sizeof(TransactionId),
|
1999-12-20 11:40:43 +01:00
|
|
|
MaxTransactionIdAttributeNumber, 0, -1, -1, '\001', 'p', '\0', 'i', '\0', '\0'
|
1996-07-09 08:22:35 +02:00
|
|
|
};
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
static FormData_pg_attribute a6 = {
|
1998-07-20 21:21:45 +02:00
|
|
|
0xffffffff, {"cmax"}, CIDOID, 0, sizeof(CommandId),
|
1999-12-20 11:40:43 +01:00
|
|
|
MaxCommandIdAttributeNumber, 0, -1, -1, '\001', 'p', '\0', 'i', '\0', '\0'
|
1996-07-09 08:22:35 +02:00
|
|
|
};
|
|
|
|
|
2000-10-11 23:28:19 +02:00
|
|
|
/*
|
2000-07-03 00:01:27 +02:00
|
|
|
We decide to call this attribute "tableoid" rather than say
|
|
|
|
"classoid" on the basis that in the future there may be more than one
|
|
|
|
table of a particular class/type. In any case table is still the word
|
2000-10-11 23:28:19 +02:00
|
|
|
used in SQL.
|
2000-07-03 00:01:27 +02:00
|
|
|
*/
|
|
|
|
static FormData_pg_attribute a7 = {
|
|
|
|
0xffffffff, {"tableoid"}, OIDOID, 0, sizeof(Oid),
|
|
|
|
TableOidAttributeNumber, 0, -1, -1, '\001', 'p', '\0', 'i', '\0', '\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
static Form_pg_attribute HeapAtt[] = {&a1, &a2, &a3, &a4, &a5, &a6, &a7};
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* XXX END OF UGLY HARD CODED BADNESS XXX
|
2000-07-03 00:01:27 +02:00
|
|
|
* ---------------------------------------------------------------- */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-11-28 05:40:40 +01:00
|
|
|
* heap_create - Create an uncataloged heap relation
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Fields relpages, reltuples, reltuples, relkeys, relhistory,
|
1998-08-19 04:04:17 +02:00
|
|
|
* relisindexed, and relkind of rel->rd_rel are initialized
|
1997-09-07 07:04:48 +02:00
|
|
|
* to all zeros, as are rd_last and rd_hook. Rd_refcnt is set to 1.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Remove the system relation specific code to elsewhere eventually.
|
|
|
|
*
|
|
|
|
* Eventually, must place information about this temporary relation
|
|
|
|
* into the transaction context block.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-06-19 00:44:35 +02:00
|
|
|
* NOTE: if istemp is TRUE then heap_create will overwrite relname with
|
|
|
|
* the unique "real" name chosen for the temp relation.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-06-19 00:44:35 +02:00
|
|
|
* If storage_create is TRUE then heap_storage_create is called here,
|
|
|
|
* else caller must call heap_storage_create later.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Relation
|
1999-02-02 04:45:56 +01:00
|
|
|
heap_create(char *relname,
|
|
|
|
TupleDesc tupDesc,
|
1999-11-04 09:01:09 +01:00
|
|
|
bool istemp,
|
2000-07-04 08:11:54 +02:00
|
|
|
bool storage_create,
|
|
|
|
bool allow_system_table_mods)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-05-21 01:11:29 +02:00
|
|
|
static unsigned int uniqueId = 0;
|
|
|
|
|
2000-10-16 16:52:28 +02:00
|
|
|
Oid relid;
|
|
|
|
Relation rel;
|
|
|
|
bool nailme = false;
|
|
|
|
int natts = tupDesc->natts;
|
|
|
|
int i;
|
|
|
|
MemoryContext oldcxt;
|
|
|
|
Oid tblNode = MyDatabaseId;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* sanity checks
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
AssertArg(natts > 0);
|
|
|
|
|
2000-07-04 08:11:54 +02:00
|
|
|
if (relname && !allow_system_table_mods &&
|
2000-01-16 20:57:00 +01:00
|
|
|
IsSystemRelationName(relname) && IsNormalProcessingMode())
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-02-23 08:54:03 +01:00
|
|
|
elog(ERROR, "Illegal class name '%s'"
|
|
|
|
"\n\tThe 'pg_' name prefix is reserved for system catalogs",
|
1997-09-07 07:04:48 +02:00
|
|
|
relname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* real ugly stuff to assign the proper relid in the relation
|
|
|
|
* descriptor follows.
|
|
|
|
* ----------------
|
|
|
|
*/
|
2000-10-16 16:52:28 +02:00
|
|
|
if (relname && IsSystemRelationName(relname))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-10-16 16:52:28 +02:00
|
|
|
if (strcmp(TypeRelationName, relname) == 0)
|
|
|
|
{
|
|
|
|
nailme = true;
|
|
|
|
relid = RelOid_pg_type;
|
|
|
|
}
|
|
|
|
else if (strcmp(AttributeRelationName, relname) == 0)
|
|
|
|
{
|
|
|
|
nailme = true;
|
|
|
|
relid = RelOid_pg_attribute;
|
|
|
|
}
|
|
|
|
else if (strcmp(ProcedureRelationName, relname) == 0)
|
|
|
|
{
|
|
|
|
nailme = true;
|
|
|
|
relid = RelOid_pg_proc;
|
|
|
|
}
|
|
|
|
else if (strcmp(RelationRelationName, relname) == 0)
|
|
|
|
{
|
|
|
|
nailme = true;
|
|
|
|
relid = RelOid_pg_class;
|
|
|
|
}
|
|
|
|
else if (strcmp(ShadowRelationName, relname) == 0)
|
|
|
|
{
|
|
|
|
tblNode = InvalidOid;
|
|
|
|
relid = RelOid_pg_shadow;
|
|
|
|
}
|
|
|
|
else if (strcmp(GroupRelationName, relname) == 0)
|
|
|
|
{
|
|
|
|
tblNode = InvalidOid;
|
|
|
|
relid = RelOid_pg_group;
|
|
|
|
}
|
|
|
|
else if (strcmp(DatabaseRelationName, relname) == 0)
|
|
|
|
{
|
|
|
|
tblNode = InvalidOid;
|
|
|
|
relid = RelOid_pg_database;
|
|
|
|
}
|
|
|
|
else if (strcmp(VariableRelationName, relname) == 0)
|
|
|
|
{
|
|
|
|
tblNode = InvalidOid;
|
|
|
|
relid = RelOid_pg_variable;
|
|
|
|
}
|
|
|
|
else if (strcmp(LogRelationName, relname) == 0)
|
|
|
|
{
|
|
|
|
tblNode = InvalidOid;
|
|
|
|
relid = RelOid_pg_log;
|
|
|
|
}
|
|
|
|
else if (strcmp(AttrDefaultRelationName, relname) == 0)
|
|
|
|
relid = RelOid_pg_attrdef;
|
|
|
|
else if (strcmp(RelCheckRelationName, relname) == 0)
|
|
|
|
relid = RelOid_pg_relcheck;
|
|
|
|
else if (strcmp(TriggerRelationName, relname) == 0)
|
|
|
|
relid = RelOid_pg_trigger;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
relid = newoid();
|
|
|
|
if (IsSharedSystemRelationName(relname))
|
|
|
|
tblNode = InvalidOid;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
|
|
|
relid = newoid();
|
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
if (istemp)
|
|
|
|
{
|
2000-06-19 00:44:35 +02:00
|
|
|
/* replace relname of caller with a unique name for a temp relation */
|
|
|
|
snprintf(relname, NAMEDATALEN, "pg_temp.%d.%u",
|
|
|
|
(int) MyProcPid, uniqueId++);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
/* ----------------
|
|
|
|
* switch to the cache context to create the relcache entry.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (!CacheMemoryContext)
|
|
|
|
CreateCacheMemoryContext();
|
|
|
|
|
|
|
|
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
|
|
|
* allocate a new relation descriptor.
|
|
|
|
* ----------------
|
|
|
|
*/
|
2000-07-02 06:46:09 +02:00
|
|
|
rel = (Relation) palloc(sizeof(RelationData));
|
|
|
|
MemSet((char *) rel, 0, sizeof(RelationData));
|
2000-11-08 23:10:03 +01:00
|
|
|
rel->rd_fd = -1; /* physical file is not open */
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-07-02 06:46:09 +02:00
|
|
|
RelationSetReferenceCount(rel, 1);
|
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
/*
|
|
|
|
* create a new tuple descriptor from the one passed in
|
1999-05-25 18:15:34 +02:00
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
rel->rd_att = CreateTupleDescCopyConstr(tupDesc);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* nail the reldesc if this is a bootstrap create reln and
|
|
|
|
* we may need it in the cache later on in the bootstrap
|
|
|
|
* process so we don't ever want it kicked out. e.g. pg_attribute!!!
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (nailme)
|
1998-08-19 04:04:17 +02:00
|
|
|
rel->rd_isnailed = true;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
/* ----------------
|
|
|
|
* initialize the fields of our new relation descriptor
|
|
|
|
* ----------------
|
|
|
|
*/
|
2000-07-02 06:46:09 +02:00
|
|
|
rel->rd_rel = (Form_pg_class) palloc(sizeof *rel->rd_rel);
|
1999-02-02 04:45:56 +01:00
|
|
|
MemSet((char *) rel->rd_rel, 0, sizeof *rel->rd_rel);
|
1999-11-16 05:14:03 +01:00
|
|
|
strcpy(RelationGetPhysicalRelationName(rel), relname);
|
1998-08-19 04:04:17 +02:00
|
|
|
rel->rd_rel->relkind = RELKIND_UNCATALOGED;
|
|
|
|
rel->rd_rel->relnatts = natts;
|
1997-09-07 07:04:48 +02:00
|
|
|
if (tupDesc->constr)
|
1998-08-19 04:04:17 +02:00
|
|
|
rel->rd_rel->relchecks = tupDesc->constr->num_check;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (i = 0; i < natts; i++)
|
1998-08-19 04:04:17 +02:00
|
|
|
rel->rd_att->attrs[i]->attrelid = relid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
RelationGetRelid(rel) = relid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (nailme)
|
|
|
|
{
|
|
|
|
/* for system relations, set the reltype field here */
|
1998-08-19 04:04:17 +02:00
|
|
|
rel->rd_rel->reltype = relid;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2000-10-16 16:52:28 +02:00
|
|
|
rel->rd_node.tblNode = tblNode;
|
|
|
|
rel->rd_node.relNode = relid;
|
|
|
|
rel->rd_rel->relfilenode = relid;
|
|
|
|
|
2000-07-02 06:46:09 +02:00
|
|
|
/* ----------------
|
|
|
|
* done building relcache entry.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
MemoryContextSwitchTo(oldcxt);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
|
|
|
* have the storage manager create the relation.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-11-04 09:01:09 +01:00
|
|
|
if (storage_create)
|
|
|
|
heap_storage_create(rel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
RelationRegisterRelation(rel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return rel;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-11-08 23:10:03 +01:00
|
|
|
void
|
1999-11-04 09:01:09 +01:00
|
|
|
heap_storage_create(Relation rel)
|
|
|
|
{
|
2000-11-08 23:10:03 +01:00
|
|
|
Assert(rel->rd_fd < 0);
|
|
|
|
rel->rd_fd = smgrcreate(DEFAULT_SMGR, rel);
|
|
|
|
Assert(rel->rd_fd >= 0);
|
1999-11-04 09:01:09 +01:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-11-28 18:28:02 +01:00
|
|
|
* heap_create_with_catalog - Create a cataloged relation
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* this is done in 6 steps:
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* 1) CheckAttributeNames() is used to make certain the tuple
|
|
|
|
* descriptor contains a valid set of attribute names
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1999-02-02 04:45:56 +01:00
|
|
|
* 2) pg_class is opened and RelationFindRelid()
|
2000-01-16 20:57:00 +01:00
|
|
|
* performs a scan to ensure that no relation with the
|
1997-09-07 07:04:48 +02:00
|
|
|
* same name already exists.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1998-08-06 07:13:14 +02:00
|
|
|
* 3) heap_create_with_catalog() is called to create the new relation
|
1997-11-28 05:40:40 +01:00
|
|
|
* on disk.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* 4) TypeDefine() is called to define a new type corresponding
|
|
|
|
* to the new relation.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* 5) AddNewAttributeTuples() is called to register the
|
|
|
|
* new relation's schema in pg_attribute.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1999-02-02 04:45:56 +01:00
|
|
|
* 6) AddNewRelationTuple() is called to register the
|
1997-09-07 07:04:48 +02:00
|
|
|
* relation itself in the catalogs.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* 7) StoreConstraints is called () - vadim 08/22/97
|
|
|
|
*
|
|
|
|
* 8) the relations are closed and the new relation's oid
|
|
|
|
* is returned.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* old comments:
|
1997-09-07 07:04:48 +02:00
|
|
|
* A new relation is inserted into the RELATION relation
|
|
|
|
* with the specified attribute(s) (newly inserted into
|
|
|
|
* the ATTRIBUTE relation). How does concurrency control
|
|
|
|
* work? Is it automatic now? Expects the caller to have
|
|
|
|
* attname, atttypid, atttyparg, attproc, and attlen domains filled.
|
|
|
|
* Create fills the attnum domains sequentually from zero,
|
2000-10-05 21:48:34 +02:00
|
|
|
* fills the attdispersion domains with zeros, and fills the
|
1997-09-07 07:04:48 +02:00
|
|
|
* attrelid fields with the relid.
|
|
|
|
*
|
|
|
|
* scan relation catalog for name conflict
|
|
|
|
* scan type catalog for typids (if not arg)
|
|
|
|
* create and insert attribute(s) into attribute catalog
|
|
|
|
* create new relation
|
|
|
|
* insert new relation into attribute catalog
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1998-08-06 07:13:14 +02:00
|
|
|
* Should coordinate with heap_create_with_catalog(). Either
|
1997-11-28 05:40:40 +01:00
|
|
|
* it should not be called or there should be a way to prevent
|
1997-09-07 07:04:48 +02:00
|
|
|
* the relation from being removed at the end of the
|
|
|
|
* transaction if it is successful ('u'/'r' may be enough).
|
|
|
|
* Also, if the transaction does not commit, then the
|
|
|
|
* relation should be removed.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* XXX amcreate ignores "off" when inserting (for now).
|
|
|
|
* XXX amcreate (like the other utilities) needs to understand indexes.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* CheckAttributeNames
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* this is used to make certain the tuple descriptor contains a
|
|
|
|
* valid set of attribute names. a problem simply generates
|
1998-01-06 20:42:33 +01:00
|
|
|
* elog(ERROR) which aborts the current transaction.
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
CheckAttributeNames(TupleDesc tupdesc)
|
|
|
|
{
|
2000-03-17 03:36:41 +01:00
|
|
|
int i;
|
|
|
|
int j;
|
1997-09-08 04:41:22 +02:00
|
|
|
int natts = tupdesc->natts;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* first check for collision with system attribute names
|
|
|
|
* ----------------
|
|
|
|
*
|
|
|
|
* also, warn user if attribute to be created has
|
|
|
|
* an unknown typid (usually as a result of a 'retrieve into'
|
|
|
|
* - jolly
|
|
|
|
*/
|
2000-03-17 03:36:41 +01:00
|
|
|
for (i = 0; i < natts; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-03-17 03:36:41 +01:00
|
|
|
for (j = 0; j < (int) (sizeof(HeapAtt) / sizeof(HeapAtt[0])); j++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-08-03 18:35:08 +02:00
|
|
|
if (strcmp(NameStr(HeapAtt[j]->attname),
|
|
|
|
NameStr(tupdesc->attrs[i]->attname)) == 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-02-23 08:54:03 +01:00
|
|
|
elog(ERROR, "Attribute '%s' has a name conflict"
|
|
|
|
"\n\tName matches an existing system attribute",
|
1999-11-08 00:08:36 +01:00
|
|
|
NameStr(HeapAtt[j]->attname));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tupdesc->attrs[i]->atttypid == UNKNOWNOID)
|
|
|
|
{
|
1999-02-23 08:54:03 +01:00
|
|
|
elog(NOTICE, "Attribute '%s' has an unknown type"
|
|
|
|
"\n\tRelation created; continue",
|
1999-11-08 00:08:36 +01:00
|
|
|
NameStr(tupdesc->attrs[i]->attname));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* next check for repeated attribute names
|
|
|
|
* ----------------
|
|
|
|
*/
|
2000-03-17 03:36:41 +01:00
|
|
|
for (i = 1; i < natts; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-03-17 03:36:41 +01:00
|
|
|
for (j = 0; j < i; j++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-08-03 18:35:08 +02:00
|
|
|
if (strcmp(NameStr(tupdesc->attrs[j]->attname),
|
|
|
|
NameStr(tupdesc->attrs[i]->attname)) == 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-02-23 08:54:03 +01:00
|
|
|
elog(ERROR, "Attribute '%s' is repeated",
|
1999-11-08 00:08:36 +01:00
|
|
|
NameStr(tupdesc->attrs[j]->attname));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1999-02-02 04:45:56 +01:00
|
|
|
* RelnameFindRelid
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-01-16 20:57:00 +01:00
|
|
|
* Find any existing relation of the given name.
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
1999-02-02 04:45:56 +01:00
|
|
|
Oid
|
2000-01-22 15:20:56 +01:00
|
|
|
RelnameFindRelid(const char *relname)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-02-02 04:45:56 +01:00
|
|
|
HeapTuple tuple;
|
|
|
|
Oid relid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is not bootstrap (initdb) time, use the catalog index on
|
|
|
|
* pg_class.
|
|
|
|
*/
|
|
|
|
if (!IsBootstrapProcessingMode())
|
|
|
|
{
|
1999-02-02 04:45:56 +01:00
|
|
|
tuple = SearchSysCacheTuple(RELNAME,
|
1999-05-25 18:15:34 +02:00
|
|
|
PointerGetDatum(relname),
|
|
|
|
0, 0, 0);
|
1999-02-02 04:45:56 +01:00
|
|
|
if (HeapTupleIsValid(tuple))
|
|
|
|
relid = tuple->t_data->t_oid;
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
1999-02-02 04:45:56 +01:00
|
|
|
relid = InvalidOid;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1999-02-02 04:45:56 +01:00
|
|
|
else
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
Relation pg_class_desc;
|
1999-02-02 04:45:56 +01:00
|
|
|
ScanKeyData key;
|
|
|
|
HeapScanDesc pg_class_scan;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
pg_class_desc = heap_openr(RelationRelationName, AccessShareLock);
|
1999-02-02 04:45:56 +01:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* At bootstrap time, we have to do this the hard way. Form the
|
|
|
|
* scan key.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
ScanKeyEntryInitialize(&key,
|
|
|
|
0,
|
|
|
|
(AttrNumber) Anum_pg_class_relname,
|
|
|
|
(RegProcedure) F_NAMEEQ,
|
|
|
|
(Datum) relname);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
/* ----------------
|
|
|
|
* begin the scan
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
pg_class_scan = heap_beginscan(pg_class_desc,
|
|
|
|
0,
|
|
|
|
SnapshotNow,
|
|
|
|
1,
|
|
|
|
&key);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
/* ----------------
|
|
|
|
* get a tuple. if the tuple is NULL then it means we
|
|
|
|
* didn't find an existing relation.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
tuple = heap_getnext(pg_class_scan, 0);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
if (HeapTupleIsValid(tuple))
|
|
|
|
relid = tuple->t_data->t_oid;
|
|
|
|
else
|
|
|
|
relid = InvalidOid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
heap_endscan(pg_class_scan);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_class_desc, AccessShareLock);
|
1999-02-02 04:45:56 +01:00
|
|
|
}
|
|
|
|
return relid;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* AddNewAttributeTuples
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* this registers the new relation's schema by adding
|
|
|
|
* tuples to pg_attribute.
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
AddNewAttributeTuples(Oid new_rel_oid,
|
1997-09-07 07:04:48 +02:00
|
|
|
TupleDesc tupdesc)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_attribute *dpp;
|
2000-03-17 03:36:41 +01:00
|
|
|
int i;
|
1997-09-08 04:41:22 +02:00
|
|
|
HeapTuple tup;
|
1998-08-19 04:04:17 +02:00
|
|
|
Relation rel;
|
1997-09-08 04:41:22 +02:00
|
|
|
bool hasindex;
|
|
|
|
Relation idescs[Num_pg_attr_indices];
|
|
|
|
int natts = tupdesc->natts;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* open pg_attribute
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
rel = heap_openr(AttributeRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* -----------------
|
|
|
|
* Check if we have any indices defined on pg_attribute.
|
|
|
|
* -----------------
|
|
|
|
*/
|
1998-09-01 05:29:17 +02:00
|
|
|
hasindex = RelationGetForm(rel)->relhasindex;
|
1996-07-09 08:22:35 +02:00
|
|
|
if (hasindex)
|
1997-09-07 07:04:48 +02:00
|
|
|
CatalogOpenIndices(Num_pg_attr_indices, Name_pg_attr_indices, idescs);
|
|
|
|
|
|
|
|
/* ----------------
|
2000-01-16 20:57:00 +01:00
|
|
|
* initialize tuple descriptor.
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
fillatt(tupdesc);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* first we add the user attributes..
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
dpp = tupdesc->attrs;
|
|
|
|
for (i = 0; i < natts; i++)
|
|
|
|
{
|
|
|
|
(*dpp)->attrelid = new_rel_oid;
|
2000-10-05 21:48:34 +02:00
|
|
|
(*dpp)->attdispersion = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
tup = heap_addheader(Natts_pg_attribute,
|
|
|
|
ATTRIBUTE_TUPLE_SIZE,
|
|
|
|
(char *) *dpp);
|
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
heap_insert(rel, tup);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (hasindex)
|
1998-08-19 04:04:17 +02:00
|
|
|
CatalogIndexInsert(idescs, Num_pg_attr_indices, rel, tup);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-12-16 23:20:03 +01:00
|
|
|
heap_freetuple(tup);
|
1997-09-07 07:04:48 +02:00
|
|
|
dpp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* next we add the system attributes..
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
dpp = HeapAtt;
|
|
|
|
for (i = 0; i < -1 - FirstLowInvalidHeapAttributeNumber; i++)
|
|
|
|
{
|
|
|
|
(*dpp)->attrelid = new_rel_oid;
|
2000-10-05 21:48:34 +02:00
|
|
|
/* (*dpp)->attdispersion = 0; unneeded */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
tup = heap_addheader(Natts_pg_attribute,
|
|
|
|
ATTRIBUTE_TUPLE_SIZE,
|
|
|
|
(char *) *dpp);
|
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
heap_insert(rel, tup);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (hasindex)
|
1998-08-19 04:04:17 +02:00
|
|
|
CatalogIndexInsert(idescs, Num_pg_attr_indices, rel, tup);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-12-16 23:20:03 +01:00
|
|
|
heap_freetuple(tup);
|
1997-09-07 07:04:48 +02:00
|
|
|
dpp++;
|
|
|
|
}
|
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(rel, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* close pg_attribute indices
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
if (hasindex)
|
1997-09-07 07:04:48 +02:00
|
|
|
CatalogCloseIndices(Num_pg_attr_indices, idescs);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1999-02-02 04:45:56 +01:00
|
|
|
* AddNewRelationTuple
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* this registers the new relation in the catalogs by
|
|
|
|
* adding a tuple to pg_class.
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
static void
|
1999-02-02 04:45:56 +01:00
|
|
|
AddNewRelationTuple(Relation pg_class_desc,
|
1999-05-25 18:15:34 +02:00
|
|
|
Relation new_rel_desc,
|
|
|
|
Oid new_rel_oid,
|
2000-01-16 20:57:00 +01:00
|
|
|
int natts,
|
1999-05-25 18:15:34 +02:00
|
|
|
char relkind,
|
|
|
|
char *temp_relname)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Form_pg_class new_rel_reltup;
|
|
|
|
HeapTuple tup;
|
|
|
|
Relation idescs[Num_pg_class_indices];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* first we munge some of the information in our
|
|
|
|
* uncataloged relation's relation descriptor.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
new_rel_reltup = new_rel_desc->rd_rel;
|
|
|
|
|
1999-04-15 06:08:07 +02:00
|
|
|
/* ----------------
|
|
|
|
* Here we insert bogus estimates of the size of the new relation.
|
|
|
|
* In reality, of course, the new relation has 0 tuples and pages,
|
|
|
|
* and if we were tracking these statistics accurately then we'd
|
|
|
|
* set the fields that way. But at present the stats will be updated
|
|
|
|
* only by VACUUM or CREATE INDEX, and the user might insert a lot of
|
|
|
|
* tuples before he gets around to doing either of those. So, instead
|
|
|
|
* of saying the relation is empty, we insert guesstimates. The point
|
|
|
|
* is to keep the optimizer from making really stupid choices on
|
|
|
|
* never-yet-vacuumed tables; so the estimates need only be large
|
|
|
|
* enough to discourage the optimizer from using nested-loop plans.
|
|
|
|
* With this hack, nested-loop plans will be preferred only after
|
|
|
|
* the table has been proven to be small by VACUUM or CREATE INDEX.
|
1999-05-27 00:57:39 +02:00
|
|
|
* Maintaining the stats on-the-fly would solve the problem more cleanly,
|
|
|
|
* but the overhead of that would likely cost more than it'd save.
|
|
|
|
* (NOTE: CREATE INDEX inserts the same bogus estimates if it finds the
|
|
|
|
* relation has 0 rows and pages. See index.c.)
|
1999-04-15 06:08:07 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
new_rel_reltup->relpages = 10; /* bogus estimates */
|
1999-04-15 06:08:07 +02:00
|
|
|
new_rel_reltup->reltuples = 1000;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
new_rel_reltup->relowner = GetUserId();
|
1998-08-06 07:13:14 +02:00
|
|
|
new_rel_reltup->relkind = relkind;
|
1997-09-07 07:04:48 +02:00
|
|
|
new_rel_reltup->relnatts = natts;
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* now form a tuple to add to pg_class
|
|
|
|
* XXX Natts_pg_class_fixed is a hack - see pg_class.h
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
tup = heap_addheader(Natts_pg_class_fixed,
|
|
|
|
CLASS_TUPLE_SIZE,
|
|
|
|
(char *) new_rel_reltup);
|
1998-11-27 20:52:36 +01:00
|
|
|
tup->t_data->t_oid = new_rel_oid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-10-06 05:08:46 +02:00
|
|
|
/*
|
|
|
|
* finally insert the new tuple and free it.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
heap_insert(pg_class_desc, tup);
|
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
if (temp_relname)
|
|
|
|
create_temp_relation(temp_relname, tup);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2000-02-18 10:30:20 +01:00
|
|
|
if (!IsIgnoringSystemIndexes())
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
|
|
|
* First, open the catalog indices and insert index tuples for the
|
|
|
|
* new relation.
|
|
|
|
*/
|
|
|
|
CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices, idescs);
|
|
|
|
CatalogIndexInsert(idescs, Num_pg_class_indices, pg_class_desc, tup);
|
|
|
|
CatalogCloseIndices(Num_pg_class_indices, idescs);
|
|
|
|
}
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-12-16 23:20:03 +01:00
|
|
|
heap_freetuple(tup);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------------------------
|
1999-02-02 04:45:56 +01:00
|
|
|
* AddNewRelationType -
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* define a complex type corresponding to the new relation
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
static void
|
1999-02-02 04:45:56 +01:00
|
|
|
AddNewRelationType(char *typeName, Oid new_rel_oid)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Oid new_type_oid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The sizes are set to oid size because it makes implementing sets
|
|
|
|
* MUCH easier, and no one (we hope) uses these fields to figure out
|
|
|
|
* how much space to allocate for the type. An oid is the type used
|
|
|
|
* for a set definition. When a user requests a set, what they
|
|
|
|
* actually get is the oid of a tuple in the pg_proc catalog, so the
|
|
|
|
* size of the "set" is the size of an oid. Similarly, byval being
|
|
|
|
* true makes sets much easier, and it isn't used by anything else.
|
|
|
|
* Note the assumption that OIDs are the same size as int4s.
|
|
|
|
*/
|
|
|
|
new_type_oid = TypeCreate(typeName, /* type name */
|
|
|
|
new_rel_oid, /* relation oid */
|
1998-02-26 05:46:47 +01:00
|
|
|
typeLen(typeidType(OIDOID)), /* internal size */
|
|
|
|
typeLen(typeidType(OIDOID)), /* external size */
|
1997-09-07 07:04:48 +02:00
|
|
|
'c', /* type-type (catalog) */
|
|
|
|
',', /* default array delimiter */
|
1999-05-10 06:02:07 +02:00
|
|
|
"int4in", /* input procedure */
|
|
|
|
"int4out", /* output procedure */
|
|
|
|
"int4in", /* receive procedure */
|
|
|
|
"int4out", /* send procedure */
|
1997-09-07 07:04:48 +02:00
|
|
|
NULL, /* array element type - irrelevent */
|
|
|
|
"-", /* default type value */
|
|
|
|
(bool) 1, /* passed by value */
|
2000-07-04 01:10:14 +02:00
|
|
|
'i', /* default alignment */
|
|
|
|
'p'); /* Not TOASTable */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-11-28 18:28:02 +01:00
|
|
|
* heap_create_with_catalog
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* creates a new cataloged relation. see comments above.
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
Oid
|
1998-08-06 07:13:14 +02:00
|
|
|
heap_create_with_catalog(char *relname,
|
|
|
|
TupleDesc tupdesc,
|
1999-02-02 04:45:56 +01:00
|
|
|
char relkind,
|
2000-07-04 08:11:54 +02:00
|
|
|
bool istemp,
|
|
|
|
bool allow_system_table_mods)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation pg_class_desc;
|
|
|
|
Relation new_rel_desc;
|
|
|
|
Oid new_rel_oid;
|
|
|
|
int natts = tupdesc->natts;
|
1999-05-25 18:15:34 +02:00
|
|
|
char *temp_relname = NULL;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
|
|
|
* sanity checks
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-02-02 04:45:56 +01:00
|
|
|
Assert(IsNormalProcessingMode() || IsBootstrapProcessingMode());
|
2000-01-16 20:57:00 +01:00
|
|
|
if (natts <= 0 || natts > MaxHeapAttributeNumber)
|
1999-02-23 08:54:03 +01:00
|
|
|
elog(ERROR, "Number of attributes is out of range"
|
|
|
|
"\n\tFrom 1 to %d attributes may be specified",
|
|
|
|
MaxHeapAttributeNumber);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
CheckAttributeNames(tupdesc);
|
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
/* temp tables can mask non-temp tables */
|
|
|
|
if ((!istemp && RelnameFindRelid(relname)) ||
|
1999-11-16 05:14:03 +01:00
|
|
|
(istemp && get_temp_rel_by_username(relname) != NULL))
|
1999-02-23 08:54:03 +01:00
|
|
|
elog(ERROR, "Relation '%s' already exists", relname);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
if (istemp)
|
|
|
|
{
|
2000-07-04 08:11:54 +02:00
|
|
|
/* save user relation name because heap_create changes it */
|
1999-05-25 18:15:34 +02:00
|
|
|
temp_relname = pstrdup(relname); /* save original value */
|
1999-02-02 04:45:56 +01:00
|
|
|
relname = palloc(NAMEDATALEN);
|
1999-05-25 18:15:34 +02:00
|
|
|
strcpy(relname, temp_relname); /* heap_create will change this */
|
1999-02-02 04:45:56 +01:00
|
|
|
}
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
1999-11-16 05:14:03 +01:00
|
|
|
* get_temp_rel_by_username() couldn't check the simultaneous
|
1999-11-04 09:01:09 +01:00
|
|
|
* creation. Uniqueness will be really checked by unique
|
|
|
|
* indexes of system tables but we couldn't check it here.
|
2000-06-19 00:44:35 +02:00
|
|
|
* We have to postpone creating the disk file for this
|
1999-11-04 09:01:09 +01:00
|
|
|
* relation.
|
|
|
|
* Another boolean parameter "storage_create" was added
|
|
|
|
* to heap_create() function. If the parameter is false
|
|
|
|
* heap_create() only registers an uncataloged relation
|
|
|
|
* to relation cache and heap_storage_create() should be
|
|
|
|
* called later.
|
|
|
|
* We could pull its relation oid from the newly formed
|
|
|
|
* relation descriptor.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1999-11-04 09:01:09 +01:00
|
|
|
* Note: The call to heap_create() changes relname for
|
2000-06-19 00:44:35 +02:00
|
|
|
* temp tables; it becomes the true physical relname.
|
1999-11-04 09:01:09 +01:00
|
|
|
* The call to heap_storage_create() does all the "real"
|
|
|
|
* work of creating the disk file for the relation.
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-07-04 08:11:54 +02:00
|
|
|
new_rel_desc = heap_create(relname, tupdesc, istemp, false,
|
|
|
|
allow_system_table_mods);
|
1999-02-02 04:45:56 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
new_rel_oid = new_rel_desc->rd_att->attrs[0]->attrelid;
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* since defining a relation also defines a complex type,
|
|
|
|
* we add a new system type corresponding to the new relation.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-02-02 04:45:56 +01:00
|
|
|
AddNewRelationType(relname, new_rel_oid);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* now add tuples to pg_attribute for the attributes in
|
|
|
|
* our new relation.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
AddNewAttributeTuples(new_rel_oid, tupdesc);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* now update the information in pg_class.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
pg_class_desc = heap_openr(RelationRelationName, RowExclusiveLock);
|
1999-02-02 04:45:56 +01:00
|
|
|
|
|
|
|
AddNewRelationTuple(pg_class_desc,
|
1999-05-25 18:15:34 +02:00
|
|
|
new_rel_desc,
|
|
|
|
new_rel_oid,
|
|
|
|
natts,
|
|
|
|
relkind,
|
|
|
|
temp_relname);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
StoreConstraints(new_rel_desc);
|
|
|
|
|
1999-02-02 04:45:56 +01:00
|
|
|
if (istemp)
|
|
|
|
{
|
|
|
|
pfree(relname);
|
|
|
|
pfree(temp_relname);
|
|
|
|
}
|
|
|
|
|
1999-11-04 09:01:09 +01:00
|
|
|
/*
|
|
|
|
* We create the disk file for this relation here
|
|
|
|
*/
|
2000-09-12 06:49:17 +02:00
|
|
|
if (relkind != RELKIND_VIEW)
|
|
|
|
heap_storage_create(new_rel_desc);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
|
|
|
* ok, the relation has been cataloged, so close our relations
|
|
|
|
* and return the oid of the newly created relation.
|
|
|
|
*
|
|
|
|
* SOMEDAY: fill the STATISTIC relation properly.
|
|
|
|
* ----------------
|
|
|
|
*/
|
2000-04-12 19:17:23 +02:00
|
|
|
heap_close(new_rel_desc, NoLock); /* do not unlock till end of xact */
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_class_desc, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
return new_rel_oid;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1999-12-10 04:56:14 +01:00
|
|
|
* heap_drop_with_catalog - removes all record of named relation from catalogs
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* 1) open relation, check for existence, etc.
|
|
|
|
* 2) remove inheritance information
|
|
|
|
* 3) remove indexes
|
|
|
|
* 4) remove pg_class tuple
|
This patch implements ORACLE's COMMENT SQL command.
>From the ORACLE 7 SQL Language Reference Manual:
-----------------------------------------------------
COMMENT
Purpose:
To add a comment about a table, view, snapshot, or
column into the data dictionary.
Prerequisites:
The table, view, or snapshot must be in your own
schema
or you must have COMMENT ANY TABLE system privilege.
Syntax:
COMMENT ON [ TABLE table ] |
[ COLUMN table.column] IS 'text'
You can effectively drop a comment from the database
by setting it to the empty string ''.
-----------------------------------------------------
Example:
COMMENT ON TABLE workorders IS
'Maintains base records for workorder information';
COMMENT ON COLUMN workorders.hours IS
'Number of hours the engineer worked on the task';
to drop a comment:
COMMENT ON COLUMN workorders.hours IS '';
The current patch will simply perform the insert into
pg_description, as per the TODO. And, of course, when
the table is dropped, any comments relating to it
or any of its attributes are also dropped. I haven't
looked at the ODBC source yet, but I do know from
an ODBC client standpoint that the standard does
support the notion of table and column comments.
Hopefully the ODBC driver is already fetching these
values from pg_description, but if not, it should be
trivial.
Hope this makes the grade,
Mike Mascari
(mascarim@yahoo.com)
1999-10-15 03:49:49 +02:00
|
|
|
* 5) remove pg_attribute tuples and related descriptions
|
2000-04-12 19:17:23 +02:00
|
|
|
* 6) remove pg_description tuples
|
This patch implements ORACLE's COMMENT SQL command.
>From the ORACLE 7 SQL Language Reference Manual:
-----------------------------------------------------
COMMENT
Purpose:
To add a comment about a table, view, snapshot, or
column into the data dictionary.
Prerequisites:
The table, view, or snapshot must be in your own
schema
or you must have COMMENT ANY TABLE system privilege.
Syntax:
COMMENT ON [ TABLE table ] |
[ COLUMN table.column] IS 'text'
You can effectively drop a comment from the database
by setting it to the empty string ''.
-----------------------------------------------------
Example:
COMMENT ON TABLE workorders IS
'Maintains base records for workorder information';
COMMENT ON COLUMN workorders.hours IS
'Number of hours the engineer worked on the task';
to drop a comment:
COMMENT ON COLUMN workorders.hours IS '';
The current patch will simply perform the insert into
pg_description, as per the TODO. And, of course, when
the table is dropped, any comments relating to it
or any of its attributes are also dropped. I haven't
looked at the ODBC source yet, but I do know from
an ODBC client standpoint that the standard does
support the notion of table and column comments.
Hopefully the ODBC driver is already fetching these
values from pg_description, but if not, it should be
trivial.
Hope this makes the grade,
Mike Mascari
(mascarim@yahoo.com)
1999-10-15 03:49:49 +02:00
|
|
|
* 7) remove pg_type tuples
|
|
|
|
* 8) RemoveConstraints ()
|
|
|
|
* 9) unlink relation
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* old comments
|
1997-09-07 07:04:48 +02:00
|
|
|
* Except for vital relations, removes relation from
|
|
|
|
* relation catalog, and related attributes from
|
|
|
|
* attribute catalog (needed?). (Anything else?)
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* get proper relation from relation catalog (if not arg)
|
|
|
|
* check if relation is vital (strcmp()/reltype?)
|
|
|
|
* scan attribute catalog deleting attributes of reldesc
|
|
|
|
* (necessary?)
|
|
|
|
* delete relation from relation catalog
|
|
|
|
* (How are the tuples of the relation discarded?)
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* XXX Must fix to work with indexes.
|
|
|
|
* There may be a better order for doing things.
|
|
|
|
* Problems with destroying a deleted database--cannot create
|
|
|
|
* a struct reldesc without having an open file descriptor.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* RelationRemoveInheritance
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Note: for now, we cause an exception if relation is a
|
|
|
|
* superclass. Someday, we may want to allow this and merge
|
|
|
|
* the type info into subclass procedures.... this seems like
|
|
|
|
* lots of work.
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
static void
|
1996-07-09 08:22:35 +02:00
|
|
|
RelationRemoveInheritance(Relation relation)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation catalogRelation;
|
|
|
|
HeapTuple tuple;
|
|
|
|
HeapScanDesc scan;
|
|
|
|
ScanKeyData entry;
|
1998-08-19 04:04:17 +02:00
|
|
|
bool found = false;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
|
|
|
* open pg_inherits
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
catalogRelation = heap_openr(InheritsRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* form a scan key for the subclasses of this class
|
|
|
|
* and begin scanning
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
ScanKeyEntryInitialize(&entry, 0x0, Anum_pg_inherits_inhparent,
|
1998-04-27 06:08:07 +02:00
|
|
|
F_OIDEQ,
|
1998-09-01 06:40:42 +02:00
|
|
|
ObjectIdGetDatum(RelationGetRelid(relation)));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
scan = heap_beginscan(catalogRelation,
|
|
|
|
false,
|
1998-07-27 21:38:40 +02:00
|
|
|
SnapshotNow,
|
1997-09-07 07:04:48 +02:00
|
|
|
1,
|
|
|
|
&entry);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* if any subclasses exist, then we disallow the deletion.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
tuple = heap_getnext(scan, 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (HeapTupleIsValid(tuple))
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
Oid subclass = ((Form_pg_inherits) GETSTRUCT(tuple))->inhrelid;
|
1999-09-18 21:08:25 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
heap_endscan(scan);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(catalogRelation, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-10 02:46:32 +02:00
|
|
|
elog(ERROR, "Relation '%u' inherits '%s'",
|
1999-09-18 21:08:25 +02:00
|
|
|
subclass, RelationGetRelationName(relation));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_endscan(scan);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* If we get here, it means the relation has no subclasses
|
|
|
|
* so we can trash it. First we remove dead INHERITS tuples.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-11-22 18:56:41 +01:00
|
|
|
entry.sk_attno = Anum_pg_inherits_inhrelid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
scan = heap_beginscan(catalogRelation,
|
|
|
|
false,
|
1998-07-27 21:38:40 +02:00
|
|
|
SnapshotNow,
|
1997-09-07 07:04:48 +02:00
|
|
|
1,
|
|
|
|
&entry);
|
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
while (HeapTupleIsValid(tuple = heap_getnext(scan, 0)))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-12-15 13:47:01 +01:00
|
|
|
heap_delete(catalogRelation, &tuple->t_self, NULL);
|
1998-08-19 04:04:17 +02:00
|
|
|
found = true;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
heap_endscan(scan);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(catalogRelation, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* now remove dead IPL tuples
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
catalogRelation = heap_openr(InheritancePrecidenceListRelationName,
|
|
|
|
RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-11-22 18:56:41 +01:00
|
|
|
entry.sk_attno = Anum_pg_ipl_iplrelid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
scan = heap_beginscan(catalogRelation,
|
|
|
|
false,
|
1998-07-27 21:38:40 +02:00
|
|
|
SnapshotNow,
|
1997-09-07 07:04:48 +02:00
|
|
|
1,
|
|
|
|
&entry);
|
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
while (HeapTupleIsValid(tuple = heap_getnext(scan, 0)))
|
1998-12-15 13:47:01 +01:00
|
|
|
heap_delete(catalogRelation, &tuple->t_self, NULL);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
heap_endscan(scan);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(catalogRelation, RowExclusiveLock);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* RelationRemoveIndexes
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
static void
|
1996-07-09 08:22:35 +02:00
|
|
|
RelationRemoveIndexes(Relation relation)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation indexRelation;
|
|
|
|
HeapTuple tuple;
|
|
|
|
HeapScanDesc scan;
|
|
|
|
ScanKeyData entry;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
indexRelation = heap_openr(IndexRelationName, RowExclusiveLock);
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
ScanKeyEntryInitialize(&entry, 0x0, Anum_pg_index_indrelid,
|
1998-04-27 06:08:07 +02:00
|
|
|
F_OIDEQ,
|
1998-09-01 06:40:42 +02:00
|
|
|
ObjectIdGetDatum(RelationGetRelid(relation)));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
scan = heap_beginscan(indexRelation,
|
|
|
|
false,
|
1998-07-27 21:38:40 +02:00
|
|
|
SnapshotNow,
|
1997-09-07 07:04:48 +02:00
|
|
|
1,
|
|
|
|
&entry);
|
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
while (HeapTupleIsValid(tuple = heap_getnext(scan, 0)))
|
2000-11-08 23:10:03 +01:00
|
|
|
{
|
1999-12-10 04:56:14 +01:00
|
|
|
index_drop(((Form_pg_index) GETSTRUCT(tuple))->indexrelid);
|
2000-11-08 23:10:03 +01:00
|
|
|
/* advance cmd counter to make catalog changes visible */
|
|
|
|
CommandCounterIncrement();
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
heap_endscan(scan);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(indexRelation, RowExclusiveLock);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1999-02-02 04:45:56 +01:00
|
|
|
* DeleteRelationTuple
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
static void
|
1999-02-02 04:45:56 +01:00
|
|
|
DeleteRelationTuple(Relation rel)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation pg_class_desc;
|
|
|
|
HeapTuple tup;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* open pg_class
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
pg_class_desc = heap_openr(RelationRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
tup = SearchSysCacheTupleCopy(RELOID,
|
2000-01-18 00:57:48 +01:00
|
|
|
ObjectIdGetDatum(rel->rd_id),
|
1998-09-01 06:40:42 +02:00
|
|
|
0, 0, 0);
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tup))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_class_desc, RowExclusiveLock);
|
2000-10-23 01:32:48 +02:00
|
|
|
elog(ERROR, "Relation \"%s\" does not exist",
|
2000-04-12 19:17:23 +02:00
|
|
|
RelationGetRelationName(rel));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* delete the relation tuple from pg_class, and finish up.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-12-15 13:47:01 +01:00
|
|
|
heap_delete(pg_class_desc, &tup->t_self, NULL);
|
1999-12-16 23:20:03 +01:00
|
|
|
heap_freetuple(tup);
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_class_desc, RowExclusiveLock);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-09-23 19:03:39 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* RelationTruncateIndexes - This routine is used to truncate all
|
|
|
|
* indices associated with the heap relation to zero tuples.
|
|
|
|
* The routine will truncate and then reconstruct the indices on
|
2000-09-30 20:28:53 +02:00
|
|
|
* the relation specified by the heapId parameter.
|
1999-09-23 19:03:39 +02:00
|
|
|
* --------------------------------
|
1999-09-24 02:25:33 +02:00
|
|
|
*/
|
|
|
|
static void
|
2000-09-30 20:28:53 +02:00
|
|
|
RelationTruncateIndexes(Oid heapId)
|
1999-09-24 02:25:33 +02:00
|
|
|
{
|
2000-09-30 20:28:53 +02:00
|
|
|
Relation indexRelation;
|
1999-09-24 02:25:33 +02:00
|
|
|
ScanKeyData entry;
|
|
|
|
HeapScanDesc scan;
|
2000-09-30 20:28:53 +02:00
|
|
|
HeapTuple indexTuple;
|
1999-09-24 02:25:33 +02:00
|
|
|
|
2000-09-30 20:28:53 +02:00
|
|
|
/* Scan pg_index to find indexes on specified heap */
|
1999-09-24 02:25:33 +02:00
|
|
|
indexRelation = heap_openr(IndexRelationName, AccessShareLock);
|
|
|
|
ScanKeyEntryInitialize(&entry, 0, Anum_pg_index_indrelid, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(heapId));
|
|
|
|
scan = heap_beginscan(indexRelation, false, SnapshotNow, 1, &entry);
|
2000-09-30 20:28:53 +02:00
|
|
|
|
1999-09-24 02:25:33 +02:00
|
|
|
while (HeapTupleIsValid(indexTuple = heap_getnext(scan, 0)))
|
|
|
|
{
|
2000-09-30 20:28:53 +02:00
|
|
|
Oid indexId,
|
|
|
|
accessMethodId;
|
|
|
|
IndexInfo *indexInfo;
|
|
|
|
HeapTuple classTuple;
|
|
|
|
Relation heapRelation,
|
|
|
|
currentIndex;
|
|
|
|
|
1999-09-24 02:25:33 +02:00
|
|
|
/*
|
2000-07-15 00:18:02 +02:00
|
|
|
* For each index, fetch info needed for index_build
|
1999-09-24 02:25:33 +02:00
|
|
|
*/
|
2000-07-15 00:18:02 +02:00
|
|
|
indexId = ((Form_pg_index) GETSTRUCT(indexTuple))->indexrelid;
|
|
|
|
indexInfo = BuildIndexInfo(indexTuple);
|
1999-09-24 02:25:33 +02:00
|
|
|
|
2000-07-15 00:18:02 +02:00
|
|
|
/* Fetch the pg_class tuple associated with this index */
|
|
|
|
classTuple = SearchSysCacheTupleCopy(RELOID,
|
|
|
|
ObjectIdGetDatum(indexId),
|
1999-09-24 02:25:33 +02:00
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(classTuple))
|
2000-07-15 00:18:02 +02:00
|
|
|
elog(ERROR, "RelationTruncateIndexes: index %u not found in pg_class",
|
|
|
|
indexId);
|
1999-09-24 02:25:33 +02:00
|
|
|
accessMethodId = ((Form_pg_class) GETSTRUCT(classTuple))->relam;
|
|
|
|
|
2000-09-30 20:28:53 +02:00
|
|
|
/*
|
|
|
|
* We have to re-open the heap rel each time through this loop
|
|
|
|
* because index_build will close it again. We need grab no lock,
|
|
|
|
* however, because we assume heap_truncate is holding an exclusive
|
|
|
|
* lock on the heap rel.
|
|
|
|
*/
|
|
|
|
heapRelation = heap_open(heapId, NoLock);
|
|
|
|
|
2000-07-15 00:18:02 +02:00
|
|
|
/* Open the index relation */
|
1999-09-24 02:25:33 +02:00
|
|
|
currentIndex = index_open(indexId);
|
|
|
|
|
|
|
|
/* Obtain exclusive lock on it, just to be sure */
|
|
|
|
LockRelation(currentIndex, AccessExclusiveLock);
|
1999-09-23 19:03:39 +02:00
|
|
|
|
1999-09-24 02:25:33 +02:00
|
|
|
/*
|
2000-11-08 23:10:03 +01:00
|
|
|
* Drop any buffers associated with this index. If they're
|
2000-04-12 19:17:23 +02:00
|
|
|
* dirty, they're just dropped without bothering to flush to disk.
|
1999-09-24 02:25:33 +02:00
|
|
|
*/
|
2000-11-08 23:10:03 +01:00
|
|
|
DropRelationBuffers(currentIndex);
|
1999-09-24 02:25:33 +02:00
|
|
|
|
|
|
|
/* Now truncate the actual data and set blocks to zero */
|
|
|
|
smgrtruncate(DEFAULT_SMGR, currentIndex, 0);
|
|
|
|
currentIndex->rd_nblocks = 0;
|
|
|
|
|
|
|
|
/* Initialize the index and rebuild */
|
2000-07-15 00:18:02 +02:00
|
|
|
InitIndexStrategy(indexInfo->ii_NumIndexAttrs,
|
|
|
|
currentIndex, accessMethodId);
|
|
|
|
index_build(heapRelation, currentIndex, indexInfo, NULL);
|
1999-09-24 02:25:33 +02:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* index_build will close both the heap and index relations (but
|
2000-09-30 20:28:53 +02:00
|
|
|
* not give up the locks we hold on them).
|
1999-09-24 02:25:33 +02:00
|
|
|
*/
|
|
|
|
}
|
1999-09-23 19:03:39 +02:00
|
|
|
|
1999-09-24 02:25:33 +02:00
|
|
|
/* Complete the scan and close pg_index */
|
2000-04-12 19:17:23 +02:00
|
|
|
heap_endscan(scan);
|
1999-09-24 02:25:33 +02:00
|
|
|
heap_close(indexRelation, AccessShareLock);
|
1999-09-23 19:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------
|
2000-04-12 19:17:23 +02:00
|
|
|
* heap_truncate
|
1999-09-23 19:03:39 +02:00
|
|
|
*
|
2000-04-12 19:17:23 +02:00
|
|
|
* This routine is used to truncate the data from the
|
|
|
|
* storage manager of any data within the relation handed
|
|
|
|
* to this routine.
|
1999-09-23 19:03:39 +02:00
|
|
|
* ----------------------------
|
|
|
|
*/
|
|
|
|
|
1999-09-24 02:25:33 +02:00
|
|
|
void
|
|
|
|
heap_truncate(char *relname)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
Relation rel;
|
|
|
|
Oid rid;
|
1999-09-24 02:25:33 +02:00
|
|
|
|
|
|
|
/* Open relation for processing, and grab exclusive access on it. */
|
|
|
|
|
|
|
|
rel = heap_openr(relname, AccessExclusiveLock);
|
1999-11-28 03:03:04 +01:00
|
|
|
rid = RelationGetRelid(rel);
|
1999-09-24 02:25:33 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* TRUNCATE TABLE within a transaction block is dangerous, because
|
|
|
|
* if the transaction is later rolled back we have no way to
|
2000-11-08 23:10:03 +01:00
|
|
|
* undo truncation of the relation's physical file. Disallow it
|
|
|
|
* except for a rel created in the current xact (which would be deleted
|
|
|
|
* on abort, anyway).
|
1999-09-24 02:25:33 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-04-12 19:17:23 +02:00
|
|
|
if (IsTransactionBlock() && !rel->rd_myxactonly)
|
2000-11-08 23:10:03 +01:00
|
|
|
elog(ERROR, "TRUNCATE TABLE cannot run inside a BEGIN/END block");
|
1999-09-24 02:25:33 +02:00
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Release any buffers associated with this relation. If they're
|
|
|
|
* dirty, they're just dropped without bothering to flush to disk.
|
1999-09-24 02:25:33 +02:00
|
|
|
*/
|
2000-11-08 23:10:03 +01:00
|
|
|
DropRelationBuffers(rel);
|
1999-09-23 19:03:39 +02:00
|
|
|
|
1999-09-24 02:25:33 +02:00
|
|
|
/* Now truncate the actual data and set blocks to zero */
|
|
|
|
|
|
|
|
smgrtruncate(DEFAULT_SMGR, rel, 0);
|
|
|
|
rel->rd_nblocks = 0;
|
|
|
|
|
|
|
|
/* If this relation has indexes, truncate the indexes too */
|
2000-09-30 20:28:53 +02:00
|
|
|
RelationTruncateIndexes(rid);
|
1999-09-24 02:25:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Close the relation, but keep exclusive lock on it until commit.
|
|
|
|
*/
|
|
|
|
heap_close(rel, NoLock);
|
1999-09-23 19:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* --------------------------------
|
1999-02-02 04:45:56 +01:00
|
|
|
* DeleteAttributeTuples
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
static void
|
1999-02-02 04:45:56 +01:00
|
|
|
DeleteAttributeTuples(Relation rel)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation pg_attribute_desc;
|
|
|
|
HeapTuple tup;
|
1998-08-19 04:04:17 +02:00
|
|
|
int2 attnum;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
|
|
|
* open pg_attribute
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
pg_attribute_desc = heap_openr(AttributeRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-21 00:07:46 +02:00
|
|
|
for (attnum = FirstLowInvalidHeapAttributeNumber + 1;
|
1998-09-01 06:40:42 +02:00
|
|
|
attnum <= rel->rd_att->natts;
|
|
|
|
attnum++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-08-21 00:07:46 +02:00
|
|
|
if (HeapTupleIsValid(tup = SearchSysCacheTupleCopy(ATTNUM,
|
2000-04-12 19:17:23 +02:00
|
|
|
ObjectIdGetDatum(RelationGetRelid(rel)),
|
|
|
|
Int16GetDatum(attnum),
|
1998-09-01 06:40:42 +02:00
|
|
|
0, 0)))
|
1998-08-21 00:07:46 +02:00
|
|
|
{
|
This patch implements ORACLE's COMMENT SQL command.
>From the ORACLE 7 SQL Language Reference Manual:
-----------------------------------------------------
COMMENT
Purpose:
To add a comment about a table, view, snapshot, or
column into the data dictionary.
Prerequisites:
The table, view, or snapshot must be in your own
schema
or you must have COMMENT ANY TABLE system privilege.
Syntax:
COMMENT ON [ TABLE table ] |
[ COLUMN table.column] IS 'text'
You can effectively drop a comment from the database
by setting it to the empty string ''.
-----------------------------------------------------
Example:
COMMENT ON TABLE workorders IS
'Maintains base records for workorder information';
COMMENT ON COLUMN workorders.hours IS
'Number of hours the engineer worked on the task';
to drop a comment:
COMMENT ON COLUMN workorders.hours IS '';
The current patch will simply perform the insert into
pg_description, as per the TODO. And, of course, when
the table is dropped, any comments relating to it
or any of its attributes are also dropped. I haven't
looked at the ODBC source yet, but I do know from
an ODBC client standpoint that the standard does
support the notion of table and column comments.
Hopefully the ODBC driver is already fetching these
values from pg_description, but if not, it should be
trivial.
Hope this makes the grade,
Mike Mascari
(mascarim@yahoo.com)
1999-10-15 03:49:49 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*** Delete any comments associated with this attribute ***/
|
|
|
|
|
|
|
|
DeleteComments(tup->t_data->t_oid);
|
This patch implements ORACLE's COMMENT SQL command.
>From the ORACLE 7 SQL Language Reference Manual:
-----------------------------------------------------
COMMENT
Purpose:
To add a comment about a table, view, snapshot, or
column into the data dictionary.
Prerequisites:
The table, view, or snapshot must be in your own
schema
or you must have COMMENT ANY TABLE system privilege.
Syntax:
COMMENT ON [ TABLE table ] |
[ COLUMN table.column] IS 'text'
You can effectively drop a comment from the database
by setting it to the empty string ''.
-----------------------------------------------------
Example:
COMMENT ON TABLE workorders IS
'Maintains base records for workorder information';
COMMENT ON COLUMN workorders.hours IS
'Number of hours the engineer worked on the task';
to drop a comment:
COMMENT ON COLUMN workorders.hours IS '';
The current patch will simply perform the insert into
pg_description, as per the TODO. And, of course, when
the table is dropped, any comments relating to it
or any of its attributes are also dropped. I haven't
looked at the ODBC source yet, but I do know from
an ODBC client standpoint that the standard does
support the notion of table and column comments.
Hopefully the ODBC driver is already fetching these
values from pg_description, but if not, it should be
trivial.
Hope this makes the grade,
Mike Mascari
(mascarim@yahoo.com)
1999-10-15 03:49:49 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
heap_delete(pg_attribute_desc, &tup->t_self, NULL);
|
|
|
|
heap_freetuple(tup);
|
This patch implements ORACLE's COMMENT SQL command.
>From the ORACLE 7 SQL Language Reference Manual:
-----------------------------------------------------
COMMENT
Purpose:
To add a comment about a table, view, snapshot, or
column into the data dictionary.
Prerequisites:
The table, view, or snapshot must be in your own
schema
or you must have COMMENT ANY TABLE system privilege.
Syntax:
COMMENT ON [ TABLE table ] |
[ COLUMN table.column] IS 'text'
You can effectively drop a comment from the database
by setting it to the empty string ''.
-----------------------------------------------------
Example:
COMMENT ON TABLE workorders IS
'Maintains base records for workorder information';
COMMENT ON COLUMN workorders.hours IS
'Number of hours the engineer worked on the task';
to drop a comment:
COMMENT ON COLUMN workorders.hours IS '';
The current patch will simply perform the insert into
pg_description, as per the TODO. And, of course, when
the table is dropped, any comments relating to it
or any of its attributes are also dropped. I haven't
looked at the ODBC source yet, but I do know from
an ODBC client standpoint that the standard does
support the notion of table and column comments.
Hopefully the ODBC driver is already fetching these
values from pg_description, but if not, it should be
trivial.
Hope this makes the grade,
Mike Mascari
(mascarim@yahoo.com)
1999-10-15 03:49:49 +02:00
|
|
|
|
1999-10-26 05:12:39 +02:00
|
|
|
}
|
|
|
|
}
|
This patch implements ORACLE's COMMENT SQL command.
>From the ORACLE 7 SQL Language Reference Manual:
-----------------------------------------------------
COMMENT
Purpose:
To add a comment about a table, view, snapshot, or
column into the data dictionary.
Prerequisites:
The table, view, or snapshot must be in your own
schema
or you must have COMMENT ANY TABLE system privilege.
Syntax:
COMMENT ON [ TABLE table ] |
[ COLUMN table.column] IS 'text'
You can effectively drop a comment from the database
by setting it to the empty string ''.
-----------------------------------------------------
Example:
COMMENT ON TABLE workorders IS
'Maintains base records for workorder information';
COMMENT ON COLUMN workorders.hours IS
'Number of hours the engineer worked on the task';
to drop a comment:
COMMENT ON COLUMN workorders.hours IS '';
The current patch will simply perform the insert into
pg_description, as per the TODO. And, of course, when
the table is dropped, any comments relating to it
or any of its attributes are also dropped. I haven't
looked at the ODBC source yet, but I do know from
an ODBC client standpoint that the standard does
support the notion of table and column comments.
Hopefully the ODBC driver is already fetching these
values from pg_description, but if not, it should be
trivial.
Hope this makes the grade,
Mike Mascari
(mascarim@yahoo.com)
1999-10-15 03:49:49 +02:00
|
|
|
|
1999-10-26 05:12:39 +02:00
|
|
|
heap_close(pg_attribute_desc, RowExclusiveLock);
|
This patch implements ORACLE's COMMENT SQL command.
>From the ORACLE 7 SQL Language Reference Manual:
-----------------------------------------------------
COMMENT
Purpose:
To add a comment about a table, view, snapshot, or
column into the data dictionary.
Prerequisites:
The table, view, or snapshot must be in your own
schema
or you must have COMMENT ANY TABLE system privilege.
Syntax:
COMMENT ON [ TABLE table ] |
[ COLUMN table.column] IS 'text'
You can effectively drop a comment from the database
by setting it to the empty string ''.
-----------------------------------------------------
Example:
COMMENT ON TABLE workorders IS
'Maintains base records for workorder information';
COMMENT ON COLUMN workorders.hours IS
'Number of hours the engineer worked on the task';
to drop a comment:
COMMENT ON COLUMN workorders.hours IS '';
The current patch will simply perform the insert into
pg_description, as per the TODO. And, of course, when
the table is dropped, any comments relating to it
or any of its attributes are also dropped. I haven't
looked at the ODBC source yet, but I do know from
an ODBC client standpoint that the standard does
support the notion of table and column comments.
Hopefully the ODBC driver is already fetching these
values from pg_description, but if not, it should be
trivial.
Hope this makes the grade,
Mike Mascari
(mascarim@yahoo.com)
1999-10-15 03:49:49 +02:00
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* --------------------------------
|
1999-02-02 04:45:56 +01:00
|
|
|
* DeleteTypeTuple
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* If the user attempts to destroy a relation and there
|
|
|
|
* exists attributes in other relations of type
|
|
|
|
* "relation we are deleting", then we have to do something
|
|
|
|
* special. presently we disallow the destroy.
|
1996-07-09 08:22:35 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
static void
|
1999-02-02 04:45:56 +01:00
|
|
|
DeleteTypeTuple(Relation rel)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation pg_type_desc;
|
|
|
|
HeapScanDesc pg_type_scan;
|
|
|
|
Relation pg_attribute_desc;
|
|
|
|
HeapScanDesc pg_attribute_scan;
|
|
|
|
ScanKeyData key;
|
|
|
|
ScanKeyData attkey;
|
|
|
|
HeapTuple tup;
|
|
|
|
HeapTuple atttup;
|
|
|
|
Oid typoid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* open pg_type
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
pg_type_desc = heap_openr(TypeRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* create a scan key to locate the type tuple corresponding
|
|
|
|
* to this relation.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-08-21 00:07:46 +02:00
|
|
|
ScanKeyEntryInitialize(&key, 0,
|
1998-09-01 06:40:42 +02:00
|
|
|
Anum_pg_type_typrelid,
|
|
|
|
F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(RelationGetRelid(rel)));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
pg_type_scan = heap_beginscan(pg_type_desc,
|
|
|
|
0,
|
1998-07-27 21:38:40 +02:00
|
|
|
SnapshotNow,
|
1997-09-07 07:04:48 +02:00
|
|
|
1,
|
|
|
|
&key);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* use heap_getnext() to fetch the pg_type tuple. If this
|
|
|
|
* tuple is not valid then something's wrong.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
tup = heap_getnext(pg_type_scan, 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
if (!HeapTupleIsValid(tup))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
heap_endscan(pg_type_scan);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_type_desc, RowExclusiveLock);
|
2000-10-23 01:32:48 +02:00
|
|
|
elog(ERROR, "DeleteTypeTuple: type \"%s\" does not exist",
|
1999-11-08 00:08:36 +01:00
|
|
|
RelationGetRelationName(rel));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* now scan pg_attribute. if any other relations have
|
|
|
|
* attributes of the type of the relation we are deleteing
|
|
|
|
* then we have to disallow the deletion. should talk to
|
|
|
|
* stonebraker about this. -cim 6/19/90
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-11-27 20:52:36 +01:00
|
|
|
typoid = tup->t_data->t_oid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
pg_attribute_desc = heap_openr(AttributeRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
ScanKeyEntryInitialize(&attkey,
|
1998-08-21 00:07:46 +02:00
|
|
|
0,
|
|
|
|
Anum_pg_attribute_atttypid,
|
|
|
|
F_OIDEQ,
|
1997-09-07 07:04:48 +02:00
|
|
|
typoid);
|
|
|
|
|
|
|
|
pg_attribute_scan = heap_beginscan(pg_attribute_desc,
|
|
|
|
0,
|
1998-07-27 21:38:40 +02:00
|
|
|
SnapshotNow,
|
1997-09-07 07:04:48 +02:00
|
|
|
1,
|
|
|
|
&attkey);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* try and get a pg_attribute tuple. if we succeed it means
|
1998-08-21 00:07:46 +02:00
|
|
|
* we can't delete the relation because something depends on
|
1997-09-07 07:04:48 +02:00
|
|
|
* the schema.
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
atttup = heap_getnext(pg_attribute_scan, 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-21 00:07:46 +02:00
|
|
|
if (HeapTupleIsValid(atttup))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-09-01 05:29:17 +02:00
|
|
|
Oid relid = ((Form_pg_attribute) GETSTRUCT(atttup))->attrelid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
heap_endscan(pg_attribute_scan);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_attribute_desc, RowExclusiveLock);
|
|
|
|
heap_endscan(pg_type_scan);
|
|
|
|
heap_close(pg_type_desc, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-10 02:46:32 +02:00
|
|
|
elog(ERROR, "DeleteTypeTuple: att of type %s exists in relation %u",
|
1999-11-08 00:08:36 +01:00
|
|
|
RelationGetRelationName(rel), relid);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-11-06 08:31:26 +01:00
|
|
|
heap_endscan(pg_attribute_scan);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_attribute_desc, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* Ok, it's safe so we delete the relation tuple
|
|
|
|
* from pg_type and finish up. But first end the scan so that
|
|
|
|
* we release the read lock on pg_type. -mer 13 Aug 1991
|
|
|
|
* ----------------
|
|
|
|
*/
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
heap_delete(pg_type_desc, &tup->t_self, NULL);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
heap_endscan(pg_type_scan);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_type_desc, RowExclusiveLock);
|
1996-11-06 08:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
1999-12-10 04:56:14 +01:00
|
|
|
* heap_drop_with_catalog
|
1996-11-06 08:31:26 +01:00
|
|
|
*
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2000-07-04 08:11:54 +02:00
|
|
|
heap_drop_with_catalog(const char *relname,
|
|
|
|
bool allow_system_table_mods)
|
1996-11-06 08:31:26 +01:00
|
|
|
{
|
1998-08-19 04:04:17 +02:00
|
|
|
Relation rel;
|
1997-09-08 04:41:22 +02:00
|
|
|
Oid rid;
|
2000-07-04 01:10:14 +02:00
|
|
|
bool has_toasttable;
|
2000-11-08 23:10:03 +01:00
|
|
|
bool istemp = (get_temp_rel_by_username(relname) != NULL);
|
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
1999-09-18 21:08:25 +02:00
|
|
|
* Open and lock the relation.
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-18 21:08:25 +02:00
|
|
|
rel = heap_openr(relname, AccessExclusiveLock);
|
1999-11-28 03:03:04 +01:00
|
|
|
rid = RelationGetRelid(rel);
|
2000-11-08 23:10:03 +01:00
|
|
|
has_toasttable = rel->rd_rel->reltoastrelid != InvalidOid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* prevent deletion of system relations
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-02-02 04:45:56 +01:00
|
|
|
/* allow temp of pg_class? Guess so. */
|
2000-07-04 08:11:54 +02:00
|
|
|
if (!istemp && !allow_system_table_mods &&
|
1999-11-08 00:08:36 +01:00
|
|
|
IsSystemRelationName(RelationGetRelationName(rel)))
|
2000-11-08 23:10:03 +01:00
|
|
|
elog(ERROR, "System relation \"%s\" may not be dropped",
|
1999-11-08 00:08:36 +01:00
|
|
|
RelationGetRelationName(rel));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-09-05 19:43:47 +02:00
|
|
|
/* ----------------
|
2000-11-08 23:10:03 +01:00
|
|
|
* Release all buffers that belong to this relation, after writing
|
|
|
|
* any that are dirty
|
1999-09-05 19:43:47 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-11-08 23:10:03 +01:00
|
|
|
i = FlushRelationBuffers(rel, (BlockNumber) 0);
|
|
|
|
if (i < 0)
|
|
|
|
elog(ERROR, "heap_drop_with_catalog: FlushRelationBuffers returned %d",
|
|
|
|
i);
|
1999-09-05 19:43:47 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
2000-11-08 23:10:03 +01:00
|
|
|
* remove rules if necessary
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-11-08 23:10:03 +01:00
|
|
|
if (rel->rd_rules != NULL)
|
|
|
|
RelationRemoveRules(rid);
|
|
|
|
|
|
|
|
/* triggers */
|
|
|
|
RelationRemoveTriggers(rel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
2000-11-08 23:10:03 +01:00
|
|
|
* remove inheritance information
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-11-08 23:10:03 +01:00
|
|
|
RelationRemoveInheritance(rel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
2000-11-08 23:10:03 +01:00
|
|
|
* remove indexes if necessary
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-11-08 23:10:03 +01:00
|
|
|
RelationRemoveIndexes(rel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* delete attribute tuples
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-02-02 04:45:56 +01:00
|
|
|
DeleteAttributeTuples(rel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
This patch implements ORACLE's COMMENT SQL command.
>From the ORACLE 7 SQL Language Reference Manual:
-----------------------------------------------------
COMMENT
Purpose:
To add a comment about a table, view, snapshot, or
column into the data dictionary.
Prerequisites:
The table, view, or snapshot must be in your own
schema
or you must have COMMENT ANY TABLE system privilege.
Syntax:
COMMENT ON [ TABLE table ] |
[ COLUMN table.column] IS 'text'
You can effectively drop a comment from the database
by setting it to the empty string ''.
-----------------------------------------------------
Example:
COMMENT ON TABLE workorders IS
'Maintains base records for workorder information';
COMMENT ON COLUMN workorders.hours IS
'Number of hours the engineer worked on the task';
to drop a comment:
COMMENT ON COLUMN workorders.hours IS '';
The current patch will simply perform the insert into
pg_description, as per the TODO. And, of course, when
the table is dropped, any comments relating to it
or any of its attributes are also dropped. I haven't
looked at the ODBC source yet, but I do know from
an ODBC client standpoint that the standard does
support the notion of table and column comments.
Hopefully the ODBC driver is already fetching these
values from pg_description, but if not, it should be
trivial.
Hope this makes the grade,
Mike Mascari
(mascarim@yahoo.com)
1999-10-15 03:49:49 +02:00
|
|
|
/* ----------------
|
2000-01-18 00:57:48 +01:00
|
|
|
* delete comments, statistics, and constraints
|
1999-11-28 03:03:04 +01:00
|
|
|
* ----------------
|
|
|
|
*/
|
This patch implements ORACLE's COMMENT SQL command.
>From the ORACLE 7 SQL Language Reference Manual:
-----------------------------------------------------
COMMENT
Purpose:
To add a comment about a table, view, snapshot, or
column into the data dictionary.
Prerequisites:
The table, view, or snapshot must be in your own
schema
or you must have COMMENT ANY TABLE system privilege.
Syntax:
COMMENT ON [ TABLE table ] |
[ COLUMN table.column] IS 'text'
You can effectively drop a comment from the database
by setting it to the empty string ''.
-----------------------------------------------------
Example:
COMMENT ON TABLE workorders IS
'Maintains base records for workorder information';
COMMENT ON COLUMN workorders.hours IS
'Number of hours the engineer worked on the task';
to drop a comment:
COMMENT ON COLUMN workorders.hours IS '';
The current patch will simply perform the insert into
pg_description, as per the TODO. And, of course, when
the table is dropped, any comments relating to it
or any of its attributes are also dropped. I haven't
looked at the ODBC source yet, but I do know from
an ODBC client standpoint that the standard does
support the notion of table and column comments.
Hopefully the ODBC driver is already fetching these
values from pg_description, but if not, it should be
trivial.
Hope this makes the grade,
Mike Mascari
(mascarim@yahoo.com)
1999-10-15 03:49:49 +02:00
|
|
|
DeleteComments(RelationGetRelid(rel));
|
|
|
|
|
1999-11-28 03:03:04 +01:00
|
|
|
RemoveStatistics(rel);
|
|
|
|
|
2000-01-18 00:57:48 +01:00
|
|
|
RemoveConstraints(rel);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
2000-01-18 00:57:48 +01:00
|
|
|
* delete type tuple
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1999-02-02 04:45:56 +01:00
|
|
|
DeleteTypeTuple(rel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* delete relation tuple
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-02-02 04:45:56 +01:00
|
|
|
DeleteRelationTuple(rel);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
1999-09-05 19:43:47 +02:00
|
|
|
* unlink the relation's physical file and finish up.
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-11-08 23:10:03 +01:00
|
|
|
if (rel->rd_rel->relkind != RELKIND_VIEW)
|
1998-08-19 04:04:17 +02:00
|
|
|
smgrunlink(DEFAULT_SMGR, rel);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
/*
|
|
|
|
* Close relcache entry, but *keep* AccessExclusiveLock on the
|
2000-04-12 19:17:23 +02:00
|
|
|
* relation until transaction commit. This ensures no one else will
|
|
|
|
* try to do something with the doomed relation.
|
1999-09-18 21:08:25 +02:00
|
|
|
*/
|
|
|
|
heap_close(rel, NoLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-09-05 19:43:47 +02:00
|
|
|
/* ----------------
|
|
|
|
* flush the relation from the relcache
|
|
|
|
* ----------------
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
RelationForgetRelation(rid);
|
1999-11-16 05:14:03 +01:00
|
|
|
|
2000-11-08 23:10:03 +01:00
|
|
|
/* and from the temp-table map */
|
1999-11-16 05:14:03 +01:00
|
|
|
if (istemp)
|
2000-10-11 23:28:19 +02:00
|
|
|
remove_temp_rel_by_relid(rid);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
|
|
|
if (has_toasttable)
|
|
|
|
{
|
|
|
|
char toast_relname[NAMEDATALEN];
|
|
|
|
|
2000-07-04 08:11:54 +02:00
|
|
|
sprintf(toast_relname, "pg_toast_%u", rid);
|
|
|
|
heap_drop_with_catalog(toast_relname, true);
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
1996-11-06 08:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Store a default expression for column attnum of relation rel.
|
|
|
|
* The expression must be presented as a nodeToString() string.
|
|
|
|
* If updatePgAttribute is true, update the pg_attribute entry
|
|
|
|
* for the column to show that a default exists.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
static void
|
1999-10-04 01:55:40 +02:00
|
|
|
StoreAttrDefault(Relation rel, AttrNumber attnum, char *adbin,
|
|
|
|
bool updatePgAttribute)
|
1997-08-22 04:58:51 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Node *expr;
|
1999-10-04 01:55:40 +02:00
|
|
|
RangeTblEntry *rte;
|
|
|
|
char *adsrc;
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation adrel;
|
|
|
|
Relation idescs[Num_pg_attrdef_indices];
|
|
|
|
HeapTuple tuple;
|
|
|
|
Datum values[4];
|
2000-04-12 19:17:23 +02:00
|
|
|
static char nulls[4] = {' ', ' ', ' ', ' '};
|
1999-10-04 01:55:40 +02:00
|
|
|
Relation attrrel;
|
|
|
|
Relation attridescs[Num_pg_attr_indices];
|
|
|
|
HeapTuple atttup;
|
|
|
|
Form_pg_attribute attStruct;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-10-04 04:12:26 +02:00
|
|
|
/*
|
|
|
|
* Need to construct source equivalent of given node-string.
|
|
|
|
*/
|
1999-10-04 01:55:40 +02:00
|
|
|
expr = stringToNode(adbin);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* deparse_expression needs a RangeTblEntry list, so make one
|
|
|
|
*/
|
|
|
|
rte = makeNode(RangeTblEntry);
|
1999-11-08 00:08:36 +01:00
|
|
|
rte->relname = RelationGetRelationName(rel);
|
1999-10-04 01:55:40 +02:00
|
|
|
rte->relid = RelationGetRelid(rel);
|
2000-09-12 23:07:18 +02:00
|
|
|
rte->eref = makeNode(Attr);
|
|
|
|
rte->eref->relname = RelationGetRelationName(rel);
|
1999-10-04 01:55:40 +02:00
|
|
|
rte->inh = false;
|
|
|
|
rte->inFromCl = true;
|
2000-09-29 20:21:41 +02:00
|
|
|
adsrc = deparse_expression(expr, makeList1(makeList1(rte)), false);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-11-28 03:03:04 +01:00
|
|
|
values[Anum_pg_attrdef_adrelid - 1] = RelationGetRelid(rel);
|
1999-10-04 01:55:40 +02:00
|
|
|
values[Anum_pg_attrdef_adnum - 1] = attnum;
|
2000-07-06 01:12:09 +02:00
|
|
|
values[Anum_pg_attrdef_adbin - 1] = DirectFunctionCall1(textin,
|
|
|
|
CStringGetDatum(adbin));
|
|
|
|
values[Anum_pg_attrdef_adsrc - 1] = DirectFunctionCall1(textin,
|
|
|
|
CStringGetDatum(adsrc));
|
1999-09-18 21:08:25 +02:00
|
|
|
adrel = heap_openr(AttrDefaultRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
tuple = heap_formtuple(adrel->rd_att, values, nulls);
|
|
|
|
heap_insert(adrel, tuple);
|
1999-10-04 01:55:40 +02:00
|
|
|
CatalogOpenIndices(Num_pg_attrdef_indices, Name_pg_attrdef_indices,
|
|
|
|
idescs);
|
1997-09-07 07:04:48 +02:00
|
|
|
CatalogIndexInsert(idescs, Num_pg_attrdef_indices, adrel, tuple);
|
|
|
|
CatalogCloseIndices(Num_pg_attrdef_indices, idescs);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(adrel, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
pfree(DatumGetPointer(values[Anum_pg_attrdef_adbin - 1]));
|
|
|
|
pfree(DatumGetPointer(values[Anum_pg_attrdef_adsrc - 1]));
|
1999-12-16 23:20:03 +01:00
|
|
|
heap_freetuple(tuple);
|
1999-10-04 01:55:40 +02:00
|
|
|
pfree(adsrc);
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!updatePgAttribute)
|
1999-10-04 01:55:40 +02:00
|
|
|
return; /* done if pg_attribute is OK */
|
|
|
|
|
|
|
|
attrrel = heap_openr(AttributeRelationName, RowExclusiveLock);
|
|
|
|
atttup = SearchSysCacheTupleCopy(ATTNUM,
|
2000-04-12 19:17:23 +02:00
|
|
|
ObjectIdGetDatum(RelationGetRelid(rel)),
|
1999-10-04 01:55:40 +02:00
|
|
|
(Datum) attnum, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(atttup))
|
|
|
|
elog(ERROR, "cache lookup of attribute %d in relation %u failed",
|
1999-11-28 03:03:04 +01:00
|
|
|
attnum, RelationGetRelid(rel));
|
1999-10-04 01:55:40 +02:00
|
|
|
attStruct = (Form_pg_attribute) GETSTRUCT(atttup);
|
2000-04-12 19:17:23 +02:00
|
|
|
if (!attStruct->atthasdef)
|
1999-10-04 01:55:40 +02:00
|
|
|
{
|
|
|
|
attStruct->atthasdef = true;
|
1999-11-24 01:44:37 +01:00
|
|
|
heap_update(attrrel, &atttup->t_self, atttup, NULL);
|
1999-10-04 01:55:40 +02:00
|
|
|
/* keep catalog indices current */
|
|
|
|
CatalogOpenIndices(Num_pg_attr_indices, Name_pg_attr_indices,
|
|
|
|
attridescs);
|
|
|
|
CatalogIndexInsert(attridescs, Num_pg_attr_indices, attrrel, atttup);
|
|
|
|
CatalogCloseIndices(Num_pg_attr_indices, attridescs);
|
|
|
|
}
|
|
|
|
heap_close(attrrel, RowExclusiveLock);
|
1999-12-16 23:20:03 +01:00
|
|
|
heap_freetuple(atttup);
|
1997-08-22 04:58:51 +02:00
|
|
|
}
|
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Store a constraint expression for the given relation.
|
|
|
|
* The expression must be presented as a nodeToString() string.
|
|
|
|
*
|
|
|
|
* Caller is responsible for updating the count of constraints
|
|
|
|
* in the pg_class entry for the relation.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
static void
|
1999-10-04 01:55:40 +02:00
|
|
|
StoreRelCheck(Relation rel, char *ccname, char *ccbin)
|
1997-08-22 04:58:51 +02:00
|
|
|
{
|
1999-10-04 01:55:40 +02:00
|
|
|
Node *expr;
|
|
|
|
RangeTblEntry *rte;
|
|
|
|
char *ccsrc;
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation rcrel;
|
|
|
|
Relation idescs[Num_pg_relcheck_indices];
|
|
|
|
HeapTuple tuple;
|
|
|
|
Datum values[4];
|
2000-04-12 19:17:23 +02:00
|
|
|
static char nulls[4] = {' ', ' ', ' ', ' '};
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
1999-10-04 01:55:40 +02:00
|
|
|
* Convert condition to a normal boolean expression tree.
|
1998-11-12 16:39:06 +01:00
|
|
|
*/
|
1999-10-04 01:55:40 +02:00
|
|
|
expr = stringToNode(ccbin);
|
|
|
|
expr = (Node *) make_ands_explicit((List *) expr);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* deparse_expression needs a RangeTblEntry list, so make one
|
|
|
|
*/
|
|
|
|
rte = makeNode(RangeTblEntry);
|
1999-11-08 00:08:36 +01:00
|
|
|
rte->relname = RelationGetRelationName(rel);
|
1999-10-04 01:55:40 +02:00
|
|
|
rte->relid = RelationGetRelid(rel);
|
2000-09-12 23:07:18 +02:00
|
|
|
rte->eref = makeNode(Attr);
|
|
|
|
rte->eref->relname = RelationGetRelationName(rel);
|
1999-10-04 01:55:40 +02:00
|
|
|
rte->inh = false;
|
|
|
|
rte->inFromCl = true;
|
2000-09-29 20:21:41 +02:00
|
|
|
ccsrc = deparse_expression(expr, makeList1(makeList1(rte)), false);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-11-28 03:03:04 +01:00
|
|
|
values[Anum_pg_relcheck_rcrelid - 1] = RelationGetRelid(rel);
|
2000-08-03 18:35:08 +02:00
|
|
|
values[Anum_pg_relcheck_rcname - 1] = DirectFunctionCall1(namein,
|
|
|
|
CStringGetDatum(ccname));
|
2000-07-06 01:12:09 +02:00
|
|
|
values[Anum_pg_relcheck_rcbin - 1] = DirectFunctionCall1(textin,
|
|
|
|
CStringGetDatum(ccbin));
|
|
|
|
values[Anum_pg_relcheck_rcsrc - 1] = DirectFunctionCall1(textin,
|
|
|
|
CStringGetDatum(ccsrc));
|
1999-09-18 21:08:25 +02:00
|
|
|
rcrel = heap_openr(RelCheckRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
tuple = heap_formtuple(rcrel->rd_att, values, nulls);
|
|
|
|
heap_insert(rcrel, tuple);
|
1999-10-04 01:55:40 +02:00
|
|
|
CatalogOpenIndices(Num_pg_relcheck_indices, Name_pg_relcheck_indices,
|
|
|
|
idescs);
|
1997-09-07 07:04:48 +02:00
|
|
|
CatalogIndexInsert(idescs, Num_pg_relcheck_indices, rcrel, tuple);
|
|
|
|
CatalogCloseIndices(Num_pg_relcheck_indices, idescs);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(rcrel, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
pfree(DatumGetPointer(values[Anum_pg_relcheck_rcname - 1]));
|
|
|
|
pfree(DatumGetPointer(values[Anum_pg_relcheck_rcbin - 1]));
|
|
|
|
pfree(DatumGetPointer(values[Anum_pg_relcheck_rcsrc - 1]));
|
1999-12-16 23:20:03 +01:00
|
|
|
heap_freetuple(tuple);
|
1999-10-04 01:55:40 +02:00
|
|
|
pfree(ccsrc);
|
1997-08-22 16:10:26 +02:00
|
|
|
}
|
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Store defaults and constraints passed in via the tuple constraint struct.
|
|
|
|
*
|
|
|
|
* NOTE: only pre-cooked expressions will be passed this way, which is to
|
|
|
|
* say expressions inherited from an existing relation. Newly parsed
|
|
|
|
* expressions can be added later, by direct calls to StoreAttrDefault
|
|
|
|
* and StoreRelCheck (see AddRelationRawConstraints()). We assume that
|
|
|
|
* pg_attribute and pg_class entries for the relation were already set
|
|
|
|
* to reflect the existence of these defaults/constraints.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
static void
|
|
|
|
StoreConstraints(Relation rel)
|
1997-08-22 16:10:26 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
TupleConstr *constr = rel->rd_att->constr;
|
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!constr)
|
|
|
|
return;
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* deparsing of constraint expressions will fail unless the
|
|
|
|
* just-created pg_attribute tuples for this relation are made
|
|
|
|
* visible. So, bump the command counter.
|
2000-01-16 20:57:00 +01:00
|
|
|
*/
|
|
|
|
CommandCounterIncrement();
|
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
for (i = 0; i < constr->num_defval; i++)
|
|
|
|
StoreAttrDefault(rel, constr->defval[i].adnum,
|
|
|
|
constr->defval[i].adbin, false);
|
|
|
|
|
|
|
|
for (i = 0; i < constr->num_check; i++)
|
|
|
|
StoreRelCheck(rel, constr->check[i].ccname,
|
|
|
|
constr->check[i].ccbin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AddRelationRawConstraints
|
|
|
|
*
|
|
|
|
* Add raw (not-yet-transformed) column default expressions and/or constraint
|
|
|
|
* check expressions to an existing relation. This is defined to do both
|
|
|
|
* for efficiency in DefineRelation, but of course you can do just one or
|
|
|
|
* the other by passing empty lists.
|
|
|
|
*
|
|
|
|
* rel: relation to be modified
|
|
|
|
* rawColDefaults: list of RawColumnDefault structures
|
|
|
|
* rawConstraints: list of Constraint nodes
|
|
|
|
*
|
|
|
|
* All entries in rawColDefaults will be processed. Entries in rawConstraints
|
|
|
|
* will be processed only if they are CONSTR_CHECK type and contain a "raw"
|
|
|
|
* expression.
|
|
|
|
*
|
|
|
|
* NB: caller should have opened rel with AccessExclusiveLock, and should
|
2000-04-12 19:17:23 +02:00
|
|
|
* hold that lock till end of transaction. Also, we assume the caller has
|
2000-01-16 20:57:00 +01:00
|
|
|
* done a CommandCounterIncrement if necessary to make the relation's catalog
|
|
|
|
* tuples visible.
|
1999-10-04 01:55:40 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
AddRelationRawConstraints(Relation rel,
|
|
|
|
List *rawColDefaults,
|
|
|
|
List *rawConstraints)
|
|
|
|
{
|
1999-11-08 00:08:36 +01:00
|
|
|
char *relname = RelationGetRelationName(rel);
|
1999-10-04 01:55:40 +02:00
|
|
|
TupleDesc tupleDesc;
|
|
|
|
TupleConstr *oldconstr;
|
|
|
|
int numoldchecks;
|
|
|
|
ConstrCheck *oldchecks;
|
|
|
|
ParseState *pstate;
|
2000-09-12 23:07:18 +02:00
|
|
|
RangeTblEntry *rte;
|
1999-10-04 01:55:40 +02:00
|
|
|
int numchecks;
|
|
|
|
List *listptr;
|
|
|
|
Relation relrel;
|
|
|
|
Relation relidescs[Num_pg_class_indices];
|
|
|
|
HeapTuple reltup;
|
|
|
|
Form_pg_class relStruct;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get info about existing constraints.
|
|
|
|
*/
|
|
|
|
tupleDesc = RelationGetDescr(rel);
|
|
|
|
oldconstr = tupleDesc->constr;
|
|
|
|
if (oldconstr)
|
|
|
|
{
|
|
|
|
numoldchecks = oldconstr->num_check;
|
|
|
|
oldchecks = oldconstr->check;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
numoldchecks = 0;
|
|
|
|
oldchecks = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Create a dummy ParseState and insert the target relation as its
|
|
|
|
* sole rangetable entry. We need a ParseState for transformExpr.
|
1999-10-04 01:55:40 +02:00
|
|
|
*/
|
|
|
|
pstate = make_parsestate(NULL);
|
2000-02-15 04:38:29 +01:00
|
|
|
makeRangeTable(pstate, NULL);
|
2000-09-12 23:07:18 +02:00
|
|
|
rte = addRangeTableEntry(pstate, relname, NULL, false, true);
|
2000-09-29 20:21:41 +02:00
|
|
|
addRTEtoJoinList(pstate, rte);
|
1999-10-04 01:55:40 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Process column default expressions.
|
|
|
|
*/
|
|
|
|
foreach(listptr, rawColDefaults)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-10-04 01:55:40 +02:00
|
|
|
RawColumnDefault *colDef = (RawColumnDefault *) lfirst(listptr);
|
|
|
|
Node *expr;
|
1999-10-04 04:12:26 +02:00
|
|
|
Oid type_id;
|
1999-10-04 01:55:40 +02:00
|
|
|
|
|
|
|
Assert(colDef->raw_default != NULL);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Transform raw parsetree to executable expression.
|
|
|
|
*/
|
|
|
|
expr = transformExpr(pstate, colDef->raw_default, EXPR_COLUMN_FIRST);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Make sure default expr does not refer to any vars.
|
|
|
|
*/
|
|
|
|
if (contain_var_clause(expr))
|
|
|
|
elog(ERROR, "Cannot use attribute(s) in DEFAULT clause");
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-09-29 20:21:41 +02:00
|
|
|
/*
|
|
|
|
* No subplans or aggregates, either...
|
|
|
|
*/
|
|
|
|
if (contain_subplans(expr))
|
|
|
|
elog(ERROR, "Cannot use subselect in DEFAULT clause");
|
|
|
|
if (contain_agg_clause(expr))
|
|
|
|
elog(ERROR, "Cannot use aggregate in DEFAULT clause");
|
|
|
|
|
1999-10-04 04:12:26 +02:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Check that it will be possible to coerce the expression to the
|
|
|
|
* column's type. We store the expression without coercion,
|
|
|
|
* however, to avoid premature coercion in cases like
|
1999-10-04 04:12:26 +02:00
|
|
|
*
|
|
|
|
* CREATE TABLE tbl (fld datetime DEFAULT 'now');
|
|
|
|
*
|
2000-04-12 19:17:23 +02:00
|
|
|
* NB: this should match the code in updateTargetListEntry() that
|
|
|
|
* will actually do the coercion, to ensure we don't accept an
|
|
|
|
* unusable default expression.
|
1999-10-04 04:12:26 +02:00
|
|
|
*/
|
|
|
|
type_id = exprType(expr);
|
|
|
|
if (type_id != InvalidOid)
|
|
|
|
{
|
|
|
|
Form_pg_attribute atp = rel->rd_att->attrs[colDef->attnum - 1];
|
|
|
|
|
|
|
|
if (type_id != atp->atttypid)
|
|
|
|
{
|
2000-01-17 03:04:16 +01:00
|
|
|
if (CoerceTargetExpr(NULL, expr, type_id,
|
2000-04-12 19:17:23 +02:00
|
|
|
atp->atttypid, atp->atttypmod) == NULL)
|
1999-10-04 04:12:26 +02:00
|
|
|
elog(ERROR, "Attribute '%s' is of type '%s'"
|
|
|
|
" but default expression is of type '%s'"
|
2000-04-12 19:17:23 +02:00
|
|
|
"\n\tYou will need to rewrite or cast the expression",
|
1999-11-08 00:08:36 +01:00
|
|
|
NameStr(atp->attname),
|
1999-10-04 04:12:26 +02:00
|
|
|
typeidTypeName(atp->atttypid),
|
|
|
|
typeidTypeName(type_id));
|
|
|
|
}
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Might as well try to reduce any constant expressions.
|
|
|
|
*/
|
|
|
|
expr = eval_const_expressions(expr);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Must fix opids, in case any operators remain...
|
|
|
|
*/
|
|
|
|
fix_opids(expr);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* OK, store it.
|
|
|
|
*/
|
|
|
|
StoreAttrDefault(rel, colDef->attnum, nodeToString(expr), true);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Process constraint expressions.
|
|
|
|
*/
|
|
|
|
numchecks = numoldchecks;
|
|
|
|
foreach(listptr, rawConstraints)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-10-04 01:55:40 +02:00
|
|
|
Constraint *cdef = (Constraint *) lfirst(listptr);
|
|
|
|
char *ccname;
|
|
|
|
Node *expr;
|
|
|
|
|
|
|
|
if (cdef->contype != CONSTR_CHECK || cdef->raw_expr == NULL)
|
|
|
|
continue;
|
|
|
|
Assert(cdef->cooked_expr == NULL);
|
|
|
|
|
|
|
|
/* Check name uniqueness, or generate a new name */
|
|
|
|
if (cdef->name != NULL)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
List *listptr2;
|
|
|
|
|
|
|
|
ccname = cdef->name;
|
|
|
|
/* Check against old constraints */
|
|
|
|
for (i = 0; i < numoldchecks; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(oldchecks[i].ccname, ccname) == 0)
|
|
|
|
elog(ERROR, "Duplicate CHECK constraint name: '%s'",
|
|
|
|
ccname);
|
|
|
|
}
|
|
|
|
/* Check against other new constraints */
|
|
|
|
foreach(listptr2, rawConstraints)
|
|
|
|
{
|
|
|
|
Constraint *cdef2 = (Constraint *) lfirst(listptr2);
|
|
|
|
|
|
|
|
if (cdef2 == cdef ||
|
|
|
|
cdef2->contype != CONSTR_CHECK ||
|
|
|
|
cdef2->raw_expr == NULL ||
|
|
|
|
cdef2->name == NULL)
|
|
|
|
continue;
|
|
|
|
if (strcmp(cdef2->name, ccname) == 0)
|
|
|
|
elog(ERROR, "Duplicate CHECK constraint name: '%s'",
|
|
|
|
ccname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ccname = (char *) palloc(NAMEDATALEN);
|
|
|
|
snprintf(ccname, NAMEDATALEN, "$%d", numchecks + 1);
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Transform raw parsetree to executable expression.
|
|
|
|
*/
|
|
|
|
expr = transformExpr(pstate, cdef->raw_expr, EXPR_COLUMN_FIRST);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 04:12:26 +02:00
|
|
|
/*
|
|
|
|
* Make sure it yields a boolean result.
|
|
|
|
*/
|
|
|
|
if (exprType(expr) != BOOLOID)
|
|
|
|
elog(ERROR, "CHECK '%s' does not yield boolean result",
|
|
|
|
ccname);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Make sure no outside relations are referred to.
|
|
|
|
*/
|
|
|
|
if (length(pstate->p_rtable) != 1)
|
2000-09-29 20:21:41 +02:00
|
|
|
elog(ERROR, "Only relation \"%s\" can be referenced in CHECK",
|
1999-10-04 01:55:40 +02:00
|
|
|
relname);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-09-29 20:21:41 +02:00
|
|
|
/*
|
|
|
|
* No subplans or aggregates, either...
|
|
|
|
*/
|
|
|
|
if (contain_subplans(expr))
|
|
|
|
elog(ERROR, "Cannot use subselect in CHECK clause");
|
|
|
|
if (contain_agg_clause(expr))
|
|
|
|
elog(ERROR, "Cannot use aggregate in CHECK clause");
|
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Might as well try to reduce any constant expressions.
|
|
|
|
*/
|
|
|
|
expr = eval_const_expressions(expr);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Constraints are evaluated with execQual, which expects an
|
|
|
|
* implicit-AND list, so convert expression to implicit-AND form.
|
|
|
|
* (We could go so far as to convert to CNF, but that's probably
|
|
|
|
* overkill...)
|
|
|
|
*/
|
|
|
|
expr = (Node *) make_ands_implicit((Expr *) expr);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Must fix opids in operator clauses.
|
|
|
|
*/
|
|
|
|
fix_opids(expr);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* OK, store it.
|
|
|
|
*/
|
|
|
|
StoreRelCheck(rel, ccname, nodeToString(expr));
|
|
|
|
|
|
|
|
numchecks++;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1999-10-04 01:55:40 +02:00
|
|
|
/*
|
|
|
|
* Update the count of constraints in the relation's pg_class tuple.
|
|
|
|
* We do this even if there was no change, in order to ensure that an
|
|
|
|
* SI update message is sent out for the pg_class tuple, which will
|
|
|
|
* force other backends to rebuild their relcache entries for the rel.
|
2000-04-12 19:17:23 +02:00
|
|
|
* (Of course, for a newly created rel there is no need for an SI
|
|
|
|
* message, but for ALTER TABLE ADD ATTRIBUTE this'd be important.)
|
1999-10-04 01:55:40 +02:00
|
|
|
*/
|
|
|
|
relrel = heap_openr(RelationRelationName, RowExclusiveLock);
|
|
|
|
reltup = SearchSysCacheTupleCopy(RELOID,
|
2000-04-12 19:17:23 +02:00
|
|
|
ObjectIdGetDatum(RelationGetRelid(rel)),
|
1999-10-04 01:55:40 +02:00
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(reltup))
|
1999-11-28 03:03:04 +01:00
|
|
|
elog(ERROR, "cache lookup of relation %u failed", RelationGetRelid(rel));
|
1999-10-04 01:55:40 +02:00
|
|
|
relStruct = (Form_pg_class) GETSTRUCT(reltup);
|
|
|
|
|
|
|
|
relStruct->relchecks = numchecks;
|
|
|
|
|
1999-11-24 01:44:37 +01:00
|
|
|
heap_update(relrel, &reltup->t_self, reltup, NULL);
|
1999-10-04 01:55:40 +02:00
|
|
|
|
|
|
|
/* keep catalog indices current */
|
|
|
|
CatalogOpenIndices(Num_pg_class_indices, Name_pg_class_indices,
|
|
|
|
relidescs);
|
|
|
|
CatalogIndexInsert(relidescs, Num_pg_class_indices, relrel, reltup);
|
|
|
|
CatalogCloseIndices(Num_pg_class_indices, relidescs);
|
|
|
|
|
|
|
|
heap_close(relrel, RowExclusiveLock);
|
1999-12-16 23:20:03 +01:00
|
|
|
heap_freetuple(reltup);
|
1997-08-22 16:10:26 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
static void
|
|
|
|
RemoveAttrDefault(Relation rel)
|
1997-08-22 16:10:26 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation adrel;
|
|
|
|
HeapScanDesc adscan;
|
|
|
|
ScanKeyData key;
|
|
|
|
HeapTuple tup;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
adrel = heap_openr(AttrDefaultRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
ScanKeyEntryInitialize(&key, 0, Anum_pg_attrdef_adrelid,
|
1999-11-28 03:03:04 +01:00
|
|
|
F_OIDEQ, RelationGetRelid(rel));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-07-27 21:38:40 +02:00
|
|
|
adscan = heap_beginscan(adrel, 0, SnapshotNow, 1, &key);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
while (HeapTupleIsValid(tup = heap_getnext(adscan, 0)))
|
1998-12-15 13:47:01 +01:00
|
|
|
heap_delete(adrel, &tup->t_self, NULL);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
heap_endscan(adscan);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(adrel, RowExclusiveLock);
|
1997-08-22 16:10:26 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
static void
|
|
|
|
RemoveRelCheck(Relation rel)
|
1997-08-22 16:10:26 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation rcrel;
|
|
|
|
HeapScanDesc rcscan;
|
|
|
|
ScanKeyData key;
|
|
|
|
HeapTuple tup;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
rcrel = heap_openr(RelCheckRelationName, RowExclusiveLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
ScanKeyEntryInitialize(&key, 0, Anum_pg_relcheck_rcrelid,
|
1999-11-28 03:03:04 +01:00
|
|
|
F_OIDEQ, RelationGetRelid(rel));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-07-27 21:38:40 +02:00
|
|
|
rcscan = heap_beginscan(rcrel, 0, SnapshotNow, 1, &key);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
while (HeapTupleIsValid(tup = heap_getnext(rcscan, 0)))
|
1998-12-15 13:47:01 +01:00
|
|
|
heap_delete(rcrel, &tup->t_self, NULL);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
heap_endscan(rcscan);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(rcrel, RowExclusiveLock);
|
1997-08-22 16:10:26 +02:00
|
|
|
}
|
1997-08-22 04:58:51 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
static void
|
|
|
|
RemoveConstraints(Relation rel)
|
1997-08-22 16:10:26 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
TupleConstr *constr = rel->rd_att->constr;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!constr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (constr->num_defval > 0)
|
|
|
|
RemoveAttrDefault(rel);
|
|
|
|
|
|
|
|
if (constr->num_check > 0)
|
|
|
|
RemoveRelCheck(rel);
|
1997-08-22 04:58:51 +02:00
|
|
|
}
|
1999-11-28 03:03:04 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
RemoveStatistics(Relation rel)
|
|
|
|
{
|
|
|
|
Relation pgstatistic;
|
|
|
|
HeapScanDesc scan;
|
|
|
|
ScanKeyData key;
|
|
|
|
HeapTuple tuple;
|
|
|
|
|
|
|
|
pgstatistic = heap_openr(StatisticRelationName, RowExclusiveLock);
|
|
|
|
|
|
|
|
ScanKeyEntryInitialize(&key, 0x0, Anum_pg_statistic_starelid,
|
|
|
|
F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(RelationGetRelid(rel)));
|
|
|
|
scan = heap_beginscan(pgstatistic, false, SnapshotNow, 1, &key);
|
|
|
|
|
|
|
|
while (HeapTupleIsValid(tuple = heap_getnext(scan, 0)))
|
|
|
|
heap_delete(pgstatistic, &tuple->t_self, NULL);
|
|
|
|
|
|
|
|
heap_endscan(scan);
|
|
|
|
heap_close(pgstatistic, RowExclusiveLock);
|
|
|
|
}
|