Quantcast
Channel: Joel on SQL
Viewing all articles
Browse latest Browse all 25

Secure deployment of PostgreSQL functions

$
0
0

Two people might be simultaneously modifying the same database function. To protect against this scenario, I created a little tool to stage the source code of all functions in pg_proc and the definition of all views. You then deploy your changes, and afterwards you call fdiff() to show a diff of the changes made, by comparing the new content of pg_proc and the definitions of all views. If the changes are what you expected, you go ahead and commit.

We’ve been using this methodology every day in both my current company and my previous one, and it has prevented us from doing mistakes.

Source code and installation instructions: https://github.com/joelonsql/fdiff

This is how to use it:

  1. Start a new transaction
  2. BEGIN;

  3. Temp store state of all views/functions
  4. SELECT fstage();

  5. Deploy changes to schema
  6. CREATE OR REPLACE FUNCTION ...
    CREATE OR REPLACE VIEW ...

  7. Show a diff of changes made
  8. SELECT fdiff();

  9. If the changes are expected, go ahead and commit
  10. COMMIT;

Example on the output from fdiff():


+-------------------------------+
| Updated or replaced functions |
+-------------------------------+

Schema................: public
Name..................: process_order
Argument data types...: _orderid bigint
Result data type......: boolean
Language..............: plpgsql
Type..................: normal
Volatility............: STABLE
Owner.................: amazon
20 c OR Orders.ShippingDate IS NOT NULL
20 c OR Orders.ShippingDate > now() - interval '2 month'

Hope you’ll like it!



Viewing all articles
Browse latest Browse all 25

Trending Articles