Thursday 24 November 2011

vacuum and analyze

1) How to Enable and Desable autovacuum on a table/toasttable.

--disable auto vacuum
ALTER TABLE my_table SET (
    autovacuum_enabled = FALSE, toast.autovacuum_enabled = FALSE
);
 
--enable auto vacuum
ALTER TABLE my_table SET (
    autovacuum_enabled = TRUE, toast.autovacuum_enabled = TRUE
);


To Change the auto-vacuum parameters at Table level
---------------------------------------------------
ALTER TABLE "PlayerEvents" SET (autovacuum_analyze_threshold = 2500, autovacuum_vacuum_threshold = 2500);


To Check the status weather at got applied thos changes are not use :
---------------------------------------------------------------------

For getting OID of Schema:
--------------------------
select nspname from pg_namespace ,pg_class where pg_namespace.oid = pg_class.relnamespace;

checking auto vacuum manual settings:
--------------------------------------------------------
SELECT c.relname, pg_catalog.array_to_string(c.reloptions || array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ') FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid) where c.relnamespace=62952 and c.relname ='PlayerEvents';



2)when the auto vacuum demon is ariseing for vacuum.
  autovacuum_starts=(autovacum_scale_factor*totale tuples of a table) + autovacuum_threshold_value

Eg:  total tuples of a emp table =1000
     av threshold =100
     av scal factor=0.2

then avstart=(1000*0.2)+100
            =200+100
            =300.
NOTE: when ever 300 updates/delete are performed on the table then only autovacuumed emp table.

ANALYZE operation also same.

Administrative queries in postgressql

1)Finding the table size

In bytes:
select relpages * 8192 as size_in_bytes
  from pg_class where relname = 'mytable';

(or)

postgres=# select schemaname,relname,pg_size_pretty(pg_relation_size(relid)) from pg_stat_user_tables;
 schemaname | relname | pg_size_pretty 
------------+---------+----------------
 public     | test1   | 72 kB
(1 row)

postgres=# 


In human-readable form:
select pg_size_pretty(relpages * 8192) as size
  from pg_class where relname = 'mytable';

2)Findout how many tuples and pages are used by a table
1page=8kb in postgresql.

select relname         as "table",
       reltuples       as "number of tuples",
       relpages        as "number of 8kb pages"
  from pg_class
where relname = 'my_table';
3)Find live and dead tuples of a table

select relname         as "table",
       n_live_tup      as "live tuples",
       n_dead_tup      as "dead tuples"
  from pg_stat_user_tables
where relname = 'my_table';

4)Findout when a table was last vacuumed and analysed

select relname         as "table",
       last_vacuum     as "last manual vacuum",
       last_autovacuum as "last auto vacuum",
       last_analyze    as "last manual analyze",
       last_autoanalyze as "last auto analyze"
  from pg_stat_user_tables
where relname = 'my_table';

Sunday 13 November 2011

psql prompt issue in postgres 9.0

[postgres@localhost ~]$ /opt/PostgreSQL/9.0/bin/psql -p 5432
psql.bin (9.0.5)
Type "help" for help.

