postgres for mysql (and other database) people

58
Greg Sabino Mullane End Point Corpora=on [email protected] Postgres (for non-Postgres people)

Upload: command-prompt-inc

Post on 27-Jan-2015

115 views

Category:

Documents


1 download

DESCRIPTION

Greg MullaneUpgrading from MySQL or other database to Postgres? While Postgres strives to be SQL standard compliant as much as possible, there are plenty of quirks and features you should know about. We'll also talk about some of the ways in which the Postgres community, infrastructure, and development differs from other database projects.

TRANSCRIPT

Page 1: Postgres for MySQL (and other database) people

Greg Sabino Mullane 

End Point Corpora=on [email protected]

Postgres(for non-Postgres people)

Page 2: Postgres for MySQL (and other database) people

SELECT SUM(info) FROM talk; 

•  Postgres philosophy and history •  Conversion advice •  Postgres gotchas •  Postgres limitaCons and features 

•  OrganizaCon and community 

•  Development and infrastructure 

Page 3: Postgres for MySQL (and other database) people

The Part of Whys 

•  Why MySQL? •  Why Postgres? 

•  Why me? 

•  Why you? 

Page 4: Postgres for MySQL (and other database) people

•  Postgres? PostgreSQL? Postgre? PostgresSQL? •  Philosphy •  Mercifully quick history lesson 

Page 5: Postgres for MySQL (and other database) people

MigraCng Your App 

•  Schema •  Data •  ApplicaCon •  Support 

Page 6: Postgres for MySQL (and other database) people

MigraCon : Schema 

•  Mysqldump? •  --compatible=postgresql --no-data •  Redesign •  Conversion tools

Page 7: Postgres for MySQL (and other database) people

MigraCon : SQL Spec 

•  Who cares? •  Postgres vs. the spec •  MySQL vs. the spec 

•  Oracle vs. the spec •  DB2 vs. the spec •  MSSQL vs. the spec 

Page 8: Postgres for MySQL (and other database) people

MigraCon : Schema 

•  Tables •  Engines and plugins •  CustomizaCon 

Page 9: Postgres for MySQL (and other database) people

MigraCon : Schema : Data Types : Numbers 

•  INTEGER (smallint, bigint, serial) •  NUMERIC (double precision, money) 

•  REAL 

Page 10: Postgres for MySQL (and other database) people

MigraCon : Schema : Data Types : Text 

•  TEXT •  VARCHAR(n) •  CHAR(n) 

Page 11: Postgres for MySQL (and other database) people

MigraCon : Schema : Data Types : Dates 

•  DATE •  TIMESTAMPTZ 

•  TIME 

•  INTERVAL 

Page 12: Postgres for MySQL (and other database) people

MigraCon : Schema : Data Types : Boolean 

•  BOOL •  TRUE, ‘t’, ‘y’, ‘yes’, ‘on’, 1 •  FALSE, ‘f’, ‘n’, ‘no’, ‘off’, 0 

Page 13: Postgres for MySQL (and other database) people

MigraCon : Schema : Data Types : Binary 

•  BYTEA •  Internal or external? 

Page 14: Postgres for MySQL (and other database) people

MigraCon : Schema : Data Types : Others 

•  Geometric (line, path, box, circle, polygon) •  Arrays •  ENUM 

•  CIDR, INET, MACADDR 

•  UUID •  XML 

Page 15: Postgres for MySQL (and other database) people

MigraCon : Schema : Sequence 

•  Auto‐increment •  SEQUENCE •  SERIAL •  INTEGER NOT NULL DEFAULT nextval(‘foo’) 

Page 16: Postgres for MySQL (and other database) people

MigraCon : Schema : DEFAULT 

•  cdate TIMESTAMPTZ NOT NULL DEFAULT now() •  (almost) anything at all 

•  No magic 

Page 17: Postgres for MySQL (and other database) people

MigraCon : Schema : FOREIGN KEYS 

•  Fully supported •  ON DELETE CASCADE •  ON DELETE RESTRICT •  ON DELETE SET NULL •  ON DELETE SET DEFAULT •  DEFERRED INITIALLY DEFERRED 

Page 18: Postgres for MySQL (and other database) people

MigraCon : Schema : Indexes 

•  CREATE INDEX foo ON mytab(mycol); •  CREATE INDEX foo2 ON mytab(mycol) WHERE size = ‘grande’; 

•  CREATE INDEX foo3 ON mytab(LOWER(blurb)); 

•  CREATE INDEX foo4 ON mytab(LOWER(blurb)) WHERE size = ‘grande’; 

Page 19: Postgres for MySQL (and other database) people

