postgresql hooks for fun and profit

66
HOOKS for Fun and Profit [email protected] SCaLE 15x March 2, 2017 http://github.com/davidfetter Tuesday, March 7, 17

Upload: david-fetter

Post on 19-Mar-2017

33 views

Category:

Software


0 download

TRANSCRIPT

Page 1: PostgreSQL Hooks for Fun and Profit

HOOKSfor Fun and Profit

[email protected] 15xMarch 2, 2017

http://github.com/davidfetterTuesday, March 7, 17

Page 2: PostgreSQL Hooks for Fun and Profit

$ whoami

Tuesday, March 7, 17

Page 3: PostgreSQL Hooks for Fun and Profit

David Fetter

Tuesday, March 7, 17

Page 4: PostgreSQL Hooks for Fun and Profit

David Fetter

Husband and father of four

Tuesday, March 7, 17

Page 5: PostgreSQL Hooks for Fun and Profit

David Fetter

Husband and father of four

Long-time PostgreSQL contributor

Tuesday, March 7, 17

Page 6: PostgreSQL Hooks for Fun and Profit

David Fetter

Husband and father of four

Long-time PostgreSQL contributor

Expander of SQL

Tuesday, March 7, 17

Page 7: PostgreSQL Hooks for Fun and Profit

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

Page 8: PostgreSQL Hooks for Fun and Profit

Rate me!

Tuesday, March 7, 17

Page 9: PostgreSQL Hooks for Fun and Profit

Have you ever?

Tuesday, March 7, 17

Page 10: PostgreSQL Hooks for Fun and Profit

Have you ever?

Tuesday, March 7, 17

Page 11: PostgreSQL Hooks for Fun and Profit

Have you ever?

Tuesday, March 7, 17

Page 12: PostgreSQL Hooks for Fun and Profit

Have you ever?

Updated or deleted every row

Tuesday, March 7, 17

Page 13: PostgreSQL Hooks for Fun and Profit

Have you ever?

Updated or deleted every row

by accident?

Tuesday, March 7, 17

Page 14: PostgreSQL Hooks for Fun and Profit

Have you ever?

Updated or deleted every row

by accident?

Tuesday, March 7, 17

Page 15: PostgreSQL Hooks for Fun and Profit

Have you ever?

Updated or deleted every row

by accident?

Tuesday, March 7, 17

Page 16: PostgreSQL Hooks for Fun and Profit

Have you ever?

Updated or deleted every row

by accident?

Tuesday, March 7, 17

Page 17: PostgreSQL Hooks for Fun and Profit

Have you ever?

Wanted to slow people down when they auth too fast

Tuesday, March 7, 17

Page 18: PostgreSQL Hooks for Fun and Profit

Have you ever?

Tuesday, March 7, 17

Page 19: PostgreSQL Hooks for Fun and Profit

Have you ever?

Made a cool constraint

Tuesday, March 7, 17

Page 20: PostgreSQL Hooks for Fun and Profit

Have you ever?

Made a cool constraint

that required SERIALIZABLE

Tuesday, March 7, 17

Page 21: PostgreSQL Hooks for Fun and Profit

Have you ever?

Wanted to replace our TSP-based planner with one based on simulated annealing

Tuesday, March 7, 17

Page 22: PostgreSQL Hooks for Fun and Profit

You could:

Updated or deleted from a whole table without meaning to?

Lock out everyone who could make a mistake...

Tuesday, March 7, 17

Page 23: PostgreSQL Hooks for Fun and Profit

You could

Tuesday, March 7, 17

Page 24: PostgreSQL Hooks for Fun and Profit

You could

Wanted to slow people down when they auth too fast

Tuesday, March 7, 17

Page 25: PostgreSQL Hooks for Fun and Profit

You could

Wanted to slow people down when they auth too fast

Hope the network layer handles it

Tuesday, March 7, 17

Page 26: PostgreSQL Hooks for Fun and Profit

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

Page 27: PostgreSQL Hooks for Fun and Profit

You could

Tuesday, March 7, 17

Page 28: PostgreSQL Hooks for Fun and Profit

You could

Made a cool constraint

Tuesday, March 7, 17

Page 29: PostgreSQL Hooks for Fun and Profit

You could

Made a cool constraint

that required SERIALIZABLE

Tuesday, March 7, 17

Page 30: PostgreSQL Hooks for Fun and Profit

You could

Made a cool constraint

that required SERIALIZABLE

Add a check for that