postgres=# \l
*** glibc detected *** /opt/PostgreSQL/9.0/bin/psql.bin: realloc(): invalid next size: 0x000000000214c1e0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3116478158]
/lib64/libc.so.6[0x311647c441]
/lib64/libc.so.6(realloc+0x12e)[0x311647d2ce]
/opt/PostgreSQL/9.0/bin/../lib/libpq.so.5(enlargePQExpBuffer+0x66)[0x1271f6]
/opt/PostgreSQL/9.0/bin/../lib/libpq.so.5(appendPQExpBuffer+0xdb)[0x1273fb]
/opt/PostgreSQL/9.0/bin/psql.bin[0x417fe5]
/opt/PostgreSQL/9.0/bin/psql.bin[0x405e17]
/opt/PostgreSQL/9.0/bin/psql.bin[0x40db88]
/opt/PostgreSQL/9.0/bin/psql.bin[0x40fb2e]
/lib64/libc.so.6(__libc_start_main+0xfa)[0x311641e32a]
/opt/PostgreSQL/9.0/bin/psql.bin[0x403a69]
======= Memory map: ========
00110000-00133000 r-xp 00000000 fd:00 3311343                            /opt/PostgreSQL/9.0/lib/libpq.so.5
00133000-00332000 ---p 00023000 fd:00 3311343                            /opt/PostgreSQL/9.0/lib/libpq.so.5
00332000-00335000 rw-p 00022000 fd:00 3311343                            /opt/PostgreSQL/9.0/lib/libpq.so.5
00400000-00456000 r-xp 00000000 fd:00 3309756                            /opt/PostgreSQL/9.0/bin/psql.bin
00655000-00656000 rw-p 00055000 fd:00 3309756                            /opt/PostgreSQL/9.0/bin/psql.bin
00656000-00657000 rw-p 00656000 00:00 0
00657000-0068f000 r-xp 00000000 fd:00 3311349                            /opt/PostgreSQL/9.0/lib/libxslt.so.1
0068f000-0088e000 ---p 00038000 fd:00 3311349                            /opt/PostgreSQL/9.0/lib/libxslt.so.1
0088e000-00890000 rw-p 00037000 fd:00 3311349                            /opt/PostgreSQL/9.0/lib/libxslt.so.1
00890000-009c8000 r-xp 00000000 fd:00 3311348                            /opt/PostgreSQL/9.0/lib/libxml2.so.2
009c8000-00bc7000 ---p 00138000 fd:00 3311348                            /opt/PostgreSQL/9.0/lib/libxml2.so.2
00bc7000-00bd1000 rw-p 00137000 fd:00 3311348                            /opt/PostgreSQL/9.0/lib/libxml2.so.2
00bd1000-00bd2000 rw-p 00bd1000 00:00 0
00bd2000-00bfa000 r-xp 00000000 fd:00 3311332                            /opt/PostgreSQL/9.0/lib/libedit.so
00bfa000-00df9000 ---p 00028000 fd:00 3311332                            /opt/PostgreSQL/9.0/lib/libedit.so
00df9000-00dfc000 rw-p 00027000 fd:00 3311332                            /opt/PostgreSQL/9.0/lib/libedit.so
00dfc000-00e07000 r-xp 00000000 fd:00 1179676                            /lib64/libnss_files-2.8.so
00e07000-01006000 ---p 0000b000 fd:00 1179676                            /lib64/libnss_files-2.8.so
01006000-01007000 r--p 0000a000 fd:00 1179676                            /lib64/libnss_files-2.8.so
01007000-01008000 rw-p 0000b000 fd:00 1179676                            /lib64/libnss_files-2.8.so
02136000-02157000 rw-p 02136000 00:00 0                                  [heap]
02874000-0288a000 r-xp 00000000 fd:00 1180000                            /lib64/libgcc_s-4.3.0-20080428.so.1
0288a000-02a89000 ---p 00016000 fd:00 1180000                            /lib64/libgcc_s-4.3.0-20080428.so.1
02a89000-02a8a000 rw-p 00015000 fd:00 1180000                            /lib64/libgcc_s-4.3.0-20080428.so.1
3115200000-311521d000 r-xp 00000000 fd:00 1179965                        /lib64/ld-2.8.so
311541c000-311541d000 r--p 0001c000 fd:00 1179965                        /lib64/ld-2.8.so
311541d000-311541e000 rw-p 0001d000 fd:00 1179965                        /lib64/ld-2.8.so
3116400000-3116562000 r-xp 00000000 fd:00 1179966                        /lib64/libc-2.8.so
3116562000-3116762000 ---p 00162000 fd:00 1179966                        /lib64/libc-2.8.so
3116762000-3116766000 r--p 00162000 fd:00 1179966                        /lib64/libc-2.8.so
3116766000-3116767000 rw-p 00166000 fd:00 1179966                        /lib64/libc-2.8.so
3116767000-311676c000 rw-p 3116767000 00:00 0
3116800000-3116884000 r-xp 00000000 fd:00 1179970                        /lib64/libm-2.8.so
3116884000-3116a83000 ---p 00084000 fd:00 1179970                        /lib64/libm-2.8.so
3116a83000-3116a84000 r--p 00083000 fd:00 1179970                        /lib64/libm-2.8.so
3116a84000-3116a85000 rw-p 00084000 fd:00 1179970                        /lib64/libm-2.8.so
3116c00000-3116c02000 r-xp 00000000 fd:00 1179969                        /lib64/libdl-2.8.so
3116c02000-3116e02000 ---p 00002000 fd:00 1179969                        /lib64/libdl-2.8.so
3116e02000-3116e03000 r--p 00002000 fd:00 1179969                        /lib64/libdl-2.8.so
3116e03000-3116e04000 rw-p 00003000 fd:00 1179969                        /lib64/libdl-2.8.so
3117000000-3117016000 r-xp 00000000 fd:00 1179973                        /lib64/libpthread-2.8.so
3117016000-3117215000 ---p 00016000 fd:00 1179973                        /lib64/libpthread-2.8.so
3117215000-3117216000 r--p 00015000 fd:00 1179973                        /lib64/libpthread-2.8.so
3117216000-3117217000 rw-p 00016000 fd:00 1179973                        /lib64/libpthread-2.8.so
3117217000-311721b000 rw-p 3117217000 00:00 0
3117400000-311741a000 r-xp 00000000 fd:00 1179974                        /lib64/libselinux.so.1
311741a000-3117619000 ---p 0001a000 fd:00 1179974                        /lib64/libselinux.so.1
3117619000-311761a000 r--p 00019000 fd:00 1179974                        /lib64/libselinux.so.1
311761a000-311761b000 rw-p 0001a000 fd:00 1179974                        /lib64/libselinux.so.1
311761b000-311761c000 rw-p 311761b000 00:00 0
3117800000-3117815000 r-xp 00000000 fd:00 1179980                        /lib64/libz.so.1.2.3
3117815000-3117a14000 ---p 00015000 fd:00 1179980                        /lib64/libz.so.1.2.3
3117a14000-3117a15000 rw-p 00014000 fd:00 1179980                        /lib64/libz.so.1.2.3
3120800000-3120811000 r-xp 00000000 fd:00 1179967                        /lib64/libresolv-2.8.so
3120811000-3120a11000 ---p 00011000 fd:00 1179967                        /lib64/libresolv-2.8.so
3120a11000-3120a12000 r--p 00011000 fd:00 1179967                        /lib64/libresolv-2.8.so
3120a12000-3120a13000 rw-p 00012000 fd:00 1179967                        /lib64/libresolv-2.8.so
3120a13000-3120a15000 rw-p 3120a13000 00:00 0
3120c00000-3120c17000 r-xp 00000000 fd:00 1180005                        /lib64/libaudit.so.0.0.0
3120c17000-3120e17000 ---p 00017000 fd:00 1180005                        /lib64/libaudit.so.0.0.0
3120e17000-3120e18000 r--p 00017000 fd:00 1180005                        /lib64/libaudit.so.0.0.0
3120e18000-3120e19000 rw-p 00018000 fd:00 1180005                        /lib64/libaudit.so.0.0.0
3121000000-3121002000 r-xp 00000000 fd:00 1179985                        /lib64/libcom_err.so.2.1
3121002000-3121201000 ---p 00002000 fd:00 1179985                        /lib64/libcom_err.so.2.1
3121201000-3121202000 rw-p 00001000 fd:00 1179985                        /lib64/libcom_err.so.2.1
3121400000-312140b000 r-xp 00000000 fd:00 1180006                        /lib64/libpam.so.0.81.12
312140b000-312160b000 ---p 0000b000 fd:00 1180006                        /lib64/libpam.so.0.81.12
312160b000-312160c000 rw-p 0000b000 fd:00 1180006                        /lib64/libpam.so.0.81.12
3122400000-3122409000 r-xp 00000000 fd:00 1179971                        /lib64/libcrypt-2.8.so
3122409000-3122608000 ---p 00009000 fd:00 1179971                        /lib64/libcrypt-2.8.so
3122608000-3122609000 r--p 00008000 fd:00 1179971                        /lib64/libcrypt-2.8.so
3122609000-312260a000 rw-p 00009000 fd:00 1179971                        /lib64/libcrypt-2.8.so
312260a000-3122638000 rw-p 312260a000 00:00 0
3123800000-312382e000 r-xp 00000000 fd:00 5316643                        /usr/lib64/libgssapi_krb5.so.2.2
312382e000-3123a2d000 ---p 0002e000 fd:00 5316643                        /usr/lib64/libgssapi_krb5.so.2.2
3123a2d000-3123a2f000 rw-p 0002d000 fd:00 5316643                        /usr/lib64/libgssapi_krb5.so.2.2
3123c00000-3123c02000 r-xp 00000000 fd:00 1179984                        /lib64/libkeyutils-1.2.so
3123c02000-3123e01000 ---p 00002000 fd:00 1179984                        /lib64/libkeyutils-1.2.so
3123e01000-3123e02000 rw-p 00001000 fd:00 1179984                        /lib64/libkeyutils-1.2.so
3124000000-3124008000 r-xp 00000000 fd:00 5316640                        /usr/lib64/libkrb5support.so.0.1
3124008000-3124207000 ---p 00008000 fd:00 5316640                        /usr/lib64/libkrb5support.so.0.1
3124207000-3124208000 rw-p 00007000 fd:00 5316640                        /usr/lib64/libkrb5support.so.0.1
3124400000-3124424000 r-xp 00000000 fd:00 5316641                        /usr/lib64/libk5crypto.so.3.1
3124424000-3124623000 ---p 00024000 fd:00 5316641                        /usr/lib64/libk5crypto.so.3.1
3124623000-3124625000 rw-p 00023000 fd:00 5316641                        /usr/lib64/libk5crypto.so.3.1
3124800000-312489f000 r-xp 00000000 fd:00 5316642                        /usr/lib64/libkrb5.so.3.3
312489f000-3124a9e000 ---p 0009f000 fd:00 5316642                        /usr/lib64/libkrb5.so.3.3
3124a9e000-3124aa2000 rw-p 0009e000 fd:00 5316642                        /usr/lib64/libkrb5.so.3.3
3498200000-349824e000 r-xp 00000000 fd:00 3311334                        /opt/PostgreSQL/9.0/lib/libncurses.so.5.5
349824e000-349844e000 ---p 0004e000 fd:00 3311334                        /opt/PostgreSQL/9.0/lib/libncurses.so.5.5
349844e000-349845c000 rw-p 0004e000 fd:00 3311334                        /opt/PostgreSQL/9.0/lib/libncurses.so.5.5
349845c000-349845d000 rw-p 349845c000 00:00 0
3f74800000-3f7492d000 r-xp 00000000 fd:00 3311323                        /opt/PostgreSQL/9.0/lib/libcrypto.so.6
3f7492d000-3f74b2c000 ---p 0012d000 fd:00 3311323                        /opt/PostgreSQL/9.0/lib/libcrypto.so.6
3f74b2c000-3f74b4d000 rw-p 0012c000 fd:00 3311323                        /opt/PostgreSQL/9.0/lib/libcrypto.so.6
3f74b4d000-3f74b51000 rw-p 3f74b4d000 00:00 0
3f74c00000-3f74c46000 r-xp /opt/PostgreSQL/9.0/bin/psql: line 21: 19271 Aborted                 "$PG_BIN_PATH/psql.bin" "$@"
[postgres@localhost ~]$ exit
logout
[root@localhost Download]# which psql
/usr/bin/which: no psql in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost Download]# /opt/PostgreSQL/9.0/bin/psql -U postgres -d postgres
Password for user postgres:
psql.bin (9.0.5)
Type "help" for help.

