Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5381eb3

Browse files
committed
Merge branch 'pathman_pgpro9_5' of gitlab.postgrespro.ru:pgpro-dev/postgrespro into pathman_pgpro9_5
2 parents1e178f0 +e5caa24 commit5381eb3

File tree

11 files changed

+563
-123
lines changed

11 files changed

+563
-123
lines changed

‎contrib/pg_pathman/Makefile

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

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

66
EXTENSION = pg_pathman
77
EXTVERSION = 0.1

‎contrib/pg_pathman/dsm_array.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ typedef BlockHeader* BlockHeaderPtr;
3232
#defineset_length(header,length) \
3333
((length) | ((*header) & FREE_BIT))
3434

35+
/*
36+
* Initialize dsm config for arrays
37+
*/
3538
void
36-
alloc_dsm_table()
39+
init_dsm_config()
3740
{
3841
boolfound;
39-
dsm_cfg=ShmemInitStruct("dsm config",sizeof(DsmConfig),&found);
42+
dsm_cfg=ShmemInitStruct("pathman dsm_array config",sizeof(DsmConfig),&found);
4043
if (!found)
4144
{
4245
dsm_cfg->segment_handle=0;
@@ -46,6 +49,15 @@ alloc_dsm_table()
4649
}
4750
}
4851

52+
/*
53+
* Attach process to dsm_array segment. This function is used for
54+
* background workers only. Use init_dsm_segment() in backend processes.
55+
*/
56+
void
57+
attach_dsm_array_segment()
58+
{
59+
segment=dsm_attach(dsm_cfg->segment_handle);
60+
}
4961

5062
/*
5163
* Initialize dsm segment. Returns true if new segment was created and

‎contrib/pg_pathman/expected/pg_pathman.out

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ CREATE TABLE test.range_rel (
5151
CREATE INDEX ON test.range_rel (dt);
5252
INSERT INTO test.range_rel (dt, txt)
5353
SELECT g, md5(g::TEXT) FROM generate_series('2015-01-01', '2015-04-30', '1 day'::interval) as g;
54-
SELECT pathman.create_range_partitions('test.range_rel', 'dt', '2015-01-01'::DATE, '1 month'::INTERVAL, 4);
54+
SELECT pathman.create_range_partitions('test.range_rel', 'dt', '2015-01-01'::DATE, '1 month'::INTERVAL, 2);
55+
ERROR: Not enough partitions to fit all the values of 'dt' P0001
56+
SELECT pathman.create_range_partitions('test.range_rel', 'dt', '2015-01-01'::DATE, '1 month'::INTERVAL);
5557
NOTICE: sequence "range_rel_seq" does not exist, skipping
5658
NOTICE: Copying data to partitions...
5759
create_range_partitions
@@ -508,23 +510,8 @@ NOTICE: Copying data to partitions...
508510

509511
INSERT INTO test.range_rel (dt)
510512
SELECT generate_series('2015-01-01', '2015-04-30', '1 day'::interval);
511-
NOTICE: partition test.range_rel_2 created
512-
NOTICE: partition test.range_rel_3 created
513-
NOTICE: partition test.range_rel_4 created
514-
NOTICE: partition test.range_rel_5 created
515-
NOTICE: partition test.range_rel_6 created
516-
NOTICE: partition test.range_rel_7 created
517-
NOTICE: partition test.range_rel_8 created
518-
NOTICE: partition test.range_rel_9 created
519-
NOTICE: partition test.range_rel_10 created
520-
NOTICE: partition test.range_rel_11 created
521-
NOTICE: partition test.range_rel_12 created
522513
INSERT INTO test.range_rel (dt)
523514
SELECT generate_series('2014-12-31', '2014-12-01', '-1 day'::interval);
524-
NOTICE: partition test.range_rel_13 created
525-
NOTICE: partition test.range_rel_14 created
526-
NOTICE: partition test.range_rel_15 created
527-
NOTICE: partition test.range_rel_16 created
528515
EXPLAIN (COSTS OFF) SELECT * FROM test.range_rel WHERE dt = '2014-12-15';
529516
QUERY PLAN
530517
--------------------------------------------------------------------------------
@@ -556,8 +543,8 @@ SELECT * FROM test.range_rel WHERE dt = '2015-03-15';
556543
DROP TABLE test.range_rel CASCADE;
557544
NOTICE: drop cascades to 16 other objects
558545
SELECT * FROM pathman.pathman_config;
559-
id | relname | attname | parttype
560-
----+---------+---------+----------
546+
id | relname | attname | parttype| range_interval
547+
----+---------+---------+----------+----------------
561548
(0 rows)
562549

563550
DROP EXTENSION pg_pathman;

‎contrib/pg_pathman/init.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,33 @@
1616

1717
HTAB*relations=NULL;
1818
HTAB*range_restrictions=NULL;
19+
// bool *config_loaded = NULL;
1920
boolinitialization_needed= true;
2021

22+
typedefstructShmemConfig
23+
{
24+
boolconfig_loaded;
25+
}ShmemConfig;
26+
ShmemConfig*shmem_cfg;
27+
2128
staticFmgrInfo*qsort_type_cmp_func;
2229

2330
staticboolvalidate_range_constraint(Expr*,PartRelationInfo*,Datum*,Datum*);
2431
staticboolvalidate_hash_constraint(Expr*expr,PartRelationInfo*prel,int*hash);
2532
staticintcmp_range_entries(constvoid*p1,constvoid*p2);
2633

34+
void
35+
init_shmem_config()
36+
{
37+
boolfound;
38+
create_relations_hashtable();
39+
create_range_restrictions_hashtable();
40+
shmem_cfg= (ShmemConfig*)
41+
ShmemInitStruct("pathman shmem config",sizeof(ShmemConfig),&found);
42+
shmem_cfg->config_loaded= false;
43+
// *config_loaded = false;
44+
}
45+
2746
/*
2847
* Initialize hashtables
2948
*/
@@ -35,9 +54,15 @@ load_config(void)
3554
initialization_needed= false;
3655
new_segment_created=init_dsm_segment(INITIAL_BLOCKS_COUNT,32);
3756

