PostgreSQL 天文插件 - pg_sphere

标签

PostgreSQL , 天文台 , pg_sphere , q3c , postgis


背景

在天文或GIS领域,找出某个位置附近的对象,或者找到两张表的位置互相关的数据,是非常普遍的需求。

在PostgreSQL中有很多方法可以实现以上需求。

比如

http://skyview.gsfc.nasa.gov/xaminblog/index.php/tag/pgsphere/

  • Haversine: Using the Haversine formula with tables clustered on an index in declination. A simple constraint on declination is included to make the index useful.
select count(*) from swiftdec a, xmmdec b
    where
a.dec between b.dec - .01 and b.dec + .01 and
( sin(radians(b.dec-a.dec)/2)^2 + cos(radians(a.dec))*cos(radians(b.dec))*sin(radians(b.ra-a.ra)/2)^2
      <
  sin(radians(.01)/2)^2
)
  • Unit Vector: Using the dot product of unit vectors with tables clustered on an index on Dec. A simple constraint on declination is included to make the index useful.
select count(*) from swiftunit a, xmmunit b
where
   a.dec between b.dec -.01 and b.dec+.01 and
   a.__x*b.__x + a.__y*b.__y + a.__z*b.__z > 0.999999984769129
  • PostGIS: Using ST_DWithin with tables where a PostGIS geography object has been added representing the RA/Dec of each row. Tables are clustered on an index using the geography object.
select count(*) from swiftgeog a, xmmgeog b
where
    st_dwithin(a.geog,b.geog,1111.950792, false)
  • PGSphere: Using the includes operator (~) in PGSphere with tables where an spoint object has been added representing the position of each row. Tables are clustered on an index on the spoint object.
select count(*) from swiftpgs a, xmmpgs b
where
    scircle(a.pnt, radians(.01))~b.pnt
  • Q3C: Using the q3c_join function in Q3C with tables clustered on an index on the q3c_ang2ipix(ra,dec). In this case no column was added to the table.
select count(*) from swiftq3c a, xmmq3c b
where q3c_join(a.ra,a.dec,b.ra,b.dec,.01)

Much greater reductions are possible using specialized indices. Of the three possibilities studied, Q3C has generally good performance, running about twice as fast the the PostGIS geography type.

The PGSphere library is very fast in doing point queries and but a bit slower than both Q3C and PostGIS for cross-correlations. The slight disagreement in cross-corrleation counts is a bit disconcerting but it’s possible that it is due to the kind of rounding issues we discovered in the positional queries.

For correlations all of the methods using spherical indexing seem to have some startup/caching cost. The second and subsequent iterations run about three times as fast as the first. The implication on an operational system are unclear and presumably depend critically upon the query mix.

For the positional queries PostGIS still shows strong evidence of caching, but PGSphere and Q3C do not. Note that the results for the positional queries are for an aggregate 162 queries. The averaged times for individual queries ranged from about 10-300 milliseconds.

Although one should be cautious extrapolating from any small number of tests, it does appear that spatial indices substantially improve performance. We see an improvement of somewhere between a factor of 2-15 in the time taken for queries.

Either Q3C or PostGIS seem like reasonable choices. Q3C gives the better performance and has a more transparent syntax for astronomers. However PostGIS has a much broader community and far greater certainty of continued support. PGSphere’s performance in positional queries is remarkably good but the lack of clear support and variance in results in the cross-correlation are worrying.

pgSphere

如上所述,pgSphere是一个天文相关的PostgreSQL数据库插件,包括针对天文数据新增的数据类型,操作符,函数等。

1. What is pgSphere?

pgSphere is an extra module for PostgreSQL which adds spherical data types.

It provides:

  • input and output of data
  • containing, overlapping, and other operators
  • various input and converting functions and operators
  • circumference and area of an object
  • spherical transformation
  • indexing of spherical data types
  • several input and output formats

Hence, you can do a fast search and analysis for objects with spherical attributes as used in geographical, astronomical, or other applications using PostgreSQL.

For instance, you can manage data of geographical objects around the world and astronomical data like star and other catalogs conveniently using an SQL interface.

