* RelationClearRelation * * Physically blow away a relation cache entry, or reset it and rebuild * it from scratch (that is, from catalog entries). The latter path is * used when we are notified of a change to an open relation (one with * refcount > 0). * * NB: when rebuilding, we'd better hold some lock on the relation, * else the catalog data we need to read could be changing unde
| 2610 | * that we're doing what the caller expects. |
| 2611 | */ |
| 2612 | static void |
| 2613 | RelationClearRelation(Relation relation, bool rebuild) |
| 2614 | { |
| 2615 | /* |
| 2616 | * As per notes above, a rel to be rebuilt MUST have refcnt > 0; while of |
| 2617 | * course it would be an equally bad idea to blow away one with nonzero |
| 2618 | * refcnt, since that would leave someone somewhere with a dangling |
| 2619 | * pointer. All callers are expected to have verified that this holds. |
| 2620 | */ |
| 2621 | Assert(rebuild ? |
| 2622 | !RelationHasReferenceCountZero(relation) : |
| 2623 | RelationHasReferenceCountZero(relation)); |
| 2624 | |
| 2625 | /* |
| 2626 | * Make sure smgr and lower levels close the relation's files, if they |
| 2627 | * weren't closed already. If the relation is not getting deleted, the |
| 2628 | * next smgr access should reopen the files automatically. This ensures |
| 2629 | * that the low-level file access state is updated after, say, a vacuum |
| 2630 | * truncation. |
| 2631 | */ |
| 2632 | RelationCloseSmgr(relation); |
| 2633 | |
| 2634 | /* Free AM cached data, if any */ |
| 2635 | if (relation->rd_amcache) |
| 2636 | pfree(relation->rd_amcache); |
| 2637 | relation->rd_amcache = NULL; |
| 2638 | |
| 2639 | /* |
| 2640 | * Treat nailed-in system relations separately, they always need to be |
| 2641 | * accessible, so we can't blow them away. |
| 2642 | */ |
| 2643 | if (relation->rd_isnailed) |
| 2644 | { |
| 2645 | RelationReloadNailed(relation); |
| 2646 | return; |
| 2647 | } |
| 2648 | |
| 2649 | /* Mark it invalid until we've finished rebuild */ |
| 2650 | relation->rd_isvalid = false; |
| 2651 | |
| 2652 | /* See RelationForgetRelation(). */ |
| 2653 | if (relation->rd_droppedSubid != InvalidSubTransactionId) |
| 2654 | return; |
| 2655 | |
| 2656 | /* |
| 2657 | * Even non-system indexes should not be blown away if they are open and |
| 2658 | * have valid index support information. This avoids problems with active |
| 2659 | * use of the index support information. As with nailed indexes, we |
| 2660 | * re-read the pg_class row to handle possible physical relocation of the |
| 2661 | * index, and we check for pg_index updates too. |
| 2662 | */ |
| 2663 | if ((relation->rd_rel->relkind == RELKIND_INDEX || |
| 2664 | relation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) && |
| 2665 | relation->rd_refcnt > 0 && |
| 2666 | relation->rd_indexcxt != NULL) |
| 2667 | { |
| 2668 | if (IsTransactionState()) |
| 2669 | RelationReloadIndexInfo(relation); |
no test coverage detected