postgres=# \dt
No relations found.
postgres=# \q
[root@localhost Download]# yum install libedit*
Loaded plugins: refresh-packagekit
fedora                                                   | 2.4 kB     00:00   
updates                                                  | 2.6 kB     00:00   
Setting up Install Process
Parsing package install arguments
Package libedit-2.10-4.20070831cvs.fc9.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package libedit-devel.x86_64 0:2.10-4.20070831cvs.fc9 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================
 Package                 Arch       Version          Repository        Size
=============================================================================
Installing:
 libedit-devel           x86_64     2.10-4.20070831cvs.fc9  fedora             25 k

Transaction Summary
=============================================================================
Install      1 Package(s)        
Update       0 Package(s)        
Remove       0 Package(s)        

Total download size: 25 k
Is this ok [y/N]: y
Downloading Packages:
(1/1): libedit-devel-2.10-4.20070831cvs.fc9.x86_64.rpm   |  25 kB     00:00   
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing: libedit-devel                ######################### [1/1]

Installed: libedit-devel.x86_64 0:2.10-4.20070831cvs.fc9
Complete!
[root@localhost Download]# yum install libreadline*
Loaded plugins: refresh-packagekit
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package libreadline-java.x86_64 0:0.8.0-20.fc9 set to be updated
---> Package libreadline-java-javadoc.x86_64 0:0.8.0-20.fc9 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================
 Package                 Arch       Version          Repository        Size