Tuesday, March 7, 17

Page 31: PostgreSQL Hooks for Fun and Profit

You could

Made a cool constraint

that required SERIALIZABLE

Add a check for that

in. every. single. trigger.

Tuesday, March 7, 17

Page 32: PostgreSQL Hooks for Fun and Profit

You could

Wanted to replace our TSP-based planner with one based on simulated annealing

Um....er....

Tuesday, March 7, 17

Page 33: PostgreSQL Hooks for Fun and Profit

Hooks to the Rescue!

Tuesday, March 7, 17

Page 34: PostgreSQL Hooks for Fun and Profit

Hooks?

Tuesday, March 7, 17

Page 35: PostgreSQL Hooks for Fun and Profit

Snag

Tuesday, March 7, 17

Page 36: PostgreSQL Hooks for Fun and Profit

Bag

Tuesday, March 7, 17

Page 37: PostgreSQL Hooks for Fun and Profit

Act

Tuesday, March 7, 17

Page 38: PostgreSQL Hooks for Fun and Profit

Back

Tuesday, March 7, 17

Page 39: PostgreSQL Hooks for Fun and Profit

You need to write C

Tuesday, March 7, 17

Page 40: PostgreSQL Hooks for Fun and Profit

Tuesday, March 7, 17

Page 41: PostgreSQL Hooks for Fun and Profit

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

Page 42: PostgreSQL Hooks for Fun and Profit

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

Page 43: PostgreSQL Hooks for Fun and Profit

Act

staticvoid blackhole_hook{ /* Your hook code goes here */}

Tuesday, March 7, 17

Page 44: PostgreSQL Hooks for Fun and Profit

Back

void _PG_fini(void){ System_hook = prev_system_hook;}

Tuesday, March 7, 17

Page 45: PostgreSQL Hooks for Fun and Profit

82 hooks

Tuesday, March 7, 17

Page 46: PostgreSQL Hooks for Fun and Profit

Tuesday, March 7, 17

Page 47: PostgreSQL Hooks for Fun and Profit

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

Page 48: PostgreSQL Hooks for Fun and Profit

Boring C Code:

Tuesday, March 7, 17

Page 49: PostgreSQL Hooks for Fun and Profit

Live Demo!

Tuesday, March 7, 17

Page 50: PostgreSQL Hooks for Fun and Profit

Have you ever?

Tuesday, March 7, 17

Page 51: PostgreSQL Hooks for Fun and Profit

Have you ever?

Made a cool constraint

Tuesday, March 7, 17

Page 52: PostgreSQL Hooks for Fun and Profit

Have you ever?

Made a cool constraint

that required SERIALIZABLE

Tuesday, March 7, 17

Page 53: PostgreSQL Hooks for Fun and Profit

Have you ever?

Made a cool constraint

that required SERIALIZABLE

Tuesday, March 7, 17

Page 54: PostgreSQL Hooks for Fun and Profit

Have you ever?

Made a cool constraint

that required SERIALIZABLE

Make a simple hook!

Tuesday, March 7, 17

Page 55: PostgreSQL Hooks for Fun and Profit

You could

Tuesday, March 7, 17

Page 56: PostgreSQL Hooks for Fun and Profit

You could

Wanted to slow people down when they auth too fast

Tuesday, March 7, 17

Page 57: PostgreSQL Hooks for Fun and Profit

You could

Wanted to slow people down when they auth too fast

Hope the network layer handles it

Tuesday, March 7, 17

Page 58: PostgreSQL Hooks for Fun and Profit

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

Page 59: PostgreSQL Hooks for Fun and Profit

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

Page 60: PostgreSQL Hooks for Fun and Profit

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

Page 61: PostgreSQL Hooks for Fun and Profit

Questions?

Tuesday, March 7, 17

Page 62: PostgreSQL Hooks for Fun and Profit

tl;dr

Tuesday, March 7, 17

Page 63: PostgreSQL Hooks for Fun and Profit

tl;dr

• Lots has already been done

Tuesday, March 7, 17

Page 64: PostgreSQL Hooks for Fun and Profit

tl;dr

• Lots has already been done

• Your imagination is the main limit

Tuesday, March 7, 17

Page 65: PostgreSQL Hooks for Fun and Profit

tl;dr

• Lots has already been done

• Your imagination is the main limit

• Have fun!

Tuesday, March 7, 17

Page 66: PostgreSQL Hooks for Fun and Profit

Thank you!

Tuesday, March 7, 17