MigraCon : Schema : Indexes 

•  CREATE INDEX CONCURENTLY foo ON mytab(mycol); 

•  No rebuilding of tables •  Reverse scan, bitmap 

Page 20: Postgres for MySQL (and other database) people

MigraCon : Schema : Triggers 

•  CREATE TRIGGER mytrig BEFORE INSERT OR DELETE OR UPDATE ON mytab FOR EACH ROW EXECUTE PROCEDURE somefunc(); 

•   Aside: funcCons and languages 

Page 21: Postgres for MySQL (and other database) people

MigraCon : Schema : FuncCons 

•  Very custom •  Pl/PGSQL == Pl/SQL •  Very powerful 

Page 22: Postgres for MySQL (and other database) people

MigraCon : Data 

•  No autocommit, fsync off, no autovacuum •  COPY vs. INSERT •  Turn off indexes and triggers (FK) •  ANALYZE 

Page 23: Postgres for MySQL (and other database) people

ANALYZE 

•  Cost‐based planner •  Autovacuum 

•  IniCal load •  Default_staCsCcs_target 

– 10 – 100 – 1000 – More? 

Page 24: Postgres for MySQL (and other database) people

VACUUM 

•  MVCC – Oracle vs. Pg •  Rollback segment 

•  Concurrency and locking •  Table bloat •  VACUUM FULL, CLUSTER, REINDEX 

Page 25: Postgres for MySQL (and other database) people

MigraCon : ApplicaCon 

•  aka SQL •  Clients (PHP excepCon) •  SQL modes 

– TradiConal – Postgresql 

•  “Any client can change its own session sql_mode value at any Cme.” 

Page 26: Postgres for MySQL (and other database) people

MigraCon : SQL : NULL 

•  NULL is NULL 

Page 27: Postgres for MySQL (and other database) people

MigraCon : SQL : SELECT 

•  Query planner •  JOINS •  Subselects 

Page 28: Postgres for MySQL (and other database) people

MigraCon : SQL : GROUP BY 

•  SELECT a,b,c FROM mytab GROUP BY a,b •  Standard or not? 

DISTINCT ON

Page 29: Postgres for MySQL (and other database) people

MigraCon : SQL : Text match 

•  CHAR / VARCHAR / TEXT case sensiCve •  SELECT count(*) FROM mytab WHERE mycol = ‘Fred’; 

•  …WHERE LOWER(mycol) = ‘fred’; 

Page 30: Postgres for MySQL (and other database) people

MigraCon : SQL : full text search 

•  Tsearch2: contrib vs. core •  Powerful, complex 

•  TransacConal! 

Page 31: Postgres for MySQL (and other database) people

MigraCon : SQL : DELETE 

•  One table only •  No LIMIT, no ORDER BY 

•  No QUICK, LOW PRIORITY, IGNORE 

•  DELETE FROM ONLY mytab WHERE… 

•  DELETE FROM mytab USING otab WHERE mycol = otab.id AND otab.size = 'foo'; 

•  DELETE FROM mytab WHERE mycol IN (SELECT id FROM otab WHERE size = ‘foo’); 

Page 32: Postgres for MySQL (and other database) people

MigraCon : SQL : UPDATE 

•  Only one table •  No ORDER BY, LIMIT, etc. 

•  UPDATE mytab SET mycol=123 FROM otab WHERE mycol = otab.id AND otab.size = ‘foo’; 

Page 33: Postgres for MySQL (and other database) people

MigraCon : SQL : INSERT 

•  Only one table •  No REPLACE or ON DUPLICATE KEY UPDATE •  UPSERT via plpgsql or app logic •  DEFAULT •  INSERT INTO mytab DEFAULT VALUES 

Page 34: Postgres for MySQL (and other database) people

MigraCon : SQL : || 

•  Ugh •  CONCAT •  Wrapper funcCons 

•  NULL an empty string 

Page 35: Postgres for MySQL (and other database) people

MigraCon : SQL: GRANT 

•  ..and REVOKE •  Almost any object 

•  Immediate effect 

Page 36: Postgres for MySQL (and other database) people

MigraCon : SQL : locking 

•  Use sparingly •  Share, exclusive •  Advisory •  ALTER TABLE, REINDEX, VACUUM FULL 

Page 37: Postgres for MySQL (and other database) people

MigraCon : SQL : Aliases 

•  AS opConal but recommended •  SELECT SUM(mycol) AS mysum 

•  SELECT * FROM pg_class c, mytab t… 

Page 38: Postgres for MySQL (and other database) people

MigraCon : SQL : Advanced 

•  WITH •  Windowing 

•  Recursive funcCons 

