SlideShare a Scribd company logo
1 of 31
Download to read offline
Debugging With
PostgreSQL
— A Strategic
Approach
• Debugging is the elephant in the
room
• Can take 50 to 90% of the time
• But gets 2% of the attention!
• PostgreSQL has great tools
• But they are most effective when
deployed as part of an overall
strategy
John Ashmead
john.ashmead@ashmeadsoftware.com
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Cleaning up after
the elephant
• Small shops: one or two
people
• Legacy code, no
documentation or handoff
• Limited resources
• Considerable time
pressure
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
See if you can destroy
this
• Write from top to bottom
• Use meaningful variable
names
• Throw the first one out
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
rogue particles
• Random inputs
• Generate_series()
• Fuzzing
• Chaos Monkey
SELECT * FROM generate_series(2,4);
generate_series
-----------------
2
3
4
(3 rows)
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
The root cause
• Generally self-
inflicted
• Can also be the
result of bitrot,
• New requirements,
• Changing
expectations, …
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
foreign keys
• Correctness
• Speed
• Documentation
• Meta-programming
CREATE TABLE cities (
city varchar(80) primary key,
location point
);
CREATE TABLE weather (
city varchar(80) references
cities(city),
temp_lo int,
temp_hi int,
prcp real,
date date
);
There are (at least) three bugs here;
you have two slides to find them!
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
timestamps not
updated
• Add fields regardless
• Ruby-on-rails will update
for you
• But more reliable in
PostgreSQL
• Using simple naming
convention lets you write
function once
CREATE FUNCTION timestamp_trg()
RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
/* assume we have updated_at
and created_at fields on the
table */
if new.created_at is null
then
new.created_at = now();
end if;
new.updated_at = now();
return new;
end;
$$;
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Krungthepmahanakhon Amonrattanakosin
Mahintharayutthaya Mahadilokphop
Noppharatratchathaniburirom
Udomratchaniwetmahasathan
Amonphimanawatansathit
Sakkathattiyawitsanukamprasit
create domain city_t as text not null;
create domain location_t as point;
create domain fahrenheit_t as int check(value between -460 and 10000);
create domain precipitation_t as real check(value >= 0);
CREATE TABLE cities (
city city_t primary key,
location location_t
);
CREATE TABLE weather (
city city_t references cities(city),
temp_lo fahrenheit_t,
temp_hi fahrenheit_t,
prcp precipitation_t,
weather_date date
);
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Split Brain Problem
create table
patients_whose_doctors_pays (
patient_id it_t,
name name_t,
address addr_t,
doctor_id id_t references
doctors(id),
medical_stuff
medical_stuff_t,
...
);
create table
patients_who_insurers_pays (
patient_id it_t,
name name_t,
address addr_t,
primary_insurance_id id_t
references insurers(id),
medical_stuff medical_stuff_t,
...
);
create view patients as
select 'doctor' as pay_type,
patient_id, name, address, medical_stuff, ...
from patients_whose_doctor_pays
union all
select 'insurance',
patient_id, name, address, medical_stuff, ...
from patients_whose_insurance_pays
;
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Write perfect code
• At a conference, I
asked Bjarne:
• How do I debug C++?
• “Write perfect code!”
• Annoying …
• But not without a
grain of truth
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Code has two readers
• Should be clear to
intended reader
• Use meaningful names
• Flow from top to bottom
• Use logical, consistent
indenting
• Use appropriate comments
• Use house style
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Cat in a Box
• Claw in every direction
until you see daylight
• Sometimes necessary
• Fuzzing & the Chaos
Monkey
• Get some sleep
• Get a 2nd pair of eyes on
the problem
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Linear Debugging
• Raise notice, print
statements
• Debuggers
• Linear process, so
slow
• Should usually be
the last resort
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
drop table if exists results_step1;
create temp table results_step1 as
select field1, field2, field3 from
fairly_complex_query;
get diagnostics rows_found1 = row_count;
raise notice '%: % step one rows found',
((extract(epoch from clock_timestamp())
- extract(epoch from
transaction_timestamp)) * 1000)::integer,
rows_found1;
Instrumentation
• Debuggers not all that
helpful — too slow
• Preplanned & targeted
has worked better for
me
• As has use of unlogged
tables to track
intermediate steps in
long computations
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
How to fix
• Don’t fix right away
• Write error report/test; verify
red
• Write fix; verify green
• Bugs travel in packs: look for
similar problems
• Find root cause, not just
immediate trigger
• See if you can prevent entirely
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Technical Debt
• Hard to read
• Code smells: lots of
cut & paste, irregular
indenting, patches of
dead code, …
• Small changes tend
to large, unwelcome
side effects
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Repair, refactor,
Rebuild
• Don’t kill the patient
• Birchall - Re-
Engineering Legacy
Software
• Ellnestam & Brolund
- Mikado Method
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Systems Test
http://sunnyday.mit.edu/accidents/Ariane5accidentreport.html
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
User Error —
Delete User
• Educate users
• Show users what
is going on
• Show them the
code
Starting
Screen
URL for Context
Intermediate
Results
Screen
Context for
decision
Result
Key Pt
Who/where/when/…
if pat_age > age_cutoff and test_res >
max_norm then
recommend(patient_id, followup);
else
routine_followup(patient_id, test_id);
end if;
Other Stuff
Bad Stuff
Useful Comment
on Bad Stuff
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Don’t Repeat Yourself
• Make
• Small shell
scripts
• Function calls
• Third normal
form
• Continuous
automation
#!/bin/bash
# sx -- sql execute -- useful tool
for f in $*
do
bn=`basename $f .sql`
sql="${bn}.sql"
out="${bn}.out"
if [ ! -s $sql ]
then
echo "No $sql file"
exit 1
fi
time psql -v ON_ERROR_STOP=1 -X -f $sql
> $out 2>&1
if [ $? -ne 0 ]
then
echo "$sql failed: see $out"
exit 1
fi
ls -l $out
done
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Automating common
tasks
• Factor out
common
parts of
frequent tasks
• Reports,
warnings,
errors,…
• Let you get
an overview
create table report_types (
id primary key,
report_name,
title name_t,
description desc_t,
parameter_list jsonb, -- name + allowed values,
fail_count count_t,
times_run count_t,
total_time_taken millisecond_t,
administrators_only boolean,
created_at tz_t,
last_run_at tz_t,
…
); create table reports (
id primary key,
report_type_id references
report_types(id),
parameters_used jsonb,
run_by references users(id),
success ok_t,
time_taken millisecond_t,
rows_found count_t,
run_at tz_t
);
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Code Generators
• Called in for portfolio mgmt
problem
• High security reqs
• Common patterns to most queries,
usually joining two tables
• Could use meta data to build SPs
• In 80 minutes the CG ground out
110,000 lines of code
• A few stragglers had to be hacked
a bit or even built from scratch
• 3 month contract done in 2 weeks
Security
check
Get rows, usually just
a master-detail join
Log stuff
Return
rows
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Foreign Data Wrappers
• PostgreSQL
good central
exchange
• Pull data in,
clean up, make
it available
• Rapid compare
of old & new
versionsTarget DbTest Db
Right DbLeft Db
Development Schema
Octopus Database
Left Schema Right Schema
Test Schema Target Schema
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Divide and Conquer
• Break into parts
• Plan dev path
• Use simple
message protocols
• Program
defensively
• Write self-
debugging code
• Debug scientificallySolution
Problem
System Test
Planning
ComponentM
essages
Component
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
plan development path
• Get something
useful up quickly
• Stub later bits out
• Share with users
• Develop
incrementally
• Agile has the right
idea
As when driving,
think globally,
act locally.
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
KISS the Messenger
• Messaging key to
Divide & Conquer
• KISS applies to
messaging as well
• CSV is just fine for
many applications
• And well supported
by PostgreSQL
-- push out from source
-- one of the few cases (in working code)
where 'select *' is considered helpful
psql -h source_database <<!
copy (select * from parent1) to '/tmp/
parent1.csv' csv header
copy (select * from child1) to '/tmp/
child1.csv' csv header
…
!
-- make sure the CSVs look good, then:
-- pull in on target
psql -h target_database <<!
copy parent1 from '/tmp/parent1.csv' csv
header
copy child1 from '/tmp/child1.csv' csv
header
…
!
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Program defensively
Validate ajax
request before
sending
Browser Web Server PostgreSQL
Validate all
parameters
before call DB
URL
Validate all
parameters
before acting
on them
SP()
Return
standardized
response
Rows
Validate &
return
standardized
response
Validate
& present
standardized
response in
standardized
way
JSON
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
create or replace function ssn_set( person_id0 people.id%type, ssn0 people.ssn%type,
debug_flag0 boolean default false)
returns ok_t as $$
-- Debugging with PostgreSQL example -- John Ashmead -- for FOSSCON 8/17/2019
declare
person_id1 people.id%type; -- more specific than id_t
ssn1 people.ssn%type; -- could use ssn_t
begin
if debug_flag0 then
raise notice 'ssn_set(%, %)' person_id0, ssn0;
end if;
select id into person_id1 from people where id = person_id0;
-- could also check number of rows found using "get diagnostics"
if person_id1, ssn1 is null then
raise exception 'ssn_set: person_id0 % is not in people table', person_id0;
end if;
-- could trap non-unique exception here
select id into person_id1 from people
where ssn = ssn0 and id != person_id0;
if person_id1 is not null then
raise exception 'ssn_set: ssn % is already in use by id %', ssn0, person_id0;
end if;
if debug_flag0 then
raise notice 'ssn_set: person %: ssn changed from % to %', person_id0, ssn0, ssn1;
end if;
return true;
end; $$ language plpgsql;
Self-debugging Code
and self-checking
Turn on tracing at will
Especially valuable in crunch mode
Self-documenting
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Gozinta’s good,
Comezouta’s Bad
• Double check the Gozinta’s
anyway
• Take notes
• Have a hypothesis (or three)
• But don’t guess, instrument
• Prefer binary search to
linear; keep cutting the
hiding space for bugs in half
Lots of weird,
complicated stuff
happens
Comezouta’s
Bad
Part 1 ? Weird/2
Part 2 ? Weird/2
Gozinta’s good
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
Plus/Minus
• More work upfront
• No silver bullet
• More reliability
• Less total time coding
• More interesting
products
http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019
References
• St. Jerome - RTFM
• Mlodgenski - PostgreSQL Server
Programming
• Fitzpatrick - Timeless Laws
• Agans - Debugging: 9 Indispensable
Rules
• Ousterhout - Philosophy of Software
Design

