Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit-testing PostgreSQL row-level locks

I am currently adding unit tests to a rather large quantity of PostgreSQL stored procedures, using pgTap.

Some of the procedures perform operations which lock rows explicitly. These locks are critical to the application.

How do I write tests that check that the rows that need to be locked have been, and that rows which shouldn't be locked aren't?

The only "clue" I have at the moment is the pgrowlocks extension, which allows a transaction to check for rows locked by another transaction. However, the current transaction doesn't seem to see its own locks, so I'd have to use something to synchronise two transaction, and unless I am quite mistaken, there's no way to do that using pgTap.

(note: using PostgreSQL 9.1)

like image 331
E.Benoît Avatar asked Jan 19 '26 23:01

E.Benoît


1 Answers

If you can identify the ctid of the rows in question, and know which transaction should have the rows locked, maybe you could use the pageinspect extension and look at the tuple info flags and xmax? The info flags should indicate the row is locked, and xmax be set to the transaction id holding it.

like image 82
araqnid Avatar answered Jan 21 '26 14:01

araqnid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!