The aim of pgSphere is to provide uniform access to spherical data.

Because PostgreSQL itself supports a lot of software interfaces, you can now use the same database with different utilities and applications.

3. Data types
3.1. Overview
3.2. Point
3.3. Euler transformation
3.4. Circle
3.5. Line
3.6. Ellipses
3.7. Path
3.8. Polygon
3.9. Coordinates range
4. Constructors
4.1. Point
4.2. Euler transformation
4.3. Circle
4.4. Line
4.5. Ellipse
4.6. Polygon
4.7. Path
4.8. Coordinates range
5. Operators
5.1. Casting
5.2. Equality
5.3. Contain and overlap
5.4. Crossing of lines
5.5. Distance
5.6. Length and circumference
5.7. Center
5.8. Change the direction
5.9. Turn the path of a line
5.10. Transformation
6. Functions
6.1. Area function
6.2. spoint functions
6.3. strans functions
6.4. scircle functions
6.5. sellipse functions
6.6. sline functions
6.7. spath functions
6.8. spoly functions
6.9. sbox functions
7. Create an index
7.1. Spherical index
8. Usage examples
8.1. General
8.2. Geographical
8.3. Astronomical

详见手册

http://pgsphere.projects.pgfoundry.org/index.html

安装

https://github.com/mnullmei/pgsphere/releases

wget https://github.com/mnullmei/pgsphere/archive/version-1-1-1-p3.tar.gz    

tar -zxvf version-1-1-1-p3.tar.gz    

cd pgsphere-version-1-1-1-p3    

export PATH=/home/digoal/pgsql9.4/bin:$PATH    

USE_PGXS=1 make clean    

USE_PGXS=1 make     

USE_PGXS=1 make install    

start database and then installcheck    

USE_PGXS=1 make crushtest

可能因为浮点精度的问题,会有几个CHECK不一定能通过

生成extension文件

pgSphere开发比较早,所以没有打包成extension。

稍作调整就可以了。

cd pgsphere-version-1-1-1-p3    

cp pg_sphere.sql pg_sphere--1.1.1.sql    

vi pg_sphere--1.1.1.sql
去除begin;commit;    

vi pg_sphere.control
comment = 'R-Tree implementation using GiST for spherical objects like spherical points and spherical circles with useful functions and operators.'
default_version = '1.1.1'
relocatable = true

将pg_sphere--1.1.1.sql和pg_sphere.control拷贝到$PGHOME/share/extension目录

将pg_sphere.so拷贝到$PGHOME/lib目录

然后就可以使用 create extension pg_sphere; 创建这个模块了

修改Makefile,略。

参考

http://pgsphere.projects.pgfoundry.org/index.html

http://skyview.gsfc.nasa.gov/xaminblog/index.php/tag/pgsphere/

https://github.com/mnullmei/pgsphere/tree/fixes-1-1-1

时间: 2024-11-30 16:17:23

PostgreSQL 天文插件 - pg_sphere的相关文章

阿里云RDS for PostgreSQL varbitx插件与实时画像应用场景介绍