More Related Content

More from John Ashmead

Practical Telepathy: The Science & Engineering of Mind-Reading
Practical Telepathy:  The Science & Engineering of Mind-ReadingPractical Telepathy:  The Science & Engineering of Mind-Reading
Practical Telepathy: The Science & Engineering of Mind-ReadingJohn Ashmead
 
From Startup to Mature Company: PostgreSQL Tips and techniques
From Startup to Mature Company:  PostgreSQL Tips and techniquesFrom Startup to Mature Company:  PostgreSQL Tips and techniques
From Startup to Mature Company: PostgreSQL Tips and techniquesJohn Ashmead
 
Practical Telepathy: The Science & Engineering of Mind-Reading
Practical Telepathy:  The Science & Engineering of Mind-ReadingPractical Telepathy:  The Science & Engineering of Mind-Reading
Practical Telepathy: The Science & Engineering of Mind-ReadingJohn Ashmead
 
Stargates: Theory and Practice
Stargates:  Theory and PracticeStargates:  Theory and Practice
Stargates: Theory and PracticeJohn Ashmead
 
StarGates: Theory and Practice
StarGates:  Theory and PracticeStarGates:  Theory and Practice
StarGates: Theory and PracticeJohn Ashmead
 
Star Gates: the Theory and Practice
Star Gates:  the Theory and PracticeStar Gates:  the Theory and Practice
Star Gates: the Theory and PracticeJohn Ashmead
 
