diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2004-08-02 04:28:29 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2004-08-02 04:28:29 +0000 |
commit | f0efe26402499f11e3f93459f0f87b666f1a0c56 (patch) | |
tree | 3d45c83c8110b561815ec70571e14ce370694ed5 /src/backend/utils/adt/ruleutils.c | |
parent | f622c5404905cb998adabe3a3527f7e9cdace229 (diff) | |
download | postgresql-f0efe26402499f11e3f93459f0f87b666f1a0c56.tar.gz postgresql-f0efe26402499f11e3f93459f0f87b666f1a0c56.zip |
Support USING INDEX TABLESPACE clause for PRIMARY KEY and UNIQUE
constraints. Christopher Kings-Lynne.
Diffstat (limited to 'src/backend/utils/adt/ruleutils.c')
-rw-r--r-- | src/backend/utils/adt/ruleutils.c | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 17ff48400e7..e3b9a4e068a 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -3,7 +3,7 @@ * back to source text * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.175 2004/07/06 04:50:21 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.176 2004/08/02 04:27:15 tgl Exp $ * * This software is copyrighted by Jan Wieck - Hamburg. * @@ -162,6 +162,7 @@ static char *pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, int prettyFlags); static char *pg_get_expr_worker(text *expr, Oid relid, char *relname, int prettyFlags); +static Oid get_constraint_index(Oid constraintRelOid, Oid constraintOid); static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc, int prettyFlags); static void make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc, @@ -1015,6 +1016,7 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, { Datum val; bool isnull; + Oid indexOid; /* Start off the constraint definition */ if (conForm->contype == CONSTRAINT_PRIMARY) @@ -1033,6 +1035,29 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, appendStringInfo(&buf, ")"); + /* Add TABLESPACE if it's not default */ + indexOid = get_constraint_index(RelationGetRelid(conDesc), + constraintId); + if (OidIsValid(indexOid)) + { + Oid reltablespace; + Oid indtablespace; + + reltablespace = get_rel_tablespace(conForm->conrelid); + indtablespace = get_rel_tablespace(indexOid); + if (OidIsValid(indtablespace) && + indtablespace != reltablespace) + { + char *spcname = get_tablespace_name(indtablespace); + + if (spcname) /* just paranoia... */ + { + appendStringInfo(&buf, " USING INDEX TABLESPACE %s", + quote_identifier(spcname)); + pfree(spcname); + } + } + } break; } case CONSTRAINT_CHECK: @@ -1343,6 +1368,67 @@ pg_get_serial_sequence(PG_FUNCTION_ARGS) PG_RETURN_NULL(); } +/* + * get_constraint_index + * Given the OID of a unique or primary-key constraint, + * look up the OID of the underlying index. + * + * We make the caller pass in the OID of pg_constraint, too, simply because + * it's probably got it at hand already. + * + * Returns InvalidOid if index can't be found. + */ +static Oid +get_constraint_index(Oid constraintRelOid, Oid constraintOid) +{ + Oid result = InvalidOid; + Relation depRel; + ScanKeyData key[3]; + SysScanDesc scan; + HeapTuple tup; + + /* Search the dependency table for the dependent index */ + depRel = heap_openr(DependRelationName, AccessShareLock); + + ScanKeyInit(&key[0], + Anum_pg_depend_refclassid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(constraintRelOid)); + ScanKeyInit(&key[1], + Anum_pg_depend_refobjid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(constraintOid)); + ScanKeyInit(&key[2], + Anum_pg_depend_refobjsubid, + BTEqualStrategyNumber, F_INT4EQ, + Int32GetDatum(0)); + + scan = systable_beginscan(depRel, DependReferenceIndex, true, + SnapshotNow, 3, key); + + while (HeapTupleIsValid(tup = systable_getnext(scan))) + { + Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup); + + /* + * We assume any internal dependency of a relation on the constraint + * must be what we are looking for. + */ + if (deprec->classid == RelOid_pg_class && + deprec->objsubid == 0 && + deprec->deptype == DEPENDENCY_INTERNAL) + { + result = deprec->objid; + break; + } + } + + systable_endscan(scan); + heap_close(depRel, AccessShareLock); + + return result; +} + /* ---------- * deparse_expression - General utility for deparsing expressions |