Posts

UPSERT/MERGE using Writable CTE in PostgreSQL 9.1

There is always discussion of having UPSERT/MERGE in postgresql. Since, oracle and other RDBMS has this feature, therefore people ask about this feature in PostgeSQL. In previous version of PostgreSQL, we used to implement it using functions. Now in PostgreSQL 9.1, user can implement this feature using Writable CTE. PostgreSQL 9.1, now has Writable CTE using WTH. WITH provides a way to write auxiliary statements for use in a larger query. These statements, which are often referred to as Common Table Expressions or CTEs, can be thought of as defining temporary tables that exist just for one query. Each auxiliary statement in a WITH clause can be a SELECT, INSERT, UPDATE, or DELETE; and the WITH clause itself is attached to a primary statement that can also be a SELECT, INSERT, UPDATE, or DELETE. Lets see how we can use Writable CTE for UPSERT. Following are SQL Code which can be use in Oracle and PostgreSQL for creating sample data: For oracle: create table myTable (pid numb

New Functions/Improvements in PostgreSQL 9.1

1. SQL function format(text, …):  This function is similar to the C function sprintf; However only the following conversion specifications are recognized: %s interpolates the corresponding argument as a string %I escapes its argument as an SQL identifier %L escapes its argument as an SQL literal %% outputs a literal %. A conversion can reference an explicit parameter position by preceding the conversion specifier with n$, where n is the argument position Some Examples are given below: postgres=# select format('%1$s %3$s', 1, 2, 3); format -------- 1 3 (1 row) postgres=# select format('Hello %s', 'World'); format ------------- Hello World (1 row) postgres=# select format('Hello %s %1$s %s', 'World', 'Hello again'); format ------------------------------- Hello World World Hello again (1 row) 2. New string functions concat(), concat_ws(), left(), right(), and reverse() (i). concat() function:

Some Object Information Functions improvements in PostgreSQL 9.1:

1. pg_describe_object:  In PostgreSQL 9.1, pg_describe_object function has been added. Using this function, user can get human readable string for understanding the pg_depend content as given below: postgres=# select classid, objid, objsubid, pg_describe_object(classid, objid, objsubid) from pg_depend where classid <> 0 and pg_describe_object(classi classid | objid | objsubid | pg_describe_object ---------+-------+----------+------------------------------------------------ 1247 | 16426 | 0 | type test_money 1247 | 16425 | 0 | type test_money[] 1259 | 16424 | 0 | table test_money 1259 | 16429 | 0 | index test_money_idx 2606 | 16431 | 0 | constraint uniq_constraint on table test_money 2. quote_all_identifiers (boolean) parameter:  With this parameter, User can force quoating of all identifiers in EXMPLAIN and in system catalog functions like pg_get_viewdef(). Example is given below: postgr

Client Applications Improvements in PostgreSQL 9.1

PostgreSQL 9.1 has come with new options and Improvement for Client Application. So, I thought to blog about those improvements.   Deprecated createlang/droplang: In PostgreSQL 9.1, createlang/dronlang is now deprecated commands. Since in PostgreSQL 9.1, for creating additional language, user has to use CREATE EXTENSION SYNTAX , there these binaries now issue command given below: CREATE EXTENSION plperl; CREATE EXTENSION plpyhthon; Added Features in psql: In PostgreSQL 9.1, following are the important improvement done in psql command:   1. Know about connection. Now psql has meta command \conninfo which gives detal about current connections. So, if you are connected to PostgreSQL database, then you can use command "\conninfo" to get detail about your connections, some example is given below: postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5432". postgres=# \conni

Asynchronous/Synchronous Streaming Replication in PostgreSQL 9.1

Since, PostgreSQL 9.1 has already been released and there are lot of new features added in it, so, I thought to blog about each features. Today, I am discussing about Synchronous and Asynchronous Replication supported in PostgreSQL 9.1. For Asynchronous Replication, user can use following method: 1. Change Following Parameters in postgresql.conf file of Primary Database: archive_command = cp -i %p /Users/postgres/archive/%f archive_mode = on max_wal_senders = 3 # Maximum 'wal_senders', processes responsible for managing a connection with a standby server wal_keep_segments = # How many WAL segments (=files) should be kept on the primary, whatever may happen (you won't have to copy them manually on the standby if the standby gets too far behind) wal_level = hot_standby 2. Make Following changes in pg_hba.conf host replication postgres [Ipv4 address of Standby Server]/32 trust host replication postgres [Ipv4 address of Master Server]/32 tr

Full Text Search in PostgreSQL 9.0

Today, I thought to share my Training slides on FTS (Full Text Search) in PostgreSQL 9.0. I have used this presentation on giving training. Enjoy :). Full Text Search Training

Implement Curo (Omniti-labs) tools (PostgreSQL Admins) using psql

Image
Depsez has Started a New project called Curo, which is currently made for Unix/Linux and is in initial phase. So, I thought to look at how it works and how normal user, who knows using psql, can benefit from it. Before Installing Curo, user has to make sure, he has postgreSQL client: psql and following package installed on his Linux/Unix System: 1. Bash 2. dialog If you have above required packages on your machine, then you can move for Curo installation. Curo Installation is very simple. Following are the steps which can be use to install Curo on Unix/Linux System: 1. Download the Curo from Following link: https://github.com/omniti-labs/curo Above link will give you file similar to given below: omniti-labs-curo-48a4f31.tar.gz 2. Untar the file using following command: tar -xvf omniti-labs-curo-48a4f31.tar.gz 3. Copy omniti-labs-curo-48a4f31 to users home directory as given below: cp omniti-labs-curo-48a4f31 ~/.curo 4. Now configure .inputrc and .editrc file as given