Time to the power of Tim
Time to the power of TimTime to the power of Tim
Time to the power of TimJohn Ashmead
 
How many universes are there, anyway
How many universes are there, anywayHow many universes are there, anyway
How many universes are there, anywayJohn Ashmead
 
A Quantum of Mystery
A Quantum of MysteryA Quantum of Mystery
A Quantum of MysteryJohn Ashmead
 
Converting from MySQL to PostgreSQL
Converting from MySQL to PostgreSQLConverting from MySQL to PostgreSQL
Converting from MySQL to PostgreSQLJohn Ashmead
 
Seven War Stories and a Moral
Seven War Stories and a MoralSeven War Stories and a Moral
Seven War Stories and a MoralJohn Ashmead
 
MAMP Stack - Macintosh, Apache, MySQL, PHP
MAMP Stack - Macintosh, Apache, MySQL, PHPMAMP Stack - Macintosh, Apache, MySQL, PHP
MAMP Stack - Macintosh, Apache, MySQL, PHPJohn Ashmead
 
Ruby on Rails & PostgreSQL - v2
Ruby on Rails & PostgreSQL - v2Ruby on Rails & PostgreSQL - v2
Ruby on Rails & PostgreSQL - v2John Ashmead
 
Invisibility: Theory & Practice - v3
Invisibility:  Theory & Practice - v3Invisibility:  Theory & Practice - v3
Invisibility: Theory & Practice - v3John Ashmead
 