38-
LWLockAcquire(load_config_lock,LW_EXCLUSIVE);
39-
load_relations_hashtable(new_segment_created);
40-
LWLockRelease(load_config_lock);
57+
/* if config is not loaded */
58+
if (shmem_cfg&& !shmem_cfg->config_loaded)
59+
{
60+
LWLockAcquire(load_config_lock,LW_EXCLUSIVE);
61+
load_relations_hashtable(new_segment_created);
62+
LWLockRelease(load_config_lock);
63+
// *config_loaded = true;
64+
shmem_cfg->config_loaded= true;
65+
}
4166
}
4267

4368
/*

‎contrib/pg_pathman/init.sql

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
/*
2-
* Relations using partitioning
2+
* Pathman config
3+
* relname - schema qualified relation name
4+
* attname - partitioning key
5+
* parttype - partitioning type:
6+
* 1 - HASH
7+
* 2 - RANGE
8+
* range_interval - base interval for RANGE partitioning in string representation
39
*/
410
CREATETABLEIF NOT EXISTS @extschema@.pathman_config (
5-
idSERIALPRIMARY KEY,
6-
relnameVARCHAR(127),
7-
attnameVARCHAR(127),
8-
parttypeINTEGER
11+
idSERIALPRIMARY KEY,
12+
relnameVARCHAR(127),
13+
attnameVARCHAR(127),
14+
parttypeINTEGER,
15+
range_intervalTEXT
916
);
1017

1118

@@ -67,9 +74,9 @@ BEGIN
6774
-- RAISE NOTICE '% rows have been copied', p_total;
6875
RETURN;
6976