=============================================================================
Installing:
 libreadline-java        x86_64     0.8.0-20.fc9     fedora             50 k
 libreadline-java-javadoc  x86_64     0.8.0-20.fc9     fedora             20 k

Transaction Summary
=============================================================================
Install      2 Package(s)        
Update       0 Package(s)        
Remove       0 Package(s)        

Total download size: 70 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): libreadline-java-javadoc-0.8.0-20.fc9.x86_64.rpm  |  20 kB     00:00   
(2/2): libreadline-java-0.8.0-20.fc9.x86_64.rpm          |  50 kB     00:00   
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing: libreadline-java             ######################### [1/2]
  Installing: libreadline-java-javadoc     ######################### [2/2]

Installed: libreadline-java.x86_64 0:0.8.0-20.fc9 libreadline-java-javadoc.x86_64 0:0.8.0-20.fc9
Complete!
[root@localhost Download]# clear

[root@localhost Download]# cd /opt/PostgreSQL/9.0/lib/
[root@localhost lib]# ls -lrth
total 9.0M
-rw-r--r-- 1 postgres postgres  50K 2011-09-23 11:15 libpgport.a
-rw-r--r-- 1 postgres postgres 255K 2011-09-23 11:15 libpq.a
-rw-r--r-- 1 postgres postgres  99K 2011-09-23 11:15 libpgtypes.a
-rw-r--r-- 1 postgres postgres  19K 2011-09-23 11:15 libecpg_compat.a
-rw-r--r-- 1 postgres postgres 147K 2011-09-23 11:15 libecpg.a
-rwxr-xr-- 1 postgres postgres 372K 2011-09-23 11:17 libncurses.so.5.5
-rw-r--r-- 1 postgres postgres  51K 2011-09-23 11:17 libuuid.so.16.0.22
-rwxr-xr-- 1 postgres postgres  18K 2011-09-23 11:17 libuuid.so.1.2
-rwxr-xr-- 1 postgres postgres 147K 2011-09-23 11:17 libpng12.so.0.10.0
-rwxr-xr-- 1 postgres postgres 136K 2011-09-23 11:17 libjpeg.so.62.0.0
-rwxr-xr-- 1 postgres postgres 308K 2011-09-23 11:17 libssl.so.6
-rwxr-xr-- 1 postgres postgres 1.4M 2011-09-23 11:17 libcrypto.so.6
-rwxr-xr-- 1 postgres postgres 189K 2011-09-23 11:17 libedit.so
-rwxr-xr-- 1 postgres postgres 4.0M 2011-09-23 11:17 libxml2.so.2
-rwxr-xr-- 1 postgres postgres 804K 2011-09-23 11:38 libxslt.so.1
-rwxr-xr-x 1 postgres postgres 171K 2011-09-23 11:38 libpq.so.5.3
-rwxr-xr-x 1 postgres postgres 171K 2011-09-23 11:38 libpq.so.5
-rwxr-xr-x 1 postgres postgres 171K 2011-09-23 11:38 libpq.so
-rwxr-xr-x 1 postgres postgres  68K 2011-09-23 11:38 libpgtypes.so.3.1
-rwxr-xr-x 1 postgres postgres  68K 2011-09-23 11:38 libpgtypes.so.3
-rwxr-xr-x 1 postgres postgres  68K 2011-09-23 11:38 libpgtypes.so
-rwxr-xr-x 1 postgres postgres  85K 2011-09-23 11:38 libecpg.so.6.2
-rwxr-xr-x 1 postgres postgres  85K 2011-09-23 11:38 libecpg.so.6
-rwxr-xr-x 1 postgres postgres  85K 2011-09-23 11:38 libecpg.so
-rwxr-xr-x 1 postgres postgres  23K 2011-09-23 11:38 libecpg_compat.so.3.2
-rwxr-xr-x 1 postgres postgres  23K 2011-09-23 11:38 libecpg_compat.so.3
-rwxr-xr-x 1 postgres postgres  23K 2011-09-23 11:38 libecpg_compat.so
drwxr-xr-x 4 postgres postgres 4.0K 2011-10-13 08:21 postgresql
lrwxrwxrwx 1 postgres postgres   18 2011-10-13 08:21 libuuid.so.16 -> libuuid.so.16.0.22
lrwxrwxrwx 1 postgres postgres   14 2011-10-13 08:21 libuuid.so.1 -> libuuid.so.1.2
lrwxrwxrwx 1 postgres postgres   18 2011-10-13 08:21 libuuid.so -> libuuid.so.16.0.22
lrwxrwxrwx 1 postgres postgres   13 2011-10-13 08:21 libtermcap.so.2 -> libncurses.so
lrwxrwxrwx 1 postgres postgres   18 2011-10-13 08:21 libpng12.so.0 -> libpng12.so.0.10.0
lrwxrwxrwx 1 postgres postgres   18 2011-10-13 08:21 libpng12.so -> libpng12.so.0.10.0
lrwxrwxrwx 1 postgres postgres   17 2011-10-13 08:21 libncurses.so.5 -> libncurses.so.5.5
lrwxrwxrwx 1 postgres postgres   15 2011-10-13 08:21 libncurses.so -> libncurses.so.5
lrwxrwxrwx 1 postgres postgres   17 2011-10-13 08:21 libjpeg.so.62 -> libjpeg.so.62.0.0
lrwxrwxrwx 1 postgres postgres   17 2011-10-13 08:21 libjpeg.so -> libjpeg.so.62.0.0
[root@localhost lib]# ls libedit.so
libedit.so
[root@localhost lib]# find / -name libedit* 2>/dev/null
/opt/PostgreSQL/9.0/lib/libedit.so
/usr/lib64/libedit.so
[root@localhost lib]# ls -lrth /usr/lib64/libedit.so
lrwxrwxrwx 1 root root 17 2011-10-13 09:00 /usr/lib64/libedit.so -> libedit.so.0.0.26
[root@localhost lib]# mv libedit.so libedit.so.bak
[root@localhost lib]# ln -s /usr/lib64/libedit.so libedit.so
[root@localhost lib]# ls -lrth
total 9.0M
-rw-r--r-- 1 postgres postgres  50K 2011-09-23 11:15 libpgport.a
-rw-r--r-- 1 postgres postgres 255K 2011-09-23 11:15 libpq.a
-rw-r--r-- 1 postgres postgres  99K 2011-09-23 11:15 libpgtypes.a
-rw-r--r-- 1 postgres postgres  19K 2011-09-23 11:15 libecpg_compat.a
-rw-r--r-- 1 postgres postgres 147K 2011-09-23 11:15 libecpg.a
-rwxr-xr-- 1 postgres postgres 372K 2011-09-23 11:17 libncurses.so.5.5
-rw-r--r-- 1 postgres postgres  51K 2011-09-23 11:17 libuuid.so.16.0.22
-rwxr-xr-- 1 postgres postgres  18K 2011-09-23 11:17 libuuid.so.1.2
-rwxr-xr-- 1 postgres postgres 147K 2011-09-23 11:17 libpng12.so.0.10.0
-rwxr-xr-- 1 postgres postgres 136K 2011-09-23 11:17 libjpeg.so.62.0.0
-rwxr-xr-- 1 postgres postgres 308K 2011-09-23 11:17 libssl.so.6
-rwxr-xr-- 1 postgres postgres 1.4M 2011-09-23 11:17 libcrypto.so.6
-rwxr-xr-- 1 postgres postgres 189K 2011-09-23 11:17 libedit.so.bak
-rwxr-xr-- 1 postgres postgres 4.0M 2011-09-23 11:17 libxml2.so.2
-rwxr-xr-- 1 postgres postgres 804K 2011-09-23 11:38 libxslt.so.1
-rwxr-xr-x 1 postgres postgres 171K 2011-09-23 11:38 libpq.so.5.3
-rwxr-xr-x 1 postgres postgres 171K 2011-09-23 11:38 libpq.so.5
-rwxr-xr-x 1 postgres postgres 171K 2011-09-23 11:38 libpq.so
-rwxr-xr-x 1 postgres postgres  68K 2011-09-23 11:38 libpgtypes.so.3.1
-rwxr-xr-x 1 postgres postgres  68K 2011-09-23 11:38 libpgtypes.so.3
-rwxr-xr-x 1 postgres postgres  68K 2011-09-23 11:38 libpgtypes.so
-rwxr-xr-x 1 postgres postgres  85K 2011-09-23 11:38 libecpg.so.6.2
-rwxr-xr-x 1 postgres postgres  85K 2011-09-23 11:38 libecpg.so.6
-rwxr-xr-x 1 postgres postgres  85K 2011-09-23 11:38 libecpg.so
-rwxr-xr-x 1 postgres postgres  23K 2011-09-23 11:38 libecpg_compat.so.3.2
-rwxr-xr-x 1 postgres postgres  23K 2011-09-23 11:38 libecpg_compat.so.3
-rwxr-xr-x 1 postgres postgres  23K 2011-09-23 11:38 libecpg_compat.so
drwxr-xr-x 4 postgres postgres 4.0K 2011-10-13 08:21 postgresql
lrwxrwxrwx 1 postgres postgres   18 2011-10-13 08:21 libuuid.so.16 -> libuuid.so.16.0.22
lrwxrwxrwx 1 postgres postgres   14 2011-10-13 08:21 libuuid.so.1 -> libuuid.so.1.2
lrwxrwxrwx 1 postgres postgres   18 2011-10-13 08:21 libuuid.so -> libuuid.so.16.0.22
lrwxrwxrwx 1 postgres postgres   13 2011-10-13 08:21 libtermcap.so.2 -> libncurses.so
lrwxrwxrwx 1 postgres postgres   18 2011-10-13 08:21 libpng12.so.0 -> libpng12.so.0.10.0
lrwxrwxrwx 1 postgres postgres   18 2011-10-13 08:21 libpng12.so -> libpng12.so.0.10.0
lrwxrwxrwx 1 postgres postgres   17 2011-10-13 08:21 libncurses.so.5 -> libncurses.so.5.5
lrwxrwxrwx 1 postgres postgres   15 2011-10-13 08:21 libncurses.so -> libncurses.so.5
lrwxrwxrwx 1 postgres postgres   17 2011-10-13 08:21 libjpeg.so.62 -> libjpeg.so.62.0.0
lrwxrwxrwx 1 postgres postgres   17 2011-10-13 08:21 libjpeg.so -> libjpeg.so.62.0.0
lrwxrwxrwx 1 root     root       21 2011-10-13 09:02 libedit.so -> /usr/lib64/libedit.so
[root@localhost lib]# chown -R postgres:postgres libedit.so
[root@localhost lib]# ls -lrth
total 9.0M
-rw-r--r-- 1 postgres postgres  50K 2011-09-23 11:15 libpgport.a
-rw-r--r-- 1 postgres postgres 255K 2011-09-23 11:15 libpq.a
-rw-r--r-- 1 postgres postgres  99K 2011-09-23 11:15 libpgtypes.a
-rw-r--r-- 1 postgres postgres  19K 2011-09-23 11:15 libecpg_compat.a
-rw-r--r-- 1 postgres postgres 147K 2011-09-23 11:15 libecpg.a
-rwxr-xr-- 1 postgres postgres 372K 2011-09-23 11:17 libncurses.so.5.5
-rw-r--r-- 1 postgres postgres  51K 2011-09-23 11:17 libuuid.so.16.0.22
-rwxr-xr-- 1 postgres postgres  18K 2011-09-23 11:17 libuuid.so.1.2
-rwxr-xr-- 1 postgres postgres 147K 2011-09-23 11:17 libpng12.so.0.10.0
-rwxr-xr-- 1 postgres postgres 136K 2011-09-23 11:17 libjpeg.so.62.0.0
-rwxr-xr-- 1 postgres postgres 308K 2011-09-23 11:17 libssl.so.6
-rwxr-xr-- 1 postgres postgres 1.4M 2011-09-23 11:17 libcrypto.so.6
-rwxr-xr-- 1 postgres postgres 189K 2011-09-23 11:17 libedit.so.bak
-rwxr-xr-- 1 postgres postgres 4.0M 2011-09-23 11:17 libxml2.so.2
-rwxr-xr-- 1 postgres postgres 804K 2011-09-23 11:38 libxslt.so.1
-rwxr-xr-x 1 postgres postgres 171K 2011-09-23 11:38 libpq.so.5.3
-rwxr-xr-x 1 postgres postgres 171K 2011-09-23 11:38 libpq.so.5
-rwxr-xr-x 1 postgres postgres 171K 2011-09-23 11:38 libpq.so
-rwxr-xr-x 1 postgres postgres  68K 2011-09-23 11:38 libpgtypes.so.3.1
-rwxr-xr-x 1 postgres postgres  68K 2011-09-23 11:38 libpgtypes.so.3
-rwxr-xr-x 1 postgres postgres  68K 2011-09-23 11:38 libpgtypes.so
-rwxr-xr-x 1 postgres postgres  85K 2011-09-23 11:38 libecpg.so.6.2
-rwxr-xr-x 1 postgres postgres  85K 2011-09-23 11:38 libecpg.so.6
-rwxr-xr-x 1 postgres postgres  85K 2011-09-23 11:38 libecpg.so
-rwxr-xr-x 1 postgres postgres  23K 2011-09-23 11:38 libecpg_compat.so.3.2
-rwxr-xr-x 1 postgres postgres  23K 2011-09-23 11:38 libecpg_compat.so.3
-rwxr-xr-x 1 postgres postgres  23K 2011-09-23 11:38 libecpg_compat.so
drwxr-xr-x 4 postgres postgres 4.0K 2011-10-13 08:21 postgresql
lrwxrwxrwx 1 postgres postgres   18 2011-10-13 08:21 libuuid.so.16 -> libuuid.so.16.0.22
lrwxrwxrwx 1 postgres postgres   14 2011-10-13 08:21 libuuid.so.1 -> libuuid.so.1.2
lrwxrwxrwx 1 postgres postgres   18 2011-10-13 08:21 libuuid.so -> libuuid.so.16.0.22
lrwxrwxrwx 1 postgres postgres   13 2011-10-13 08:21 libtermcap.so.2 -> libncurses.so
lrwxrwxrwx 1 postgres postgres   18 2011-10-13 08:21 libpng12.so.0 -> libpng12.so.0.10.0
lrwxrwxrwx 1 postgres postgres   18 2011-10-13 08:21 libpng12.so -> libpng12.so.0.10.0
lrwxrwxrwx 1 postgres postgres   17 2011-10-13 08:21 libncurses.so.5 -> libncurses.so.5.5
lrwxrwxrwx 1 postgres postgres   15 2011-10-13 08:21 libncurses.so -> libncurses.so.5
lrwxrwxrwx 1 postgres postgres   17 2011-10-13 08:21 libjpeg.so.62 -> libjpeg.so.62.0.0
lrwxrwxrwx 1 postgres postgres   17 2011-10-13 08:21 libjpeg.so -> libjpeg.so.62.0.0
lrwxrwxrwx 1 postgres postgres   21 2011-10-13 09:02 libedit.so -> /usr/lib64/libedit.so
[root@localhost lib]# clear