Invisibility: Theory and Practice - v2
Invisibility:  Theory and Practice - v2Invisibility:  Theory and Practice - v2
Invisibility: Theory and Practice - v2John Ashmead
 
invisibility: Theory and Practice - v1
invisibility:  Theory and Practice - v1invisibility:  Theory and Practice - v1
invisibility: Theory and Practice - v1John Ashmead
 
Installing postgres & postgis
Installing postgres & postgisInstalling postgres & postgis
Installing postgres & postgisJohn Ashmead
 
Quantum Mechanics, Reality, & You
Quantum Mechanics, Reality, & YouQuantum Mechanics, Reality, & You
Quantum Mechanics, Reality, & YouJohn Ashmead
 

More from John Ashmead (20)

Practical Telepathy: The Science & Engineering of Mind-Reading
Practical Telepathy:  The Science & Engineering of Mind-ReadingPractical Telepathy:  The Science & Engineering of Mind-Reading
Practical Telepathy: The Science & Engineering of Mind-Reading
 
From Startup to Mature Company: PostgreSQL Tips and techniques
From Startup to Mature Company:  PostgreSQL Tips and techniquesFrom Startup to Mature Company:  PostgreSQL Tips and techniques
From Startup to Mature Company: PostgreSQL Tips and techniques
 
Practical Telepathy: The Science & Engineering of Mind-Reading
Practical Telepathy:  The Science & Engineering of Mind-ReadingPractical Telepathy:  The Science & Engineering of Mind-Reading
Practical Telepathy: The Science & Engineering of Mind-Reading
 
Stargates: Theory and Practice
Stargates:  Theory and PracticeStargates:  Theory and Practice
Stargates: Theory and Practice
 
StarGates: Theory and Practice
StarGates:  Theory and PracticeStarGates:  Theory and Practice
StarGates: Theory and Practice
 
Quantum dots
Quantum dotsQuantum dots
Quantum dots
 
Star Gates: the Theory and Practice
Star Gates:  the Theory and PracticeStar Gates:  the Theory and Practice
Star Gates: the Theory and Practice
 
Time to the power of Tim
Time to the power of TimTime to the power of Tim
Time to the power of Tim
 
How many universes are there, anyway
How many universes are there, anywayHow many universes are there, anyway
How many universes are there, anyway
 
A Quantum of Mystery
A Quantum of MysteryA Quantum of Mystery
A Quantum of Mystery
 
