Skip to content

Commit 0344edd

Browse files
committed
pathman: two patches by Julien Rouhaud (shmem allocation and post_parse_analyze_hook) and one by Alexander Korotkov (version check)
1 parent b23f756 commit 0344edd

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

contrib/pg_pathman/dsm_array.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ typedef BlockHeader* BlockHeaderPtr;
4343
#define set_length(header, length) \
4444
((length) | ((*header) & FREE_BIT))
4545

46+
/*
47+
* Amount of memory that need to be requested in shared memory to store dsm
48+
* config
49+
*/
50+
Size
51+
get_dsm_shared_size()
52+
{
53+
return (Size) MAXALIGN(sizeof(DsmConfig));
54+
}
55+
4656
/*
4757
* Initialize dsm config for arrays
4858
*/

contrib/pg_pathman/init.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ static bool validate_range_constraint(Expr *, PartRelationInfo *, Datum *, Datum
3434
static bool validate_hash_constraint(Expr *expr, PartRelationInfo *prel, int *hash);
3535
static int cmp_range_entries(const void *p1, const void *p2);
3636

37+
Size
38+
pathman_memsize()
39+
{
40+
Size size;
41+
42+
size = get_dsm_shared_size() + MAXALIGN(sizeof(PathmanState));
43+
return size;
44+
}
45+
3746
void
3847
init_shmem_config()
3948
{

contrib/pg_pathman/pathman.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#include "storage/dsm.h"
1919
#include "storage/lwlock.h"
2020

21+
/* Check PostgreSQL version */
22+
#if PG_VERSION_NUM < 90500
23+
#error "You are trying to build pg_pathman with PostgreSQL version lower than 9.5. Please, check you environment."
24+
#endif
2125

2226
#define ALL NIL
2327
#define INITIAL_BLOCKS_COUNT 8192
@@ -152,6 +156,7 @@ int irange_list_length(List *rangeset);
152156
bool irange_list_find(List *rangeset, int index, bool *lossy);
153157

154158
/* Dynamic shared memory functions */
159+
Size get_dsm_shared_size(void);
155160
void init_dsm_config(void);
156161
bool init_dsm_segment(size_t blocks_count, size_t block_size);
157162
void init_dsm_table(size_t block_size, size_t start, size_t end);
@@ -167,6 +172,7 @@ HTAB *range_restrictions;
167172
bool initialization_needed;
168173

169174
/* initialization functions */
175+
Size pathman_memsize(void);
170176
void init_shmem_config(void);
171177
void load_config(void);
172178
void create_relations_hashtable(void);

contrib/pg_pathman/pg_pathman.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "optimizer/planner.h"
2323
#include "optimizer/restrictinfo.h"
2424
#include "optimizer/cost.h"
25+
#include "parser/analyze.h"
2526
#include "parser/parsetree.h"
2627
#include "utils/hsearch.h"
2728
#include "utils/tqual.h"
@@ -56,6 +57,7 @@ typedef struct
5657
/* Original hooks */
5758
static set_rel_pathlist_hook_type set_rel_pathlist_hook_original = NULL;
5859
static shmem_startup_hook_type shmem_startup_hook_original = NULL;
60+
static post_parse_analyze_hook_type post_parse_analyze_hook_original = NULL;
5961
static planner_hook_type planner_hook_original = NULL;
6062

6163
/* pg module functions */
@@ -65,6 +67,7 @@ void _PG_fini(void);
6567
/* Hook functions */
6668
static void pathman_shmem_startup(void);
6769
static void pathman_set_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte);
70+
void pathman_post_parse_analysis_hook(ParseState *pstate, Query *query);
6871
static PlannedStmt * pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams);
6972

7073
/* Utility functions */
@@ -127,10 +130,16 @@ _PG_init(void)
127130
}
128131
#endif
129132

133+
/* Request additional shared resources */
134+
RequestAddinShmemSpace(pathman_memsize());
135+
RequestAddinLWLocks(3);
136+
130137
set_rel_pathlist_hook_original = set_rel_pathlist_hook;
131138
set_rel_pathlist_hook = pathman_set_rel_pathlist_hook;
132139
shmem_startup_hook_original = shmem_startup_hook;
133140
shmem_startup_hook = pathman_shmem_startup;
141+
post_parse_analyze_hook_original = post_parse_analyze_hook;
142+
post_parse_analyze_hook = pathman_post_parse_analysis_hook;
134143
planner_hook_original = planner_hook;
135144
planner_hook = pathman_planner_hook;
136145
}
@@ -140,6 +149,7 @@ _PG_fini(void)
140149
{
141150
set_rel_pathlist_hook = set_rel_pathlist_hook_original;
142151
shmem_startup_hook = shmem_startup_hook_original;
152+
post_parse_analyze_hook = post_parse_analyze_hook_original;
143153
planner_hook = planner_hook_original;
144154
}
145155

@@ -182,6 +192,20 @@ get_cmp_func(Oid type1, Oid type2)
182192
return cmp_func;
183193
}
184194

195+
/*
196+
* Post parse analysis hook. It makes sure the config is loaded before executing
197+
* any statement, including utility commands
198+
*/
199+
void
200+
pathman_post_parse_analysis_hook(ParseState *pstate, Query *query)
201+
{
202+
if (initialization_needed)
203+
load_config();
204+
205+
if (post_parse_analyze_hook_original)
206+
post_parse_analyze_hook_original(pstate, query);
207+
}
208+
185209
/*
186210
* Planner hook. It disables inheritance for tables that have been partitioned
187211
* by pathman to prevent standart PostgreSQL partitioning mechanism from
@@ -193,11 +217,6 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
193217
PlannedStmt *result;
194218
ListCell *lc;
195219

196-
if (initialization_needed)
197-
{
198-
load_config();
199-
}
200-
201220
inheritance_disabled = false;
202221
switch(parse->commandType)
203222
{
@@ -325,9 +344,6 @@ handle_modification_query(Query *parse)
325344
static void
326345
pathman_shmem_startup(void)
327346
{
328-
/* Initialize locks */
329-
RequestAddinLWLocks(3);
330-
331347
/* Allocate shared memory objects */
332348
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
333349
init_dsm_config();

0 commit comments

Comments
 (0)