/* lock the table the attribute belongs to */
tablerel = table_open(relid, AccessExclusiveLock);
+ /* Don't do anything unless it's a plain table */
+ if (tablerel->rd_rel->relkind != RELKIND_RELATION)
+ {
+ table_close(tablerel, AccessExclusiveLock);
+ return;
+ }
+
/* Lock the attribute row and get the data */
attrrel = table_open(AttributeRelationId, RowExclusiveLock);
atttup = SearchSysCacheAttName(relid, attname);
valuesAtt[Anum_pg_attribute_atthasdef - 1] = true;
replacesAtt[Anum_pg_attribute_atthasdef - 1] = true;
- if (add_column_mode && !attgenerated)
+ if (rel->rd_rel->relkind == RELKIND_RELATION && add_column_mode &&
+ !attgenerated)
{
expr2 = expression_planner(expr2);
estate = CreateExecutorState();
/*
* Here we go --- change the recorded column type and collation. (Note
* heapTup is a copy of the syscache entry, so okay to scribble on.) First
- * fix up the missing value if any.
+ * fix up the missing value if any. There shouldn't be any missing values
+ * for anything except plain tables, but if there are, ignore them.
*/
- if (attTup->atthasmissing)
+ if (rel->rd_rel->relkind == RELKIND_RELATION && attTup->atthasmissing)
{
Datum missingval;
bool missingNull;
{
Form_pg_attribute attp;
int attnum;
+ bool atthasmissing;
attp = (Form_pg_attribute) GETSTRUCT(pg_attribute_tuple);
attp,
ATTRIBUTE_FIXED_PART_SIZE);
+ /*
+ * Fix atthasmissing flag - it's only for plain tables. Others
+ * should not have missing values set, but there may be some left from
+ * before when we placed that check, so this code defensively ignores
+ * such values.
+ */
+ atthasmissing = attp->atthasmissing;
+ if (relation->rd_rel->relkind != RELKIND_RELATION && atthasmissing)
+ {
+ Form_pg_attribute nattp;
+
+ atthasmissing = false;
+ nattp = TupleDescAttr(relation->rd_att, attnum - 1);
+ nattp->atthasmissing = false;
+ }
+
/* Update constraint/default info */
if (attp->attnotnull)
constr->has_not_null = true;
}
/* Likewise for a missing value */
- if (attp->atthasmissing)
+ if (atthasmissing)
{
Datum missingval;
bool missingNull;
(1 row)
ROLLBACK;
+-- verify that a default set on a non-plain table doesn't set a missing
+-- value on the attribute
+CREATE FOREIGN DATA WRAPPER dummy;
+CREATE SERVER s0 FOREIGN DATA WRAPPER dummy;
+CREATE FOREIGN TABLE ft1 (c1 integer NOT NULL) SERVER s0;
+ALTER FOREIGN TABLE ft1 ADD COLUMN c8 integer DEFAULT 0;
+ALTER FOREIGN TABLE ft1 ALTER COLUMN c8 TYPE char(10);
+SELECT count(*)
+ FROM pg_attribute
+ WHERE attrelid = 'ft1'::regclass AND
+ (attmissingval IS NOT NULL OR atthasmissing);
+ count
+-------
+ 0
+(1 row)
+
-- cleanup
+DROP FOREIGN TABLE ft1;
+DROP SERVER s0;
+DROP FOREIGN DATA WRAPPER dummy;
DROP TABLE vtype;
DROP TABLE vtype2;
DROP TABLE follower;
SELECT * FROM t WHERE a IS NULL;
ROLLBACK;
+-- verify that a default set on a non-plain table doesn't set a missing
+-- value on the attribute
+CREATE FOREIGN DATA WRAPPER dummy;
+CREATE SERVER s0 FOREIGN DATA WRAPPER dummy;
+CREATE FOREIGN TABLE ft1 (c1 integer NOT NULL) SERVER s0;
+ALTER FOREIGN TABLE ft1 ADD COLUMN c8 integer DEFAULT 0;
+ALTER FOREIGN TABLE ft1 ALTER COLUMN c8 TYPE char(10);
+SELECT count(*)
+ FROM pg_attribute
+ WHERE attrelid = 'ft1'::regclass AND
+ (attmissingval IS NOT NULL OR atthasmissing);
-- cleanup
+DROP FOREIGN TABLE ft1;
+DROP SERVER s0;
+DROP FOREIGN DATA WRAPPER dummy;
DROP TABLE vtype;
DROP TABLE vtype2;
DROP TABLE follower;