Skip to content

Commit 5764bb3

Browse files
committed
pathman: cleanups
1 parent 24c39fd commit 5764bb3

File tree

5 files changed

+223
-257
lines changed

5 files changed

+223
-257
lines changed

contrib/pathman/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# contrib/pathman/Makefile
22

33
MODULE_big = pathman
4-
OBJS = init.o pathman.o dsm_array.o rangeset.o $(WIN32RES)
4+
OBJS = init.o pathman.o dsm_array.o rangeset.o pl_funcs.o $(WIN32RES)
55

66
EXTENSION = pathman
77
EXTVERSION = 0.1

contrib/pathman/README.rus.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ CHECK ( id >= 200 AND id < 300 )
2929
```
3030
WHERE id = 150
3131
```
32+
Затем основываясь на стратегии секционирования и условиях запроса `pathman` выбирает соответствующие секции и строит план.
3233

3334
## Installation
3435

contrib/pathman/pathman.c

Lines changed: 14 additions & 214 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ typedef struct
4141
/* Original hooks */
4242
static set_rel_pathlist_hook_type set_rel_pathlist_hook_original = NULL;
4343
static shmem_startup_hook_type shmem_startup_hook_original = NULL;
44+
static planner_hook_type planner_hook_original = NULL;
4445

4546
void _PG_init(void);
4647
void _PG_fini(void);
4748
static void pathman_shmem_startup(void);
48-
static void my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte);
49-
static PlannedStmt * my_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams);
49+
static void pathman_set_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte);
50+
static PlannedStmt * pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams);
5051

5152
static void append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
5253
RangeTblEntry *rte, int index, Oid childOID, List *wrappers);
@@ -57,7 +58,6 @@ bool inheritance_disabled;
5758

