* RelationBuildPartitionKey * Build partition key data of relation, and attach to relcache * * Partitioning key data is a complex structure; to avoid complicated logic to * free individual elements whenever the relcache entry is flushed, we give it * its own memory context, a child of CacheMemoryContext, which can easily be * deleted on its own. To avoid leaking memory in that context in c
| 78 | * permanently. |
| 79 | */ |
| 80 | static void |
| 81 | RelationBuildPartitionKey(Relation relation) |
| 82 | { |
| 83 | Form_pg_partitioned_table form; |
| 84 | HeapTuple tuple; |
| 85 | bool isnull; |
| 86 | int i; |
| 87 | PartitionKey key; |
| 88 | AttrNumber *attrs; |
| 89 | oidvector *opclass; |
| 90 | oidvector *collation; |
| 91 | ListCell *partexprs_item; |
| 92 | Datum datum; |
| 93 | MemoryContext partkeycxt, |
| 94 | oldcxt; |
| 95 | int16 procnum; |
| 96 | |
| 97 | tuple = SearchSysCache1(PARTRELID, |
| 98 | ObjectIdGetDatum(RelationGetRelid(relation))); |
| 99 | |
| 100 | if (!HeapTupleIsValid(tuple)) |
| 101 | elog(ERROR, "cache lookup failed for partition key of relation %u", |
| 102 | RelationGetRelid(relation)); |
| 103 | |
| 104 | partkeycxt = AllocSetContextCreate(CurTransactionContext, |
| 105 | "partition key", |
| 106 | ALLOCSET_SMALL_SIZES); |
| 107 | MemoryContextCopyAndSetIdentifier(partkeycxt, |
| 108 | RelationGetRelationName(relation)); |
| 109 | |
| 110 | key = (PartitionKey) MemoryContextAllocZero(partkeycxt, |
| 111 | sizeof(PartitionKeyData)); |
| 112 | |
| 113 | /* Fixed-length attributes */ |
| 114 | form = (Form_pg_partitioned_table) GETSTRUCT(tuple); |
| 115 | key->strategy = form->partstrat; |
| 116 | key->partnatts = form->partnatts; |
| 117 | |
| 118 | /* |
| 119 | * We can rely on the first variable-length attribute being mapped to the |
| 120 | * relevant field of the catalog's C struct, because all previous |
| 121 | * attributes are non-nullable and fixed-length. |
| 122 | */ |
| 123 | attrs = form->partattrs.values; |
| 124 | |
| 125 | /* But use the hard way to retrieve further variable-length attributes */ |
| 126 | /* Operator class */ |
| 127 | datum = SysCacheGetAttr(PARTRELID, tuple, |
| 128 | Anum_pg_partitioned_table_partclass, &isnull); |
| 129 | Assert(!isnull); |
| 130 | opclass = (oidvector *) DatumGetPointer(datum); |
| 131 | |
| 132 | /* Collation */ |
| 133 | datum = SysCacheGetAttr(PARTRELID, tuple, |
| 134 | Anum_pg_partitioned_table_partcollation, &isnull); |
| 135 | Assert(!isnull); |
| 136 | collation = (oidvector *) DatumGetPointer(datum); |
| 137 |
no test coverage detected