Postgre sql unleashed

1,173 views
1,087 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,173
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
21
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Postgre sql unleashed

  1. 1. PostgreSQL Unleashed Marian Marinov Head of System Operations at Siteground.com
  2. 2. What is this talk about <ul><ul><ul><ul><ul><li>Storage Architecture
  3. 3. Authentication
  4. 4. Commands
  5. 5. Writing Functions </li></ul></ul></ul></ul></ul>
  6. 6. Storage Architecture
  7. 7. File system structure Default directory /var/lib/pgsql/data (PGDATA) / |- postgresql.conf |- pg_hba.conf |- pg_ident.conf |- postmaster.opts |- postmaster.pid |- PG_VERSION |- base per-database subdirectories |- global cluster-wide tables, such as pg_database |- pg_clog transaction commit status data |- pg_multixact multitransaction status data |- pg_stat_tmp temporary files for the statistics subsystem |- pg_subtrans subtransaction status data |- pg_tblspc symbolic links to tablespaces |- pg_twophase state files for prepared transactions |- pg_xlog WAL (Write Ahead Log) files
  8. 8. File system structure Directory representation: base - |- 1 |- 11510 |- 11511 |- 16384 - 24576 template1=# SELECT datname,datlastsysoid FROM pg_database ; datname | datlastsysoid -----------------+--------------- template1 | 11510 template0 | 11510 postgres | 11510 os | 11510 (5 rows)
  9. 9. File system structure Directory representation: base - |- 11510 - | |- 24765 os# SELECT relname,relfilenode,reltablespace FROM pg_class WHERE relfilenode=24765; Relname | relfilenode | reltablespace -------------+----------------+--------------- disk_io | 24765 | 0 (1 row)
  10. 10. File system structure Table and Indexes are stored in separate files Tables bigger then 1GB are split into different files: filenode.1, filenode.2, ..., filenode.N The 1GB limit can be changed during build using –with-segsize configure option.
  11. 11. DataBase Design DBNAME SCHME OBJECTS(table/view/sequence/domain) database - |- public |- scheme1 - | |- table1 | |- table2 | |- view1 | |- view2 | |- seq1 | - seq2 - scheme2
  12. 12. Authentication
  13. 13. Authentication pg_hba.conf – Host based authentication pg_ident.conf – Identification information Authentication methods: <ul><ul><li>trust – anyone to any DB
  14. 14. reject – do not allow any connections (useful for filtering)
  15. 15. ident -use the system user name or what identd provided
  16. 16. password – use cleartext passwords
  17. 17. md5 – md5 encrypted passwords
  18. 18. pam – use the Password Authentication Mechanism system </li></ul></ul>
  19. 19. Authentication pg_hba.conf: local database user auth-method [auth-options] host database user CIDR-address auth-method [auth-options] hostssl database user CIDR-address auth-method [auth-options] hostnossl database user CIDR-address auth-method [auth-options] host database user IP-address IP-mask auth-method [auth-options] hostssl database user IP-address IP-mask auth-method [auth-options] hostnossl database user IP-address IP-mask auth-method [auth-options]
  20. 20. Authentication pg_ident.conf: map-name system-username database-username example: # MAPNAME SYSTEM-USERNAME PG-USERNAME omicron bryanh bryanh omicron ann ann # bob has user name robert on these machines omicron robert bob # bryanh can also connect as guest1 omicron bryanh guest1
  21. 21. Permissions Every object has its own privileges: <ul><ul><li>Database privileges
  22. 22. Scheme privileges
  23. 23. Table privileges
  24. 24. View privileges
  25. 25. Sequence privileges
  26. 26. Function privileges
  27. 27. Domain privileges </li></ul></ul>
  28. 28. Commands
  29. 29. Commands Manage users: CREATE ROLE xxx PASSWORD 'string' ALTER ROLE username PASSWORD 'string' ALTER ROLE username SET enable_indexscan TO off ALTER ROLE username RESET varname GRANT CONNECT ON DATABASE 'xxx' TO 'username' GRANT UPDATE ON accounts TO username REVOKE ALL ON accounts FROM PUBLIC DROP ROLE username
  30. 30. Commands Manage databases: List all databases: SELECT datname FROM pg_database; Or use l from the CLI. Create DB using the default template: CREATE DATABASE name; CREATE DATABASE name OWNER username; # createdb -O rolename dbname Create DB using different templates: CREATE DATABASE dbname TEMPLATE template0; # createdb -T template0 dbname
  31. 31. Commands Manage databases: ALTER DATABASE mydb SET geqo TO off; DROP DATABASE name; dropdb dbname Manage tablespaces: CREATE TABLESPACE space1 LOCATION '/mnt/sda1/pgsql/data'; CREATE TABLE foo(i int) TABLESPACE space1; SET default_tablespace = space1; CREATE TABLE foo(i int); SELECT spcname FROM pg_tablespace;
  32. 32. Commands Database maintanance: Vaccuming <ul><li>To recover or reuse disk space occupied by updated or deleted rows.
  33. 33. To update data statistics used by the PostgreSQL query planner.
  34. 34. To protect against loss of very old data due to transaction ID wraparound. </li></ul>Routine Reindexing Log File Maintenance $ pg_ctl start | rotatelogs /var/log/pgsql_log 86400
  35. 35. Backup
  36. 36. Backup <ul><ul><li>SQL dump
  37. 37. File system level backup
  38. 38. Continuous archiving </li></ul></ul>
  39. 39. Writing Functions

×