提交 4d3a6c1f 编写于 作者: H Heikki Linnakangas

Replace CaQL calls with syscache lookups, to match upstream.

These created merge conflicts while working on the 8.3 merge. In order to
keep that upcoming big merge slightly smaller, cherry-pick this change to
master now. pg_depend.c was already completely identical to the PostgreSQL
8.3 version, except for the use of CaQL.

Well, almost: there was also one instance of changing an old
heap_formtuple() call to the new heap_form_tuple() interface. I kept that
change. But other than that, the file is now 100% identical to upstream.
上级 442d896d
......@@ -3,12 +3,12 @@
* pg_depend.c
* routines to support manipulation of the pg_depend relation
*
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/catalog/pg_depend.c,v 1.24 2007/01/05 22:19:25 momjian Exp $
* $PostgreSQL: pgsql/src/backend/catalog/pg_depend.c,v 1.26 2008/01/01 19:45:48 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -16,7 +16,6 @@
#include "access/genam.h"
#include "access/heapam.h"
#include "catalog/catquery.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/pg_constraint.h"
......@@ -24,8 +23,6 @@
#include "miscadmin.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/tqual.h"
static bool isObjectPinned(const ObjectAddress *object, Relation rel);
......@@ -57,12 +54,11 @@ recordMultipleDependencies(const ObjectAddress *depender,
DependencyType behavior)
{
Relation dependDesc;
CatalogIndexState indstate;
HeapTuple tup;
int i;
bool nulls[Natts_pg_depend];
Datum values[Natts_pg_depend];
cqContext cqc;
cqContext *pcqCtx;
if (nreferenced <= 0)
return; /* nothing to do */
......@@ -76,10 +72,8 @@ recordMultipleDependencies(const ObjectAddress *depender,
dependDesc = heap_open(DependRelationId, RowExclusiveLock);
pcqCtx = caql_beginscan(
caql_addrel(cqclr(&cqc), dependDesc),
cql("INSERT INTO pg_depend ",
NULL));
/* Don't open indexes unless we need to make an update */
indstate = NULL;
memset(nulls, false, sizeof(nulls));
......@@ -106,16 +100,22 @@ recordMultipleDependencies(const ObjectAddress *depender,
values[Anum_pg_depend_deptype - 1] = CharGetDatum((char) behavior);
tup = caql_form_tuple(pcqCtx, values, nulls);
tup = heap_form_tuple(dependDesc->rd_att, values, nulls);
caql_insert(pcqCtx, tup);
/* and Update indexes (implicit) */
simple_heap_insert(dependDesc, tup);
/* keep indexes current */
if (indstate == NULL)
indstate = CatalogOpenIndexes(dependDesc);
CatalogIndexInsert(indstate, tup);
heap_freetuple(tup);
}
}
caql_endscan(pcqCtx);
if (indstate != NULL)
CatalogCloseIndexes(indstate);
heap_close(dependDesc, RowExclusiveLock);
}
......@@ -132,14 +132,34 @@ long
deleteDependencyRecordsFor(Oid classId, Oid objectId)
{
long count = 0;
Relation depRel;
ScanKeyData key[2];
SysScanDesc scan;
HeapTuple tup;
count = caql_getcount(
NULL,
cql("DELETE FROM pg_depend "
" WHERE classid = :1 "
" AND objid = :2 ",
ObjectIdGetDatum(classId),
ObjectIdGetDatum(objectId)));
depRel = heap_open(DependRelationId, RowExclusiveLock);
ScanKeyInit(&key[0],
Anum_pg_depend_classid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(classId));
ScanKeyInit(&key[1],
Anum_pg_depend_objid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(objectId));
scan = systable_beginscan(depRel, DependDependerIndexId, true,
SnapshotNow, 2, key);
while (HeapTupleIsValid(tup = systable_getnext(scan)))
{
simple_heap_delete(depRel, &tup->t_self);
count++;
}
systable_endscan(scan);
heap_close(depRel, RowExclusiveLock);
return count;
}
......@@ -163,8 +183,8 @@ changeDependencyFor(Oid classId, Oid objectId,
{
long count = 0;
Relation depRel;
cqContext *pcqCtx;
cqContext cqc;
ScanKeyData key[2];
SysScanDesc scan;
HeapTuple tup;
ObjectAddress objAddr;
bool newIsPinned;
......@@ -196,16 +216,19 @@ changeDependencyFor(Oid classId, Oid objectId,
newIsPinned = isObjectPinned(&objAddr, depRel);
/* Now search for dependency records */
pcqCtx = caql_beginscan(
caql_addrel(cqclr(&cqc), depRel),
cql("SELECT * FROM pg_depend "
" WHERE classid = :1 "
" AND objid = :2 "
" FOR UPDATE ",
ObjectIdGetDatum(classId),
ObjectIdGetDatum(objectId)));
while (HeapTupleIsValid((tup = caql_getnext(pcqCtx))))
ScanKeyInit(&key[0],
Anum_pg_depend_classid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(classId));
ScanKeyInit(&key[1],
Anum_pg_depend_objid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(objectId));
scan = systable_beginscan(depRel, DependDependerIndexId, true,
SnapshotNow, 2, key);
while (HeapTupleIsValid((tup = systable_getnext(scan))))
{
Form_pg_depend depform = (Form_pg_depend) GETSTRUCT(tup);
......@@ -213,7 +236,7 @@ changeDependencyFor(Oid classId, Oid objectId,
depform->refobjid == oldRefObjectId)
{
if (newIsPinned)
caql_delete_current(pcqCtx);
simple_heap_delete(depRel, &tup->t_self);
else
{
/* make a modifiable copy */
......@@ -222,7 +245,8 @@ changeDependencyFor(Oid classId, Oid objectId,
depform->refobjid = newRefObjectId;
caql_update_current(pcqCtx, tup);
simple_heap_update(depRel, &tup->t_self, tup);
CatalogUpdateIndexes(depRel, tup);
heap_freetuple(tup);
}
......@@ -231,7 +255,7 @@ changeDependencyFor(Oid classId, Oid objectId,
}
}
caql_endscan(pcqCtx);
systable_endscan(scan);
heap_close(depRel, RowExclusiveLock);
......@@ -251,16 +275,22 @@ static bool
isObjectPinned(const ObjectAddress *object, Relation rel)
{
bool ret = false;
cqContext cqc;
SysScanDesc scan;
HeapTuple tup;
ScanKeyData key[2];
ScanKeyInit(&key[0],
Anum_pg_depend_refclassid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(object->classId));
ScanKeyInit(&key[1],
Anum_pg_depend_refobjid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(object->objectId));
tup = caql_getfirst(
caql_addrel(cqclr(&cqc), rel),
cql("SELECT * FROM pg_depend "
" WHERE refclassid = :1 "
" AND refobjid = :2 ",
ObjectIdGetDatum(object->classId),
ObjectIdGetDatum(object->objectId)));
scan = systable_beginscan(rel, DependReferenceIndexId, true,
SnapshotNow, 2, key);
/*
* Since we won't generate additional pg_depend entries for pinned
......@@ -268,7 +298,7 @@ isObjectPinned(const ObjectAddress *object, Relation rel)
* Hence, it's sufficient to look at the first returned tuple; we don't
* need to loop.
*/
tup = systable_getnext(scan);
if (HeapTupleIsValid(tup))
{
Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
......@@ -277,6 +307,8 @@ isObjectPinned(const ObjectAddress *object, Relation rel)
ret = true;
}
systable_endscan(scan);
return ret;
}
......@@ -301,19 +333,26 @@ bool
sequenceIsOwned(Oid seqId, Oid *tableId, int32 *colId)
{
bool ret = false;
cqContext *pcqCtx;
Relation depRel;
ScanKeyData key[2];
SysScanDesc scan;
HeapTuple tup;
depRel = heap_open(DependRelationId, AccessShareLock);
ScanKeyInit(&key[0],
Anum_pg_depend_classid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(RelationRelationId));
ScanKeyInit(&key[1],
Anum_pg_depend_objid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(seqId));
pcqCtx = caql_beginscan(
NULL,
cql("SELECT * FROM pg_depend "
" WHERE classid = :1 "
" AND objid = :2 ",
ObjectIdGetDatum(RelationRelationId),
ObjectIdGetDatum(seqId)));
scan = systable_beginscan(depRel, DependDependerIndexId, true,
SnapshotNow, 2, key);
while (HeapTupleIsValid((tup = caql_getnext(pcqCtx))))
while (HeapTupleIsValid((tup = systable_getnext(scan))))
{
Form_pg_depend depform = (Form_pg_depend) GETSTRUCT(tup);
......@@ -327,7 +366,9 @@ sequenceIsOwned(Oid seqId, Oid *tableId, int32 *colId)
}
}
caql_endscan(pcqCtx);
systable_endscan(scan);
heap_close(depRel, AccessShareLock);
return ret;
}
......@@ -341,30 +382,39 @@ sequenceIsOwned(Oid seqId, Oid *tableId, int32 *colId)
void
markSequenceUnowned(Oid seqId)
{
cqContext *pcqCtx;
Relation depRel;
ScanKeyData key[2];
SysScanDesc scan;
HeapTuple tup;
pcqCtx = caql_beginscan(
NULL,
cql("SELECT * FROM pg_depend "
" WHERE classid = :1 "
" AND objid = :2 "
" FOR UPDATE ",
ObjectIdGetDatum(RelationRelationId),
ObjectIdGetDatum(seqId)));
depRel = heap_open(DependRelationId, RowExclusiveLock);
ScanKeyInit(&key[0],
Anum_pg_depend_classid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(RelationRelationId));
ScanKeyInit(&key[1],
Anum_pg_depend_objid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(seqId));
while (HeapTupleIsValid((tup = caql_getnext(pcqCtx))))
scan = systable_beginscan(depRel, DependDependerIndexId, true,
SnapshotNow, 2, key);
while (HeapTupleIsValid((tup = systable_getnext(scan))))
{
Form_pg_depend depform = (Form_pg_depend) GETSTRUCT(tup);
if (depform->refclassid == RelationRelationId &&
depform->deptype == DEPENDENCY_AUTO)
{
caql_delete_current(pcqCtx);
simple_heap_delete(depRel, &tup->t_self);
}
}
caql_endscan(pcqCtx);
systable_endscan(scan);
heap_close(depRel, RowExclusiveLock);
}
......@@ -380,22 +430,31 @@ Oid
get_constraint_index(Oid constraintId)
{
Oid indexId = InvalidOid;
Relation depRel;
ScanKeyData key[3];
SysScanDesc scan;
HeapTuple tup;
cqContext *pcqCtx;
/* Search the dependency table for the dependent index */
pcqCtx = caql_beginscan(
NULL,
cql("SELECT * FROM pg_depend "
" WHERE refclassid = :1 "
" AND refobjid = :2 "
" AND refobjsubid = :3 ",
ObjectIdGetDatum(ConstraintRelationId),
ObjectIdGetDatum(constraintId),
Int32GetDatum(0)));
while (HeapTupleIsValid(tup = caql_getnext(pcqCtx)))
depRel = heap_open(DependRelationId, AccessShareLock);
ScanKeyInit(&key[0],
Anum_pg_depend_refclassid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(ConstraintRelationId));
ScanKeyInit(&key[1],
Anum_pg_depend_refobjid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(constraintId));
ScanKeyInit(&key[2],
Anum_pg_depend_refobjsubid,
BTEqualStrategyNumber, F_INT4EQ,
Int32GetDatum(0));
scan = systable_beginscan(depRel, DependReferenceIndexId, true,
SnapshotNow, 3, key);
while (HeapTupleIsValid(tup = systable_getnext(scan)))
{
Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
......@@ -414,7 +473,8 @@ get_constraint_index(Oid constraintId)
}
}
caql_endscan(pcqCtx);
systable_endscan(scan);
heap_close(depRel, AccessShareLock);
return indexId;
}
......@@ -428,28 +488,37 @@ Oid
get_index_constraint(Oid indexId)
{
Oid constraintId = InvalidOid;
Relation depRel;
ScanKeyData key[3];
SysScanDesc scan;
HeapTuple tup;
cqContext *pcqCtx;
/* Search the dependency table for the index */
pcqCtx = caql_beginscan(
NULL,
cql("SELECT * FROM pg_depend "
" WHERE classid = :1 "
" AND objid = :2 "
" AND objsubid = :3 ",
ObjectIdGetDatum(RelationRelationId),
ObjectIdGetDatum(indexId),
Int32GetDatum(0)));
while (HeapTupleIsValid(tup = caql_getnext(pcqCtx)))
depRel = heap_open(DependRelationId, AccessShareLock);
ScanKeyInit(&key[0],
Anum_pg_depend_classid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(RelationRelationId));
ScanKeyInit(&key[1],
Anum_pg_depend_objid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(indexId));
ScanKeyInit(&key[2],
Anum_pg_depend_objsubid,
BTEqualStrategyNumber, F_INT4EQ,
Int32GetDatum(0));
scan = systable_beginscan(depRel, DependDependerIndexId, true,
SnapshotNow, 3, key);
while (HeapTupleIsValid(tup = systable_getnext(scan)))
{
Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
/*
* We assume any internal dependency on a constraint must be what we
* are looking for.
* We assume any internal dependency on a constraint
* must be what we are looking for.
*/
if (deprec->refclassid == ConstraintRelationId &&
deprec->refobjsubid == 0 &&
......@@ -460,7 +529,8 @@ get_index_constraint(Oid indexId)
}
}
caql_endscan(pcqCtx);
systable_endscan(scan);
heap_close(depRel, AccessShareLock);
return constraintId;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册