70-
EXCEPTION WHEN others THEN
71-
PERFORM on_remove_partitions(p_parent::regclass::integer);
72-
RAISE EXCEPTION'% %', SQLERRM, SQLSTATE;
77+
--EXCEPTION WHEN others THEN
78+
-- PERFORM on_remove_partitions(p_parent::regclass::integer);
79+
-- RAISE EXCEPTION '% %', SQLERRM, SQLSTATE;
7380
END
7481
$$
7582
LANGUAGE plpgsql;
@@ -139,6 +146,18 @@ END
139146
$$
140147
LANGUAGE plpgsql;
141148

149+
/*
150+
* Check if regclass if date or timestamp
151+
*/
152+
CREATEOR REPLACE FUNCTION @extschema@.is_date(cls REGTYPE)
153+
RETURNSBOOLEANAS
154+
$$
155+
BEGIN
156+
RETURN clsIN ('timestamp'::regtype,'timestamptz'::regtype,'date'::regtype);
157+
END
158+
$$
159+
LANGUAGE plpgsql;
160+
142161
/*
143162
* DDL trigger that deletes entry from pathman_config
144163
*/

‎contrib/pg_pathman/pathman.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,21 @@ LWLock *dsm_init_lock;
131131

132132

133133
/* Dynamic shared memory functions */
134-
voidalloc_dsm_table(void);
134+
voidinit_dsm_config(void);
135135
boolinit_dsm_segment(size_tblocks_count,size_tblock_size);
136136
voidinit_dsm_table(size_tblock_size,size_tstart,size_tend);
137137
voidalloc_dsm_array(DsmArray*arr,size_tentry_size,size_tlength);
138138
voidfree_dsm_array(DsmArray*arr);
139139
void*dsm_array_get_pointer(constDsmArray*arr);
140-
140+
dsm_handleget_dsm_array_segment(void);
141+
voidattach_dsm_array_segment(void);
141142

142143
HTAB*relations;
143144
HTAB*range_restrictions;
144145
boolinitialization_needed;
145146

146147
/* initialization functions */
148+
voidinit_shmem_config(void);
147149
voidload_config(void);
148150
voidcreate_relations_hashtable(void);
149151
voidcreate_hash_restrictions_hashtable(void);
@@ -157,5 +159,8 @@ PartRelationInfo *get_pathman_relation_info(Oid relid, bool *found);
157159
RangeRelation*get_pathman_range_relation(Oidrelid,bool*found);
158160
intrange_binary_search(constRangeRelation*rangerel,FmgrInfo*cmp_func,Datumvalue,bool*fountPtr);
159161
char*get_extension_schema(void);
162+
FmgrInfo*get_cmp_func(Oidtype1,Oidtype2);
163+
Oidcreate_partitions_bg_worker(Oidrelid,Datumvalue,Oidvalue_type);
164+
Oidcreate_partitions(Oidrelid,Datumvalue,Oidvalue_type);
160165

161166
#endif/* PATHMAN_H */

‎contrib/pg_pathman/pg_pathman.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ get_pathman_range_relation(Oid relid, bool *found)
147147
returnhash_search(range_restrictions, (constvoid*)&key,HASH_FIND,found);
148148
}
149149

150+
FmgrInfo*
151+
get_cmp_func(Oidtype1,Oidtype2)
152+
{
153+
FmgrInfo*cmp_func;
154+
Oidcmp_proc_oid;
155+
TypeCacheEntry*tce;
156+
157+
cmp_func=palloc(sizeof(FmgrInfo));
158+
tce=lookup_type_cache(type1,
159+
TYPECACHE_EQ_OPR |TYPECACHE_LT_OPR |TYPECACHE_GT_OPR |
160+
TYPECACHE_CMP_PROC |TYPECACHE_CMP_PROC_FINFO);
161+
cmp_proc_oid=get_opfamily_proc(tce->btree_opf,
162+
type1,
163+
type2,
164+
BTORDER_PROC);
165+
fmgr_info(cmp_proc_oid,cmp_func);
166+
returncmp_func;
167+
}
168+
150169
/*
151170
* Planner hook. It disables inheritance for tables that have been partitioned
152171
* by pathman to prevent standart PostgreSQL partitioning mechanism from
@@ -233,9 +252,8 @@ pathman_shmem_startup(void)
233252
LWLockAcquire(AddinShmemInitLock,LW_EXCLUSIVE);
234253

235254
/* Allocate shared memory objects */
236-
alloc_dsm_table();
237-
create_relations_hashtable();
238-
create_range_restrictions_hashtable();
255+
init_dsm_config();
256+
init_shmem_config();
239257