Page 39: Postgres for MySQL (and other database) people

MigraCon : Support 

•  RouCne stuff: autovacuum •  Monitoring (check_postgres) 

•  Backups (MVCC based) 

Page 40: Postgres for MySQL (and other database) people

Postgres : Quirks : lowercase 

•  Implicit case folding •  CREATE TABLE abc (A int); •  CREATE TABLE ABC (a int); •  CREATE TABLE “abc” (“a” int); •  CREATE TABLE “ABC” (“A” int); •  SELECT * FROM “ABC” WHERE “A”=123; 

Page 41: Postgres for MySQL (and other database) people

Postgres : Quirks : CasCng 

•  8.3 removed some implicit casts •  SELECT 1 = ‘1’::text; •  ERROR: operator does not exist: integer=text •  1. Fix the app •  2. Add the casts back in 

Page 42: Postgres for MySQL (and other database) people

Postgres : Quirks : COUNT(*) 

•  MyISAM vs INNODB •  Use an index •  Triggers •  SELECT reltuples FROM pg_class WHERE relname = ‘foo’; 

Page 43: Postgres for MySQL (and other database) people

Postgres : Quirks : Schemas 

•  Database versus schemas versus cluster •  Changes are cheap •  No name‐based schemas 

•  The ‘public’ schema 

•  contrib/dblink 

Page 44: Postgres for MySQL (and other database) people

Postgres : Quirks : vacuum 

•  Newer the berer •  autovacuum

Page 45: Postgres for MySQL (and other database) people

Postgres : Quirks : i18n 

•  SQL_ASCII vs UTF‐8 •  Per database only 

Page 46: Postgres for MySQL (and other database) people

Postgres : Quirks : Hints 

•  No hints per se •  Smart planner 

•  SET enable_* EXPLAIN •  Few other knobs – per cluster 

Page 47: Postgres for MySQL (and other database) people

Postgres : Quirks : Rules 

•  Last resort. ‘nuff

Page 48: Postgres for MySQL (and other database) people

Postgres : Drawbacks 

•  ReplicaCon •  In‐place upgrade (pg_migrator, Bucardo) 

•  Tuned for a toaster •  Name 

•  Publicity and markeCng 

pg_upgradepg_upgrade

Page 49: Postgres for MySQL (and other database) people

Postgres : Strengths 

•  Custom everything •  TransacConal DDL •  PostGIS •  Query planner •  Support •  AuthenCcaCon: pg_hba.conf 

– PAM, ident, Kerberos, LDAP, … 

Page 50: Postgres for MySQL (and other database) people

Postgres : OrganizaCon 

•  Who? •  Transparent meritocracy •  “core” •  Commirers •  Mailing list, IRC, wiki •  Company roles •  Spread the risk •  Cannot be bought (assets or people) 

Page 51: Postgres for MySQL (and other database) people

Postgres : Community 

•  Core vs. “core” •  Pgfoundry, github, bucardo.org, … •  PostGIS •  Mailing list? Bug report? User? •  InteracCon! •  Wiki, advocacy, sysadmin, docs, packagers •  Clients, tools, replicaCon systems •  Volunteers (IRC) 

Page 52: Postgres for MySQL (and other database) people

Postgres : Infrastructure 

•  See Dave’s talk later today •  Servers, companies 

•  Build farm 

•  Wiki 

•  Commisest 

Page 53: Postgres for MySQL (and other database) people

Postgres : Development 

•  Show me the money! •  Individual developers vs. companies 

•  CVS vs. git •  Commirers vs. patchers vs. reviewers 

git wins! :)git wins!

Page 54: Postgres for MySQL (and other database) people

Postgres : Version 

•  Roughly every year •  8.3  8.3.10 •  Major version, minor version, revision 

•  Naming a version 

Page 55: Postgres for MySQL (and other database) people

Postgres : Patches 

•  Bug report, mailing list post, TODO list •  Run up the flagpole (Tom Lane test) 

–  Stable? –  Useful? –  Spec compliant? –  Side effects? –  Best approach to problem? 

•  Diff format, formaung, docs •  Added to patch queue, commisest •  Patch reviewer •  Commit (20 vs 300) 

Page 56: Postgres for MySQL (and other database) people

Postgres : Patches 

•  About that flagpole… •  High standards •  \Cming 

•  \dfS 

Page 57: Postgres for MySQL (and other database) people

Postgres : Licensing 

•  BSD (or MIT) •  Free as in speech •  Free as in beer •  Plays well with others (PostGIS) 

PostgreSQL license

Page 58: Postgres for MySQL (and other database) people

SELECT questions FROM audience;