标签 PostgreSQL , varbitx , 阿里云 , 实时画像 背景 PostgreSQL 内置的varbit, bit类型的操作函数比较简单,阿里云RDS for PostgreSQL对其进行了扩展. 支持更多的bit操作,可以覆盖更广的应用场景,例如实时用户画像推荐系统.门禁广告系统.购票系统等. 阿里云 varbitx 插件介绍 增加的函数接口如下 1. bit_count bit_count ( varbit, int, -- (0|1) int, -- (n) int --

PostgreSQL cube 插件 - 多维空间对象

标签 PostgreSQL , cube , GiST索引 , 多维 , 欧几里得 背景 CUBE是一个多维数据类型,支持两种多维类型:多维POINT.区间(左下+右上).以及这些几何对象的几何特性搜索和计算(方位搜索.距离计算),这些搜索都支持GiST索引. 我们甚至可以将多个字段合并成多维POINT,实现对大量数据的高效空间聚集.空间计算. 语法 External Syntax Meaning x 点,A one-dimensional point (or, zero-length one-

AliCloudDB for PostgreSQL pg_hint_plan插件的用法

使用rds_superuser登陆需要安装pg_hint_plan插件的数据库. postgres=> \du+ List of roles Role name | Attributes | Member of | Description --------------+------------------------------------------------+-----------+--------------- digoal | | {} | rds_superuser postgres

固若金汤 - PostgreSQL pgcrypto加密插件

背景 默认情况下数据都是以明文存储在数据库中的. 如果未使用数据传输层加密手段的话, 数据一旦在传输中被截获的话就很容易泄漏数据. 本文将要介绍的是数据内容的加密. 数据内容的加密可以在数据库服务端进行加解密, 也可以在客户端进行加解密. 在数据库服务端加解密的话, 网络上传输的还是未加密的内容, 所以为了防御网络数据截获, 建议还是要搭配使用数据传输加密. 如果是在客户端进行加解密的话, 网络上传输的是加密后的内容, 所以不必担心网络数据截获的问题. 下面以pgcrypto这个扩展包为例, 讲

PgSQL · 内核开发 · 如何管理你的 PostgreSQL 插件

一.背景 我们都知道 PostgreSQL 提供了丰富数据库内核编程的接口,允许开发者以插件的形式把功能融入数据库内核. PostgreSQL 提供了一个插件管理模块,用于管理用户创建的插件. 本文给大家介绍 PostgreSQL 插件管理模块,帮助大家管理自己的插件. 二.PostgreSQL的插件内容 通常一个 PostgreSQL 内核插件包括下面的部分 1. 包含功能的逻辑的动态库,即 so 文件. 2. 描述插件信息的的控制文件,即 control 文件. 3. 一组文件用于创建.更新

PostgreSQL SQL OUTLINE插件sr_plan (保存、篡改、固定 执行计划)

标签 PostgreSQL , sql plan outline , 执行计划篡改 , query rewrite , sr_plan , pg plan hint 背景 功能较为强大的数据库,通常都有query rewrite的功能,比如JOIN时提升或下推条件,调整JOIN顺序等. 例如 create table a(id int, info text); create table b(id int, info text); create index idx_a_info on a (inf

PostgreSQL 电商小需求 - 凑单商品的筛选

标签 PostgreSQL , 电商 , 凑单 , 最佳凑单 , 任意字段组合 背景 电商的促销活动非常多,规则可能比较复杂,要薅羊毛的话,数学可能要比较好才行.因此也出现了大量的导购网站,比如SMZDM. 但是实际上电商里面也有类似的应用,可以智能的分析买家的需求,根据买家的需求.已有的券.购物车,向用户推荐凑单品. 凑单的需求,本质上是多个字段组合搜索的需求. 1.购物车总金额 2.用户标签 3.用户优惠券 4.店铺活动标签 5.商品本身的多种标签 等. 根据规则计算出一些条件,根据这些条件

PostgreSQL 10 + PostGIS + Sharding(pg_pathman) + MySQL(fdw外部表) on ECS 部署指南(适合新用户)

标签 PostgreSQL , Linux , CentOS 背景 新用户部署PostgreSQL以及空间数据库插件PostGIS的指南. 内网环境RPM打包(可选项) 如果是内网环境,可以将包全部下载到本地再用rpm安装. 安装epel和postgresql yum rpm两个包后再执行: 1.使用yum-utils的yumdownloader下载需要的安装包,包括依赖包. yum install -y yum-utils yumdownloader --resolve --destdir=/

PostgreSQL 9种索引的原理和应用场景

标签 PostgreSQL , btree , hash , gin , gist , sp-gist , brin , bloom , rum , zombodb , bitmap 背景 PostgreSQL 拥有众多开放特性,例如 1.开放的数据类型接口,使得PG支持超级丰富的数据类型,除了传统数据库支持的类型,还支持GIS,JSON,RANGE,IP,ISBN,图像特征值,化学,DNA等等扩展的类型,用户还可以根据实际业务扩展更多的类型. 2.开放的操作符接口,使得PG不仅仅支持常见的类型