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:
- Start a new transaction
- Temp store state of all views/functions
- Deploy changes to schema
- Show a diff of changes made
- If the changes are expected, go ahead and commit
BEGIN;
SELECT fstage();
CREATE OR REPLACE FUNCTION ...
CREATE OR REPLACE VIEW ...
SELECT fdiff();
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!
