hash partitioning based on v10Beta2

Started by yangjie@highgo.comover 9 years ago6 messageshackers
Jump to latest
#1yangjie@highgo.com
yangjie@highgo.com

Hi all,

Now we have had the range / list partition, but hash partitioning is not implemented yet.
Attached is a POC patch based on the v10Beta2 to add the hash partitioning feature.
Although we will need more discussions about the syntax and other specifications before going ahead the project,
but I think this runnable code might help to discuss what and how we implement this.

Description

The hash partition's implement is on the basis of the original range / list partition,and using similar syntax.

To create a partitioned table ,use:

CREATE TABLE h (id int) PARTITION BY HASH(id);

The partitioning key supports only one value, and I think the partition key can support multiple values,
which may be difficult to implement when querying, but it is not impossible.

A partition table can be create as bellow:

CREATE TABLE h1 PARTITION OF h;
CREATE TABLE h2 PARTITION OF h;
CREATE TABLE h3 PARTITION OF h;

FOR VALUES clause cannot be used, and the partition bound is calclulated automatically as partition index of single integer value.

An inserted record is stored in a partition whose index equals
DatumGetUInt32(OidFunctionCall1(lookup_type_cache(key->parttypid[0], TYPECACHE_HASH_PROC)->hash_proc, values[0])) % nparts /* Number of partitions */
;
In the above example, this is DatumGetUInt32(OidFunctionCall1(lookup_type_cache(key->parttypid[0], TYPECACHE_HASH_PROC)->hash_proc, id)) % 3;

postgres=# insert into h select generate_series(1,20);
INSERT 0 20
postgres=# select tableoid::regclass,* from h;
tableoid | id
----------+----
h1 | 3
h1 | 5
h1 | 17
h1 | 19
h2 | 2
h2 | 6
h2 | 7
h2 | 11
h2 | 12
h2 | 14
h2 | 15
h2 | 18
h2 | 20
h3 | 1
h3 | 4
h3 | 8
h3 | 9
h3 | 10
h3 | 13
h3 | 16
(20 rows)

The number of partitions here can be dynamically added, and if a new partition is created, the number of partitions changes, the calculated target partitions will change, and the same data is not reasonable in different partitions,So you need to re-calculate the existing data and insert the target partition when you create a new partition.

postgres=# create table h4 partition of h;
CREATE TABLE
postgres=# select tableoid::regclass,* from h;
tableoid | id
----------+----
h1 | 5
h1 | 17
h1 | 19
h1 | 6
h1 | 12
h1 | 8
h1 | 13
h2 | 11
h2 | 14
h3 | 1
h3 | 9
h3 | 2
h3 | 15
h4 | 3
h4 | 7
h4 | 18
h4 | 20
h4 | 4
h4 | 10
h4 | 16
(20 rows)

When querying the data, the hash partition uses the same algorithm as the insertion, and filters out the table that does not need to be scanned.

postgres=# explain analyze select * from h where id = 1;
QUERY PLAN
----------------------------------------------------------------------------------------------------
Append (cost=0.00..41.88 rows=13 width=4) (actual time=0.020..0.023 rows=1 loops=1)
-> Seq Scan on h3 (cost=0.00..41.88 rows=13 width=4) (actual time=0.013..0.016 rows=1 loops=1)
Filter: (id = 1)
Rows Removed by Filter: 3
Planning time: 0.346 ms
Execution time: 0.061 ms
(6 rows)

postgres=# explain analyze select * from h where id in (1,5);;
QUERY PLAN
----------------------------------------------------------------------------------------------------
Append (cost=0.00..83.75 rows=52 width=4) (actual time=0.016..0.028 rows=2 loops=1)
-> Seq Scan on h1 (cost=0.00..41.88 rows=26 width=4) (actual time=0.015..0.018 rows=1 loops=1)
Filter: (id = ANY ('{1,5}'::integer[]))
Rows Removed by Filter: 6
-> Seq Scan on h3 (cost=0.00..41.88 rows=26 width=4) (actual time=0.005..0.007 rows=1 loops=1)
Filter: (id = ANY ('{1,5}'::integer[]))
Rows Removed by Filter: 3
Planning time: 0.720 ms
Execution time: 0.074 ms
(9 rows)

