แสดงบทความที่มีป้ายกำกับ postgres แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ postgres แสดงบทความทั้งหมด

วันพุธที่ 31 ตุลาคม พ.ศ. 2555

What make me here?

Life has no boundaries
...
Thank for พระพุทธเจ้า to give me the way to do
Thank to my family for living
Thank for my teacher to teach
Thank for friend to completed me
Thank for other to support me
...

Favorit
Music: Classic, Soundtrack,
Band: X-Japan, Within Temptation,
Movie: ET, Star war, Beautiful mind,
Cartoon: Ghibli
Country: Thailand, Japan, Germany,
Hobby: Traveling, Book, Movie,

Far Future
50-year old
A little house on High mountain
2 dogs run around me
A few farm
Fresh air

Near Future
Contribute my knowledge to community

Next
MQ i don't want to "lost" anything
Twisted Matrix "amazing project"

Migrate
PHP to Python
SQL to ORM
html to template
Web page to Web Application
Horizontal to Vertically
Everyone is user for system
"in Band, very skilled and less skilled musician need to adopt together"

Platform
2012
We need other platform
Codeigniter.... no
Cake PHP ... no
Zend ... no
Symfony ... no
Java? ... no
Python ... yes
django ... yes
"Simple is more complexities behind"

Software Project Management
2007
Flag ship, CRM
Project management on Trac
Revision control on SVN
{{Applications}}
CRM, Case management, Knowledge management
Sales Management, Marketing Management
Email Mass Marketing
Business Inteligent
{{Application}}
We'd get our platform?
Hosting service
Customization service
Software as service
Cloud service
Python
django, my hero
"Service on Software"

Size Does Matter
2006
Fast growing database, web application was delivered to biggest TV response company
AIX RS/600 was amazing in Thailand
PostgreSQL on AIX, Possible here!
... Now our system serve more than 150GB of data...
... more than 500 users concurrent ...
"Big Huge Giant"

Software Engineering
2003
Requirement analysis
Design
Documentation
Develop
Project management
Implement
Maintain
Version control was implement
CVS SVN
Software Test
Software Quality
"Imagine is more important"

Coding Era
2001
Open source came back
PHP + PostgreSQL+ Apache
First software/service served for businesses
All software written for web, web page!
"team work"

Telephony Network
2001
Move to PABX system
Where voice and data merge here!
Business applications, CRM, messaging system, monitoring software were introduced
Many software are integrated together to complete requirement
"meeting, meeting, business!"

Work!
2000
1-year computer networks, internet servers
ATM, Router, Switch had been setup to serve user
Some on MCU to make meeting over network
We served 600 users with single mail server! with Linux Debian
Open source begin!
Python first introduced
"i'm hungry to know more"

Big Moves
1999
Rosenheim, Germany was next moves
Signal Processing in Big Firm, Siemens
Linux first come in to my life
Environment change
Vision changes
Attitude changes
Life changes
Explore Resenheim
"2nd miracle, Amazing! Incridible"

University
1999
Electrical Engineering Department
Computer/Communication was my major
Digital Image processing using DSP was my project
i fork here
"First miracle, it happend all the time."

Technical School
1996
Knight Rider brought me here
"Time and Time to wait and go for get it"

Next School
1993
Math-Sci was my major here
Music, was my hobby
Trumpet, French-horn were my next instruments in school band
"in a Band, very skilled and low skilled musician need to adopt together"

School
1990
My school was near Wat
I had to walk to school from home every day
I played music on last 3 year in first school
My first instrument was melodeon
"music is one of meditation"

Childhood
I was the second child of three children
We lived close to Wat
I have to learn Buddhism without knowing it
"know nothing means know everything"

Begin
1977
I was born on 10 April
Male
Bangkok, Thailand was my birth place
"a Child can make things"

วันอังคารที่ 20 กันยายน พ.ศ. 2554

postgresql cluster บน Debian

