Skip to content

Commit 205c4d0

Browse files
committed
[Feature] Support Invisible Columns
1 parent 5c5e5ae commit 205c4d0

File tree

22 files changed

+393
-20
lines changed

22 files changed

+393
-20
lines changed

src/backend/access/common/tupdesc.c

+4
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
462462
return false;
463463
if (attr1->attisdropped != attr2->attisdropped)
464464
return false;
465+
if (attr1->attisinvisible != attr2->attisinvisible)
466+
return false;
465467
if (attr1->attislocal != attr2->attislocal)
466468
return false;
467469
if (attr1->attinhcount != attr2->attinhcount)
@@ -644,6 +646,7 @@ TupleDescInitEntry(TupleDesc desc,
644646
att->atthasmissing = false;
645647
att->attidentity = '\0';
646648
att->attisdropped = false;
649+
att->attisinvisible = false;
647650
att->attislocal = true;
648651
att->attinhcount = 0;
649652
/* attacl, attoptions and attfdwoptions are not present in tupledescs */
@@ -703,6 +706,7 @@ TupleDescInitBuiltinEntry(TupleDesc desc,
703706
att->atthasmissing = false;
704707
att->attidentity = '\0';
705708
att->attisdropped = false;
709+
att->attisinvisible = false;
706710
att->attislocal = true;
707711
att->attinhcount = 0;
708712
/* attacl, attoptions and attfdwoptions are not present in tupledescs */

src/backend/catalog/heap.c

100644100755
+10-9
Original file line numberDiff line numberDiff line change
@@ -152,37 +152,37 @@ static List *insert_ordered_unique_oid(List *list, Oid datum);
152152
static FormData_pg_attribute a1 = {
153153
0, {"ctid"}, TIDOID, 0, sizeof(ItemPointerData),
154154
SelfItemPointerAttributeNumber, 0, -1, -1,
155-
false, 'p', 's', true, false, false, '\0', false, true, 0
155+
false, 'p', 's', true, false, false, '\0', false, false, true, 0
156156
};
157157

158158
static FormData_pg_attribute a2 = {
159159
0, {"oid"}, OIDOID, 0, sizeof(Oid),
160160
ObjectIdAttributeNumber, 0, -1, -1,
161-
true, 'p', 'i', true, false, false, '\0', false, true, 0
161+
true, 'p', 'i', true, false, false, '\0', false, false, true, 0
162162
};
163163

164164
static FormData_pg_attribute a3 = {
165165
0, {"xmin"}, XIDOID, 0, sizeof(TransactionId),
166166
MinTransactionIdAttributeNumber, 0, -1, -1,
167-
true, 'p', 'i', true, false, false, '\0', false, true, 0
167+
true, 'p', 'i', true, false, false, '\0', false, false, true, 0
168168
};
169169

170170
static FormData_pg_attribute a4 = {
171171
0, {"cmin"}, CIDOID, 0, sizeof(CommandId),
172172
MinCommandIdAttributeNumber, 0, -1, -1,
173-
true, 'p', 'i', true, false, false, '\0', false, true, 0
173+
true, 'p', 'i', true, false, false, '\0', false, false, true, 0
174174
};
175175

176176
static FormData_pg_attribute a5 = {
177177
0, {"xmax"}, XIDOID, 0, sizeof(TransactionId),
178178
MaxTransactionIdAttributeNumber, 0, -1, -1,
179-
true, 'p', 'i', true, false, false, '\0', false, true, 0
179+
true, 'p', 'i', true, false, false, '\0', false, false, true, 0
180180
};
181181

182182
static FormData_pg_attribute a6 = {
183183
0, {"cmax"}, CIDOID, 0, sizeof(CommandId),
184184
MaxCommandIdAttributeNumber, 0, -1, -1,
185-
true, 'p', 'i', true, false, false, '\0', false, true, 0
185+
true, 'p', 'i', true, false, false, '\0', false, false, true, 0
186186
};
187187

188188
/*
@@ -194,20 +194,20 @@ static FormData_pg_attribute a6 = {
194194
static FormData_pg_attribute a7 = {
195195
0, {"tableoid"}, OIDOID, 0, sizeof(Oid),
196196
TableOidAttributeNumber, 0, -1, -1,
197-
true, 'p', 'i', true, false, false, '\0', false, true, 0
197+
true, 'p', 'i', true, false, false, '\0', false, false, true, 0
198198
};
199199

200200
/* POLAR px */
201201
static FormData_pg_attribute a8 = {
202202
0, {"_px_worker_id"}, INT4OID, 0, sizeof(PxWorkerId),
203203
PxWorkerIdAttributeNumber, 0, -1, -1,
204-
true, 'p', 'i', true, false, false, '\0', false, true, 0
204+
true, 'p', 'i', true, false, false, '\0', false, false, true, 0
205205
};
206206

207207
static FormData_pg_attribute a9 = {
208208
0, {"_root_ctid"}, TIDOID, 0, sizeof(ItemPointerData),
209209
RootSelfItemPointerAttributeNumber, 0, -1, -1,
210-
false, 'p', 's', true, false, false, '\0', false, true, 0
210+
false, 'p', 's', true, false, false, '\0', false, false, true, 0
211211
};
212212
/* POLAR end */
213213

@@ -691,6 +691,7 @@ HeapTuple heaptuple_from_pg_attribute(Relation pg_attribute_rel,
691691
values[Anum_pg_attribute_atthasmissing - 1] = BoolGetDatum(new_attribute->atthasmissing);
692692
values[Anum_pg_attribute_attidentity - 1] = CharGetDatum(new_attribute->attidentity);
693693
values[Anum_pg_attribute_attisdropped - 1] = BoolGetDatum(new_attribute->attisdropped);
694+
values[Anum_pg_attribute_attisinvisible - 1] = BoolGetDatum(new_attribute->attisinvisible);
694695
values[Anum_pg_attribute_attislocal - 1] = BoolGetDatum(new_attribute->attislocal);
695696
values[Anum_pg_attribute_attinhcount - 1] = Int32GetDatum(new_attribute->attinhcount);
696697
values[Anum_pg_attribute_attcollation - 1] = ObjectIdGetDatum(new_attribute->attcollation);

src/backend/commands/sequence.c

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ DefineSequence(ParseState *pstate, CreateSeqStmt *seq)
162162
ColumnDef *coldef = makeNode(ColumnDef);
163163

164164
coldef->inhcount = 0;
165+
coldef->is_invisible = false;
165166
coldef->is_local = true;
166167
coldef->is_not_null = true;
167168
coldef->is_from_type = false;

src/backend/commands/tablecmds.c

+144
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,10 @@ static ObjectAddress ATExecDropNotNull(Relation rel, const char *colName, LOCKMO
381381
static void ATPrepSetNotNull(Relation rel, bool recurse, bool recursing);
382382
static ObjectAddress ATExecSetNotNull(AlteredTableInfo *tab, Relation rel,
383383
const char *colName, LOCKMODE lockmode);
384+
static ObjectAddress ATExecSetVisible(AlteredTableInfo *tab, Relation rel,
385+
const char *colName, LOCKMODE lockmode);
386+
static ObjectAddress ATExecSetInvisible(AlteredTableInfo *tab, Relation rel,
387+
const char *colName, LOCKMODE lockmode);
384388
static ObjectAddress ATExecColumnDefault(Relation rel, const char *colName,
385389
Node *newDefault, LOCKMODE lockmode);
386390
static ObjectAddress ATExecAddIdentity(Relation rel, const char *colName,
@@ -772,6 +776,9 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
772776

773777
if (colDef->identity)
774778
attr->attidentity = colDef->identity;
779+
780+
if (colDef->is_invisible)
781+
attr->attisinvisible = colDef->is_invisible;
775782
}
776783

777784
/*
@@ -3527,6 +3534,14 @@ AlterTableGetLockLevel(List *cmds)
35273534
cmd_lockmode = AccessExclusiveLock;
35283535
break;
35293536

3537+
/*
3538+
* changing visibilty can affect concurrent SELECTs
3539+
*/
3540+
case AT_SetInvisible:
3541+
case AT_SetVisible:
3542+
cmd_lockmode = AccessExclusiveLock;
3543+
break;
3544+
35303545
/*
35313546
* These subcommands affect write operations only.
35323547
*/
@@ -3816,6 +3831,16 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
38163831
/* No command-specific prep needed */
38173832
pass = AT_PASS_ADD_CONSTR;
38183833
break;
3834+
case AT_SetVisible: /* ALTER COLUMN SET VISIBLE */
3835+
ATSimplePermissions(rel, ATT_TABLE | ATT_FOREIGN_TABLE);
3836+
ATSimpleRecursion(wqueue, rel, cmd, recurse, lockmode);
3837+
/* No command-specific prep needed */
3838+
pass = AT_PASS_ADD_CONSTR;
3839+
case AT_SetInvisible: /* ALTER COLUMN SET INVISIBLE */
3840+
ATSimplePermissions(rel, ATT_TABLE | ATT_FOREIGN_TABLE);
3841+
ATSimpleRecursion(wqueue, rel, cmd, recurse, lockmode);
3842+
/* No command-specific prep needed */
3843+
pass = AT_PASS_ADD_CONSTR;
38193844
case AT_SetStatistics: /* ALTER COLUMN SET STATISTICS */
38203845
ATSimpleRecursion(wqueue, rel, cmd, recurse, lockmode);
38213846
/* Performs own permission checks */
@@ -4149,6 +4174,12 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
41494174
case AT_SetNotNull: /* ALTER COLUMN SET NOT NULL */
41504175
address = ATExecSetNotNull(tab, rel, cmd->name, lockmode);
41514176
break;
4177+
case AT_SetVisible: /* ALTER COLUMN SET VISIBLE */
4178+
address = ATExecSetVisible(tab, rel, cmd->name, lockmode);
4179+
break;
4180+
case AT_SetInvisible: /* ALTER COLUMN SET INVISIBLE */
4181+
address = ATExecSetInvisible(tab, rel, cmd->name, lockmode);
4182+
break;
41524183
case AT_SetStatistics: /* ALTER COLUMN SET STATISTICS */
41534184
address = ATExecSetStatistics(rel, cmd->name, cmd->num, cmd->def, lockmode);
41544185
break;
@@ -5646,6 +5677,7 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
56465677
attribute.atthasmissing = false;
56475678
attribute.attidentity = colDef->identity;
56485679
attribute.attisdropped = false;
5680+
attribute.attisinvisible = colDef->is_invisible;
56495681
attribute.attislocal = colDef->is_local;
56505682
attribute.attinhcount = colDef->inhcount;
56515683
attribute.attcollation = collOid;
@@ -6228,6 +6260,118 @@ ATExecSetNotNull(AlteredTableInfo *tab, Relation rel,
62286260
return address;
62296261
}
62306262

6263+
/*
6264+
* Return the address of the modified column. If the column was already Invisible,
6265+
* InvalidObjectAddress is returned.
6266+
*/
6267+
static ObjectAddress
6268+
ATExecSetVisible(AlteredTableInfo *tab, Relation rel,
6269+
const char *colName, LOCKMODE lockmode)
6270+
{
6271+
HeapTuple tuple;
6272+
AttrNumber attnum;
6273+
Relation attr_rel;
6274+
ObjectAddress address;
6275+
6276+
/*
6277+
* lookup the attribute
6278+
*/
6279+
attr_rel = heap_open(AttributeRelationId, RowExclusiveLock);
6280+
6281+
tuple = SearchSysCacheCopyAttName(RelationGetRelid(rel), colName);
6282+
6283+
if (!HeapTupleIsValid(tuple))
6284+
ereport(ERROR,
6285+
(errcode(ERRCODE_UNDEFINED_COLUMN),
6286+
errmsg("column \"%s\" of relation \"%s\" does not exist",
6287+
colName, RelationGetRelationName(rel))));
6288+
6289+
attnum = ((Form_pg_attribute) GETSTRUCT(tuple))->attnum;
6290+
6291+
/* Prevent them from altering a system attribute */
6292+
if (attnum <= 0)
6293+
ereport(ERROR,
6294+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6295+
errmsg("cannot alter system column \"%s\"",
6296+
colName)));
6297+
6298+
if (((Form_pg_attribute) GETSTRUCT(tuple))->attisinvisible)
6299+
{
6300+
((Form_pg_attribute) GETSTRUCT(tuple))->attisinvisible = false;
6301+
6302+
CatalogTupleUpdate(attr_rel, &tuple->t_self, tuple);
6303+
6304+
ObjectAddressSubSet(address, RelationRelationId,
6305+
RelationGetRelid(rel), attnum);
6306+
}
6307+
else
6308+
address = InvalidObjectAddress;
6309+
6310+
InvokeObjectPostAlterHook(RelationRelationId,
6311+
RelationGetRelid(rel), attnum);
6312+
6313+
heap_close(attr_rel, RowExclusiveLock);
6314+
heap_freetuple(tuple);
6315+
6316+
return address;
6317+
}
6318+
6319+
/*
6320+
* Return the address of the modified column. If the column was already Invisible,
6321+
* InvalidObjectAddress is returned.
6322+
*/
6323+
static ObjectAddress
6324+
ATExecSetInvisible(AlteredTableInfo *tab, Relation rel,
6325+
const char *colName, LOCKMODE lockmode)
6326+
{
6327+
HeapTuple tuple;
6328+
AttrNumber attnum;
6329+
Relation attr_rel;
6330+
ObjectAddress address;
6331+
6332+
/*
6333+
* lookup the attribute
6334+
*/
6335+
attr_rel = heap_open(AttributeRelationId, RowExclusiveLock);
6336+
6337+
tuple = SearchSysCacheCopyAttName(RelationGetRelid(rel), colName);
6338+
6339+
if (!HeapTupleIsValid(tuple))
6340+
ereport(ERROR,
6341+
(errcode(ERRCODE_UNDEFINED_COLUMN),
6342+
errmsg("column \"%s\" of relation \"%s\" does not exist",
6343+
colName, RelationGetRelationName(rel))));
6344+
6345+
attnum = ((Form_pg_attribute) GETSTRUCT(tuple))->attnum;
6346+
6347+
/* Prevent them from altering a system attribute */
6348+
if (attnum <= 0)
6349+
ereport(ERROR,
6350+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6351+
errmsg("cannot alter system column \"%s\"",
6352+
colName)));
6353+
6354+
if (!((Form_pg_attribute) GETSTRUCT(tuple))->attisinvisible)
6355+
{
6356+
((Form_pg_attribute) GETSTRUCT(tuple))->attisinvisible = true;
6357+
6358+
CatalogTupleUpdate(attr_rel, &tuple->t_self, tuple);
6359+
6360+
ObjectAddressSubSet(address, RelationRelationId,
6361+
RelationGetRelid(rel), attnum);
6362+
}
6363+
else
6364+
address = InvalidObjectAddress;
6365+
6366+
InvokeObjectPostAlterHook(RelationRelationId,
6367+
RelationGetRelid(rel), attnum);
6368+
6369+
heap_close(attr_rel, RowExclusiveLock);
6370+
heap_freetuple(tuple);
6371+
6372+
return address;
6373+
}
6374+
62316375
/*
62326376
* ALTER TABLE ALTER COLUMN SET/DROP DEFAULT
62336377
*

src/backend/nodes/makefuncs.c

+1
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ makeColumnDef(const char *colname, Oid typeOid, int32 typmod, Oid collOid)
493493
n->colname = pstrdup(colname);
494494
n->typeName = makeTypeNameFromOid(typeOid, typmod);
495495
n->inhcount = 0;
496+
n->is_invisible = false;
496497
n->is_local = true;
497498
n->is_not_null = false;
498499
n->is_from_type = false;

src/backend/nodes/outfuncs.c

+4
Original file line numberDiff line numberDiff line change
@@ -3733,6 +3733,10 @@ _outConstraint(StringInfo str, const Constraint *node)
37333733
WRITE_CHAR_FIELD(generated_when);
37343734
break;
37353735

3736+
case CONSTR_INVISIBLE:
3737+
appendStringInfoString(str, "INVISIBLE");
3738+
break;
3739+
37363740
case CONSTR_CHECK:
37373741
appendStringInfoString(str, "CHECK");
37383742
WRITE_BOOL_FIELD(is_no_inherit);

0 commit comments

Comments
 (0)