25
Heroku Postgres SQL Tips, Tricks, Hacks Craig Kerstiens, Heroku, Growth Hacker @craigkerstiens

Heroku Postgres SQL Tips, Tricks, Hacks

Embed Size (px)

Citation preview

Heroku Postgres

SQL Tips, Tricks, Hacks

Craig Kerstiens, Heroku, Growth Hacker

@craigkerstiens

Safe Harbor

Safe harbor statement under the Private Securities Litigation Reform Act of 1995:

This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if

any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-

looking statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of

product or service availability, subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of

management for future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments

and customer contracts or use of our services.

The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our

service, new products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth,

interruptions or delays in our Web hosting, breach of our security measures, the outcome of intellectual property and other l itigation, risks associated

with possible mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain,

and motivate our employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling

non-salesforce.com products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the

financial results of salesforce.com, inc. is included in our annual report on Form 10-Q for the most recent fiscal quarter ended July 31, 2012. This

documents and others containing important disclosures are available on the SEC Filings section of the Investor Information section of our Web site.

Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and may

not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently

available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.

Craig Kerstiens

Growth Hacker

@craigkerstiens

DISCLAIMER

SQL Ahead

Why Postgres

“ its the emacs of

databases”

Datatypes

smallint bigint

integer

numeric float serial

money char

varchar text

bytea

timestamp

timestamptz date

time timetz

interval boolean

enum

point

line

polygon

box

circle

path

inet

cidr

macaddr tsvector

tsquery

array XML

UUID

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Datatypes CREATE TABLE purchases ( id integer NOT NULL, user_id integer, items decimal(10,2) [100][1],

items[x][0] = item_iditems [x][1] = qty

items [x][2] = price

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Datatypes INSERT INTO purchases VALUES (1, 37, '{{15.0, 1.0, 25.0}, {15.0, 1.0, 25.0}}', now()); INSERT INTO purchases VALUES (2, 2, '{{11.0, 1.0, 4.99}}', now());

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Datatypes

smallint bigint

integer

numeric float serial

money char

varchar text

bytea

timestamp

timestamptz date

time timetz

interval boolean

enum

point

line

polygon

box

circle

path

inet

cidr

macaddr tsvector

tsquery

array XML

UUID

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Languages python

ruby

pgsql

SQL tcl sh

R V8

Java javascript

lolcode

scheme

php

j

lua psm

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Languages CREATE OR REPLACE FUNCTION total(decimal(10,2)[][]) RETURNS decimal(10,2) AS $$DECLARE s decimal(10,2) := 0; x decimal[];BEGIN FOREACH x SLICE 1 IN ARRAY $1 LOOP s := s + (x[2] * x[3]); END LOOP; RETURN s;END;$$ LANGUAGE plpgsql;

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Languages FOREACH x SLICE 1 IN ARRAY $1 LOOP s := s + (x[2] * x[3]); END LOOP; RETURN s;

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Extensions dblink hstore

citext

ltree isn

cube

pgcrypto

tablefunc

uuid-ossp

earthdistance

trigram

fuzzystrmatch pgrowlocks

pgstattuple

btree_gist

dict_int dict_xsyn unaccent

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Extensions CREATE EXTENSION hstore;

CREATE TABLE users ( id integer NOT NULL, first_name

character varying(50), last_name character varying(50), email

character varying(255), data hstore, created_at timestamp without

time zone, last_login timestamp without time zone);

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Extensions INSERT INTO users

VALUES (

1,

'Craig',

'Kerstiens',

'[email protected]',

'sex => "M"',

now(),

now()

); Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Window Functions Example:

Biggest spender by

state

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

SELECT

email,

users.data->'state',

sum(total(items)),

rank() OVER

(PARTITION BY users.data->'state'

ORDER BY sum(total(items)) desc)

FROM

users, purchases

WHERE purchases.user_id = users.id

GROUP BY 1, 2;

Window Functions

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Foreign Data Wrappers oracle mysql

informix

twitter

files

www couch

sybase

ldap

odbc

s3 redis jdbc

database.com

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

CREATE EXTENSION redis_fdw;

CREATE SERVER redis_server FOREIGN DATA WRAPPER redis_fdw

OPTIONS (address '127.0.0.1', port '6379');CREATE FOREIGN TABLE

redis_db0 (key text, value text) SERVER redis_server OPTIONS (database

'0');CREATE USER MAPPING FOR PUBLIC SERVER redis_server

OPTIONS (password 'secret');

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Foreign Data Wrappers

Etc.

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

CREATE INDEX

CONCURRENTLY;

Doesn’t lock table

Etc. CREATE INDEX

CONCURRENTLY;

CREATE INDEX ON foo

WHERE x=y;

Indexes only on

condition

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Etc. CREATE INDEX

CONCURRENTLY;

CREATE INDEX ON foo

WHERE x=y;

Listen/Notify

Pub sub in Postgres

Datatypes | Custom Languages | Extensions | NoSQL | Window Functions |

FDW | Etc.

Why Postgres Open Community

Broad feature set (datatypes, extensions, functions, foreign data

wrappers, etc)

Reliability

Craig Kerstiens

Growth Hacker @craigkerstiens