[root@localhost lib]# sudo su - postgres
[postgres@localhost ~]$ psql
-bash: psql: command not found
[postgres@localhost ~]$ pwd
/home/postgres
[postgres@localhost ~]$ cd /opt/PostgreSQL/9.0/bin/
[postgres@localhost bin]$ ./psql
psql.bin (9.0.5)
Type "help" for help.

postgres=# \dt
No relations found.
postgres=# \dt
No relations found.
postgres=# \q
[postgres@localhost bin]$

copy command usage in postgres


copy data from file to table:
---------------------------------------

copy vacuumtest from  '/home/postgres/test1.csv' with delimiter ',' CSV ;

copy data from table to a file:
-----------------------------------------
copy (select * from vacuumtest) to '/home/postgres/test1.csv' with delimiter ',' CSV;

 

copy data from file to table with headers information :
-----------------------------------------------------------------------------
copy vacuumtest from  '/home/postgres/test1.csv' with delimiter ',' CSV header;

copy data from table to a file with headers information:
-------------------------------------------------------------------------------
copy (select * from vacuumtest) to '/home/postgres/test1.csv' with delimiter ',' CSV header ;


copy standard input into a table:
-----------------------------------------------
Eg1 :  COPY abc (id) FROM stdin; 
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 1
>> 2
>> 3
>> 4
>> 5
>> \.