เป็นที่เข้าใจกันว่าหากทำการติดตั้ง PostgreSQL แล้ว package manager จะทำการกำหนดค่าต่าง ๆ ไว้ รวมถึง Folder ที่เก็บ Data

แต่โดยส่วนมากแล้วผมจะสร้าง partition ไว้สำหรับเก็บ Data ไว้ต่างหาก ทำให้เวลาจะใช้งาน PostgreSQL ทีไร ต้องมาทำการแก้ไขค่า configure ต่าง ๆ ยุ่ง

มาคราวนี้ทาง Package นั้นมีเครื่องมือต่าง ๆ ไว้ให้เล่นอีกมากมาย เช่น

  • pg_createcluster
  • pg_dropcluster
  • pg_upgradecluster
  • pg_ctlcluster
  • pg_lsclusters
ผมเลยขอย้าย Data Directory ไปไว้ที่ /db/PGDATA/8.4/main

เริ่มจาก ตรวจดูก่อนว่า cluster เดิมของเราอยู่ที่ไหน



# pg_lsclusters
Version Cluster   Port     Status     Owner        Data directory                         Log file
8.4         main      5432    online    postgres      /var/lib/postgresql/8.4/main    /var/log/postgresql/postgresql-8.4-main.log

ลบ cluster เดิมออก
# pg_dropcluster --stop 8.4 main

สร้าง Folder ไว้รอก่อน
# mkdir -p /db/PGDATA

สร้าง cluster 


# pg_createcluster -u postgres -d /db/PGDATA/8.4/main  --locale=en_US.utf8 --start --start-conf=auto  8.4 main 
Creating new cluster (configuration: /etc/postgresql/8.4/main, data: /db/PGDATA/8.4/main)...
Moving configuration file /db/PGDATA/8.4/main/postgresql.conf to /etc/postgresql/8.4/main...
Moving configuration file /db/PGDATA/8.4/main/pg_hba.conf to /etc/postgresql/8.4/main...
Moving configuration file /db/PGDATA/8.4/main/pg_ident.conf to /etc/postgresql/8.4/main...
Configuring postgresql.conf to use port 5432...

ตรวจดูความเรียบร้อย
# pg_lsclusters 
Version   Cluster   Port     Status    Owner       Data directory                     Log file
8.4          main       5432    online   postgres     /db/PGDATA/8.4/main      /var/log/postgresql/postgresql-8.4-main.log


เรียกได้ว่าเหนื่อยน้อยลงเยอะ


5 Nov 2012:
วันนี้พอเอกสารอ้างอิง อีกอันทีนี่ http://people.debian.org/~mpitt/architecture.html


วันอังคารที่ 4 พฤษภาคม พ.ศ. 2553

Secure TCP/IP Connections with SSH Tunnels กับ PostgreSQL

ผมชอบ SSH ตรงที่มันสามารถทำ SSH Tunnels ได้นี่แหละ และก็อีกครั้งที่ผมได้ประสบการณ์ดี ๆ เกี่ยวกับการใช้งาน Database ระยะไกลอีกครั้ง

|---------------------|
| 222.222.22.222 |
| Postgres:5432 |
|----------|----------|
|
|
|---------------------|
| 10.0.0.01 |
| Firewall |
|----------|----------|
|
|
|---------------------|
| 10.0.0.20 |
| local web server |
|----------|----------|


ดูจากรูป เครื่อง Server ของผมฝากไว้ที่ ISP โดยได้ทำการ ติดตั้ง PostgreSQL และ run ไว้ที่พอร์ต 127.0.0.1:5432 เพราะว่าไม่อยากให้ใครมาต่อ นอกจากที่ตัวมันเอง

คราว นี้ก็เป็นประเด็นว่า แล้วถ้าอยากต่อ Database ไปยังเครื่อง Server จากที่อยู่หลัง FireWall หละ จะทำอย่างไร อย่างแรกที่นึกออกก็คือ VPN

