|
7 | 7 | *
|
8 | 8 | *
|
9 | 9 | * IDENTIFICATION
|
10 |
| - * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.103 1999/10/07 05:48:03 tgl Exp $ |
| 10 | + * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.104 1999/10/15 01:49:39 momjian Exp $ |
11 | 11 | *
|
12 | 12 | *
|
13 | 13 | * INTERFACE ROUTINES
|
|
38 | 38 | #include "catalog/index.h"
|
39 | 39 | #include "catalog/indexing.h"
|
40 | 40 | #include "catalog/pg_attrdef.h"
|
| 41 | +#include "catalog/pg_description.h" |
41 | 42 | #include "catalog/pg_index.h"
|
42 | 43 | #include "catalog/pg_inherits.h"
|
43 | 44 | #include "catalog/pg_ipl.h"
|
@@ -67,6 +68,7 @@ static void AddNewRelationTuple(Relation pg_class_desc,
|
67 | 68 | Relation new_rel_desc, Oid new_rel_oid, unsigned natts,
|
68 | 69 | char relkind, char *temp_relname);
|
69 | 70 | static void AddToNoNameRelList(Relation r);
|
| 71 | + |
70 | 72 | static void DeleteAttributeTuples(Relation rel);
|
71 | 73 | static void DeleteRelationTuple(Relation rel);
|
72 | 74 | static void DeleteTypeTuple(Relation rel);
|
@@ -861,10 +863,11 @@ heap_create_with_catalog(char *relname,
|
861 | 863 | * 2) remove inheritance information
|
862 | 864 | * 3) remove indexes
|
863 | 865 | * 4) remove pg_class tuple
|
864 |
| - * 5) remove pg_attribute tuples |
865 |
| - * 6) remove pg_type tuples |
866 |
| - * 7) RemoveConstraints () |
867 |
| - * 8) unlink relation |
| 866 | + * 5) remove pg_attribute tuples and related descriptions |
| 867 | + * 6) remove pg_description tuples |
| 868 | + * 7) remove pg_type tuples |
| 869 | + * 8) RemoveConstraints () |
| 870 | + * 9) unlink relation |
868 | 871 | *
|
869 | 872 | * old comments
|
870 | 873 | * Except for vital relations, removes relation from
|
@@ -1269,18 +1272,152 @@ DeleteAttributeTuples(Relation rel)
|
1269 | 1272 | attnum++)
|
1270 | 1273 | {
|
1271 | 1274 | if (HeapTupleIsValid(tup = SearchSysCacheTupleCopy(ATTNUM,
|
1272 |
| - ObjectIdGetDatum(RelationGetRelid(rel)), |
1273 |
| - Int16GetDatum(attnum), |
| 1275 | + ObjectIdGetDatum(RelationGetRelid(rel)), |
| 1276 | + Int16GetDatum(attnum), |
1274 | 1277 | 0, 0)))
|
1275 | 1278 | {
|
1276 |
| - heap_delete(pg_attribute_desc, &tup->t_self, NULL); |
1277 |
| - pfree(tup); |
| 1279 | + DeleteComments(tup->t_data->t_oid); |
| 1280 | + heap_delete(pg_attribute_desc, &tup->t_self, NULL); |
| 1281 | + pfree(tup); |
1278 | 1282 | }
|
1279 | 1283 | }
|
1280 | 1284 |
|
1281 | 1285 | heap_close(pg_attribute_desc, RowExclusiveLock);
|
1282 | 1286 | }
|
1283 | 1287 |
|
| 1288 | +/* ---------------------------------------------------------- |
| 1289 | + * CreateComments |
| 1290 | + * |
| 1291 | + * This routine is handed the oid and the command associated |
| 1292 | + * with that id and will insert, update, or delete (if the |
| 1293 | + * comment is an empty string or a NULL pointer) the associated |
| 1294 | + * comment from the system cataloge, pg_description. |
| 1295 | + * |
| 1296 | + * ---------------------------------------------------------- |
| 1297 | + */ |
| 1298 | + |
| 1299 | +void |
| 1300 | +CreateComments(Oid oid, char *comment) |
| 1301 | +{ |
| 1302 | + |
| 1303 | + Relation description; |
| 1304 | + TupleDesc tupDesc; |
| 1305 | + HeapScanDesc scan; |
| 1306 | + ScanKeyData entry; |
| 1307 | + HeapTuple desctuple, searchtuple; |
| 1308 | + Datum values[Natts_pg_description]; |
| 1309 | + char nulls[Natts_pg_description]; |
| 1310 | + char replaces[Natts_pg_description]; |
| 1311 | + bool modified = false; |
| 1312 | + int i; |
| 1313 | + |
| 1314 | + /*** Open pg_description, form a new tuple, if necessary ***/ |
| 1315 | + |
| 1316 | + description = heap_openr(DescriptionRelationName, RowExclusiveLock); |
| 1317 | + tupDesc = description->rd_att; |
| 1318 | + if ((comment != NULL) && (strlen(comment) > 0)) { |
| 1319 | + for (i = 0; i < Natts_pg_description; i++) { |
| 1320 | + nulls[i] = ' '; |
| 1321 | + replaces[i] = 'r'; |
| 1322 | + values[i] = (Datum) NULL; |
| 1323 | + } |
| 1324 | + i = 0; |
| 1325 | + values[i++] = ObjectIdGetDatum(oid); |
| 1326 | + values[i++] = (Datum) fmgr(F_TEXTIN, comment); |
| 1327 | + } |
| 1328 | + |
| 1329 | + /*** Now, open pg_description and attempt to find the old tuple ***/ |
| 1330 | + |
| 1331 | + ScanKeyEntryInitialize(&entry, 0x0, Anum_pg_description_objoid, F_OIDEQ, |
| 1332 | + ObjectIdGetDatum(oid)); |
| 1333 | + scan = heap_beginscan(description, false, SnapshotNow, 1, &entry); |
| 1334 | + searchtuple = heap_getnext(scan, 0); |
| 1335 | + |
| 1336 | + /*** If a previous tuple exists, either delete it or prepare a replacement ***/ |
| 1337 | + |
| 1338 | + if (HeapTupleIsValid(searchtuple)) { |
| 1339 | + |
| 1340 | + /*** If the comment is blank, call heap_delete, else heap_replace ***/ |
| 1341 | + |
| 1342 | + if ((comment == NULL) || (strlen(comment) == 0)) { |
| 1343 | + heap_delete(description, &searchtuple->t_self, NULL); |
| 1344 | + } else { |
| 1345 | + desctuple = heap_modifytuple(searchtuple, description, values, nulls, replaces); |
| 1346 | + setheapoverride(true); |
| 1347 | + heap_replace(description, &searchtuple->t_self, desctuple, NULL); |
| 1348 | + setheapoverride(false); |
| 1349 | + modified = TRUE; |
| 1350 | + } |
| 1351 | + |
| 1352 | + } else { |
| 1353 | + desctuple = heap_formtuple(tupDesc, values, nulls); |
| 1354 | + heap_insert(description, desctuple); |
| 1355 | + modified = TRUE; |
| 1356 | + } |
| 1357 | + |
| 1358 | + /*** Complete the scan, update indices, if necessary ***/ |
| 1359 | + |
| 1360 | + heap_endscan(scan); |
| 1361 | + |
| 1362 | + if (modified) { |
| 1363 | + if (RelationGetForm(description)->relhasindex) { |
| 1364 | + Relation idescs[Num_pg_description_indices]; |
| 1365 | + |
| 1366 | + CatalogOpenIndices(Num_pg_description_indices, Name_pg_description_indices, idescs); |
| 1367 | + CatalogIndexInsert(idescs, Num_pg_description_indices, description, desctuple); |
| 1368 | + CatalogCloseIndices(Num_pg_description_indices, idescs); |
| 1369 | + } |
| 1370 | + pfree(desctuple); |
| 1371 | + |
| 1372 | + } |
| 1373 | + |
| 1374 | + heap_close(description, RowExclusiveLock); |
| 1375 | + |
| 1376 | +} |
| 1377 | + |
| 1378 | +/* -------------------------------- |
| 1379 | + * DeleteComments |
| 1380 | + * |
| 1381 | + * This routine is used to purge any comments |
| 1382 | + * associated with the Oid handed to this routine, |
| 1383 | + * regardless of the actual object type. It is |
| 1384 | + * called, for example, when a relation is destroyed. |
| 1385 | + * -------------------------------- |
| 1386 | + */ |
| 1387 | + |
| 1388 | +void |
| 1389 | +DeleteComments(Oid oid) |
| 1390 | +{ |
| 1391 | + |
| 1392 | + Relation description; |
| 1393 | + TupleDesc tupDesc; |
| 1394 | + ScanKeyData entry; |
| 1395 | + HeapScanDesc scan; |
| 1396 | + HeapTuple searchtuple; |
| 1397 | + |
| 1398 | + description = heap_openr(DescriptionRelationName, RowExclusiveLock); |
| 1399 | + tupDesc = description->rd_att; |
| 1400 | + |
| 1401 | + /*** Now, open pg_description and attempt to find the old tuple ***/ |
| 1402 | + |
| 1403 | + ScanKeyEntryInitialize(&entry, 0x0, Anum_pg_description_objoid, F_OIDEQ, |
| 1404 | + ObjectIdGetDatum(oid)); |
| 1405 | + scan = heap_beginscan(description, false, SnapshotNow, 1, &entry); |
| 1406 | + searchtuple = heap_getnext(scan, 0); |
| 1407 | + |
| 1408 | + /*** If a previous tuple exists, delete it ***/ |
| 1409 | + |
| 1410 | + if (HeapTupleIsValid(searchtuple)) { |
| 1411 | + heap_delete(description, &searchtuple->t_self, NULL); |
| 1412 | + } |
| 1413 | + |
| 1414 | + /*** Complete the scan, update indices, if necessary ***/ |
| 1415 | + |
| 1416 | + heap_endscan(scan); |
| 1417 | + heap_close(description, RowExclusiveLock); |
| 1418 | + |
| 1419 | +} |
| 1420 | + |
1284 | 1421 | /* --------------------------------
|
1285 | 1422 | * DeleteTypeTuple
|
1286 | 1423 | *
|
@@ -1471,6 +1608,13 @@ heap_destroy_with_catalog(char *relname)
|
1471 | 1608 | */
|
1472 | 1609 | DeleteAttributeTuples(rel);
|
1473 | 1610 |
|
| 1611 | + /* ---------------- |
| 1612 | + * delete comments |
| 1613 | + * ---------------- |
| 1614 | + */ |
| 1615 | + |
| 1616 | + DeleteComments(RelationGetRelid(rel)); |
| 1617 | + |
1474 | 1618 | if (istemp)
|
1475 | 1619 | remove_temp_relation(rid);
|
1476 | 1620 |
|
|
0 commit comments