07 2013 档案
摘要:接前文:初步学习pg_control文件之五,DB_IN_ARCHIVE_RECOVERY何时出现?看代码:如果recovery.conf文件存在,则返回InArchiveRecovery = true。/* * See if there is a recovery command file (recovery.conf), and if so * read in parameters for archive recovery and XL...
阅读全文
摘要:接前文初步学习pg_control文件之四,继续看何时出现 DB_IN_CRASH_RECOVERY:看下面代码就比较清楚了:如果对InArchiveRecovery 判断值为假,而且 读取出来pg_control文件的 state不是 SHUTDOWNED状态,表明当初没有来得及把SHUTDOWNED状态写入到pg_control文件,那么就是说系统已经崩溃了。/* * This must be called ONCE during postmaster or standalone-backend startup ...
阅读全文
摘要:接前文,初步学习pg_control文件之三继续分析何时出现 DB_SHUTDOWNING状态:在正常的shutdown的时候,需要进行checkpoint,所以就在此处,设置pg_control文件的state状态为DB_SHUTDOWNING。/* * Perform a checkpoint --- either during shutdown, or on-the-fly * * flags is a bitwise OR of the following: * CHECKPOINT_IS_SHUTDOWN: checkpoint is for database shutdown...
阅读全文
摘要:接前文,初步学习pg_control文件之二继续学习:研究 DBState,先研究DB_IN_PRODUCTION ,看它如何出现:它出现在启动Postmaster时运行的函数处:/* * This must be called ONCE during postmaster or standalone-backend startup */ void ...
阅读全文
摘要:接前文:初步认识pg_control文件继续学习,pg_control文件在何处形成的?是在initdb的时候,运用的函数如下:/* * This func must be called ONCE on system install. It creates pg_control * and the initial XLOG segment. */voidBootStrapXLOG(void){ CheckPoint checkPoint; char *buffer; XLogPageHeader page; XLogLongPageHeader lo...
阅读全文
摘要:这个据说是PostgreSQL的control file。到底如何呢,先看看改名后如何,把pg_control文件改名,然后启动 Postgres,运行时得到信息:[postgres@pg101 bin]$ postgres: could not find the database systemExpected to find it in the directory "/usr/local/pgsql/bin/../data",but could not open file "/usr/local/pgsql/bin/../data/global/pg_contr
阅读全文
摘要:客户的问题是:向Slony-I运行环境中,增加新的slaveDB节点的时候发生错误。log中反复出现错误,然后再重新开始(重新开始部分的log省略):CONFIG remoteWorkerThread_1: connected to provider DBCONFIG remoteWorkerThread_1: prepare to copy table "tst"."a_tbl"CONFIG remoteWorkerThread_1: prepare to copy table "tst"."b_tbl"CONF
阅读全文
摘要:http://slony.info/documentation/2.1/advanced.html#AEN14254.1.2. Event ConfirmationsWhen an event is processed by the slon process for a remote node, a CONFIRM message is generated byinserting a tuple into the sl_confirm table.This tuple indicates that a particular event has been confirmed by a parti
阅读全文
摘要:客户质询的现象是:Slony-I运行中,log中发现FATAL信息:FATAL storeListen: unknown node ID 3出现了上述错误后,再看后继的log,又恢复正常运行了。客户的问题在于:如何看待这个错误信息,它是否是设计上就是这样的?言外之意,这到底是否是一个bug?设计上是否是这样,是无从知晓的,只有问Vendor。而我的想法是,先分析源代码看看:/* ----------* SlonWatchdog* ----------*/static voidSlonWatchdog(void){ … slon_log(SLON_INFO, "slon: watchd
阅读全文
摘要:与Oracle不同的是,PostgreSQL中压根没有这种的东西。若以,如果因为写在线WAL文件是发生磁盘I/O错误,那么数据库系统就启动不了了。解决的办法,我想,在PostgreSQL中,如论如何都要启动Archive模式,否则会很惨。或者,把自己的cluster目录放在网络目录上,比如通过NFS,然后看能否把这个网络目录实际导向两个以上的目录。
阅读全文
摘要:写这篇文章,是想好心地给打算使用Pgpool的人提个醒:Pgpool 真的不适合在企业范围使用。我的主要理由是:设计陈旧: 一旦后台任何节点Down掉,都会引发failover,它会杀掉所有子进程,再重新创建子进程,在此过程中,所有事务不分青红皂白都被停止,实际上相当于被rollback。这一点引起很多次的很多客户的质疑。也许这是不得已而为之,因为它没有transaction manager。代码混乱: 因为项目的原因,多次深入到Pgpool-II的源代码中进行调查,发现代码写的很随意,注释很随意,而对是否输出日志,输出哪种类型的日志,到底是 DEBUG,还是INFO,或者...
阅读全文
摘要:因为Pgpool-II采用Master/Slave模式的时候,因偶然因素导致Pgpool-II与 SlaveDB之间的通信中断(L4SW主动切断)。而此时某事务向Master数据库提交commit已经成功,在向SlaveDB提交因通信中断而失败。这样,导致的结果是一方面向Master数据库提交成功,另一方面向前台程序返回出错信息的奇怪现象。(由于客户的 fail_over_on_backend_error为假,故没有发生failover)这是因为,首先,Pgpool-II中没有事务处理机制,或者说没有包含Transanction Manager,如果有,那么它可以利用数据库的两阶段提交能力,保
阅读全文
摘要:http://www.onlamp.com/pub/a/onlamp/2004/11/18/slony.html 我特别喜欢这篇文章,就进行了转载和翻译。Introducing SlonybyA. Elein MustainSlony is the Russian plural for elephant. It is also the name of the new replication project being developed by Jan Weick. The mascot for Slony, Slon, is a good variation of the usual Post
阅读全文
摘要:我的看法是:日志一定要能够起到提供切实有效的信息的作用。如果作为原有产品开发维护人员,只要客户给你打个电话,报一下日志中的错误、警告号码或信息,你立即就可以知道具体哪个模块的哪个函数出错了,这是最好的。没有产品是完美的,一个可以很快定位错误便于修正的产品是一个相对理想的选择。那种产品卖出去就拉倒了,bug定位很慢,更新补丁不及时的公司,客户在暗叹倒霉的同时,会很生气的,后果会很严重!我的建议是这样:例如A模块调用B模块,B模块调用C模块A模块中日志信息输出格式(假定为log级别,而不是error或fatal级别):log_A_001log_A_002log_A_003...B模块中日志信息输出
阅读全文
摘要:客户来邮件,问到:为何我们所用的软件产品,输出日志中有FATAL:xxxx 之类的,然后反复发生对同一模块调用,直到成功为止。那么,这个软件当初的设计就是这样的吗?言外之意,它是一个Bug吗?在我看来,一个产品,无论它是开源的,还是封闭的商业软件,都需要注意一个问题:对于挑剔的客户而言,不要说FATAL或ERROR字眼,就是日志中有WARNING,客户也会不放心,也有可能引发疑虑呢。解决的方法:要么尽量不要在编程的时候太过随意,开发人员不能想输出什么就胡乱用FATAL/ERROR/PANIC/WARNING之类的标记。对信息分类设定严格标准,并建立一套简明实用的可以Guideline,从客户运
阅读全文
摘要:停止模式分为: smart, fast, immediate ;分别对应着: SIGTERM, SIGINT, SIGQUIT 信号当我采用 fast模式无法关机的时候,可以使用如下办法:kill -s SIGINT
阅读全文
摘要:磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL集群方案相关索引页 回到顶级页面:PostgreSQL索引页接前面例子,简单的Slony-I设置实例这次我增加一台机器C: 192.168.10.100,我尽量从该机器上发送slonik命令机器A和机器B启动之后:执行初始化cluster动作:[postgres@pg102 ~]$ cat setup.sh#!/bin/shCLUSTERNAME=testdb3_clusterMASTERDBNAME=testdb3SLAVEDBNAME=testdb3MASTERHOST=192.168.10.102SLAVEHO.
阅读全文
摘要:接前篇:http://www.cnblogs.com/gaojian/p/3196244.html简单的Slony-I设置实例如何删除slony相关的内容呢。我再另外找一台机器,安装slony,也安装postgresql(这个是为防止万一而已)编写脚本:[postgres@pg100 ~]$ cat remove.sh#!/bin/shCLUSTERNAME=testdb3_clusterMASTERDBNAME=testdb3SLAVEDBNAME=testdb3MASTERHOST=192.168.10.102SLAVEHOST=192.168.10.101REPLICATIONUSER=
阅读全文
摘要:磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL集群方案相关索引页 回到顶级页面:PostgreSQL索引页参考如下链接:http://lets.postgresql.jp/documents/technical/pgpool/4/这里我使用slony-I 2.1.0。下载地址如下:http://slony.info/downloads/2.1/source/安装过程:我有两台机器,机器A: 作为master数据库,IP地址: 192.168.10.102机器B: 作为slave数据库,IP地址: 192.168.10.101两台机器上都安装了Postgresq..
阅读全文
摘要:限制如下:http://slony.info/documentation/limitations.htmlSlony-I does not automatically replicate •Changes to large objects (BLOBS)•Changes made by DDL commands•Changes to users and roles•Changes made by the TRUNCATE command, though this might change in a future version of Slony-I其原因是:因为trigger自身所限The m
阅读全文
摘要:这一次,我用三台机器。其他步骤和中讲到的一样。http://www.cnblogs.com/gaojian/p/3195321.html只是,pg_dump 命令是在第三台机器上执行:[root@cmdsv ~]# ifconfigeth0 Link encap:Ethernet HWaddr 08:00:27:38:B7:DE inet addr:192.168.10.200 Bcast:192.168.10.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe38:b7de/64 S...
阅读全文
摘要:目前我有两台机器,分别已经安装了PPAS9.1,安装后建立了OS系统用户enterprisedb和数据库用户enterprisedb。机器1:master 192.168.10.88机器2: slave 192.168.10.99为了可以在机器1和机器2之间方便进行访问,设置pg_hba.conf如下:机器1和机器2都如此设置:[root@master ~]# su - enterprisedb-bash-3.2$ pwd/opt/PostgresPlus/9.1AS-bash-3.2$ cd data-bash-3.2$ cat pg_hba.conf# PostgreSQL Client
阅读全文
摘要:http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html39.3.4. Row 类型name table_name%ROWTYPE;name composite_type_name;一个复合类型被称为row变量(或row-类型变量)。这样的变量可以保持一整行当select或for 查询的结果,该查询的列集合正好匹配声明的变量的数据类型。此行值得单个域值可以通过通常的点引用来访问,例如:rowvar.field。通过使用table_name%ROWTYPE 标记法,一个row变量可以被声明为与现存的表或视图的行拥有同样
阅读全文
摘要:http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html39.3.3. 类型拷贝variable%TYPE%TYPE提供了变量或者表列的数据类型。你可以用这个来声明将持有这些数据库值得变量。例如,假定在你的users表中你有一列,名称为 user_id。为了定义一个和users.user_id拥有同样数据类型的数据类型,你可以写:user_id users.user_id%TYPE;通过使用%TYPE,你不需要知道你所参照的数据类型的结构,而且更为重要地,如果将来你所参照的数据Lexington变了(例如:你把use
阅读全文
摘要:http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html39.3.2. ALIASnewname ALIAS FOR oldname;ALIAS 语法比前面章节中提到的更为广泛使用:你可以给任何变量声明别名,而不仅仅是针对函数参数。在实践中用得比较多的是给变量赋予一个另外的预先定义好的名字,如在一个触发器过程中的NEW和OLD。例如:DECLARE prior ALIAS FOR old; updated ALIAS FOR new;这样 ALIAS 创建了两种不同同的方式去命名同一个对象,不加限制的使用会导致困..
阅读全文
摘要:http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html另外一种声明 PL/pgSQL 函数的方法是使用 returns table,例如:CREATE FUNCTION extended_sales(p_itemno int)RETURNS TABLE(quantity int, total numeric) AS $$BEGIN RETURN QUERY SELECT quantity, quantity * price FROM sales WHERE itemno = p...
阅读全文
摘要:建立函数:CREATE OR REPLACE FUNCTION enumtest(anyenum) RETURNS text AS$$SELECT $1::text;$$ LANGUAGE SQL;建立enum:pgsql=# CREATE TYPE ecolor AS ENUM ('white','green','blue');CREATE TYPEpgsql=# pgsql=# CREATE TYPE esize AS ENUM ('small','middle','big');CREATE T
阅读全文
摘要:https://wiki.postgresql.org/wiki/Enum建立enum:pgsql=# CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');CREATE TYPEpgsql=# 建表:pgsql=# CREATE TABLE person (pgsql(# name text,pgsql(# current_mood moodpgsql(# );CREATE TABLEpgsql=# 数据输入:pgsql=# INSERT INTO person VALUES ('Larry&#
阅读全文
摘要:http://www.postgresql.org/docs/current/static/extend-type-system.html#EXTEND-TYPES-POLYMORPHIC先看一个例子:CREATE OR REPLACE FUNCTION myappend01(anyelement, anyelement) RETURNS anyelement AS$$SELECT $1 || $2;$$ LANGUAGE SQL;执行(按 AnynonArray来传递输入参数):pgsql=# select myappend01('abc', 'def');E
阅读全文
摘要:程序:CREATE OR REPLACE FUNCTION kappend(anynonarray, anyelement) RETURNS text AS$$SELECT $1 || $2;$$ LANGUAGE SQL;调用方法(错误):pgsql=# select kappend('def','abc');ERROR: could not determine polymorphic type because input has type "unknown"pgsql=# 调用方法(正确):pgsql=# select kappend(&
阅读全文
摘要:http://www.joeconway.com/presentations/function_basics.pdfCREATE FUNCTION myappend(anyarray, anyelement) RETURNS anyarray AS$$SELECT $1 || $2;$$ LANGUAGE SQL;查询一:pgsql=# SELECT myappend(ARRAY[42,6], 21); myappend ----------- {42,6,21}(1 row)pgsql=# 查询二:pgsql=# SELECT myappend(ARRAY['abc',
阅读全文
摘要:建立表:CREATE TABLE sal_emp (name text,pay_by_quarter integer[],schedule text[][]);插入数据:INSERT INTO sal_emp VALUES ('Bill', ARRAY[10000, 11000, 9000, 13000], ARRAY[['meeting', 'lunch'], ['training', 'presentation']]);INSERT INTO sal_emp VALUES ('Carol', A
阅读全文
摘要:http://www.postgresonline.com/journal/archives/239-The-wonders-of-Any-Element.html定义函数pgsql=# CREATE OR REPLACE FUNCTION diff_inc(IN anyelement, IN anyelementpgsql(# , OUT diff integer, OUT f_val anyelement, OUT l_val anyelement)pgsql-# RETURNS recordpgsql-# ASpgsql-# $$pgsql$# BEGINpgsql$# dif...
阅读全文
摘要:实验如下:RETURNS TABLE 中的变量名和SQL文中的变量名同名时,执行时会出错:pgsql=# create table sales(itemno integer,quantity integer,price numeric);CREATE TABLEpgsql=# insert into sales values (100,15,11.2),(101,22,12.3);INSERT 0 2pgsql=# CREATE FUNCTION extended_sales(p_itemno int)pgsql-# RETURNS TABLE(quantity int, total nume
阅读全文
摘要:例子一,不带returns:postgres=# CREATE FUNCTION sum_n_product(x int, y int, OUT sum int, OUT prod int) AS $$postgres$# BEGINpostgres$# sum := x + y;postgres$# prod := x * y;postgres$# END;postgres$# $$ LANGUAGE plpgsql;CREATE FUNCTIONpostgres=# postgres=# select sum_n_product(3,4); sum_n_product --...
阅读全文
摘要:例子1,不带returns :[postgres@cnrd56 bin]$ ./psqlpsql (9.1.2)Type "help" for help.postgres=# CREATE FUNCTION sales_tax(subtotal real, OUT tax real) AS $$postgres$# BEGINpostgres$# tax := subtotal * 0.06;postgres$# END;postgres$# $$ LANGUAGE plpgsql;CREATE FUNCTIONpostgres=# postgres=# select sa
阅读全文
摘要:http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html如果一个PL/pgSQL函数声明了输出参数,输出参数被赋予$n名称和可选的别名,和正常输入参数的作法一样。输出参数是一个从NULL开始的变量;它将被在函数的执行过程中被赋值。此参数的最后的值就是函数的返回值。例如,the sales-tax 例子可以这样实现:例子:CREATE FUNCTION sales_tax(subtotal real, OUT tax real) AS $$BEGIN tax := subtotal * 0.06;END;$$ L...
阅读全文
摘要:实验过程如下:启动一个客户端:[postgres@cnrd56 bin]$ ./psqlpsql (9.1.2)Type "help" for help.postgres=# begin;BEGINpostgres=# select count(*), pg_sleep(1000) from test;然后强制停止:pg_ctl stop -m f -D /usr/local/pgsql/data此时在客户端出现如下信息:FATAL: terminating connection due to administrator commandThe connection to t
阅读全文
摘要:http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html39.3.1. 声明函数参数传递给函数的参数被用 $1、$2等依次类推的标志符命名。作为可选项,为了提高可读性,可以为$n 参数名称定义别名。此后,既可以用数字标志符也可以用别名来指代参数值。有两种方式来创建别名。推荐使用的方法是在CREATE FUNCTION命令你个中给参数一个名字,例如:CREATE FUNCTION sales_tax(subtotal real) RETURNS real AS $$BEGIN RETURN subtotal * ...
阅读全文
摘要:参考学习此文:http://blog.163.com/digoal@126/blog/static/163877040201173003547236/建库postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+------------...
阅读全文
摘要:cat /etc/redhat-release lsb_release -a
阅读全文
摘要:http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html39.3. 声明块中使用的所有的变量必须在块的声明节中进行声明。(唯一的例外是,子一个For循环中,在一个整数范围内轮询的循环变量被自动认为是整型变量,而只For循环中,轮询一个游标的变量被自动声明为记录变量。)PL/pgSQL 变量可以是任何SQL数据类型,如integer,varchar,还有char等。下面是变量声明的一些个例子:user_id integer;quantity numeric(5);url varchar;myrow tablename%
阅读全文
摘要:http://www.postgresql.org/docs/9.1/static/plpgsql-structure.html39.2. PL/pgSQL 的结构PL/pgSQL是一种块式结构的语言。完整的函数定义必须是一个块。一个块的定义形式如下:[ > ][ DECLARE declarations ]BEGIN statementsEND [ label ];在块中,每一个声明或语句都以分号结束。一个块出现在另外一个块中的时候,必须接 END;形式,如上图所示。但是包含函数体的最后一个END后,可以不接分号。注意:在BEGIN后不要接分号。当你需要在EXIT语句中使用一个块...
阅读全文
摘要:pgsql=# -- Multiline commentspgsql=# SELECT 'Multi' /* This comment extends acrosspgsql*# * numberous lines, and can bepgsql*# * /*nested safely */ pgsql*# Can you understand pgsql*# * Now the end*/pgsql-# || '-test' AS exa...
阅读全文
摘要:http://www.postgresql.org/docs/9.1/static/plpgsql-overview.html39.1.2. Supported Argument and Result Data Types用PL/pgSQL写的函数可以接受标量类型或者服务器支持的数组类型作为参数,它们可以返回前述的数据类型的结果。它们也能接受或返回通过名称指定的复杂类型(rwo type)。也可以声明一个PL/pgSQL函数返回 record类型,此时返回的结果是一个row type,它的列由调用它的查询所定义。PL/pgSQL 函数也可以被声明为返回一个任意数据类型的”集合“(或者表)。这样
阅读全文
摘要:我的例子:数据准备:create table custinfo(custid integer,callingcnt integer);insert into custoinfo valuse(1,10),(2,6),(3,8);函数生成:CREATE OR REPLACE FUNCTION get_callingcnt(custid int) RETURNS TABLE ( custid int ,callingcnt int ) AS$$BEGIN RETURN QUERY SELECT t.custid ,t.callingcnt FROM ...
阅读全文
摘要:从网上找到例子:可以说,RETURN NEXT要用在循环中:例子一:数据准备:CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT);INSERT INTO foo VALUES (1, 2, 'three');INSERT INTO foo VALUES (4, 5, 'six');生成函数:CREATE OR REPLACE FUNCTION getAllFoo() RETURNS SETOF foo AS$BODY$DECLARE r foo%rowtype;BEGIN FOR r IN SELEC
阅读全文
摘要:39.1.1 使用 PL/pgSQL的好处SQL是 PostgreSQL和其他大多数关系型数据库作为查询语言而使用的语言。它可移植,并容易学习。但是SQL语句必须被数据库服务器逐条地执行。这意味着你的客户应用必须向数据库发送每一条查询,等待它被处理,接收并处理结果,进行一些计算,然后再依次类推送出更多的查询。所有这些导致进程间通信,如果你的客户程序和数据库服务器位于不同的机器上,网络负载也会大量增加。使用 PL/pgSQL, 你可以把一个计算块和一系列的数据库服务器内部的查询组合到一起,这样保持了过程语言的威力,也发挥了SQL容易使用的特点,并节省了客户端与服务器间的通信量。客户端和服务器端的
阅读全文
摘要:开始资料来源:http://www.postgresql.org/docs/9.1/static/plpgsql-overview.html39.1 概要:PL/pgSQL是一种可载入的过程语言,它的设计目标:可以用来创建函数和触发器过程,给SQL语言提供控制结构,能够完成复杂计算,能继承所有用户定义的类型、函数和操作符,定义为被服务器所信任,易于使用。使用PL/pgSQL创建的话函数可以像内置的函数一样被使用。例如,可以创建一个复杂的条件计算函数,而后在索引表达式中使用它,或者用其来定义操作符。在PostgreSQL 9.0及以后的版本,PL/pgSQL已经被缺省安装。但是它仍然是一个可载入
阅读全文
摘要:继续分析: make_postgres();展开:目的是创建postgres数据库。cmd是:/home/pgsql/project/bin/postgres" --single -F -O -c search_path=pg_catalog -c exit_on_error=true template1 >/dev/null/* * copy template1 to postgres */static voidmake_postgres(void){ PG_CMD_DECL; const char **line; static const char *postgre...
阅读全文
摘要:继续分析: make_template0();展开:无需再作解释,就是创建template0数据库/* * copy template1 to template0 */static voidmake_template0(void){ PG_CMD_DECL; const char **line; static const char *template0_setup[] = { "CREATE DATABASE template0;\n", "UPDATE pg_database SET " " datistemplate ...
阅读全文
摘要:继续分析: vacuum_db();展开:cmd是:/home/pgsql/project/bin/postgres" --single -F -O -c search_path=pg_catalog -c exit_on_error=true template1 >/dev/null其目的很明确,就是 对 template1 数据库执行ANALYZE;VACUUM FULL;VACUUM FREEZE;/* * clean everything up in template1 */static voidvacuum_db(void){ PG_CMD_DECL; fputs(.
阅读全文
摘要:继续分析 load_plpgsql();展开:就是让postgres 执行 create extension plpgsqlcmd是:"/home/pgsql/project/bin/postgres" --single -F -O -c search_path=pg_catalog -c exit_on_error=true template1 >/dev/nullok/* * load PL/pgsql server-side language */static voidload_plpgsql(void){ PG_CMD_DECL; fputs(_("
阅读全文
摘要:继续分析: setup_schema();展开:实质就是创建info_schema。cmd 是:"/home/pgsql/project/bin/postgres" --single -F -O -c search_path=pg_catalog -c exit_on_error=true -j template1 >/dev/null infor_schem_file是:/home/pgsql/project/share/information_schema.sql/* * load info schema and populate from features fi
阅读全文
摘要:继续分析: setup_privileges();展开:这是设置权限。其cmd是:"/home/pgsql/project/bin/postgres" --single -F -O -c search_path=pg_catalog -c exit_on_error=true template1 >/dev/null其命令是:UPDATE pg_class SET relacl = E'{"=r/\\"$POSTGRES_SUPERUSERNAME\\""}' WHERE relkind IN ('r&#
阅读全文
摘要:继续分析: setup_dictionary();展开:其中:cmd 是:"/home/pgsql/project/bin/postgres" --single -F -O -c search_path=pg_catalog -c exit_on_error=true -j template1 >/dev/nulldictionary_file 是:/home/pgsql/project/share/snowball_create.sql/* * load extra dictionaries (Snowball stemmers) */static voidsetu
阅读全文
摘要:继续分析: setup_conversion();展开:其实质是:运行命令:"/home/pgsql/project/bin/postgres" --single -F -O -c search_path=pg_catalog -c exit_on_error=true template1 >/dev/null传递参数:/home/pgsql/project/share/conversion_create.sql/* * load conversion functions */static voidsetup_conversion(void){ PG_CMD_DECL
阅读全文
摘要:继续分析: setup_collation()展开:/* * populate pg_collation */static voidsetup_collation(void){#if defined(HAVE_LOCALE_T) && !defined(WIN32) int i; FILE *locale_a_handle; char localebuf[NAMEDATALEN]; int count = 0; PG_CMD_DECL;#endif fputs(_("creating...
阅读全文
摘要:继续分析 setup_description();展开后:就是要把share/postgres.description 文件的内容读入到 pg_description 和 pg_shdescription/* * load description data */static voidsetup_description(void){ PG_CMD_DECL; fputs(_("loading system objects' descriptions ... "), stdout); fflush(stdout); snprintf(cmd, sizeof(cmd),.
阅读全文
摘要:继续分析: if (pwprompt || pwfilename) get_set_pwd();由于我启动initdb的时候,没有设置口令相关的选项,故此略过。接下来: setup_depend();展开:就是一组sql问,送给 postgres 执行:cmd的值是:"/home/pgsql/project/bin/postgres" --single -F -O -c search_path=pg_catalog -c exit_on_error=true template1 >/dev/null/* * set up pg_depend */static void
阅读全文
摘要:继续分析: /* * Make the per-database PG_VERSION for template1 only after init'ing it */ write_version_file("base/1");就是在base/1目录下,生成一个 PG_VERSION 文件。[pgsql@localhost 1]$ pwd/home/pgsql/DemoDir/base/1[pgsql@localhost 1]$ cat PG_VERSION9.1[pgsql@localhost 1]$ 接下来: /* Create the stuff we d...
阅读全文
摘要:继续分析: /* Bootstrap template1 */ bootstrap_template1();展开:我这里读入的文件是:/home/pgsql/project/share/postgres.bki/* * run the BKI script in bootstrap mode to create template1 */static voidbootstrap_template1(void){ PG_CMD_DECL; char **line; char *talkargs = ""; char **bki_l...
阅读全文
摘要:继续分析 /* Now create all the text config files */ setup_config();将其展开:实质就是,确定各种参数,分别写入 postgresql.conf 、pg_hba.conf、pg_indent.conf 文件。/* * set up all the config files */static voidsetup_config(void){ char **conflines; char repltok[100]; char path[MAXPGPATH]; fputs(...
阅读全文
摘要:继续分析: /* Top level PG_VERSION is checked by bootstrapper, so make it first */ write_version_file(NULL);就是建立了一个 PG_VERSION的文件在我系统里,可以看到:[pgsql@localhost DemoDir]$ cat PG_VERSION9.1[pgsql@localhost DemoDir]$ 接下来:我先看看 set_null_conf 函数 /* Select suitable configuration settings */ set_null_co...
阅读全文
摘要:继续分析,如下这段,因为条件不成立,被跳过: /* Create transaction log symlink, if required */ if (strcmp(xlog_dir, "") != 0) { fprintf(stderr,"In main function -----------------190\n"); char *linkloc; /* clean up xlog directory name, check it's absolute */ canonicalize_path(xlo...
阅读全文
摘要:继续:下面的是定义信号处理函数。 /* * now we are starting to do real work, trap signals so we can clean up */ /* some of these are not valid on Windows */#ifdef SIGHUP pqsignal(SIGHUP, trapsig);#endif#ifdef SIGINT pqsignal(SIGINT, trapsig);#endif#ifdef SIGQUIT pqsignal(SIGQUIT, trapsig);#endi...
阅读全文
摘要:[pgsql@localhost tst]$ cat sig01.c#include #include static voidtrapsigterm(int signum){ fprintf(stderr,"+++++++++++Got SIGTERM with %d\n",signum);}static voidtrapsigquit(int signum){ fprintf(stderr,"+++++++++++Got SIGQUIT with %d\n",signum);}int main(){ signal(SIGTERM,trapsigterm
阅读全文
摘要:继续分析由于 我并未进行特殊的参数设置,所以(strlen(default_text_search_config) == 0) 成立。故 调用 default_text_search_config = find_matching_ts_config(lc_ctype)最后输出:The default text search configuration will be set to "english". if (strlen(default_text_search_config) == 0) { default_text_search_config = find_mat...
阅读全文
摘要:继续分析:由于我使用initdb的时候,没有指定 locale,所以会使用OS的缺省locale,这里是 en_US.UTF-8 printf(_("The files belonging to this database system will be owned " "by user \"%s\".\n" "This user must also own the server process.\n\n"), effective_user); if (strcmp(lc_ctype, lc_collate) ==
阅读全文
摘要:继续分析下面的是获取运行此程序的用户名称,主要还是为了防止在linux下用root来运行的情形。 effective_user = get_id(); if (strlen(username) == 0) username = effective_user;接下来,是准备好一写预备生成的文件的名称变量: set_input(&bki_file, "postgres.bki"); set_input(&desc_file, "postgres.description"); set_input(&shdesc_file, "
阅读全文
摘要:接前面,继续分析:putenv("TZ=GMT") 设置了时区信息。find_other_exec(argv[0], "postgres", PG_BACKEND_VERSIONSTR, backend_exec))就是要找到同目录下、同版本的postgres备用。initdb 执行后期,很多事情要依赖 postgres来处理的。 /* * Also ensure that TZ is set, so that we don't waste time identifying the * system timezone each of the ma
阅读全文
摘要:继续分析: if (pwprompt && pwfilename) { fprintf(stderr, _("%s: password prompt and password file cannot be specified together\n"), progname); exit(1); } if (authmethod == NULL || !strlen(authmethod)) { authwarning = _("\nWARNING: enabling \"trust\" authentication for ...
阅读全文
摘要:继续其实接前面,整个while循环是这样的: while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:", long_options, &option_index)) != -1) { switch (c) { ...... } ...... }这一句,c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:", lo...
阅读全文
摘要:继续分析下面这一段,当 initdb --version 或者 initdb --help 才有意义。 if (argc > 1) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { usage(progname); exit(0); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { ...
阅读全文
摘要:开始第一段:intmain(int argc, char *argv[]){/* * options with no short version return a low integer, the rest return * their short version value */ static struct option long_options[] = { {"pgdata", required_argument, NULL, 'D'}, {"encoding", required_argument, NULL, 'E'
阅读全文
摘要:首先,建立表:pgsql=# create table tab10(id integer);CREATE TABLEpgsql=# select 147525::regclass; regclass ---------- tab10(1 row)pgsql=# 查看此时的文件信息:[pgsql@localhost 16384]$ pwd/home/pgsql/DemoDir/base/16384[pgsql@localhost 16384]$ ls -l 147525-rw------- 1 pgsql pgsql 0 Jul 4 13:45 147525[pgsql@localhost 1.
阅读全文
摘要:查看schema信息:[pgsql@localhost bin]$ ./psqlpsql (9.1.2)Type "help" for help.pgsql=# create schema gaosche;CREATE SCHEMApgsql=# select catalog_name,schema_name from information_schema.schemata ; catalog_name | schema_name --------------+-------------------- pgsql | pg_toast pgsql | ...
阅读全文
摘要:从逻辑上看,schema,table,都是位于database之下。首先,在postgres数据库下建立表(相当于建立在public schema下):[pgsql@localhost bin]$ ./psql -d postgrespsql (9.1.2)Type "help" for help.postgres=# create table tab200(id integer);CREATE TABLEpostgres=# \q然后,再在tester数据库下建立表(相当于建立在public schema下):[pgsql@localhost bin]$ ./psql -
阅读全文
摘要:db1=# \xExpanded display is on.db1=# SELECT * FROM information_schema.tables WHERE table_name='tab100';-[ RECORD 1 ]----------------+-----------table_catalog | db1table_schema | publictable_name | tab100table_type | BASE TABLEself_re...
阅读全文
摘要:开始:当前的tablesapce信息pgsql=# select * from pg_tablespace; spcname | spcowner | spclocation | spcacl | spcoptions ------------+----------+-----------------------+--------+------------ pg_default | 10 | | | pg_global | 10 | | ...
阅读全文
摘要:[pgsql@localhost bin]$ ./psql -d testerpsql (9.1.2)Type "help" for help.tester=# select current_database(); current_database ------------------ tester(1 row)tester=# http://www.postgresql.org/docs/7.3/static/functions-misc.html
阅读全文
摘要:[pgsql@localhost bin]$ ./psql -d postgrespsql (9.1.2)Type "help" for help.postgres=# \dNo relations found.postgres=# create table tab001(id integer);CREATE TABLEpostgres=# select pg_relation_filepath('tab001'); pg_relation_filepath ---------------------- base/12699/139312(1 row)pos
阅读全文
摘要:base目录,这是所有数据库目录的父目录。在base目录下第一层,每个目录就是一个数据库所对应的文件。那么如何知道哪个目录对应哪个数据呢?查询如下:先看数据库列表[pgsql@localhost bin]$ ./psqlpsql (9.1.2)Type "help" for help.pgsql=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+--------...
阅读全文
摘要:读取普通的table或者系统表,都会调用heap_open函数:/* ---------------- * heap_open - open a heap relation by relation OID * * This is essentially relation_open plus check that the relation * is not an index nor a composite type. (The caller should also * check that it's not a view or forei...
阅读全文
摘要:看了PostgreSQL的代码后,我觉得有不理解的地方,比如:例如这样的:/* Options that may appear after CATALOG (on the same line) */#define BKI_BOOTSTRAP#define BKI_SHARED_RELATION#define BKI_WITHOUT_OIDS#define BKI_ROWTYPE_OID(oid)#define BKI_SCHEMA_MACROCATALOG(pg_tablespace,1213) BKI_SHARED_RELATION{ NameData spcname; ...
阅读全文
摘要:先说 heap_insert 函数:/* * heap_insert - insert tuple into a heap * * The new tuple is stamped with current transaction ID and the specified * command ID. * * If the HEAP_INSERT_SKIP_WAL option is specified, the new tuple is not * logged in WAL, even for a non-temp relation. Safe usage of thi...
阅读全文
摘要:看例子:开两个终端来对比:在终端A:[pgsql@localhost bin]$ ./psqlpsql (9.1.2)Type "help" for help.pgsql=# begin;BEGINpgsql=# select xmin,xmax,cmin,cmax,* from tab01; xmin | xmax | cmin | cmax | id | cd ------+------+------+------+-----------+---- 1878 | 0 | 0 | 0 | 1 | 1 1884 | 0 | 0 | ...
阅读全文
摘要:xmaxThe identity (transaction ID) of the deleting transaction, or zero for an undeleted row version. It is possible for this column to be nonzero in a visible row version. That usually indicates that the deleting transaction hasn't committed yet, or that an attempted deletion was rolled back.htt
阅读全文