Eg2:COPY test2 (id, name, loc) FROM stdin with delimiter ',';
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 1,rc2,chiranjeevi
>> \.




checking all the tables record count cluster level in postgres

create table:
--------------------
create table table_count(databasename name,schemaname name,relname name,reccount integer);

create function :
------------------------
create or replace function table_record_count() returns text as $$
declare
dbname1 name;
schemaname1 name;
relationname1 name;
tablecount integer;
r record;
r1 record;
done text default 'successes';
reccount integer;
stmt1 text;
stmt2 text;
stmt3 text;
begin
for r in select datname from pg_stat_database
loop
dbname1 :=r.datname;
CONTINUE WHEN dbname1='template0';
stmt1 :='select * from dblink('
|| ''''
|| 'dbname='
|| dbname1
|| ''''
|| ','
|| ''''
|| 'select schemaname,relname from pg_statio_user_tables'
|| ''''
|| ') as t1(schemaname name,relname name)'
|| ';';
for r1 in execute stmt1
loop
schemaname1 :=r1.schemaname;
relationname1 :=r1.relname;
CONTINUE WHEN  relationname1 is null or schemaname1='_shadow';
stmt2 := 'select * from dblink('
||''''
|| 'dbname='
||dbname1
||''''
||','
||''''
||'select count(*) from  '
|| schemaname1
|| '.'
||relationname1
|| ''''
||') as t2(reccount integer)'
|| ';' ;
execute stmt2 into reccount;
stmt3 := 'insert into table_count(databasename,schemaname,relname,reccount) values ('
|| ''''
||dbname1
|| ''''
|| ','
|| ''''
|| schemaname1
|| ''''
|| ','
|| ''''
||relationname1
|| ''''
|| ','
|| reccount
|| ')'
|| ';' ;
execute stmt3;
end loop;
end loop;
return done;
end;
$$ language plpgsql;

Tuesday 1 November 2011

function for checking postgres config file parameters

create or replace function pgconfun2() returns void as $$
declare
--tis function will give alert on changes made by postgres.conf file
name1 text;
name2 text;
setting1 text;
setting2 text;
stmt1 text;
stmt2 text;
rec pgmansettings%rowtype;
begin
for rec in select name,setting from pg_settings
loop
name1 :=rec.name;
setting1 :=rec.setting;
stmt1 := 'select name,setting from pgmansettings where name='
|| ''''
||name1
|| ''''
|| ';' ;
execute stmt1 into name2,setting2;
if setting1 != setting2
then
raise notice 'parameter : % standard table value :% pg settings value :%',name2,setting2,setting1;
stmt2 :='update pgmansettings set setting = '
|| ''''
|| setting1
|| ''''
|| ' where name='
|| ''''
|| name2
|| ''''
|| ';' ;
execute stmt2;
raise notice 'updated standard table parameter :%  with value :% ',name2,setting1;
end if;
end loop;
end;
$$language plpgsql;