Converting from MySQL to PostgreSQL
Converting from MySQL to PostgreSQLConverting from MySQL to PostgreSQL
Converting from MySQL to PostgreSQL
 
Seven War Stories and a Moral
Seven War Stories and a MoralSeven War Stories and a Moral
Seven War Stories and a Moral
 
MAMP Stack - Macintosh, Apache, MySQL, PHP
MAMP Stack - Macintosh, Apache, MySQL, PHPMAMP Stack - Macintosh, Apache, MySQL, PHP
MAMP Stack - Macintosh, Apache, MySQL, PHP
 
Automation
AutomationAutomation
Automation
 
Ruby on Rails & PostgreSQL - v2
Ruby on Rails & PostgreSQL - v2Ruby on Rails & PostgreSQL - v2
Ruby on Rails & PostgreSQL - v2
 
Invisibility: Theory & Practice - v3
Invisibility:  Theory & Practice - v3Invisibility:  Theory & Practice - v3
Invisibility: Theory & Practice - v3
 
Invisibility: Theory and Practice - v2
Invisibility:  Theory and Practice - v2Invisibility:  Theory and Practice - v2
Invisibility: Theory and Practice - v2
 
invisibility: Theory and Practice - v1
invisibility:  Theory and Practice - v1invisibility:  Theory and Practice - v1
invisibility: Theory and Practice - v1
 
Installing postgres & postgis
Installing postgres & postgisInstalling postgres & postgis
Installing postgres & postgis
 
Quantum Mechanics, Reality, & You
Quantum Mechanics, Reality, & YouQuantum Mechanics, Reality, & You
Quantum Mechanics, Reality, & You
 

Recently uploaded

Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROmotivationalword821
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 

Recently uploaded (20)

Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTRO
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 

