8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.171 2002/04/01 22:36:09 tgl Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.172 2002/04/02 08:51:50 inoue Exp $
12
12
*
13
13
* NOTES
14
14
* The PerformAddAttribute() code, like most of the relation
@@ -1115,140 +1115,6 @@ AlterTableAlterColumnFlags(Oid myrelid,
1115
1115
}
1116
1116
1117
1117
1118
- #ifdef _DROP_COLUMN_HACK__
1119
- /*
1120
- * ALTER TABLE DROP COLUMN trial implementation
1121
- */
1122
-
1123
- /*
1124
- * find a specified attribute in a node entry
1125
- */
1126
- static bool
1127
- find_attribute_walker (Node * node , int * attnump )
1128
- {
1129
- if (node == NULL )
1130
- return false;
1131
- if (IsA (node , Var ))
1132
- {
1133
- Var * var = (Var * ) node ;
1134
-
1135
- if (var -> varlevelsup == 0 && var -> varno == 1 &&
1136
- var -> varattno == * attnump )
1137
- return true;
1138
- }
1139
- return expression_tree_walker (node , find_attribute_walker ,
1140
- (void * ) attnump );
1141
- }
1142
-
1143
- static bool
1144
- find_attribute_in_node (Node * node , int attnum )
1145
- {
1146
- return find_attribute_walker (node , & attnum );
1147
- }
1148
-
1149
- /*
1150
- * Remove/check references for the column
1151
- */
1152
- static bool
1153
- RemoveColumnReferences (Oid reloid , int attnum , bool checkonly , HeapTuple reltup )
1154
- {
1155
- Relation indexRelation ,
1156
- rcrel ;
1157
- ScanKeyData entry ;
1158
- HeapScanDesc scan ;
1159
- void * sysscan ;
1160
- HeapTuple htup ,
1161
- indexTuple ;
1162
- Form_pg_index index ;
1163
- Form_pg_class pgcform = (Form_pg_class ) NULL ;
1164
- int i ;
1165
- bool checkok = true;
1166
-
1167
-
1168
- if (!checkonly )
1169
- pgcform = (Form_pg_class ) GETSTRUCT (reltup );
1170
-
1171
- /*
1172
- * Remove/check constraints here
1173
- */
1174
- ScanKeyEntryInitialize (& entry , (bits16 ) 0x0 ,
1175
- Anum_pg_relcheck_rcrelid ,
1176
- (RegProcedure ) F_OIDEQ ,
1177
- ObjectIdGetDatum (reloid ));
1178
-
1179
- rcrel = heap_openr (RelCheckRelationName , RowExclusiveLock );
1180
- sysscan = systable_beginscan (rcrel , RelCheckIndex , true,
1181
- SnapshotNow ,
1182
- 1 , & entry );
1183
-
1184
- while (HeapTupleIsValid (htup = systable_getnext (sysscan )))
1185
- {
1186
- Form_pg_relcheck relcheck ;
1187
- char * ccbin ;
1188
- Node * node ;
1189
-
1190
- relcheck = (Form_pg_relcheck ) GETSTRUCT (htup );
1191
- ccbin = DatumGetCString (DirectFunctionCall1 (textout ,
1192
- PointerGetDatum (& relcheck -> rcbin )));
1193
- node = stringToNode (ccbin );
1194
- pfree (ccbin );
1195
- if (find_attribute_in_node (node , attnum ))
1196
- {
1197
- if (checkonly )
1198
- {
1199
- checkok = false;
1200
- elog (ERROR , "target column is used in a constraint" );
1201
- }
1202
- else
1203
- {
1204
- simple_heap_delete (rcrel , & htup -> t_self );
1205
- pgcform -> relchecks -- ;
1206
- }
1207
- }
1208
- }
1209
-
1210
- systable_endscan (sysscan );
1211
- heap_close (rcrel , NoLock );
1212
-
1213
- /*
1214
- * What to do with triggers/rules/views/procedues ?
1215
- */
1216
-
1217
- /*
1218
- * Remove/check indexes
1219
- */
1220
- indexRelation = heap_openr (IndexRelationName , RowExclusiveLock );
1221
- ScanKeyEntryInitialize (& entry , 0 , Anum_pg_index_indrelid , F_OIDEQ ,
1222
- ObjectIdGetDatum (reloid ));
1223
- scan = heap_beginscan (indexRelation , false, SnapshotNow , 1 , & entry );
1224
- while (HeapTupleIsValid (indexTuple = heap_getnext (scan , 0 )))
1225
- {
1226
- index = (Form_pg_index ) GETSTRUCT (indexTuple );
1227
- for (i = 0 ; i < INDEX_MAX_KEYS ; i ++ )
1228
- {
1229
- if (index -> indkey [i ] == InvalidAttrNumber )
1230
- break ;
1231
- else if (index -> indkey [i ] == attnum )
1232
- {
1233
- if (checkonly )
1234
- {
1235
- checkok = false;
1236
- elog (ERROR , "target column is used in an index" );
1237
- }
1238
- else
1239
- {
1240
- index_drop (index -> indexrelid );
1241
- }
1242
- break ;
1243
- }
1244
- }
1245
- }
1246
- heap_endscan (scan );
1247
- heap_close (indexRelation , NoLock );
1248
-
1249
- return checkok ;
1250
- }
1251
- #endif /* _DROP_COLUMN_HACK__ */
1252
1118
1253
1119
/*
1254
1120
* ALTER TABLE DROP COLUMN
@@ -1258,171 +1124,7 @@ AlterTableDropColumn(Oid myrelid,
1258
1124
bool inh , const char * colName ,
1259
1125
int behavior )
1260
1126
{
1261
- #ifdef _DROP_COLUMN_HACK__
1262
- Relation rel ,
1263
- attrdesc ;
1264
- HeapTuple reltup ;
1265
- HeapTupleData classtuple ;
1266
- Buffer buffer ;
1267
- Form_pg_attribute attribute ;
1268
- HeapTuple tup ;
1269
- Relation idescs [Num_pg_attr_indices ];
1270
- int attnum ;
1271
- bool hasindex ;
1272
- char dropColname [32 ];
1273
-
1274
- if (inh )
1275
- elog (ERROR , "ALTER TABLE / DROP COLUMN with inherit option is not supported yet" );
1276
-
1277
- /*
1278
- * Grab an exclusive lock on the target table, which we will NOT
1279
- * release until end of transaction.
1280
- */
1281
- rel = heap_open (myrelid , AccessExclusiveLock );
1282
-
1283
- if (rel -> rd_rel -> relkind != RELKIND_RELATION )
1284
- elog (ERROR , "ALTER TABLE: relation \"%s\" is not a table" ,
1285
- RelationGetRelationName (rel ));
1286
-
1287
- if (!allowSystemTableMods
1288
- && IsSystemRelationName (RelationGetRelationName (rel )))
1289
- elog (ERROR , "ALTER TABLE: relation \"%s\" is a system catalog" ,
1290
- RelationGetRelationName (rel ));
1291
-
1292
- /*
1293
- * permissions checking. this would normally be done in utility.c,
1294
- * but this particular routine is recursive.
1295
- *
1296
- * normally, only the owner of a class can change its schema.
1297
- */
1298
- if (!pg_class_ownercheck (myrelid , GetUserId ()))
1299
- elog (ERROR , "ALTER TABLE: \"%s\": permission denied" ,
1300
- RelationGetRelationName (rel ));
1301
-
1302
- heap_close (rel , NoLock ); /* close rel but keep lock! */
1303
-
1304
- /*
1305
- * What to do when rel has inheritors ?
1306
- */
1307
- if (length (find_all_inheritors (myrelid )) > 1 )
1308
- elog (ERROR , "ALTER TABLE: cannot drop a column on table that is inherited from" );
1309
-
1310
- /*
1311
- * lock the pg_class tuple for update
1312
- */
1313
- rel = heap_openr (RelationRelationName , RowExclusiveLock );
1314
- reltup = SearchSysCache (RELOID ,
1315
- ObjectIdGetDatum (myrelid ),
1316
- 0 , 0 , 0 );
1317
- if (!HeapTupleIsValid (reltup ))
1318
- {
1319
- Relation myrel ;
1320
- char * myrelname ;
1321
-
1322
- myrel = heap_open (myrelid , AccessExclusiveLock );
1323
- myrelname = pstrdup (RelationGetRelationName (myrel ));
1324
- heap_close (myrel , AccessExclusiveLock );
1325
-
1326
- elog (ERROR , "ALTER TABLE: relation \"%s\" not found" ,
1327
- myrelname );
1328
- }
1329
- classtuple .t_self = reltup -> t_self ;
1330
- ReleaseSysCache (reltup );
1331
-
1332
- switch (heap_mark4update (rel , & classtuple , & buffer ))
1333
- {
1334
- case HeapTupleSelfUpdated :
1335
- case HeapTupleMayBeUpdated :
1336
- break ;
1337
- default :
1338
- elog (ERROR , "couldn't lock pg_class tuple" );
1339
- }
1340
- reltup = heap_copytuple (& classtuple );
1341
- ReleaseBuffer (buffer );
1342
-
1343
- attrdesc = heap_openr (AttributeRelationName , RowExclusiveLock );
1344
-
1345
- /*
1346
- * Get the target pg_attribute tuple and make a modifiable copy
1347
- */
1348
- tup = SearchSysCacheCopy (ATTNAME ,
1349
- ObjectIdGetDatum (myrelid ),
1350
- PointerGetDatum (colName ),
1351
- 0 , 0 );
1352
- if (!HeapTupleIsValid (tup ))
1353
- {
1354
- Relation myrel ;
1355
- char * myrelname ;
1356
-
1357
- myrel = heap_open (myrelid , AccessExclusiveLock );
1358
- myrelname = pstrdup (RelationGetRelationName (myrel ));
1359
- heap_close (myrel , AccessExclusiveLock );
1360
-
1361
- elog (ERROR , "ALTER TABLE: column name \"%s\" doesn't exist in table \"%s\"" ,
1362
- colName , myrelname );
1363
- }
1364
-
1365
- attribute = (Form_pg_attribute ) GETSTRUCT (tup );
1366
- attnum = attribute -> attnum ;
1367
- if (attnum <= 0 )
1368
- elog (ERROR , "ALTER TABLE: column name \"%s\" was already dropped" ,
1369
- colName );
1370
-
1371
- /*
1372
- * Check constraints/indices etc here
1373
- */
1374
- if (behavior != CASCADE )
1375
- {
1376
- if (!RemoveColumnReferences (myrelid , attnum , true, NULL ))
1377
- elog (ERROR , "the column is referenced" );
1378
- }
1379
-
1380
- /*
1381
- * change the target pg_attribute tuple
1382
- */
1383
- sprintf (dropColname , "*already Dropped*%d" , attnum );
1384
- namestrcpy (& (attribute -> attname ), dropColname );
1385
- ATTRIBUTE_DROP_COLUMN (attribute );
1386
-
1387
- simple_heap_update (attrdesc , & tup -> t_self , tup );
1388
- hasindex = (!IsIgnoringSystemIndexes () && RelationGetForm (attrdesc )-> relhasindex );
1389
- if (hasindex )
1390
- {
1391
- CatalogOpenIndices (Num_pg_attr_indices , Name_pg_attr_indices , idescs );
1392
- CatalogIndexInsert (idescs , Num_pg_attr_indices ,
1393
- attrdesc , tup );
1394
- CatalogCloseIndices (Num_pg_attr_indices , idescs );
1395
- }
1396
- heap_close (attrdesc , NoLock );
1397
- heap_freetuple (tup );
1398
-
1399
- /* delete comment for this attribute only */
1400
- CreateComments (RelationGetRelid (rel ), RelOid_pg_class ,
1401
- (int32 ) attnum , NULL );
1402
-
1403
- /* delete attrdef */
1404
- drop_default (myrelid , attnum );
1405
-
1406
- /*
1407
- * Remove objects which reference this column
1408
- */
1409
- if (behavior == CASCADE )
1410
- {
1411
- Relation ridescs [Num_pg_class_indices ];
1412
-
1413
- RemoveColumnReferences (myrelid , attnum , false, reltup );
1414
- /* update pg_class tuple */
1415
- simple_heap_update (rel , & reltup -> t_self , reltup );
1416
- CatalogOpenIndices (Num_pg_class_indices , Name_pg_class_indices , ridescs );
1417
- CatalogIndexInsert (ridescs , Num_pg_class_indices , rel , reltup );
1418
- CatalogCloseIndices (Num_pg_class_indices , ridescs );
1419
- }
1420
-
1421
- heap_freetuple (reltup );
1422
- heap_close (rel , NoLock );
1423
- #else
1424
1127
elog (ERROR , "ALTER TABLE / DROP COLUMN is not implemented" );
1425
- #endif /* _DROP_COLUMN_HACK__ */
1426
1128
}
1427
1129
1428
1130
0 commit comments