This talk is for people relatively new to PostgreSQL who are wondering:
How do I get going with PostgreSQL -- in a way that won’t create problems later on!
We’ll go over best practice in:
Table design
Indexing
PostgreSQL types
Stored procedures -- when & how to use, when not
Triggers
How to work with a web framework (i.e. Ruby-on-Rails): what works belongs in the framework, what should be done in the database
Error & exception management
Doing the right amount of planning
Why you might want to build the help system first, and use it to help build the rest.
Nistica has its ownership in Japan, engineering in New Jersey, & manufacturing in Vietnam so we’ll take a special look at:
Handling different languages & character sets
Timestamps & time zones
How to sync data from one part of the world to another without letting data fall on the floor or creating infinite loopiness.
Nistica has gone from startup to world player in the manufacture of optical switches. It has run its manufacturing on PostgreSQL from the start, using PostgreSQL to drive every step from assembly to quality assurance & tracking all part data in the database.
Going from the ad hoc procedures appropriate for a startup to the disciplined approaches required by the world market has taught us a lot about how to get the best out of PostgreSQL.
We’ve learned a lot from the PostgreSQL community; now we’d like to share some of what we’ve learned from our experience.
2. ÊWavelength Selective Switches (WSS)☑
Agile Optical Networks
2
MSAN 10Gbà
100Gb
LOCAL HD
DISTRIBUTION
IPDSLAM
IPDSLAM
Data Center
Access Metro Edge Metro Core Long-Haul
**
Æ Ê
Ê
☑ ☑
☑
☑
CRITICAL BUILDING BLOCKS à AGILE OPTICAL NETWORKS
Tunable Pluggable Lasers
* Optical Channel Monitors (OCM)
Agile Optical Networks
☑
☑
3. 3
INPUT EXPRESS
DROP
1x2 WSS
1) Automated routing of individual channels
– Reconfigurable Optical Add-Drop Mux
(ROADM)
2) Dynamic channel power management
– Allows long distance transmission
3) Integrated optical channel monitoring
– Information feedback for power
management
Wavelength Selective Switch (WSS)
4. What is a Zeppelin?
jashmead@nistica.com 4
•Manufacturing
Database
•Drives manufacture via
automated checklists
•Work flow management
•Training checks
•Module data
•Customer datasheets
•Process engineering
6. How to design a database
• Listen to the client talk about their business
• Where they use nouns, use tables.
• Where they use verbs, use functions.
• Turn their adjectives into columns,
• and their adverbs into the parameters for
the functions.
jashmead@nistica.com 6
7. It’s about the relationships
• Like nouns, tables
only valuable in
combination
• Foreign keys
• Bones and
tendons
• Documentation
• Performance
jashmead@nistica.com 7
8. Keys are primary
• Anonymous
• Natural keys
• Sequences
• Fast enough for
temp tables
• And can set
ranges in useful
ways
jashmead@nistica.com 8
CREATE SEQUENCE
serials_id_seq
START WITH
1000000000
INCREMENT BY 1
MINVALUE
1000000000
MAXVALUE
1249999999
CACHE 1;
9. Timestamps — Or where on the planet are we?
jashmead@nistica.com 9
10. Time to pull the trigger
• Timestamps
• Keep scope
limited
• Complex
validation
• History
• Error trapping
jashmead@nistica.com 10
CREATE TRIGGER
serials_timestamp
BEFORE UPDATE ON
serials FOR EACH ROW
EXECUTE PROCEDURE
timestamp_trg();
11. CREATE FUNCTION timestamp_trg() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
/*
force timestamps
-- created_at & updated_at fields exist
-- set Model.record_timestamps = false;
-- this keeps ActiveRecord from trying to do the
update
*/
if new.created_at is null
then
new.created_at = now();
end if;
new.updated_at = now();
return new;
end;
$$;
Stored Procedures - Muscles of a Database
jashmead@nistica.com 11
12. Staying true to type
•Rich type system
•Induced types
•Defined domains
•Conversions
•General functions
jashmead@nistica.com 12
CREATE FUNCTION
sn_process(sn0 sn_t, stuff
description_t)
LANGUAGE plpgsql
AS $$
declare
sn1 sn_t;
serial_id1 serials.id
%type;
top_serial_id1
serials.id%type;
begin
13. Metadata
jashmead@nistica.com 13
select a.attname
from pg_catalog.pg_attribute a,
pg_catalog.pg_class c,
pg_catalog.pg_namespace n
where a.attrelid = c.oid
and c.relnamespace = n.oid
and c.relname = ?
and n.nspname = 'public'
and a.attnum > 0 -- gets rid
of internal columns
and a.attisdropped = 'f'
order by a.attnum