PostgreSQL Hooks for Fun and Profit
-
Upload
david-fetter -
Category
Software
-
view
33 -
download
0
Transcript of PostgreSQL Hooks for Fun and Profit
HOOKSfor Fun and Profit
[email protected] 15xMarch 2, 2017
http://github.com/davidfetterTuesday, March 7, 17
$ whoami
Tuesday, March 7, 17
David Fetter
Tuesday, March 7, 17
David Fetter
Husband and father of four
Tuesday, March 7, 17
David Fetter
Husband and father of four
Long-time PostgreSQL contributor
Tuesday, March 7, 17
David Fetter
Husband and father of four
Long-time PostgreSQL contributor
Expander of SQL
Tuesday, March 7, 17
David Fetter
Husband and father of four
Long-time PostgreSQL contributor
Expander of SQL
Happy contractor for OpenSCG at GE Digital
Tuesday, March 7, 17
Rate me!
Tuesday, March 7, 17
Have you ever?
Tuesday, March 7, 17
Have you ever?
Tuesday, March 7, 17
Have you ever?
Tuesday, March 7, 17
Have you ever?
Updated or deleted every row
Tuesday, March 7, 17
Have you ever?
Updated or deleted every row
by accident?
Tuesday, March 7, 17
Have you ever?
Updated or deleted every row
by accident?
Tuesday, March 7, 17
Have you ever?
Updated or deleted every row
by accident?
Tuesday, March 7, 17
Have you ever?
Updated or deleted every row
by accident?
Tuesday, March 7, 17
Have you ever?
Wanted to slow people down when they auth too fast
Tuesday, March 7, 17
Have you ever?
Tuesday, March 7, 17
Have you ever?
Made a cool constraint
Tuesday, March 7, 17
Have you ever?
Made a cool constraint
that required SERIALIZABLE
Tuesday, March 7, 17
Have you ever?
Wanted to replace our TSP-based planner with one based on simulated annealing
Tuesday, March 7, 17
You could:
Updated or deleted from a whole table without meaning to?
Lock out everyone who could make a mistake...
Tuesday, March 7, 17
You could
Tuesday, March 7, 17
You could
Wanted to slow people down when they auth too fast
Tuesday, March 7, 17
You could
Wanted to slow people down when they auth too fast
Hope the network layer handles it
Tuesday, March 7, 17
You could
Wanted to slow people down when they auth too fast
Hope the network layer handles it
and isn’t Mallory
Tuesday, March 7, 17
You could
Tuesday, March 7, 17
You could
Made a cool constraint
Tuesday, March 7, 17
You could
Made a cool constraint
that required SERIALIZABLE
Tuesday, March 7, 17
You could
Made a cool constraint
that required SERIALIZABLE
Add a check for that
Tuesday, March 7, 17
You could
Made a cool constraint
that required SERIALIZABLE
Add a check for that
in. every. single. trigger.
Tuesday, March 7, 17
You could
Wanted to replace our TSP-based planner with one based on simulated annealing
Um....er....
Tuesday, March 7, 17
Hooks to the Rescue!
Tuesday, March 7, 17
Hooks?
Tuesday, March 7, 17
Snag
Tuesday, March 7, 17
Bag
Tuesday, March 7, 17
Act
Tuesday, March 7, 17
Back
Tuesday, March 7, 17
You need to write C
Tuesday, March 7, 17
Tuesday, March 7, 17
Preamble#include "postgres.h"
/* Headers you need start here: */
/* Headers you need end here: */
PG_MODULE_MAGIC;
/* Hooking: All hooks start with this. */void _PG_init(void);
/* Unhooking: All hooks end with this. */void _PG_fini(void);
Tuesday, March 7, 17
Snag/Bag/* * There is a type for each hook. * This is not one of them. * You will need it for hooking and unhooking. */
static System_hook_type prev_system_hook = NULL;
void _PG_init(void){ prev_system_hook = System_hook; System_hook = blackhole_hook;}
Tuesday, March 7, 17
Act
staticvoid blackhole_hook{ /* Your hook code goes here */}
Tuesday, March 7, 17
Back
void _PG_fini(void){ System_hook = prev_system_hook;}
Tuesday, March 7, 17
82 hooks
Tuesday, March 7, 17
Tuesday, March 7, 17
You could:
Updated or deleted from a whole table without meaning to?
Lock out everyone who could make a mistake...
Make a simple hook for simple errors
Tuesday, March 7, 17
Boring C Code:
Tuesday, March 7, 17
Live Demo!
Tuesday, March 7, 17
Have you ever?
Tuesday, March 7, 17
Have you ever?
Made a cool constraint
Tuesday, March 7, 17
Have you ever?
Made a cool constraint
that required SERIALIZABLE
Tuesday, March 7, 17
Have you ever?
Made a cool constraint
that required SERIALIZABLE
Tuesday, March 7, 17
Have you ever?
Made a cool constraint
that required SERIALIZABLE
Make a simple hook!
Tuesday, March 7, 17
You could
Tuesday, March 7, 17
You could
Wanted to slow people down when they auth too fast
Tuesday, March 7, 17
You could
Wanted to slow people down when they auth too fast
Hope the network layer handles it
Tuesday, March 7, 17
You could
Wanted to slow people down when they auth too fast
Hope the network layer handles it
and isn’t Mallory
Tuesday, March 7, 17
You could
Wanted to slow people down when they auth too fast
Hope the network layer handles it
and isn’t Mallory
auth_delay (contrib)
Tuesday, March 7, 17
You could
Wanted to replace our TSP-based planner with one based on simulated annealing
Um....er....
2313 LoC later: Wow!
Tuesday, March 7, 17
Questions?
Tuesday, March 7, 17
tl;dr
Tuesday, March 7, 17
tl;dr
• Lots has already been done
Tuesday, March 7, 17
tl;dr
• Lots has already been done
• Your imagination is the main limit
Tuesday, March 7, 17
tl;dr
• Lots has already been done
• Your imagination is the main limit
• Have fun!
Tuesday, March 7, 17
Thank you!
Tuesday, March 7, 17