postgresql简介,PostgreSQL全文检索简介
朋友们,你是否曾想过深入了解postgresql简介和PostgreSQL全文检索简介的内涵?在本文中,我将为你详细解析这两个话题,希望能给你带来全新的视角和思考。
PostgreSQL服务器启动及关闭方法
1.启动数据库服务器(posgres用户):
[postgres@localhost bin]$ postgres-D/opt/postgresql/data/>/opt/postgresql/log/pg_server.log 2>&1&
[1] 4508
当然如果设置了环境变量
PGDATA=/opt/postgresql/data
export PGDATA
后,可使用pg_ctl工具进行启动:
[postgres@localhost log]$ pg_ctl start-l/opt/postgresql/log/pg_server.log
pg_ctl: another server might be running; trying to start server anyway
pg_ctl: could not start server
Examine the log output.
[postgres@localhost log]$
因为之前已经启动,所以打印“another server might be running”。此时,查看日志,有如下信息:
[postgres@localhost log]$ cat pg_server.log
FATAL: lock file"postmaster.pid" already exists
HINT: Is another postmaster(PID 4491) running in data directory"/opt/postgresql/data"?
[postgres@localhost log]$
当然,最简的.启动方式是:
[postgres@localhost~]$ pg_ctl start
server starting
[postgres@localhost~]$ LOG: database system was shut down at 2011-07-09 13:58:00 CST
LOG: autovacuum launcher started
LOG: database system is ready to accept connections
如果要在操作系统启动时就启动PG,可以在/etc/rc.d/rc.local文件中加以下语句:
/opt/postgresql/bin/pg_ctl start-l/opt/postgresql/log/pg_server.log-D/opt/postgresql/data
2.关闭服务器
最简单方法:
[postgres@localhost~]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
与Oracle相同,在关闭时也可采用不同的模式,简介如下:
SIGTERM
不再允许新的连接,但是允许所有活跃的会话正常完成他们的工作,只有在所有会话都结束任务后才关闭。这是智能关闭。
SIGINT
不再允许新的连接,向所有活跃服务器发送 SIGTERM(让它们立刻退出),然后等待所有子进程退出并关闭数据库。这是快速关闭。
SIGQUIT
令 postgres向所有子进程发送 SIGQUIT并且立即退出(所有子进程也会立即退出),而不会妥善地关闭数据库系统。这是立即关闭。这样做会导致下次启动时的恢复(通过重放 WAL日志)。我们推荐只在紧急的时候使用这个方法。
SIGKILL
此选项尽量不要使用,这样会阻止服务器清理共享内存和信号灯资源,那样的话你只能在启动服务器之前自己手工做这件事。另外,SIGKILL直接把 postgres杀掉,而不会等它把信号中继给它的子进程,因此我们还需要手工杀掉每个独立子进程。
使用方法举例:
[postgres@localhost~]$ pg_ctl stop-o SIGTERM
LOG: received smart shutdown request
LOG: autovacuum launcher shutting down
waiting for server to shut down....LOG: shutting down
LOG: database system is shut down
done
server stopped
[postgres@localhost~]$
最快速关闭方法:kill postgres进程
[postgres@localhost~]$ kill-INT `head-1/opt/postgresql/data/postmaster.pid`
[postgres@localhost~]$ LOG: received fast shutdown request
LOG: aborting any active transactions
LOG: autovacuum launcher shutting down
LOG: shutting down
LOG: database system is shut down
附:postgre启动后的进程,如下:
[postgres@localhost~]$ ps-ef|grep post
root 4609 4543 0 13:57 pts/2 00:00:00 su- postgres
postgres 4610 4609 0 13:57 pts/2 00:00:00-bash
postgres 4724 1 0 14:08 pts/2 00:00:00/opt/postgresql/bin/postgres
postgres 4726 4724 0 14:08? 00:00:00 postgres: writer process
postgres 4727 4724 0 14:08? 00:00:00 postgres: wal writer process
postgres 4728 4724 0 14:08? 00:00:00 postgres: autovacuum launcher process
postgres 4729 4724 0 14:08? 00:00:00 postgres: stats collector process
postgres 4752 4610 0 14:11 pts/2 00:00:00 ps-ef
postgres 4753 4610 0 14:11 pts/2 00:00:00 grep post
[postgres@localhost~]$
PostgreSQL全文检索简介
PostgreSQL自带有一个简易的全文检索引擎,可以实现小规模数据量的全文检索功能。本文我们将引导介绍一下这个功能,对于小数据量的搜索这个功能是足够使用的,而无需搭建额外的ES等重量级的全文检索服务器。
PG的全文检索操作符是@@,当一个 tsvector(文档)和 tsquery(条件)匹配时返回 true,并且前后顺序无影响:
和普通的SQL查询一样,只要在 WHERE条件中使用这个符号就代表使用全文检索条件筛选文档了。如:
@@操作符支持隐式转换,对于 text类型可以无需强类型转换(::tsvector或 to_tsvector(config_name, text)),所以这个操作符实际支持的参数类型是这样的:
tsquery查询条件并不是简单的正则,而是一组搜索术语,使用并且使用布尔操作符&(AND)、|(OR)和!(NOT)来组合它们,还有短语搜索操作符<->(FOLLOWED BY)。更详细的语法参见此文档。
此外,PostgreSQL还提供了两个相对简化的版本 plainto_tsquery和 phraseto_tsquery。
plainto_tsquery( plainto_tsquery([ config regconfig, ] querytext text) returns tsquery)用户将未格式化的 text经过分词之后,插入&符号转为 tsquery:
phraseto_tsquery( phraseto_tsquery([ config regconfig, ] querytext text) returns tsquery)行为和 plainto_tsquery行为类似,但是分词之后不是插入&而是<->(FOLLOWED BY):
使用索引可以加快全文检索的速度。对于全文检索来说,可选的索引类型是 GIN(通用倒排索引)和 GIST(通用搜索树),官方文档更推荐使用 GIN索引。创建一个 GIN索引的范例:
也可以是一个连接列:
还可以单独创建一个 tsvector列,为这个列创建索引:
除了普通的 ORDER BY条件之外,PostgreSQL为全文检索提供了两个可选的排序函数 ts_rank([ weights float4[], ] vector tsvector, query tsquery [, normalization integer ]) returns float4和 ts_rank_cd([ weights float4[], ] vector tsvector, query tsquery [, normalization integer ]) returns float4,以便实现基于权重的排序。
此外,对于PostgreSQL 9.6以上的版本还可以使用 RUM index排序。(注意,这个是扩展,默认不包含)。
PostgreSQL默认的分词字典中并不包含中文分词字典,因此我们必须手工引入。目前一个比较好的项目是 zhparser,同时这个插件也是阿里云的RDS默认包含的。安装和启用没什么好说的。值得一提的是分词配置参数。
在 CREATE EXTENSION之后,必须配置分词参数才能正确进行分词和查找,否则什么都查不到。官方文档提供的一个配置策略是:
n,v,a,i,e,l这几个字母分别表示一种token策略,只启用了这几种token mapping,其余则被屏蔽。具体支持的参数和含义可以用\dFp+ zhparser显示:
WITH simple表示词典使用的是内置的simple词典,即仅做小写转换。根据需要可以灵活定义词典和token映射,以实现屏蔽词和同义词归并等功能。
比如我们看下面这个例子:
可以看到江淮这个词组在查询的时候被忽略了,我们启用 j(abbreviation,简称)再看看结果:
所以实际使用中要设置合理的token types,过少将导致搜索结果不准确,过多将导致性能下降。此外,还有一些诸如短词复合: zhparser.multi_short= f这一类的控制分词结果的选项,根据实际使用酌情开启。
有没办法在postgreSQL中查询oracle上的数据
提供三个思路:
第一个:
PostgreSQL中,是可以创建各种语言写的Function的。
你可以用C写一个PostgreSQL的Function,
http://www.postgresql.org/docs/9.2/static/xfunc-c.html
在此Function中,可以试着去调用Oracle的C语言访问接口访问Oracle数据库
第二个:
与第一类似。PostgreSQL中,可以运行各种语言:如PL/pgSQL,PL/pgPerl等。
如果你安装了PL/Java
http://pljava.projects.pgfoundry.org/
http://www.slideshare.net/petereisentraut/postgresql-and-pljava
可考虑在PL/Java的Function中,通过Oracle的JDBC接口来访问Oracle。
下面的链接中非常明确地提到了一个在PostgreSQL的PL/java中访问Oracle的例子:
http://my.opera.com/myrkraverk/blog/2012/06/21/performing-sql-with-pl-java-in-postgresql
第三个:
采用DBI-Link
其简介说:
DBI-Link is a partial implementation of the SQL/MED portion of the SQL:2008 specification written in PL/Perl....
If you want to join Oracle tables from PostgreSQL on Debian GNU/Linux, you can use DBI-Link. Also, you can use PostgreSQL queries to access Oracle tables as local schemas.
DBI-Link,部分符合SQL:2008标准,
故此按照此标准你就可以在PostgreSQL中访问Oracle了。
如下的链接是在Debian Linux上的安装和使用例子:
http://www.techforce.com.br/news/linux_blog/dbi_link_to_oracle_for_postgresql_on_debian#.Ud49CflIjJA
END,本文到此结束,如果可以帮助到大家,还望关注本站哦!