天天看點

PostGIS 地理資訊資料 多核并行處理

postgresql , postgis , 栅格 , raster , 多核并行

自從postgresql 9.6支援cpu多核并行計算後,postgresql最流行的插件之一postgis,使用者對多核需求也越來越多。

原因是postgis中有大量的運算是非常耗費cpu資源的,比如raster類型相關的運算。

postgis tends to involve cpu-intensive calculations on geometries, support for parallel query has been at the top of our request list to the core team for a long time.

now that it is finally arriving the question is: does it really help?

postgis 釋出的2.3.1 版本,已經可以看到誠意了吧,以下函數已經支援并行計算了。

mark st_extent, st_3dextent and st_mem* agg functions as parallel safe so they can be parallelized

PostGIS 地理資訊資料 多核并行處理

掃描并行,比如掃描節點有大量資料要被過濾時,或者說查詢子句中有大量的運算時,使用并行可以大大提升其效率。

例如

1. filter過濾掉大量的資料,并且filter本身運算量較大時,使用cpu多核并行,效果明顯

2. 當func函數或者op操作符運算量較大時,使用cpu多核并行,效果非常明顯

比如聚合運算,或者一些業務邏輯運算(雖然table本身沒幾條記錄,但是每條記錄的運算耗時很長時,并行效果明顯)。

我們在使用explain 觀察sql時,或者使用perf跟蹤sql的開銷時,對于一個多個表資料join的sql,如果join的資料量很大,可能就會成為整個sql的性能瓶頸。

現在可以使用cpu的多核并行來加速join了。

聚合操作,比如統計某個次元的平均值、最大、最小、sum等,在金融、分析行業用得非常多,處理的資料量大,運算量也較大。

除了掃描碼并行,聚合函數本身也要支援并行才行,比如sum,count, avg, 可以想象并行處理都是安全的。

應該這麼說,凡是在分布式資料庫中支援的2階段聚合函數,并行都是安全的。

關于分布式資料庫的2階段并行聚合的原理請參考

<a href="https://github.com/digoal/blog/blob/master/201608/20160825_02.md">《hll插件在greenplum中的使用 以及 分布式聚合函數優化思路》</a>

<a href="https://github.com/digoal/blog/blob/master/201305/20130502_01.md">《postgres-xc customized aggregate introduction》</a>

如果你要觀看并行效果,這樣設定一下就好了

postgresql.conf

table

關閉并行效果

參考

<a href="https://github.com/digoal/blog/blob/master/201610/20161002_01.md">《postgresql 9.6 并行計算 優化器算法淺析》</a>

<a href="https://github.com/digoal/blog/blob/master/201608/20160816_02.md">《postgresql 9.6 并行計算 優化器算法淺析》</a>

前面說的是幾種使用多核并行的應用場景,那麼對于gis資料,如何利用這些并行技術呢?

下面的例子取自

<a href="http://blog.cleverelephant.ca/2016/03/parallel-postgis.html">http://blog.cleverelephant.ca/2016/03/parallel-postgis.html</a>

1. 掃描并行

2. join并行

找出與藍色區域重疊的點

PostGIS 地理資訊資料 多核并行處理

update: marking the geometry_overlaps function which is bound to the &amp;&amp; operator as parallel safe allows postgresql to generate parallel join plans when the index is in place.

3. 聚合并行

以st_union為例。

不建議使用st_union(),因為它使用的是級聯union的方式,開啟并行并沒有效果,原因是需要使用memory copy, 聚合過程中,越到後面,耗費越大。

st_memunion 可以替代st_union,在并行中取得很好的效果

“fortunately” we have such an aggregate, the old union implementation from before we added “cascaded union”.

the “memory friendly” union saves memory by not building up the array of geometries in memory, at the cost of spending lots of cpu unioning each input geometry into the transfer state.

支援并行的st_memunion聚合函數如下

1. 自從postgresql 9.6支援并行後,由于postgresql開放了并行接口,比如聚合函數,使用并行時,會以兩階段方式運作,你需要增加一個合并函數。

周邊的插件,也可以很友善的将原有的聚合或者操作符,改造為并行的模式,進而享受postgresql多核并行帶來的效果。

2. 其他加速技術,包括llvm,列存儲,向量化,算子複用,gpu加速等。

<a href="https://github.com/digoal/blog/blob/master/201702/20170225_01.md">《postgresql 向量化執行插件(瓦片式實作) 10x提速olap》</a>

<a href="https://github.com/digoal/blog/blob/master/201612/20161216_01.md">《分析加速引擎黑科技 - llvm、列存、多核并行、算子複用 大聯姻 - 一起來開啟postgresql的百寶箱》</a>

<a href="https://github.com/pg-strom">https://github.com/pg-strom</a>

<a href="http://postgis.net/docs/rt_faq.html">http://postgis.net/docs/rt_faq.html</a>

<a href="https://trac.osgeo.org/postgis/wiki/wktraster">https://trac.osgeo.org/postgis/wiki/wktraster</a>

<a href="http://osgeo-org.1560.x6.nabble.com/parallel-support-td5258386.html">http://osgeo-org.1560.x6.nabble.com/parallel-support-td5258386.html</a>

<a href="https://github.com/pramsey/postgis/tree/parallel">https://github.com/pramsey/postgis/tree/parallel</a>

<a href="http://postgis.net/docs/manual-2.3/release_notes.html#idm40209">http://postgis.net/docs/manual-2.3/release_notes.html#idm40209</a>

繼續閱讀