5859
static WrapperNode *walk_expr_tree(Expr *expr, const PartRelationInfo *prel);
5960
static int make_hash(const PartRelationInfo *prel, int value);
60-
static int range_binary_search(const RangeRelation *rangerel, FmgrInfo *cmp_func, Datum value, bool *fountPtr);
6161
static void handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result, const Var *v, const Const *c);
6262
static WrapperNode *handle_opexpr(const OpExpr *expr, const PartRelationInfo *prel);
6363
static WrapperNode *handle_boolexpr(const BoolExpr *expr, const PartRelationInfo *prel);
@@ -71,14 +71,6 @@ static void change_varnos_in_restrinct_info(RestrictInfo *rinfo, change_varno_co
7171
static void change_varnos(Node *node, Oid old_varno, Oid new_varno);
7272
static bool change_varno_walker(Node *node, change_varno_context *context);
7373

74-
/* callbacks */
75-
PG_FUNCTION_INFO_V1( on_partitions_created );
76-
PG_FUNCTION_INFO_V1( on_partitions_updated );
77-
PG_FUNCTION_INFO_V1( on_partitions_removed );
78-
PG_FUNCTION_INFO_V1( find_range_partition );
79-
PG_FUNCTION_INFO_V1( get_range_by_idx );
80-
PG_FUNCTION_INFO_V1( get_partition_range );
81-
8274

8375
/*
8476
* Compare two Datums with the given comarison function
@@ -105,11 +97,11 @@ void
10597
_PG_init(void)
10698
{
10799
set_rel_pathlist_hook_original = set_rel_pathlist_hook;
108-
set_rel_pathlist_hook = my_hook;
100+
set_rel_pathlist_hook = pathman_set_rel_pathlist_hook;
109101
shmem_startup_hook_original = shmem_startup_hook;
110102
shmem_startup_hook = pathman_shmem_startup;
111-
112-
planner_hook = my_planner_hook;
103+
planner_hook_original = planner_hook;
104+
planner_hook = pathman_planner_hook;
113105
}
114106

115107
void
@@ -121,7 +113,7 @@ _PG_fini(void)
121113

122114
/* TODO: rename and write a descritption */
123115
PlannedStmt *
124-
my_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
116+
pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
125117
{
126118
PlannedStmt *result;
127119

@@ -138,7 +130,8 @@ my_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
138130
}
139131

140132
/*
141-
*
133+
* Disables inheritance for partitioned by pathman relations. It must be done to
134+
* prevent PostgresSQL from full search.
142135
*/
143136
static void
144137
disable_inheritance(Query *parse)
@@ -213,7 +206,7 @@ pathman_shmem_startup(void)
213206
* The hook function. All the magic goes here
214207
*/
215208
void
216-
my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
209+
pathman_set_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
217210
{
218211
PartRelationInfo *prel = NULL;
219212

@@ -788,7 +781,7 @@ make_hash(const PartRelationInfo *prel, int value)
788781
* If item wasn't found then function returns closest position and sets
789782
* foundPtr to false.
790783
*/
791-
static int
784+
int
792785
range_binary_search(const RangeRelation *rangerel, FmgrInfo *cmp_func, Datum value, bool *foundPtr)
793786
{
794787
RangeEntry *ranges = dsm_array_get_pointer(&rangerel->ranges);
@@ -968,7 +961,9 @@ handle_arrexpr(const ScalarArrayOpExpr *expr, const PartRelationInfo *prel)
968961
return result;
969962
}
970963

971-
/* copy-past from allpaths.c with modifications */
964+
/*
965+
* Copy-paste functions from allpaths.c with (or without) some modifications
966+
*/
972967

973968
/*
974969
* set_plain_rel_pathlist
@@ -1132,198 +1127,3 @@ accumulate_append_subpath(List *subpaths, Path *path)
11321127
{
11331128
return lappend(subpaths, path);
11341129
}
1135-
1136-
1137-
/*
1138-
* Callbacks
1139-
*/
1140-
Datum
1141-
on_partitions_created(PG_FUNCTION_ARGS)
1142-
{
1143-
// Oid relid;
1144-
1145-
LWLockAcquire(load_config_lock, LW_EXCLUSIVE);
1146-
1147-
/* Reload config */
1148-
/* TODO: reload just the specified relation */
1149-
// relid = DatumGetInt32(PG_GETARG_DATUM(0))
1150-
load_part_relations_hashtable(false);
1151-
1152-
LWLockRelease(load_config_lock);
1153-
1154-
PG_RETURN_NULL();
1155-
}
1156-
1157-
Datum
1158-
on_partitions_updated(PG_FUNCTION_ARGS)
1159-
{
1160-
Oid relid;
1161-
PartRelationInfo *prel;
1162-
1163-
/* parent relation oid */
1164-
relid = DatumGetInt32(PG_GETARG_DATUM(0));
1165-
prel = (PartRelationInfo *)
1166-
hash_search(relations, (const void *) &relid, HASH_FIND, 0);
1167-
if (prel != NULL)
1168-
{
1169-
LWLockAcquire(load_config_lock, LW_EXCLUSIVE);
1170-
remove_relation_info(relid);
1171-
load_part_relations_hashtable(false);
1172-
LWLockRelease(load_config_lock);
1173-
}
1174-
1175-
PG_RETURN_NULL();
1176-
}
1177-
1178-
Datum
1179-
on_partitions_removed(PG_FUNCTION_ARGS)
1180-
{
1181-
Oid relid;
1182-
1183-
LWLockAcquire(load_config_lock, LW_EXCLUSIVE);
1184-
1185-
/* parent relation oid */
1186-
relid = DatumGetInt32(PG_GETARG_DATUM(0));
1187-
remove_relation_info(relid);
1188-
1189-
LWLockRelease(load_config_lock);
1190-
1191-
PG_RETURN_NULL();
1192-
}
1193-
1194-
/*
1195-
* Returns partition oid for specified parent relid and value
1196-
*/
1197-
Datum
1198-
find_range_partition(PG_FUNCTION_ARGS)
1199-
{
1200-
int relid = DatumGetInt32(PG_GETARG_DATUM(0));
1201-
Datum value = PG_GETARG_DATUM(1);
1202-
Oid value_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1203-
int pos;
1204-
bool found;
1205-
RangeRelation *rangerel;
1206-
RangeEntry *ranges;
1207-
TypeCacheEntry *tce;
1208-
FmgrInfo *cmp_func;
1209-
1210-
tce = lookup_type_cache(value_type,
1211-
TYPECACHE_EQ_OPR | TYPECACHE_LT_OPR | TYPECACHE_GT_OPR |
1212-
TYPECACHE_CMP_PROC | TYPECACHE_CMP_PROC_FINFO);
1213-
cmp_func = &tce->cmp_proc_finfo;
1214-
1215-
rangerel = (RangeRelation *)
1216-
hash_search(range_restrictions, (const void *) &relid, HASH_FIND, NULL);
1217-
1218-
if (!rangerel)
1219-
PG_RETURN_NULL();
1220-
1221-
ranges = dsm_array_get_pointer(&rangerel->ranges);
1222-
pos = range_binary_search(rangerel, cmp_func, value, &found);
1223-
1224-
if (found)
1225-
PG_RETURN_OID(ranges[pos].child_oid);
1226-
1227-
PG_RETURN_NULL();
1228-
}
1229-
1230-
/*
1231-
* Returns range (min, max) as output parameters
1232-
*
1233-
* first argument is the parent relid
1234-
* second is the partition relid
1235-
* third and forth are MIN and MAX output parameters
1236-
*/
1237-
Datum
1238-
get_partition_range(PG_FUNCTION_ARGS)
1239-
{
1240-
int parent_oid = DatumGetInt32(PG_GETARG_DATUM(0));
1241-
int child_oid = DatumGetInt32(PG_GETARG_DATUM(1));
1242-
int nelems = 2;
1243-
int i;
1244-
bool found = false;
1245-
Datum *elems;
1246-
PartRelationInfo *prel;
1247-
RangeRelation *rangerel;
1248-
RangeEntry *ranges;
1249-
TypeCacheEntry *tce;
1250-
ArrayType *arr;
1251-
1252-
prel = (PartRelationInfo *)
1253-
hash_search(relations, (const void *) &parent_oid, HASH_FIND, NULL);
1254-
1255-
rangerel = (RangeRelation *)
1256-
hash_search(range_restrictions, (const void *) &parent_oid, HASH_FIND, NULL);
1257-
1258-
if (!prel || !rangerel)
1259-
PG_RETURN_NULL();
1260-
1261-
ranges = dsm_array_get_pointer(&rangerel->ranges);
1262-
tce = lookup_type_cache(prel->atttype, 0);
1263-
1264-
/* looking for specified partition */
1265-
for(i=0; i<rangerel->ranges.length; i++)
1266-
if (ranges[i].child_oid == child_oid)
1267-
{
1268-
found = true;
1269-
break;
1270-
}
1271-
1272-
if (found)
1273-
{
1274-
elems = palloc(nelems * sizeof(Datum));
1275-
elems[0] = ranges[i].min;
1276-
elems[1] = ranges[i].max;
1277-
1278-
arr = construct_array(elems, nelems, prel->atttype,
1279-
tce->typlen, tce->typbyval, tce->typalign);
1280-
PG_RETURN_ARRAYTYPE_P(arr);
1281-
}
1282-
1283-
PG_RETURN_NULL();
1284-
}
1285-
1286-
1287-
/*
1288-
* Returns N-th range (in form of array)
1289-
*
1290-
* First argument is the parent relid.
1291-
* Second argument is the index of the range (if it is negative then the last
1292-
* range will be returned).
1293-
*/
1294-
Datum
1295-
get_range_by_idx(PG_FUNCTION_ARGS)
1296-
{
1297-
int parent_oid = DatumGetInt32(PG_GETARG_DATUM(0));
1298-
int idx = DatumGetInt32(PG_GETARG_DATUM(1));
1299-
PartRelationInfo *prel;
1300-
RangeRelation *rangerel;
1301-
RangeEntry *ranges;
1302-
RangeEntry *re;
1303-
Datum *elems;
1304-
TypeCacheEntry *tce;
1305-
1306-
prel = (PartRelationInfo *)
1307-
hash_search(relations, (const void *) &parent_oid, HASH_FIND, NULL);
1308-
1309-
rangerel = (RangeRelation *)
1310-
hash_search(range_restrictions, (const void *) &parent_oid, HASH_FIND, NULL);
1311-
1312-
if (!prel || !rangerel || idx >= (int)rangerel->ranges.length)
1313-
PG_RETURN_NULL();
1314-
1315-
tce = lookup_type_cache(prel->atttype, 0);
1316-
ranges = dsm_array_get_pointer(&rangerel->ranges);
1317-
if (idx >= 0)
1318-
re = &ranges[idx];
1319-
else
1320-
re = &ranges[rangerel->ranges.length - 1];
1321-
1322-
elems = palloc(2 * sizeof(Datum));
1323-
elems[0] = re->min;
1324-
elems[1] = re->max;
1325-
1326-
PG_RETURN_ARRAYTYPE_P(
1327-
construct_array(elems, 2, prel->atttype,
1328-
tce->typlen, tce->typbyval, tce->typalign));
1329-
}

0 commit comments

Comments
 (0)