|
13 | 13 | #include <access/xact.h> |
14 | 14 | #include <catalog/indexing.h> |
15 | 15 | #include <catalog/namespace.h> |
| 16 | +#include <catalog/pg_attribute.h> |
16 | 17 | #include <catalog/pg_class.h> |
17 | 18 | #include <catalog/pg_constraint.h> |
18 | 19 | #include <catalog/pg_inherits.h> |
@@ -1116,6 +1117,47 @@ chunk_create_from_hypercube_after_lock(const Hypertable *ht, Hypercube *cube, |
1116 | 1117 | return chunk; |
1117 | 1118 | } |
1118 | 1119 |
|
| 1120 | +/* |
| 1121 | + * A chunk should have all columns inherited and none marked as local, so clear |
| 1122 | + * the attislocal flag that ALTER TABLE ... INHERIT leaves set when attaching |
| 1123 | + * a pre-existing table. Otherwise a later DROP COLUMN on the hypertable would |
| 1124 | + * not propagate to the chunk. |
| 1125 | + */ |
| 1126 | +static void |
| 1127 | +chunk_reset_attislocal(Oid chunk_relid) |
| 1128 | +{ |
| 1129 | + Relation attrel = table_open(AttributeRelationId, RowExclusiveLock); |
| 1130 | + Relation chunkrel = table_open(chunk_relid, AccessShareLock); |
| 1131 | + TupleDesc tupdesc = RelationGetDescr(chunkrel); |
| 1132 | + |
| 1133 | + for (int i = 0; i < tupdesc->natts; i++) |
| 1134 | + { |
| 1135 | + Form_pg_attribute att = TupleDescAttr(tupdesc, i); |
| 1136 | + |
| 1137 | + /* Only touch inherited user columns that are still marked local */ |
| 1138 | + if (att->attnum <= 0 || att->attisdropped || !att->attislocal || att->attinhcount == 0) |
| 1139 | + { |
| 1140 | + continue; |
| 1141 | + } |
| 1142 | + |
| 1143 | + HeapTuple tuple = SearchSysCacheCopyAttNum(chunk_relid, att->attnum); |
| 1144 | + if (!HeapTupleIsValid(tuple)) |
| 1145 | + { |
| 1146 | + elog(ERROR, |
| 1147 | + "cache lookup failed for attribute %d of relation %u", |
| 1148 | + att->attnum, |
| 1149 | + chunk_relid); |
| 1150 | + } |
| 1151 | + |
| 1152 | + ((Form_pg_attribute) GETSTRUCT(tuple))->attislocal = false; |
| 1153 | + CatalogTupleUpdate(attrel, &tuple->t_self, tuple); |
| 1154 | + heap_freetuple(tuple); |
| 1155 | + } |
| 1156 | + |
| 1157 | + table_close(chunkrel, NoLock); |
| 1158 | + table_close(attrel, RowExclusiveLock); |
| 1159 | +} |
| 1160 | + |
1119 | 1161 | /* |
1120 | 1162 | * Make a chunk table inherit a hypertable. |
1121 | 1163 | * |
@@ -1149,6 +1191,8 @@ chunk_add_inheritance(Chunk *chunk, const Hypertable *ht) |
1149 | 1191 | }; |
1150 | 1192 |
|
1151 | 1193 | AlterTable(&alterstmt, lockmode, &atcontext); |
| 1194 | + |
| 1195 | + chunk_reset_attislocal(atcontext.relid); |
1152 | 1196 | } |
1153 | 1197 |
|
1154 | 1198 | static Chunk * |
|
0 commit comments