หลัง จากติดตั้ง OpenVPN แล้ว ปัญหาก็ตามมา คือ มันช้า แล้วก็หลุดบ่อยด้วย หาทางแก้อยู่ตั้งนาน สุดท้ายก็มาจบที่ SSH Tunnel อีกตามเคย


เอกสาร ที่ผมนำมาเล่าให้ฟังนั้นสามารถอ่านแบบภาษาอังกฤษได้ที่ http://developer.postgresql.org/pgdocs/postgres/ssh-tunnels.html


ขั้น แรกก็ SSh เข้าไปที่เครื่อง local ก่อน
$ ssh username@10.0.0.20

ต่อ มาก็ต้องลุยด้วยการสร้าง Tunnel
$ ssh -L 63333:localhost:5432 username@222.222.222.222
# อธิบายได้ดังนี้
# จะทำการสร้าง Tunnel ที่ 10.0.0.20:63333 (เค้าเรียกว่า End of Tunnel) ไปยัง 222.222.222.222:5432 (เรียกว่า End of Remote Tunnel) และก็เป็นพอร์ตที่เปิดให้บริการ Database อยู่ด้วย
# โดยใช้ผู้ใช้งาน username ต่อไปยัง 222.222.222.222

ที่เครื่อง local web server ตรวจดูความเรียบร้อยหน่อยว่า มีการเปิดพอร์ต 63333 ไว้จริงหรือไม่ด้วย
$ netstat -an |grep 63333

จากนั้นก็ลองดูว่าสามารถใช้งาน postgres ได้จากเครื่อง local หรือไม่
$ psql -h localhost -p 63333 -U postgresuser -l

การใช้งาาน PostgreSQL ในส่วนงานต่าง ๆ OLTP, DW, WEB

คราวก่อนนำเนื้อหามาแปะไว้ (PostgreSQL กับการกำหนดค่าที่น่าสนใจของ WEB, OLPT, DW ) กะว่าจะอธิบายต่อที่ตรงนี้ แต่เท่าที่ดูแล้วคิดว่านำออกมาอธิบายต่างหากดีกว่า

การติดตั้ง PostgreSQL โดย Package ที่สำเร็จรูปนั้น แน่นอนว่าการทำ Package ออกมาจะมีการกำหนดค่ามาเพื่อรองรับ Application ทั่ว ๆ ไป แต่เนื่องจากความเป็นจริง เมื่อมีการนำมาใช้งานแล้ว เราต้องทำการปรับค่าต่าง ๆ เพื่อให้เหมาะสมกับงานที่ต่างกันออกไป

งานที่ต่าง ๆ กันก็ขอแบ่งออกเป็น 3 อย่างดังนี้ก่อน
  1. WEB - Web Transactional
  2. OLTP - Online Transactional Processing
  3. DW - Data Warehouse
งานทั้ง 3 ประเภทนี้ต่างกัน ทำให้การกำหนดค่าต่าง ๆ ของ Database แตกต่างกันด้วย ลองนึกถึงว่าถ้าหากเราต้องการขับรถ ไปยังที่แตกต่างกัน เราจะเลือกรถอย่างไรดี

ในด้าน Database ก็เหมือนกัน ทุกวันนี้หลายคนใช้ Database Configurature เดียว เพื่องานทุกอย่าง อาจเนื่องจากเหตุผลทางด้านการเงิน แต่เมื่อทำงานไปซักพักก็จะเข้าใจว่าทำไม

ก่อนอื่นเราต้องทำความเข้าใจกับ ประเภทของงานทั้ง 3 กันก่อน
"WEB" การเรียกใช้งานส่วนใหญ่เป็นการแสดงผล มีการเรียกการใช้งาน Database บ้าง แต่ส่วนใหญ่จะถูกออกแบบมาเพื่อบริการข้อมูล ไม่ค่อยมีการทำ Transaction
"OLTP" มีการเรียกใช้งานข้อมูลเพื่อประกอบการใช้งาน และมีการบันทึกข้อมูล Transaction กลับไปยัง ฐานข้อมูล มีรายงานที่เกี่ยวกับทางด้าน Tranaction ในแต่ละวัน
"DW" Datawarehouse มีลักษณะเฉพาะ โดยมีจุดประสงค์เพื่อนำข้อมูลที่เก็บไว้มาทำเป็นการวิเคราะห์ และตัดสินใจ และบางครั้งส่งผลที่ได้กลับไปยัง OLTP เพื่อกำหนดวิธีการทำงานต่าง ๆ รายงานส่วนใหญ่ถูกออกแบบมาเพื่อ ทำเป็นข้อมูลเพื่อการตัดสินใจ

