* Return the address of the modified column. If the column was already * nullable, InvalidObjectAddress is returned. */
| 8995 | * nullable, InvalidObjectAddress is returned. |
| 8996 | */ |
| 8997 | static ObjectAddress |
| 8998 | ATExecDropNotNull(Relation rel, const char *colName, LOCKMODE lockmode) |
| 8999 | { |
| 9000 | HeapTuple tuple; |
| 9001 | Form_pg_attribute attTup; |
| 9002 | AttrNumber attnum; |
| 9003 | Relation attr_rel; |
| 9004 | List *indexoidlist; |
| 9005 | ListCell *indexoidscan; |
| 9006 | ObjectAddress address; |
| 9007 | |
| 9008 | /* |
| 9009 | * lookup the attribute |
| 9010 | */ |
| 9011 | attr_rel = table_open(AttributeRelationId, RowExclusiveLock); |
| 9012 | |
| 9013 | tuple = SearchSysCacheCopyAttName(RelationGetRelid(rel), colName); |
| 9014 | if (!HeapTupleIsValid(tuple)) |
| 9015 | ereport(ERROR, |
| 9016 | (errcode(ERRCODE_UNDEFINED_COLUMN), |
| 9017 | errmsg("column \"%s\" of relation \"%s\" does not exist", |
| 9018 | colName, RelationGetRelationName(rel)))); |
| 9019 | attTup = (Form_pg_attribute) GETSTRUCT(tuple); |
| 9020 | attnum = attTup->attnum; |
| 9021 | |
| 9022 | /* Prevent them from altering a system attribute */ |
| 9023 | if (attnum <= 0) |
| 9024 | ereport(ERROR, |
| 9025 | (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 9026 | errmsg("cannot alter system column \"%s\"", |
| 9027 | colName))); |
| 9028 | |
| 9029 | if (attTup->attidentity) |
| 9030 | ereport(ERROR, |
| 9031 | (errcode(ERRCODE_SYNTAX_ERROR), |
| 9032 | errmsg("column \"%s\" of relation \"%s\" is an identity column", |
| 9033 | colName, RelationGetRelationName(rel)))); |
| 9034 | |
| 9035 | /* |
| 9036 | * Check that the attribute is not in a primary key or in an index used as |
| 9037 | * a replica identity. |
| 9038 | * |
| 9039 | * Note: we'll throw error even if the pkey index is not valid. |
| 9040 | */ |
| 9041 | |
| 9042 | /* Loop over all indexes on the relation */ |
| 9043 | indexoidlist = RelationGetIndexList(rel); |
| 9044 | |
| 9045 | foreach(indexoidscan, indexoidlist) |
| 9046 | { |
| 9047 | Oid indexoid = lfirst_oid(indexoidscan); |
| 9048 | HeapTuple indexTuple; |
| 9049 | Form_pg_index indexStruct; |
| 9050 | int i; |
| 9051 | |
| 9052 | indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexoid)); |
| 9053 | if (!HeapTupleIsValid(indexTuple)) |
| 9054 | elog(ERROR, "cache lookup failed for index %u", indexoid); |
no test coverage detected