Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon...

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

Transcript of Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon...

Page 1: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

Greg Sabino Mullane 

End Point Corpora=on [email protected]

PgCon 2010

Postgres(for non-Postgres people)

Page 2: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

The Part of Whys 

•  Why MySQL? •  Why Postgres? 

•  Why me? 

•  Why you? 

Page 4: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

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

Page 5: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCng Your App 

•  Schema •  Data •  ApplicaCon •  Support 

Page 6: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Schema 

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

Page 7: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Schema 

•  Tables •  Engines and plugins •  CustomizaCon 

Page 9: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Schema : Data Types : Numbers 

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

•  REAL 

Page 10: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Schema : Data Types : Text 

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

Page 11: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Schema : Data Types : Dates 

•  DATE •  TIMESTAMPTZ 

•  TIME 

•  INTERVAL 

Page 12: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Schema : Data Types : Boolean 

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

Page 13: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Schema : Data Types : Binary 

•  BYTEA •  Internal or external? 

Page 14: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Schema : Data Types : Others 

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

•  CIDR, INET, MACADDR 

•  UUID •  XML 

Page 15: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Schema : Sequence 

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

Page 16: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Schema : DEFAULT 

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

•  No magic 

Page 17: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Schema : Indexes 

•  CREATE INDEX CONCURENTLY foo ON mytab(mycol); 

•  No rebuilding of tables •  Reverse scan, bitmap 

Page 20: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Schema : FuncCons 

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

Page 22: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Data 

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

Page 23: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

ANALYZE 

•  Cost‐based planner •  Autovacuum 

•  IniCal load •  Default_staCsCcs_target 

– 10 – 100 – 1000 – More? 

Page 24: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

VACUUM 

•  MVCC – Oracle vs. Pg •  Rollback segment 

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

Page 25: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : SQL : NULL 

•  NULL is NULL 

Page 27: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : SQL : SELECT 

•  Query planner •  JOINS •  Subselects 

Page 28: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : SQL : GROUP BY 

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

DISTINCT ON

Page 29: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : SQL : full text search 

•  Tsearch2: contrib vs. core •  Powerful, complex 

•  TransacConal! 

Page 31: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : SQL : || 

•  Ugh •  CONCAT •  Wrapper funcCons 

•  NULL an empty string 

Page 35: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : SQL: GRANT 

•  ..and REVOKE •  Almost any object 

•  Immediate effect 

Page 36: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : SQL : locking 

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

Page 37: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : SQL : Aliases 

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

•  SELECT * FROM pg_class c, mytab t… 

Page 38: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : SQL : Advanced 

•  WITH •  Windowing 

•  Recursive funcCons 

Page 39: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

MigraCon : Support 

•  RouCne stuff: autovacuum •  Monitoring (check_postgres) 

•  Backups (MVCC based) 

Page 40: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

Postgres : Quirks : COUNT(*) 

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

Page 43: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

Postgres : Quirks : vacuum 

•  Newer the berer •  autovacuum

Page 45: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

Postgres : Quirks : i18n 

•  SQL_ASCII vs UTF‐8 •  Per database only 

Page 46: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

Postgres : Quirks : Hints 

•  No hints per se •  Smart planner 

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

Page 47: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

Postgres : Quirks : Rules 

•  Last resort. ‘nuff

Page 48: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

Postgres : Strengths 

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

– PAM, ident, Kerberos, LDAP, … 

Page 50: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

Postgres : Infrastructure 

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

•  Build farm 

•  Wiki 

•  Commisest 

Page 53: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

Postgres : Version 

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

•  Naming a version 

Page 55: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

Postgres : Patches 

•  About that flagpole… •  High standards •  \Cming 

•  \dfS 

Page 57: Postgres (for non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres 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 non-Postgres people) · 2020-01-04 · Greg Sabino Mullane End Point Corporaon greg@endpoint.com PgCon 2010 Postgres (for non-Postgres people)

SELECT questions FROM audience;