เมื่อทราบลักษณะงานต่าง ๆ แล้ว เราก็เริ่มดำเนินการกำหนดค่ากันเลย ซึ่งในส่วนนี้จะมุ่งไปยัง Database เท่านั้น ส่วน Application นั้น ก็ขึ้นอยู่กับประเภทของงาน


CPU
ปัจจุบันเท่าที่ประสบมา จำนวน CPU 4-8 cores ก็เพียงพอกับงานที่เกี่ยวกับ Database แล้ว และแต่ละ CPU ก็มีความเร็วที่ค่อนข้างมากแล้วด้วย

MEMORY
หลักง่าย ๆ คือ ถ้าอยากให้เร็วก็ใส่ เยอะ ๆ ปัจจุบันเท่าที่เห็นคือ 16GB

HARDDISK
"many spindles are better" หมายความว่า ถ้าทำ RAID ให้ใช้ HD หลาย ๆ ตัวทำ RAID ใน 1 Volume ในส่วนนี้มีผลต่อ การทำงานของ Database เป็นอย่างมาก เพราะว่า งานส่วนใหญ่ให้ Load Data แล้วประมวลผลใน MEMORY ไม่พอหรอก

NETWORK
หลาย ๆ คนซื้อ Server ที่มี Networks Interface เยอะ ๆ แต่ว่าใช้อันเดียวเอง (เป็นเพราะว่า ไม่เคยเจอ Traffice เยอะ ๆ อย่างผม)


คราวนี้มาดูการปรับแต่งของ Database กัน

max_connections หมายถึงจำนวน connection ที่สามารถเข้ามาใช้งานได้เมื่อขนาดที่มีการใช้งานมากที่สุด โดยแต่ละ connection นั้นจะใช้ ทรัพยากร shared_buffer ที่กำหนดไว้ แน่นอนว่า ถ้ากำหนดไม่ดีจะมีอาการที่บอกว่า "out of memory" นะ

max_connections = 200  # small server   max_connections = 700  # web application database   max_connections = 40   # data warehousing database
shared_buffer หมายถึงปริมาณของหน่วยความจำที่ต้องใช้งานเพื่อการประมวลผล process ก่อนที่จะมีการใช้ disk เพื่อทำ operation ประมาณว่ามี active operations ได้มากเท่าไหร่

# shared_buffers = ( Available RAM / 4 ) # shared_buffers = 512MB   # basic 2GB web server # shared_buffers = 8GB     # 64-bit server with 32GB RAM

work_mem หมายถึงปริมาณของหน่วยความจำที่สามารถใช้ในแต่ละ query ได้ ถ้า Query อะไรที่ซับซ้อนก็ต้องเพิ่มค่านี้เลยครับ
# Most web applications should use the formula below, because their  # queries often require no work_mem.  # work_mem = ( AvRAM / max_connections ) ROUND DOWN to 2^x # work_mem = 2MB  # for 2GB server with 700 connections    # Formula for most BI/DW applications, or others running many complex # queries: # work_mem = ( AvRAM / ( 2 * max_connections ) ) ROUND DOWN to 2^x # work_mem = 128MB   # DW server with 32GB RAM and 40 connections 
ในส่วนตัวแล้ว กว่าจะปรับค่านี้ลงตัวก็ต้องลองหลาย ๆ รอบเหมือนกัน บางทีดูรูปแล้วน่าจะเข้าใจมากขึ้น ref: http://momjian.us/main/presentations.html