240258
LWLockRelease(AddinShmemInitLock);
241259

‎contrib/pg_pathman/pl_funcs.c

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,9 @@ find_or_create_range_partition(PG_FUNCTION_ARGS)
116116
PG_RETURN_OID(ranges[pos].child_oid);
117117
else
118118
{
119-
intret;
120-
Datumvals[4];
121-
Oidoids[]= {OIDOID,value_type,value_type,value_type};
122-
boolnulls[]= {false, false, false, false};
123-
RangeEntry*re=&ranges[rangerel->ranges.length-1];
124-
intcmp_upper=FunctionCall2(&cmp_func,value,ranges[rangerel->ranges.length-1].max);
125-
intcmp_lower=FunctionCall2(&cmp_func,value,ranges[0].min);
126-
char*sql;
127-
128-
/* Lock relation before appending new partitions */
119+
Oidchild_oid;
120+
121+
/* Lock config before appending new partitions */
129122
LWLockAcquire(load_config_lock,LW_EXCLUSIVE);
130123

131124
/*
@@ -139,45 +132,22 @@ find_or_create_range_partition(PG_FUNCTION_ARGS)
139132
PG_RETURN_OID(ranges[pos].child_oid);
140133
}
141134

142-
/* Determine nearest range partition */
143-
if (cmp_upper>0)
144-
re=&ranges[rangerel->ranges.length-1];
145-
elseif (cmp_lower<0)
146-
re=&ranges[0];
147-
148-
vals[0]=ObjectIdGetDatum(relid);
149-
vals[1]=re->min;
150-
vals[2]=re->max;
151-
vals[3]=value;
152-
153-
/* Create new partitions */
154-
SPI_connect();
155-
sql=psprintf("SELECT %s.append_partitions_on_demand_internal($1, $2, $3, $4)",
156-
get_extension_schema());
157-
ret=SPI_execute_with_args(sql,4,oids,vals,nulls, false,0);
158-
// ret = SPI_execute_with_args("SELECT append_partitions_on_demand_internal($1, $2, $3, $4)",
159-
// 4, oids, vals, nulls, false, 0);
160-
if (ret>0)
161-
{
162-
/* Update relation info */
163-
free_dsm_array(&rangerel->ranges);
164-
free_dsm_array(&prel->children);
165-
load_check_constraints(relid,GetCatalogSnapshot(relid));
166-
}
167-
else
168-
elog(WARNING,"Attempt to create new partitions failed");
135+
/* Start background worker to create new partitions */
136+
child_oid=create_partitions_bg_worker(relid,value,value_type);
169137

170-
SPI_finish();
138+
// SPI_connect();
139+
// child_oid = create_partitions(relid, value, value_type);
140+
// SPI_finish();
141+
// elog(WARNING, "Worker finished");
171142

172-
/* Releaselocks */
143+
/* Releaselock */
173144
LWLockRelease(load_config_lock);
174-
// pfree(sql);
175145

176146
/* Repeat binary search */
177147
ranges=dsm_array_get_pointer(&rangerel->ranges);
178148
pos=range_binary_search(rangerel,&cmp_func,value,&found);
179149
if (found)
180-
PG_RETURN_OID(ranges[pos].child_oid);
150+
PG_RETURN_OID(child_oid);
181151
}
182152

183153
PG_RETURN_NULL();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp