diff options
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/catalog/heap.c | 41 | ||||
-rw-r--r-- | src/backend/rewrite/rewriteDefine.c | 11 |
2 files changed, 50 insertions, 2 deletions
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 6edd11fb986..8818b680c8b 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -1435,6 +1435,47 @@ DeleteAttributeTuples(Oid relid) } /* + * DeleteSystemAttributeTuples + * + * Remove pg_attribute rows for system columns of the given relid. + * + * Note: this is only used when converting a table to a view. Views don't + * have system columns, so we should remove them from pg_attribute. + */ +void +DeleteSystemAttributeTuples(Oid relid) +{ + Relation attrel; + SysScanDesc scan; + ScanKeyData key[2]; + HeapTuple atttup; + + /* Grab an appropriate lock on the pg_attribute relation */ + attrel = heap_open(AttributeRelationId, RowExclusiveLock); + + /* Use the index to scan only system attributes of the target relation */ + ScanKeyInit(&key[0], + Anum_pg_attribute_attrelid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(relid)); + ScanKeyInit(&key[1], + Anum_pg_attribute_attnum, + BTLessEqualStrategyNumber, F_INT2LE, + Int16GetDatum(0)); + + scan = systable_beginscan(attrel, AttributeRelidNumIndexId, true, + SnapshotNow, 2, key); + + /* Delete all the matching tuples */ + while ((atttup = systable_getnext(scan)) != NULL) + simple_heap_delete(attrel, &atttup->t_self); + + /* Clean up after the scan */ + systable_endscan(scan); + heap_close(attrel, RowExclusiveLock); +} + +/* * RemoveAttributeById * * This is the guts of ALTER TABLE DROP COLUMN: actually mark the attribute diff --git a/src/backend/rewrite/rewriteDefine.c b/src/backend/rewrite/rewriteDefine.c index 8efc9fc9d52..55b0fed5f79 100644 --- a/src/backend/rewrite/rewriteDefine.c +++ b/src/backend/rewrite/rewriteDefine.c @@ -18,6 +18,7 @@ #include "access/htup_details.h" #include "catalog/catalog.h" #include "catalog/dependency.h" +#include "catalog/heap.h" #include "catalog/indexing.h" #include "catalog/namespace.h" #include "catalog/objectaccess.h" @@ -510,13 +511,19 @@ DefineQueryRewrite(char *rulename, } /* - * IF the relation is becoming a view, delete the storage files associated - * with it. NB: we had better have AccessExclusiveLock to do this ... + * If the relation is becoming a view, delete the storage files associated + * with it. Also, get rid of any system attribute entries in pg_attribute, + * because a view shouldn't have any of those. + * + * NB: we had better have AccessExclusiveLock to do this ... * * XXX what about getting rid of its TOAST table? For now, we don't. */ if (RelisBecomingView) + { RelationDropStorage(event_relation); + DeleteSystemAttributeTuples(event_relid); + } /* Close rel, but keep lock till commit... */ heap_close(event_relation, NoLock); |