* RelationBuildRuleLock * * Form the relation's rewrite rules from information in * the pg_rewrite system catalog. * * Note: The rule parsetrees are potentially very complex node structures. * To allow these trees to be freed when the relcache entry is flushed, * we make a private memory context to hold the RuleLock information for * each relcache entry that has associated rules. The c
| 749 | * to be easy to free explicitly, anyway. |
| 750 | */ |
| 751 | static void |
| 752 | RelationBuildRuleLock(Relation relation) |
| 753 | { |
| 754 | MemoryContext rulescxt; |
| 755 | MemoryContext oldcxt; |
| 756 | HeapTuple rewrite_tuple; |
| 757 | Relation rewrite_desc; |
| 758 | TupleDesc rewrite_tupdesc; |
| 759 | SysScanDesc rewrite_scan; |
| 760 | ScanKeyData key; |
| 761 | RuleLock *rulelock; |
| 762 | int numlocks; |
| 763 | RewriteRule **rules; |
| 764 | int maxlocks; |
| 765 | |
| 766 | /* |
| 767 | * Make the private context. Assume it'll not contain much data. |
| 768 | */ |
| 769 | rulescxt = AllocSetContextCreate(CacheMemoryContext, |
| 770 | "relation rules", |
| 771 | ALLOCSET_SMALL_SIZES); |
| 772 | relation->rd_rulescxt = rulescxt; |
| 773 | MemoryContextCopyAndSetIdentifier(rulescxt, |
| 774 | RelationGetRelationName(relation)); |
| 775 | |
| 776 | /* |
| 777 | * allocate an array to hold the rewrite rules (the array is extended if |
| 778 | * necessary) |
| 779 | */ |
| 780 | maxlocks = 4; |
| 781 | rules = (RewriteRule **) |
| 782 | MemoryContextAlloc(rulescxt, sizeof(RewriteRule *) * maxlocks); |
| 783 | numlocks = 0; |
| 784 | |
| 785 | /* |
| 786 | * form a scan key |
| 787 | */ |
| 788 | ScanKeyInit(&key, |
| 789 | Anum_pg_rewrite_ev_class, |
| 790 | BTEqualStrategyNumber, F_OIDEQ, |
| 791 | ObjectIdGetDatum(RelationGetRelid(relation))); |
| 792 | |
| 793 | /* |
| 794 | * open pg_rewrite and begin a scan |
| 795 | * |
| 796 | * Note: since we scan the rules using RewriteRelRulenameIndexId, we will |
| 797 | * be reading the rules in name order, except possibly during |
| 798 | * emergency-recovery operations (ie, IgnoreSystemIndexes). This in turn |
| 799 | * ensures that rules will be fired in name order. |
| 800 | */ |
| 801 | rewrite_desc = table_open(RewriteRelationId, AccessShareLock); |
| 802 | rewrite_tupdesc = RelationGetDescr(rewrite_desc); |
| 803 | rewrite_scan = systable_beginscan(rewrite_desc, |
| 804 | RewriteRelRulenameIndexId, |
| 805 | true, NULL, |
| 806 | 1, &key); |
| 807 | |
| 808 | while (HeapTupleIsValid(rewrite_tuple = systable_getnext(rewrite_scan))) |
no test coverage detected