Debugging With PostgreSQL: A Strategic Approach

  • 1. Debugging With PostgreSQL — A Strategic Approach • Debugging is the elephant in the room • Can take 50 to 90% of the time • But gets 2% of the attention! • PostgreSQL has great tools • But they are most effective when deployed as part of an overall strategy John Ashmead john.ashmead@ashmeadsoftware.com
  • 2. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Cleaning up after the elephant • Small shops: one or two people • Legacy code, no documentation or handoff • Limited resources • Considerable time pressure
  • 3. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 See if you can destroy this • Write from top to bottom • Use meaningful variable names • Throw the first one out
  • 4. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 rogue particles • Random inputs • Generate_series() • Fuzzing • Chaos Monkey SELECT * FROM generate_series(2,4); generate_series ----------------- 2 3 4 (3 rows)
  • 5. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 The root cause • Generally self- inflicted • Can also be the result of bitrot, • New requirements, • Changing expectations, …
  • 6. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 foreign keys • Correctness • Speed • Documentation • Meta-programming CREATE TABLE cities ( city varchar(80) primary key, location point ); CREATE TABLE weather ( city varchar(80) references cities(city), temp_lo int, temp_hi int, prcp real, date date ); There are (at least) three bugs here; you have two slides to find them!
  • 7. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 timestamps not updated • Add fields regardless • Ruby-on-rails will update for you • But more reliable in PostgreSQL • Using simple naming convention lets you write function once CREATE FUNCTION timestamp_trg() RETURNS trigger LANGUAGE plpgsql AS $$ begin /* assume we have updated_at and created_at fields on the table */ if new.created_at is null then new.created_at = now(); end if; new.updated_at = now(); return new; end; $$;
  • 8. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Krungthepmahanakhon Amonrattanakosin Mahintharayutthaya Mahadilokphop Noppharatratchathaniburirom Udomratchaniwetmahasathan Amonphimanawatansathit Sakkathattiyawitsanukamprasit create domain city_t as text not null; create domain location_t as point; create domain fahrenheit_t as int check(value between -460 and 10000); create domain precipitation_t as real check(value >= 0); CREATE TABLE cities ( city city_t primary key, location location_t ); CREATE TABLE weather ( city city_t references cities(city), temp_lo fahrenheit_t, temp_hi fahrenheit_t, prcp precipitation_t, weather_date date );
  • 9. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Split Brain Problem create table patients_whose_doctors_pays ( patient_id it_t, name name_t, address addr_t, doctor_id id_t references doctors(id), medical_stuff medical_stuff_t, ... ); create table patients_who_insurers_pays ( patient_id it_t, name name_t, address addr_t, primary_insurance_id id_t references insurers(id), medical_stuff medical_stuff_t, ... ); create view patients as select 'doctor' as pay_type, patient_id, name, address, medical_stuff, ... from patients_whose_doctor_pays union all select 'insurance', patient_id, name, address, medical_stuff, ... from patients_whose_insurance_pays ;
  • 10. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Write perfect code • At a conference, I asked Bjarne: • How do I debug C++? • “Write perfect code!” • Annoying … • But not without a grain of truth
  • 11. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Code has two readers • Should be clear to intended reader • Use meaningful names • Flow from top to bottom • Use logical, consistent indenting • Use appropriate comments • Use house style
  • 12. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Cat in a Box • Claw in every direction until you see daylight • Sometimes necessary • Fuzzing & the Chaos Monkey • Get some sleep • Get a 2nd pair of eyes on the problem
  • 13. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Linear Debugging • Raise notice, print statements • Debuggers • Linear process, so slow • Should usually be the last resort
  • 14. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 drop table if exists results_step1; create temp table results_step1 as select field1, field2, field3 from fairly_complex_query; get diagnostics rows_found1 = row_count; raise notice '%: % step one rows found', ((extract(epoch from clock_timestamp()) - extract(epoch from transaction_timestamp)) * 1000)::integer, rows_found1; Instrumentation • Debuggers not all that helpful — too slow • Preplanned & targeted has worked better for me • As has use of unlogged tables to track intermediate steps in long computations
  • 15. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 How to fix • Don’t fix right away • Write error report/test; verify red • Write fix; verify green • Bugs travel in packs: look for similar problems • Find root cause, not just immediate trigger • See if you can prevent entirely
  • 16. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Technical Debt • Hard to read • Code smells: lots of cut & paste, irregular indenting, patches of dead code, … • Small changes tend to large, unwelcome side effects
  • 17. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Repair, refactor, Rebuild • Don’t kill the patient • Birchall - Re- Engineering Legacy Software • Ellnestam & Brolund - Mikado Method
  • 18. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Systems Test http://sunnyday.mit.edu/accidents/Ariane5accidentreport.html
  • 19. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 User Error — Delete User • Educate users • Show users what is going on • Show them the code Starting Screen URL for Context Intermediate Results Screen Context for decision Result Key Pt Who/where/when/… if pat_age > age_cutoff and test_res > max_norm then recommend(patient_id, followup); else routine_followup(patient_id, test_id); end if; Other Stuff Bad Stuff Useful Comment on Bad Stuff
  • 20. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Don’t Repeat Yourself • Make • Small shell scripts • Function calls • Third normal form • Continuous automation #!/bin/bash # sx -- sql execute -- useful tool for f in $* do bn=`basename $f .sql` sql="${bn}.sql" out="${bn}.out" if [ ! -s $sql ] then echo "No $sql file" exit 1 fi time psql -v ON_ERROR_STOP=1 -X -f $sql > $out 2>&1 if [ $? -ne 0 ] then echo "$sql failed: see $out" exit 1 fi ls -l $out done
  • 21. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Automating common tasks • Factor out common parts of frequent tasks • Reports, warnings, errors,… • Let you get an overview create table report_types ( id primary key, report_name, title name_t, description desc_t, parameter_list jsonb, -- name + allowed values, fail_count count_t, times_run count_t, total_time_taken millisecond_t, administrators_only boolean, created_at tz_t, last_run_at tz_t, … ); create table reports ( id primary key, report_type_id references report_types(id), parameters_used jsonb, run_by references users(id), success ok_t, time_taken millisecond_t, rows_found count_t, run_at tz_t );
  • 22. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Code Generators • Called in for portfolio mgmt problem • High security reqs • Common patterns to most queries, usually joining two tables • Could use meta data to build SPs • In 80 minutes the CG ground out 110,000 lines of code • A few stragglers had to be hacked a bit or even built from scratch • 3 month contract done in 2 weeks Security check Get rows, usually just a master-detail join Log stuff Return rows
  • 23. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Foreign Data Wrappers • PostgreSQL good central exchange • Pull data in, clean up, make it available • Rapid compare of old & new versionsTarget DbTest Db Right DbLeft Db Development Schema Octopus Database Left Schema Right Schema Test Schema Target Schema
  • 24. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Divide and Conquer • Break into parts • Plan dev path • Use simple message protocols • Program defensively • Write self- debugging code • Debug scientificallySolution Problem System Test Planning ComponentM essages Component
  • 25. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 plan development path • Get something useful up quickly • Stub later bits out • Share with users • Develop incrementally • Agile has the right idea As when driving, think globally, act locally.
  • 26. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 KISS the Messenger • Messaging key to Divide & Conquer • KISS applies to messaging as well • CSV is just fine for many applications • And well supported by PostgreSQL -- push out from source -- one of the few cases (in working code) where 'select *' is considered helpful psql -h source_database <<! copy (select * from parent1) to '/tmp/ parent1.csv' csv header copy (select * from child1) to '/tmp/ child1.csv' csv header … ! -- make sure the CSVs look good, then: -- pull in on target psql -h target_database <<! copy parent1 from '/tmp/parent1.csv' csv header copy child1 from '/tmp/child1.csv' csv header … !
  • 27. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Program defensively Validate ajax request before sending Browser Web Server PostgreSQL Validate all parameters before call DB URL Validate all parameters before acting on them SP() Return standardized response Rows Validate & return standardized response Validate & present standardized response in standardized way JSON
  • 28. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 create or replace function ssn_set( person_id0 people.id%type, ssn0 people.ssn%type, debug_flag0 boolean default false) returns ok_t as $$ -- Debugging with PostgreSQL example -- John Ashmead -- for FOSSCON 8/17/2019 declare person_id1 people.id%type; -- more specific than id_t ssn1 people.ssn%type; -- could use ssn_t begin if debug_flag0 then raise notice 'ssn_set(%, %)' person_id0, ssn0; end if; select id into person_id1 from people where id = person_id0; -- could also check number of rows found using "get diagnostics" if person_id1, ssn1 is null then raise exception 'ssn_set: person_id0 % is not in people table', person_id0; end if; -- could trap non-unique exception here select id into person_id1 from people where ssn = ssn0 and id != person_id0; if person_id1 is not null then raise exception 'ssn_set: ssn % is already in use by id %', ssn0, person_id0; end if; if debug_flag0 then raise notice 'ssn_set: person %: ssn changed from % to %', person_id0, ssn0, ssn1; end if; return true; end; $$ language plpgsql; Self-debugging Code and self-checking Turn on tracing at will Especially valuable in crunch mode Self-documenting
  • 29. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Gozinta’s good, Comezouta’s Bad • Double check the Gozinta’s anyway • Take notes • Have a hypothesis (or three) • But don’t guess, instrument • Prefer binary search to linear; keep cutting the hiding space for bugs in half Lots of weird, complicated stuff happens Comezouta’s Bad Part 1 ? Weird/2 Part 2 ? Weird/2 Gozinta’s good
  • 30. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 Plus/Minus • More work upfront • No silver bullet • More reliability • Less total time coding • More interesting products
  • 31. http://timeandquantummechanics.comStrategic Debugging/Ashmead — FOSSCon - August 17th, 2019 References • St. Jerome - RTFM • Mlodgenski - PostgreSQL Server Programming • Fitzpatrick - Timeless Laws • Agans - Debugging: 9 Indispensable Rules • Ousterhout - Philosophy of Software Design