|
16 | 16 |
|
17 | 17 | #include "access/heapam.h"
|
18 | 18 | #include "access/htup_details.h"
|
| 19 | +#include "access/multixact.h" |
| 20 | +#include "access/transam.h" |
| 21 | +#include "access/xact.h" |
19 | 22 | #include "catalog/catalog.h"
|
20 | 23 | #include "catalog/dependency.h"
|
21 | 24 | #include "catalog/heap.h"
|
@@ -409,7 +412,7 @@ DefineQueryRewrite(char *rulename,
|
409 | 412 | * strict, because they will reject relations that once had such but
|
410 | 413 | * don't anymore. But we don't really care, because this whole
|
411 | 414 | * business of converting relations to views is just a kluge to allow
|
412 |
| - * loading ancient pg_dump files.) |
| 415 | + * dump/reload of views that participate in circular dependencies.) |
413 | 416 | */
|
414 | 417 | if (event_relation->rd_rel->relkind != RELKIND_VIEW)
|
415 | 418 | {
|
@@ -500,30 +503,103 @@ DefineQueryRewrite(char *rulename,
|
500 | 503 | replace);
|
501 | 504 |
|
502 | 505 | /*
|
503 |
| - * Set pg_class 'relhasrules' field TRUE for event relation. If |
504 |
| - * appropriate, also modify the 'relkind' field to show that the |
505 |
| - * relation is now a view. |
| 506 | + * Set pg_class 'relhasrules' field TRUE for event relation. |
506 | 507 | *
|
507 | 508 | * Important side effect: an SI notice is broadcast to force all
|
508 | 509 | * backends (including me!) to update relcache entries with the new
|
509 | 510 | * rule.
|
510 | 511 | */
|
511 |
| - SetRelationRuleStatus(event_relid, true, RelisBecomingView); |
| 512 | + SetRelationRuleStatus(event_relid, true); |
512 | 513 | }
|
513 | 514 |
|
514 |
| - /* |
515 |
| - * If the relation is becoming a view, delete the storage files associated |
516 |
| - * with it. Also, get rid of any system attribute entries in pg_attribute, |
517 |
| - * because a view shouldn't have any of those. |
| 515 | + /* --------------------------------------------------------------------- |
| 516 | + * If the relation is becoming a view: |
| 517 | + * - delete the associated storage files |
| 518 | + * - get rid of any system attributes in pg_attribute; a view shouldn't |
| 519 | + * have any of those |
| 520 | + * - remove the toast table; there is no need for it anymore, and its |
| 521 | + * presence would make vacuum slightly more complicated |
| 522 | + * - set relkind to RELKIND_VIEW, and adjust other pg_class fields |
| 523 | + * to be appropriate for a view |
518 | 524 | *
|
519 | 525 | * NB: we had better have AccessExclusiveLock to do this ...
|
520 |
| - * |
521 |
| - * XXX what about getting rid of its TOAST table? For now, we don't. |
| 526 | + * --------------------------------------------------------------------- |
522 | 527 | */
|
523 | 528 | if (RelisBecomingView)
|
524 | 529 | {
|
| 530 | + Relation relationRelation; |
| 531 | + Oid toastrelid; |
| 532 | + HeapTuple classTup; |
| 533 | + Form_pg_class classForm; |
| 534 | + |
| 535 | + relationRelation = heap_open(RelationRelationId, RowExclusiveLock); |
| 536 | + toastrelid = event_relation->rd_rel->reltoastrelid; |
| 537 | + |
| 538 | + /* drop storage while table still looks like a table */ |
525 | 539 | RelationDropStorage(event_relation);
|
526 | 540 | DeleteSystemAttributeTuples(event_relid);
|
| 541 | + |
| 542 | + /* |
| 543 | + * Drop the toast table if any. (This won't take care of updating |
| 544 | + * the toast fields in the relation's own pg_class entry; we handle |
| 545 | + * that below.) |
| 546 | + */ |
| 547 | + if (OidIsValid(toastrelid)) |
| 548 | + { |
| 549 | + ObjectAddress toastobject; |
| 550 | + |
| 551 | + /* |
| 552 | + * Delete the dependency of the toast relation on the main |
| 553 | + * relation so we can drop the former without dropping the latter. |
| 554 | + */ |
| 555 | + deleteDependencyRecordsFor(RelationRelationId, toastrelid, |
| 556 | + false); |
| 557 | + |
| 558 | + /* Make deletion of dependency record visible */ |
| 559 | + CommandCounterIncrement(); |
| 560 | + |
| 561 | + /* Now drop toast table, including its index */ |
| 562 | + toastobject.classId = RelationRelationId; |
| 563 | + toastobject.objectId = toastrelid; |
| 564 | + toastobject.objectSubId = 0; |
| 565 | + performDeletion(&toastobject, DROP_RESTRICT, |
| 566 | + PERFORM_DELETION_INTERNAL); |
| 567 | + } |
| 568 | + |
| 569 | + /* |
| 570 | + * SetRelationRuleStatus may have updated the pg_class row, so we must |
| 571 | + * advance the command counter before trying to update it again. |
| 572 | + */ |
| 573 | + CommandCounterIncrement(); |
| 574 | + |
| 575 | + /* |
| 576 | + * Fix pg_class entry to look like a normal view's, including setting |
| 577 | + * the correct relkind and removal of reltoastrelid/reltoastidxid of |
| 578 | + * the toast table we potentially removed above. |
| 579 | + */ |
| 580 | + classTup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(event_relid)); |
| 581 | + if (!HeapTupleIsValid(classTup)) |
| 582 | + elog(ERROR, "cache lookup failed for relation %u", event_relid); |
| 583 | + classForm = (Form_pg_class) GETSTRUCT(classTup); |
| 584 | + |
| 585 | + classForm->reltablespace = InvalidOid; |
| 586 | + classForm->relpages = 0; |
| 587 | + classForm->reltuples = 0; |
| 588 | + classForm->relallvisible = 0; |
| 589 | + classForm->reltoastrelid = InvalidOid; |
| 590 | + classForm->reltoastidxid = InvalidOid; |
| 591 | + classForm->relhasindex = false; |
| 592 | + classForm->relkind = RELKIND_VIEW; |
| 593 | + classForm->relhasoids = false; |
| 594 | + classForm->relhaspkey = false; |
| 595 | + classForm->relfrozenxid = InvalidTransactionId; |
| 596 | + classForm->relminmxid = InvalidMultiXactId; |
| 597 | + |
| 598 | + simple_heap_update(relationRelation, &classTup->t_self, classTup); |
| 599 | + CatalogUpdateIndexes(relationRelation, classTup); |
| 600 | + |
| 601 | + heap_freetuple(classTup); |
| 602 | + heap_close(relationRelation, RowExclusiveLock); |
527 | 603 | }
|
528 | 604 |
|
529 | 605 | /* Close rel, but keep lock till commit... */
|
|
0 commit comments