Archive for the ‘Database’ Category
先来看一个场景:当一个用户要使用银行的转帐服务,他要从自己的卡上转一部分钱到另外的一张卡上,他希望这个操作是在自己的卡上减少金额,在另外的卡上增 加金额,这两个操作要么都发生,要么都不发生。但是一些软件和硬件的因素会影响这两个操作的正常发生,这就使系统存在不稳定状态的可能。
事务就是解决这种情况的方式,它可以保证软硬件发生故障是系统的稳定性。
一个事务是有下列属性的工作单元。
原子性(ATOMICITY):
一个事务要被完全的无二义性的做完或撤消。在任何操作出现一个错误的情况下,构成事务的所有操作的效果必须被撤消,数据应被回滚到以前的状态。
一致性(CONSISTENCY):
一个事务应该保护所有定义在数据上的不变的属性(例如完整性约束)。在完成了一个成功的事务时,数据应处于一致的状态。换句话说,一个事务应该把系统从一 个一致-状态转换到另一个一致状态。举个例子,在关系数据.......
More>>
PL/Proxy和PostgreSQL集群的结构关系可以用下图清楚地表示
以下操作是在三台不同机器上执行的情况,其中plproxy节点的机器名是P1,数据库节点的机器名分别是D1和D2。机器硬件配置如下,同时需要Linux-4.2、postgresql-8.3.0和plproxy-2.0.4,pgbouncer的安装过程略去。
plproxy节点:
hostname: P1
inet addr:10.0.0.1
OS: Linux 2.6.9-42.ELsmp
CPU:Intel(R) Xeon(R) CPU L5320 @ 1.86GHz
MemTotal: 514440 kB
node1节点:
hostname:D1
inet addr:10.0.0.2
OS: Linux 2.6.9-42.ELsmp
CPU:Intel(R) Xeon(R) CPU L5320 @ 1.86GHz
MemTotal: 254772 kB
node2节点:
hostname:D2
inet addr:10.0.0.3
OS: Linux 2.6.9-42.ELsmp
CPU:Intel(R) Xeon(R) CPU L5320 @ 1.86GHz
MemTotal: 254772 kB
1. 在P1, D1,D2上安装postgresql-8.3.0,并创建URTCluster数据库
## Compile and install
gunzip postgre.......
More>>
PostgreSQL Notes
PostgreSQL is an open source relational database. While PostgreSQL doesn’t support stored procedures in the same sense that Sybase, MS SQL Server or Oracle does, it does support functions that can return sets of records.
While there is documentation saying how to do this, some of it is a little hard to find. Also I couldn’t find any examples anywhere on the web. So hopefully this page will help others that want to do the same thing.
I’m not sure about other people, but I find that an example of how to do something is often much easier to understand and follow than an explination of how something could be done. So the example that I am showing here is from my own code.
The Example
The context for the e.......
More>>
Examples:
Simple echo example:
CREATE OR REPLACE FUNCTION echo() RETURNS integer AS ’
echo ”Somethingn”;
return 0;
‘ LANGUAGE ’plphp’;
A more complicated Array example:
CREATE OR REPLACE FUNCTION insertIntoArray() RETURNS varchar AS ’
function array_insert(&$array, $value, $pos)
{
if (!is_array($array))
return FALSE;
$last = array_splice($array, $pos);
array_push($array, $value);
$array = array_merge($array, $last);
}
function array_2string($array)
{
$vals=array_values($array);
$keys=array_keys($array);
$x=0;
while($x<count($keys))
{
if ($x!=0)
{
$result.=”|”;
}
$result.=$keys[$x].”=”.$vals[$x];
$x++;
}
return $result;
}
$a .......
More>>
Hello,
Beta 3 of plPHP has been released with full trigger support for
PostgreSQL.
PL/PHP - PHP Procedural Language
Copyright 2003 Command Prompt, Inc.
http://www.commandprompt.com/
+1 503 222 2783 info@commandprompt.com
1. PL/PHP language installation notes
2. PL/PHP functions and arguments
3. Data values in PL/PHP
4. Database Access from PL/PHP
5. Trigger Procedures in PL/PHP
6. Trusted and Untrusted PL/PHP
7. PL/PHP Procedure Names
8. Missing features
PL/PHP is a loadable procedural language that enables you to write
PostgreSQL functions
in the PHP programming language.
1. PL/PHP language installation notes
To install PL/PHP language Postgres DBA should have compiled shared
library plphp.so i.......
More>>
postgres的record类型只是一个虚拟的东西,它并没有实际的存储空间,因此结果集合的返回一定要依靠一种实在的表的结构,你或者在定义
函数时候返回一个表或者视图或者象下面的例子一样在调用函数的时候
后面跟着一个表的结构。
说明:表和视图创建成功以后在postgres的类型系统表中会自动增加
以他们的名字命名的类型),也就是说某个表和某个视图其实也是一
种数据类型。
create table mm(a int, b int);
insert into mm values(1,1);
insert into mm values(2,2);
insert into mm values(3,3);
create or replace function mmm() returns setof record as
‘
declare
rec record;
begin
FOR rec IN SELECT * FROM mm LOOP
RETURN NEXT rec;
END LOOP;
end;’language ’plpgsql’;
select a.a,a.b from mmm() a(a int, b int);
a | b
.......
More>>