postgres=# explain analyze select * from h where id = 1 or id = 5;;
QUERY PLAN
----------------------------------------------------------------------------------------------------
Append (cost=0.00..96.50 rows=50 width=4) (actual time=0.017..0.078 rows=2 loops=1)
-> Seq Scan on h1 (cost=0.00..48.25 rows=25 width=4) (actual time=0.015..0.019 rows=1 loops=1)
Filter: ((id = 1) OR (id = 5))
Rows Removed by Filter: 6
-> Seq Scan on h3 (cost=0.00..48.25 rows=25 width=4) (actual time=0.005..0.010 rows=1 loops=1)
Filter: ((id = 1) OR (id = 5))
Rows Removed by Filter: 3
Planning time: 0.396 ms
Execution time: 0.139 ms
(9 rows)

Can not detach / attach / drop partition table.

Best regards,
young

https://yonj1e.github.io/

Attachments:

t37305_1
hash_part_on_beta2_v1.patchapplication/octet-stream; name=hash_part_on_beta2_v1.patchDownload+806-16
#2Rushabh Lathia
rushabh.lathia@gmail.com
In reply to: yangjie@highgo.com (#1)
Re: hash partitioning based on v10Beta2

On Sat, Aug 26, 2017 at 10:10 AM, yangjie@highgo.com <yangjie@highgo.com>
wrote:

Hi all,

Now we have had the range / list partition, but hash partitioning is not
implemented yet.
Attached is a POC patch based on the v10Beta2 to add the
hash partitioning feature.
Although we will need more discussions about the syntax
and other specifications before going ahead the project,
but I think this runnable code might help to discuss
what and how we implement this.

FYI, there is already an existing commitfest entry for this project.

https://commitfest.postgresql.org/14/1059/

Description

The hash partition's implement is on the basis of
the original range / list partition,and using similar syntax.

To create a partitioned table ,use:

CREATE TABLE h (id int) PARTITION BY HASH(id);

The partitioning key supports only one value, and I think
the partition key can support multiple values,
which may be difficult to implement when querying, but
it is not impossible.

A partition table can be create as bellow:

CREATE TABLE h1 PARTITION OF h;
CREATE TABLE h2 PARTITION OF h;
CREATE TABLE h3 PARTITION OF h;

FOR VALUES clause cannot be used, and the partition bound
is calclulated automatically as partition index of single integer value.

An inserted record is stored in a partition whose index equals
DatumGetUInt32(OidFunctionCall1(lookup_type_cache(key->parttypid[0],
TYPECACHE_HASH_PROC)->hash_proc, values[0])) % nparts
/* Number of partitions */
;
In the above example, this is DatumGetUInt32(OidFunctionCall1(lookup_type_
cache(key->parttypid[0], TYPECACHE_HASH_PROC)->hash_proc, id)) % 3;

postgres=# insert into h select generate_series(1,20);
INSERT 0 20
postgres=# select tableoid::regclass,* from h;
tableoid | id
----------+----
h1 | 3
h1 | 5
h1 | 17
h1 | 19
h2 | 2
h2 | 6
h2 | 7
h2 | 11
h2 | 12
h2 | 14
h2 | 15
h2 | 18
h2 | 20
h3 | 1
h3 | 4
h3 | 8
h3 | 9
h3 | 10
h3 | 13
h3 | 16
(20 rows)

The number of partitions here can be dynamically added, and
if a new partition is created, the number of partitions
changes, the calculated target partitions will change,
and the same data is not reasonable in different
partitions,So you need to re-calculate the existing data
and insert the target partition when you create a new partition.

postgres=# create table h4 partition of h;
CREATE TABLE
postgres=# select tableoid::regclass,* from h;
tableoid | id
----------+----
h1 | 5
h1 | 17
h1 | 19
h1 | 6
h1 | 12
h1 | 8
h1 | 13
h2 | 11
h2 | 14
h3 | 1
h3 | 9
h3 | 2
h3 | 15
h4 | 3
h4 | 7
h4 | 18
h4 | 20
h4 | 4
h4 | 10
h4 | 16
(20 rows)

When querying the data, the hash partition uses the same
algorithm as the insertion, and filters out the table
that does not need to be scanned.

postgres=# explain analyze select * from h where id = 1;
QUERY PLAN

------------------------------------------------------------
----------------------------------------
Append (cost=0.00..41.88 rows=13 width=4) (actual time=
0.020..0.023 rows=1 loops=1)
-> Seq Scan on h3 (cost=0.00..41.88 rows=13 width=4) (
actual time=0.013..0.016 rows=1 loops=1)
Filter: (id = 1)
Rows Removed by Filter: 3
Planning time: 0.346 ms
Execution time: 0.061 ms
(6 rows)

postgres=# explain analyze select * from h where id in (1,5);;
QUERY PLAN

------------------------------------------------------------
----------------------------------------
Append (cost=0.00..83.75 rows=52 width=4) (actual time=
0.016..0.028 rows=2 loops=1)
-> Seq Scan on h1 (cost=0.00..41.88 rows=26 width=4) (
actual time=0.015..0.018 rows=1 loops=1)
Filter: (id = ANY ('{1,5}'::integer[]))
Rows Removed by Filter: 6
-> Seq Scan on h3 (cost=0.00..41.88 rows=26 width=4) (
actual time=0.005..0.007 rows=1 loops=1)
Filter: (id = ANY ('{1,5}'::integer[]))
Rows Removed by Filter: 3
Planning time: 0.720 ms
Execution time: 0.074 ms
(9 rows)

postgres=# explain analyze select * from h where id = 1 or id = 5;;
QUERY PLAN

------------------------------------------------------------
----------------------------------------
Append (cost=0.00..96.50 rows=50 width=4) (actual time=
0.017..0.078 rows=2 loops=1)
-> Seq Scan on h1 (cost=0.00..48.25 rows=25 width=4) (
actual time=0.015..0.019 rows=1 loops=1)
Filter: ((id = 1) OR (id = 5))
Rows Removed by Filter: 6
-> Seq Scan on h3 (cost=0.00..48.25 rows=25 width=4) (
actual time=0.005..0.010 rows=1 loops=1)
Filter: ((id = 1) OR (id = 5))
Rows Removed by Filter: 3
Planning time: 0.396 ms
Execution time: 0.139 ms
(9 rows)

Can not detach / attach / drop partition table.

Best regards,
young

------------------------------

https://yonj1e.github.io/

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

--
Rushabh Lathia

#3yangjie@highgo.com
yangjie@highgo.com
In reply to: yangjie@highgo.com (#1)
Re: hash partitioning based on v10Beta2

On Sat, Aug 26, 2017 at 10:10 AM, yangjie@highgo.com <yangjie@highgo.com>
wrote:

Hi all,

Now we have had the range / list partition, but hash partitioning is not
implemented yet.
Attached is a POC patch based on the v10Beta2 to add the
hash partitioning feature.
Although we will need more discussions about the syntax
and other specifications before going ahead the project,
but I think this runnable code might help to discuss
what and how we implement this.

FYI, there is already an existing commitfest entry for this project.

https://commitfest.postgresql.org/14/1059/

Description

The hash partition's implement is on the basis of
the original range / list partition,and using similar syntax.

To create a partitioned table ,use:

CREATE TABLE h (id int) PARTITION BY HASH(id);

The partitioning key supports only one value, and I think
the partition key can support multiple values,
which may be difficult to implement when querying, but
it is not impossible.

A partition table can be create as bellow:

CREATE TABLE h1 PARTITION OF h;
CREATE TABLE h2 PARTITION OF h;
CREATE TABLE h3 PARTITION OF h;

FOR VALUES clause cannot be used, and the partition bound
is calclulated automatically as partition index of single integer value.

An inserted record is stored in a partition whose index equals
DatumGetUInt32(OidFunctionCall1(lookup_type_cache(key->parttypid[0],
TYPECACHE_HASH_PROC)->hash_proc, values[0])) % nparts
/* Number of partitions */
;
In the above example, this is DatumGetUInt32(OidFunctionCall1(lookup_type_
cache(key->parttypid[0], TYPECACHE_HASH_PROC)->hash_proc, id)) % 3;

postgres=# insert into h select generate_series(1,20);
INSERT 0 20
postgres=# select tableoid::regclass,* from h;
tableoid | id
----------+----
h1 | 3
h1 | 5
h1 | 17
h1 | 19
h2 | 2
h2 | 6
h2 | 7
h2 | 11
h2 | 12
h2 | 14
h2 | 15
h2 | 18
h2 | 20
h3 | 1
h3 | 4
h3 | 8
h3 | 9
h3 | 10
h3 | 13
h3 | 16
(20 rows)

The number of partitions here can be dynamically added, and
if a new partition is created, the number of partitions
changes, the calculated target partitions will change,
and the same data is not reasonable in different
partitions,So you need to re-calculate the existing data
and insert the target partition when you create a new partition.

postgres=# create table h4 partition of h;
CREATE TABLE
postgres=# select tableoid::regclass,* from h;
tableoid | id
----------+----
h1 | 5
h1 | 17
h1 | 19
h1 | 6
h1 | 12
h1 | 8
h1 | 13
h2 | 11
h2 | 14
h3 | 1
h3 | 9
h3 | 2
h3 | 15
h4 | 3
h4 | 7
h4 | 18
h4 | 20
h4 | 4
h4 | 10
h4 | 16
(20 rows)

When querying the data, the hash partition uses the same
algorithm as the insertion, and filters out the table
that does not need to be scanned.

postgres=# explain analyze select * from h where id = 1;
QUERY PLAN

------------------------------------------------------------
----------------------------------------
Append (cost=0.00..41.88 rows=13 width=4) (actual time=
0.020..0.023 rows=1 loops=1)
-> Seq Scan on h3 (cost=0.00..41.88 rows=13 width=4) (
actual time=0.013..0.016 rows=1 loops=1)
Filter: (id = 1)
Rows Removed by Filter: 3
Planning time: 0.346 ms
Execution time: 0.061 ms
(6 rows)

postgres=# explain analyze select * from h where id in (1,5);;
QUERY PLAN

------------------------------------------------------------
----------------------------------------
Append (cost=0.00..83.75 rows=52 width=4) (actual time=
0.016..0.028 rows=2 loops=1)
-> Seq Scan on h1 (cost=0.00..41.88 rows=26 width=4) (
actual time=0.015..0.018 rows=1 loops=1)
Filter: (id = ANY ('{1,5}'::integer[]))
Rows Removed by Filter: 6
-> Seq Scan on h3 (cost=0.00..41.88 rows=26 width=4) (
actual time=0.005..0.007 rows=1 loops=1)
Filter: (id = ANY ('{1,5}'::integer[]))
Rows Removed by Filter: 3
Planning time: 0.720 ms
Execution time: 0.074 ms
(9 rows)

postgres=# explain analyze select * from h where id = 1 or id = 5;;
QUERY PLAN

------------------------------------------------------------
----------------------------------------
Append (cost=0.00..96.50 rows=50 width=4) (actual time=
0.017..0.078 rows=2 loops=1)
-> Seq Scan on h1 (cost=0.00..48.25 rows=25 width=4) (
actual time=0.015..0.019 rows=1 loops=1)
Filter: ((id = 1) OR (id = 5))
Rows Removed by Filter: 6
-> Seq Scan on h3 (cost=0.00..48.25 rows=25 width=4) (
actual time=0.005..0.010 rows=1 loops=1)
Filter: ((id = 1) OR (id = 5))
Rows Removed by Filter: 3
Planning time: 0.396 ms
Execution time: 0.139 ms
(9 rows)

Can not detach / attach / drop partition table.

Best regards,
young

------------------------------

https://yonj1e.github.io/

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

--
Rushabh Lathia

Attachments:

18486_image008(08-26-11-51-49).pngimage/png; name="18486_image008(08-26-11-51-49).png"Download
#4Robert Haas
robertmhaas@gmail.com
In reply to: yangjie@highgo.com (#1)
Re: hash partitioning based on v10Beta2

On Sat, Aug 26, 2017 at 12:40 AM, yangjie@highgo.com <yangjie@highgo.com> wrote:

A partition table can be create as bellow:

CREATE TABLE h1 PARTITION OF h;
CREATE TABLE h2 PARTITION OF h;
CREATE TABLE h3 PARTITION OF h;

This syntax is very problematic for reasons that have been discussed
on the existing hash partitioning thread. Fortunately, a solution has
already been implemented... you're the third person to try to write a
patch for this feature.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#5yangjie@highgo.com
yangjie@highgo.com
In reply to: Robert Haas (#4)
Re: hash partitioning based on v10Beta2

<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
</head>
<body>
<style>
font{
line-height: 1.7;
}
</style>
<div style = 'font-family:"微软雅黑"; font-size: 14px; color:#000000; line-height:1.7;'>
<div>
<div><div class="input-wrap">
<div class="textarea-warp">Hi,</div><div class="textarea-warp"><br></div>
</div>
<div class="output-wrap">

<div class="text-output" id="text-output" style="top: 15px;"><span class="text-dst sentence-0">When the number of partitions and the data are more, adding new partitions, there will be some efficiency problems.</span></div></div></div>
<div><div class="output-wrap">

<div class="text-output" id="text-output" style="top: 15px;"><span class="text-dst sentence-0">I don't know how the solution you're talking about is how to implement a hash partition?</span></div></div></div>
<div><span><br></span></div>
<div id="ntes-pcmail-signature" style="font-family:'微软雅黑'">
<style type="text/css">
a#ntes-pcmail-signature-default:hover {
text-decoration: underline;
color: #199cff;
cursor: pointer;
}
a#ntes-pcmail-signature-default:active {
text-decoration: underline;
color: #246fce;
cursor: pointer;
}
</style>

<div style="font-size:14px; padding: 0; margin:0;">
<div style="font-family:&quot;微软雅黑&quot;; font-size: 14px; color:#000000">
<style>
font{
line-height: 1.7;
}
</style>
<div style="font-family: 微软雅黑; font-size: 14px;">
<style>
font{
line-height: 1.7;
}
</style>
<div style="font-family: 微软雅黑; font-size: 14px;">
<style>
font{
line-height: 1.7;
}
</style>
<div style="font-size: 14px;">
<style>
font{
line-height: 1.7;
}
</style>
<div style="font-size: 14px;">
<style>
font{
line-height: 1.7;
}
</style>
<div style="font-size: 14px;">
<style>
font{
line-height: 1.7;
}
</style>
<div style="font-size: 14px;">
<style>
font{
line-height: 1.7;
}
</style>
<div id="ntes-pcmail-signature-default" style="font-size: 14px; text-decoration: none;"><p class="MsoNormal" style="margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 23.8px;">---</p><p class="MsoNormal" style="margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 23.8px;">young</p><p class="MsoNormal" style="margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 23.8px;">HighGo Database:&nbsp;<a href="http://www.highgo.com">http://www.highgo.com</a></p></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

</div><br>
</div><div class="J-reply" style="background-color:#f2f2f2;color:black;padding-top:6px;padding-bottom:6px;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;margin-top:45px;margin-bottom:20px;font-family:'微软雅黑';">
<div style="font-size:14px;line-height:1.5;word-break:break-all;margin-left:10px;margin-right:10px">On <span class="mail-date">8/28/2017 22:25</span>,<a class="mail-to" style="text-decoration:none;color:#2a83f2;" href="mailto:robertmhaas@gmail.com">Robert Haas&lt;robertmhaas@gmail.com&gt;</a> wrote: </div>
</div>
<blockquote id="ntes-pcmail-quote" style="margin: 0; padding: 0; font-size: 14px; font-family: '微软雅黑';">
On&nbsp;Sat,&nbsp;Aug&nbsp;26,&nbsp;2017&nbsp;at&nbsp;12:40&nbsp;AM,&nbsp;yangjie@highgo.com&nbsp;&lt;yangjie@highgo.com&gt;&nbsp;wrote:
<br>&gt;&nbsp;A&nbsp;partition&nbsp;table&nbsp;can&nbsp;be&nbsp;create&nbsp;as&nbsp;bellow:
<br>&gt;
<br>&gt;&nbsp;&nbsp;CREATE&nbsp;TABLE&nbsp;h1&nbsp;PARTITION&nbsp;OF&nbsp;h;
<br>&gt;&nbsp;&nbsp;CREATE&nbsp;TABLE&nbsp;h2&nbsp;PARTITION&nbsp;OF&nbsp;h;
<br>&gt;&nbsp;&nbsp;CREATE&nbsp;TABLE&nbsp;h3&nbsp;PARTITION&nbsp;OF&nbsp;h;
<br>
<br>This&nbsp;syntax&nbsp;is&nbsp;very&nbsp;problematic&nbsp;for&nbsp;reasons&nbsp;that&nbsp;have&nbsp;been&nbsp;discussed
<br>on&nbsp;the&nbsp;existing&nbsp;hash&nbsp;partitioning&nbsp;thread.&nbsp;&nbsp;Fortunately,&nbsp;a&nbsp;solution&nbsp;has
<br>already&nbsp;been&nbsp;implemented...&nbsp;you're&nbsp;the&nbsp;third&nbsp;person&nbsp;to&nbsp;try&nbsp;to&nbsp;write&nbsp;a
<br>patch&nbsp;for&nbsp;this&nbsp;feature.
<br>
<br>--&nbsp;
<br>Robert&nbsp;Haas
<br>EnterpriseDB:&nbsp;http://www.enterprisedb.com
<br>The&nbsp;Enterprise&nbsp;PostgreSQL&nbsp;Company
<br></blockquote><!--😀-->
</div>
</body>
</html>

#6Robert Haas
robertmhaas@gmail.com
In reply to: yangjie@highgo.com (#5)
Re: hash partitioning based on v10Beta2

On Mon, Aug 28, 2017 at 10:44 PM, yangjie <yangjie@highgo.com> wrote:

When the number of partitions and the data are more, adding new partitions,
there will be some efficiency problems.
I don't know how the solution you're talking about is how to implement a
hash partition?

I am having difficulty understanding this. There was discussion on
the other thread of how splitting partitions could be done reasonably
efficiently with the proposed design; of course, it's never going to
be super-cheap.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers