วันจันทร์ที่ 13 ธันวาคม พ.ศ. 2553

Postgresql เมื่อ index bloat (บวม)

เมื่อเราใช้ Postgres ไปนาน ๆ แล้ว สิ่งที่ติดตามมาคือ
  1. จำนวน record ที่เพิ่มมากขึ้น
  2. ขนาดของ index ที่มากขึ้น
  3. ขนาดของ dead rows ที่มากขึ้น
  4. ค่า free space map ดูว่าจะไม่พอแล้ว ในเวอร์ชั่นที่ต่ำกว่า 8.4
  5. มันสายไปแล้วถ้าคุณลืมเปิด autovacumm

หลาย ๆ คน อาจต้องยอมทำ VACUUM FULL แต่ปัญหาก็ตามมาคือ ระบบผมหยุดไม่ได้!


ส่ิงที่ต้องรีบดำเนินการโดยด่วนคือ
  1. เพิ่ม free space map
  2. เปิด auto vacuum
  3. จากเดิมที่ใช้ DELETE TABLE tablename; ก็เปลี่ยนเป็น TRUNCATE TABLE tablename;

อ้างอิงจากอันนี้ http://gkoenig.wordpress.com/2009/11/24/postgres-and-bloated-table/

postgres and bloated table
November 24, 2009 in open source, PostgreSQL
From time to time there are news/messages about bloated tables in postgres and a thereby decreased performance of the database.
The postgres-wiki contains a view (extracted from a script of the bucardo project) to check for bloat in your database here
For a quick reference you can check your table/index sizes regularly and check the no. of tuples to assume where bloat comes in.
e.g. check size of your tables and indexes:

SELECT relname AS table_name,
pg_size_pretty(pg_relation_size(oid)) AS table_size,
pg_size_pretty(pg_total_relation_size(oid)) AS total_size
FROM pg_class
WHERE relkind in ('r','i')
ORDER BY pg_relation_size(oid) DESC;

check no. of tuples

SELECT relname, relkind, reltuples, relpages
FROM pg_class
ORDER BY relpages DESC;

If you’re facing such a situation you have to check your (auto)vacuum settings and if it’s working correctly.
Starting with Postgres 8.3.x the autovacuum daemon works reliable for most cases and should be enabled. On some workloads a manual vacuum is needed anyhow. In combination with setting the (auto)vacuum parameters you have to consider the Free Space Map (FSM) parameter.
Open your postgresql.conf and check it out (every parameter has a very good comment there):
* enable autovacuum and log every run > 1 sec.

autovacuum = on
log_autovacuum_min_duration = 1000

fine tune when autovacuum will check the tables. The number is the fraction of the table size which needs modified data to run an autovacuum.

Apart from the bloated-table/index stuff, for the query planner it is very important to have up-to-date statistics. If you need an analyze more often, shrink the number to e.g. autovacuum_analyze_scale_factor = 0.02 and the tables will be autoanalyzed more often

autovacuum_vacuum_scale_factor = 0.2
autovacuum_analyze_scale_factor = 0.1

prevent autovacuum from running too often

autovacuum_naptime = 1min #time between two runs

And don’t forget to check your postgresql.log file for any hints the database system provides.
Regarding bloated tables I detected something like:

WARNING: relation "public.table_one" contains more than "max_fsm_pages" pages with useful free space
HINT: Consider using VACUUM FULL on this relation or increasing the configuration parameter "max_fsm_pages"

A “normal” vacuum wouldn’t help in this case because it doesn’t release the pages to the os, they will only be marked as free in the FSM (Free Space Map).
If your FSM isn’t big enough your tables will be bloated even with running autovacuum and perhaps manual vacuum, because the pages with no longer needed data cannot be marked as free and therefore never be overwritten.
One solution would be to run a

vacuum full verbose analyze table_name

This “defragment”-command marks unused pages as “can be overwritten” and moves pages from the bottom into this marked pages. => The table size will shrink
! But be aware: vacuum full locks the table exclusive. Consider running this command in maintenance windows or during night.

You should also keep in mind to “defragment” bloated indexes with

REINDEX INDEX indexname # recreate index explicitly
REINDEX TABLE tablename # recreate all indexes for this table

วันศุกร์ที่ 8 ตุลาคม พ.ศ. 2553

framework!

ช่วง 2-3 สัปดาห์ที่ผ่านมาใช้เวลาส่วนใหญ่กับการทำความเข้าใจกับระบบ ERP ซึ่ง หลาย ๆ คนคงหาหนังสืออ่าน แต่ผมมีเวลาน้อย ก็เลยอ่านผ่านการใช้งานโปรแกรม

นอกจากแนวทางการสร้าง Sofware ที่เป็นแบบ Opensource แล้ว เมื่ออ่านเสร็จแล้วก็ค้นพบว่า การวางรากฐานที่ดีในการพัฒนานั้น จะทำให้ การต่อยอดของ Software มีประสิทธิภาพดีด้วย และสิ่งที่เรียกว่ารากฐานนั้นก็คือ FrameWork นั้นเอง

หลาย ๆ คน พัฒนา Application ไป ก็พยายามสร้าง function ต่าง ๆ ขึ้นมามากมาย ท้ายสุดก็เกิดเป็น Framework ในการพัฒนาขึ้นมา ซึ่งผมก็เป็นอย่างนั้นเหมือนกัน

แต่ที่น่าแปลกใจคือ คนริเริ่มพัฒนา OpenERP นั้น เค้าศึกษาขบวนการทางธุรกิจ ได้อย่างมากมายได้อย่างไร

วันพฤหัสบดีที่ 23 กันยายน พ.ศ. 2553

How to Install OpenERP on Leopard 10.6.4 with VirtualENV

Over 2 weeks i spent time to install OpenERP on my MAC.
Luckily i found the way.

First i try to install all with python2.6. Unfortunately OpenERP 5.0.14 work well with python2.5. Please try my step

  1. you need python2.5!
  2. you need postgres
  3. install VirtualENV from fink
  4. cd
  5. virtualenv openerp-py25
  6. source openerp-py25/bin/activate
  7. install these packages by pip or easy_install
    • Babel
    • Beaker
    • CherryPy
    • FormEncode
    • Mako
    • MarkupSafe
    • PIL
    • PyChart
    • PyXML
    • pydot
    • pyparsing
    • reportlab
    • simplejson
    • psycopg2
  8. # Download openerp server
  9. $ bzr branch lp:openobject-server/5.0 openerp-server-5.0
  10. # Install openerp server
  11. $ cd openerp-server-5.0
  12. $ python setup.py install
  13. # Download openerp web
  14. $ bzr branch lp:openobject-client-web/5.0 openerp-web-5.0
  15. # Install openerp web
  16. $ cd openerp-web-5.0
  17. $ python setup.py install
  18. # Start Open Server and web
  19. $ openerp-server --db_user=openuser --db_password=
  20. $ openerp-web
  21. #Open Safari > http://localhost:8080 with username admin password admin

ref: http://devteam.taktik.be/index.php?/archives/9-How-to-install-Open-ERP-5.0.0-on-Mac-OS-X-10.5-Leopard.html

วันจันทร์ที่ 23 สิงหาคม พ.ศ. 2553

ดูสถานะของ HW RAID ผ่าน Linux

วันนี้หลังจากติดตั้ง Linux Debian เรียบร้อยแล้ว สิ่งที่ตามมาคือเราจะรู้ได้อย่างไรว่า HW-RAID ของเรานั้นทำงานเป็นปกติดี

อันดับแรกก็ต้องตรวจสอบก่อนว่า เรามี HW รองรับหรือไม่

Server# dmesg |grep mpt
[ 2.174757] mptbase: ioc0: Initiating bringup
[ 3.369272] mptbase: ioc0: PCI-MSI enabled

เมื่อแน่ใจแล้วก็ติดตั้ง mpt-status (mpt-status - get RAID status out of mpt (and other) HW RAID controllers)

Server # aptitude install mpt-status

แต่เมื่อลองใช้งานดูปรากฏว่า ยังไม่มี device และยังไม่ได้โหลด module นี้ไว้ใน Kernel เลย
Server # mpt-status
open /dev/mptctl: No such file or directory
Try: mknod /dev/mptctl c 10 220
Make sure mptctl is loaded into the kernel

ดังนั้นก็เริ่มโหลดกัน

Server # mknod /dev/mptctl c 10 220
Server # modprobe mptctl

ดูหน่อยว่ามี module โหลดแล้วแน่
Server# dmesg |tail -n 3
[ 1786.460073] Fusion MPT misc device (ioctl) driver 3.04.06
[ 1786.460073] mptctl: Registered with Fusion MPT base driver
[ 1786.460073] mptctl: /dev/mptctl @ (major,minor=10,220)

Server# mpt-status

You seem to have no SCSI disks attached to your HBA or you have
them on a different scsi_id. To get your SCSI id, run:

mpt-status -p

Server# mpt-status -p
Checking for SCSI ID:0
Checking for SCSI ID:1
Found SCSI id=1, use ''mpt-status -i 1`` to get more information.

Server# mpt-status -i 1
ioc0 vol_id 1 type IM, 2 phy, 465 GB, state OPTIMAL, flags ENABLED
ioc0 phy 0 scsi_id 4 ATA ST3500514NS BB23, 465 GB, state ONLINE, flags NONE
ioc0 phy 1 scsi_id 2 ATA ST3500320NS BB12, 465 GB, state ONLINE, flags NONE


เท่านี้เราก็รู้ว่า RAID เราทำงานหรือยัง

สุดท้ายก็ต้องให้ mptctl โหลดตอน Boot

Server # vi /etc/modules

แล้วเพิ่ม mptctl เข้าไปอีก 1 บรรทัด

ที่มา http://linuxhostingsupport.net/blog/open-devmptctl-no-such-device-make-sure-mptctl-is-loaded-into-the-kernel

วันจันทร์ที่ 9 สิงหาคม พ.ศ. 2553

SMTP relay with authenticate


เวลาเราใช้ SPF ในการตรวจสอบที่มาของการส่ง email เราก็จะพบว่าต้องคอยไปเพิ่ม ip ของเครื่อง server ที่เราใช้ส่งอยู่บ่อยไป ดังนั้น มาวันนี้ก็ขอใช้ความรู้เรื่องการทำ authenticate relay smtp มาใช้งานดู ซึ่งสามารถใช้ได้กับ smtp ของ gmail ได้ด้วย (นี่ยิ่งทีเข้าไปใหญ่)

ก่อนอื่นเราต้องแน่ใจก่อนว่าเรามีการใช้งาน sasl ก่อน

# sudo apttitude install postfix libsasl2-2 ca-catificate libsasl2-modules








แล้วก็ต้องมาแก้ main.cf ตามมา โดยให้มีการกำหนดค่าสำคัญ ๆดังต่อไปนี้

[สร้างไฟล์ /etc/postfix/sasl_passwd.db]
Server # vi /etc/postfix/sasl_passwd
 # [smtp.mydomain.com]:25 user.name@mydomain.com:password
Server # chmod 400 /etc/postfix/sasl_passwd
Server # postmap /etc/postfix/sasl_passwd #เราก็จะได้ไฟล์ sasl_passwd.db ออกมาใช้งาน

[สร้างไฟล์เพื่อใช้สำหรับการตรวจสอบ certificate]
Server # cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | tee -a /etc/postfix/cacert.pem

[main.cf]
relayhost=[mail.mydomain.com]:25
smtp_use_tls=yes
smtp_sasl_auth_enable = yes
smtp_tls_loglevel=1
smtp_sasl_security_options = noanonymous
smtp_sasl_local_domain =
broken_sasl_auth_clients = yes # ไว้สำหรับ MS Outlook ดีนักแล
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_authenticated_header = yes # สำหรับบอกว่า email นี้ได้ผ่านการตรวจสอบผู้ใช้แล้วนะ
smtp_tls_CAfile = /etc/postfix/cacert.pem # ไฟล์ Certificate

ที่มา : http://ubuntu-tutorials.com/2008/11/11/relaying-postfix-smtp-via-smtpgmailcom/

วันพุธที่ 19 พฤษภาคม พ.ศ. 2553

DNS Postfix กับ SPF

ช่วงนี้งานพวกการติดตั้งระบบหรือปรับปรุงระบบเพื่อทำให้การใช้งานมีประสิทธิภาพดูเหมือนว่าจะทยอยมาให้ทำเรื่อย ๆ เช่นคราวนี้ เป็นคราวของ EMAIL

ในการติดตั้ง email server นั้นปกติแล้วหากเราไม่ได้กำหนดอะไรไว้มากนักเราก็จะเจอว่ามีผู้ประสงค์ดีคอยส่ง email พวกโฆษณาให้เราเยอะมาก เอาเป็นว่าวันละหลาย ๆ รอบ

วันนี้เลยต้องพึ่ง SPF (Sender Policy Framework) หลักการของ spf นั้นง่ายมาก มันเริ่มด้วยเครื่องที่รับ email เวลาที่ได้รับการติดต่อมานั้นก็ทำการถามไปยัง dns ที่ควบคุม domain ที่ส่งมา ถามว่า "email ที่กำลังรับอยู่นี้ ได้ถูกส่งมาจากเครื่องที่กำหนดไว้หรือไม่"

มาดูรูปแบบที่กำหนดไว้ DNS กัน

mydomain.com TXT "v=spf1 a mx a:mydomain.com mx:mydomain.com mx:mail.mydomain.com ip4:xxx.xxx.xxx.0/24 ip4:xxx.xxx.xxx.xxx ~all"

อธิบายได้ดังนี้
  • v=spf1 แสดงว่า TXT นี้เป็นการกำหนด SPF
  • a แสดงว่า เครื่องนี้เป็นเครื่อง ส่ง email ของ mydomain.com ด้วย
  • mx แสดงว่า MX server นี้ ส่ง email ของ mydomain.com ด้วย
  • ip4:xxx.xxx.xxx.0/24 แสดงว่า ทุกเครื่องที่มีช่วงของ ip นี้ เป็นเครื่องที่ใช้ส่ง email ของ mydomain.com (อันนี้มีประโยชน์มากสำหรับคนที่ใช้ smtp ของ ISP เป็นคนส่ง)
  • ip4:xxx.xxx.xxx.xxx แสดงว่า เครื่องที่มี ip นี้ เป็นเครื่องที่อนุญาติให้ส่ง email ได้
  • ~all แสดงว่า SPF ที่ได้รับมาหากตรวจสอบแล้วไม่ตรงก็จะทำการบอก mail server ว่า ให้กำหนดเป็น softfail
หากเราทำการกำหนดดังกล่าวแล้ว เวลาเปิด email ดู เช่นใน gmail เราก็จะได้ header ว่า
Received-SPF: pass (google.com: domain of user@mydomain.com designates xxx.xxx.xxx.xxx as permitted sender) client-ip=xxx.xxx.xxx.xxx;

อ้างอิง: http://old.openspf.org/wizard.html?mydomain= เอาไว้สร้าง spf
อ้าวอิง: http://www.debianhelp.co.uk/dnsrecords.htm เอาไว้อ่านความหมายของ DNS record

แถมในส่วนของ postfix อีกหน่อย ...

ปกติแล้ว postfix นั้นไม่ได้มีการตรวจสอบ spf ไว้ก่อน เราต้องทำเอง ด้วยวิธีดังนี

  1. อันแรกเอาเป็นว่าก็ต้องติดตั้ง postfix ก่อน (อันนี้ไปทำเอง)
  2. จากนั้นก็ติดตั้ง tumgreyspf ด้วย $ aptitude install tumgreyspf
  3. แล้วก็แวะไปอ่าน /usr/share/doc/tumgreyspf/README.Debian เพื่อมากำหนดค่าต่าง ๆ
  4. แล้วก็ reload postfix
เท่านี้เอง

อ้างอิง: http://workaround.org/ispmail/lenny/spf

วันจันทร์ที่ 10 พฤษภาคม พ.ศ. 2553

TTS กับเพชรพระอุมา

จำได้ว่าใช้เวลาในการอ่านเพชรพระอุมาไป 2 ปีเต็ม ๆ ตอนนี้กะว่าจะอ่านอีกรอบ แต่ก็ยังไม่มีเวลานั่งอย่างจริงจังเท่าไหร่ ในรอบใหม่นี้จะทำความเข้าใจกับสำนวนมากกว่าเนื้อหาเหมือนคราวก่อน ๆ พอดีไปค้นว่าคนอื่นเค้าอ่านแล้วได้อะไรกันบ้าง กลับเจอว่ามีคนอ่านเพชรพระอุมา ให้คนพิการทางสายตา หรือเข้าถึงหนังสือได้ยากฟัง

หลังจากที่ได้ฟังการอ่านบางตอนแล้วก็พบว่า คิดถึง "นกน้อย" หรือคณะพากษ์ละคร ที่มีในสมัยก่อนมาก

แต่ในฐานะนักพัฒนาระบบแล้ว ผมกลับอยากพัฒนาระบบ Text to Speech เพื่ออ่านหนังสืออื่น ๆ ด้วย เพราะจะพึ่งการอ่านจากคนต้องใช้เวลาและความร่วมมือค่อนข้างสูง

ที่ดูไว้ก็มี pyTTS, pyTTSx นั่นแหละ