Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SQL comment to original lock string for PostgreSQL #28

Merged
merged 2 commits into from
Sep 28, 2017

Conversation

joshuaflanagan
Copy link
Contributor

The PostgreSQL implementation requires translating the user-specified lock string into
a pair of 32 bit numbers, which makes the lock and unlock statements opaque in the
SQL logs.

This PR adds a SQL comment containing the original lock string. The comment will be
removed from the input by the PostgreSQL parser, but it will still show up in logs.

Example rails log before this change:

2.4.1 :001 > User.with_advisory_lock_result("my_unique_lock_name"){}
   (0.4ms)  SELECT pg_try_advisory_lock(258664313,0) AS t5e3e0f0fd108ec9b914765bb4f539780
   (0.2ms)  SELECT pg_advisory_unlock(258664313,0) AS t1dcddec5851647e514718db76f7b3217

Example rails log after this change:

2.4.1 :008 > User.with_advisory_lock_result("my_unique_lock_name"){}
   (0.3ms)  SELECT pg_try_advisory_lock(258664313,0) AS t983e4b7811f1304eed9685ceb82f93b5 /* my_unique_lock_name */
   (0.2ms)  SELECT pg_advisory_unlock(258664313,0) AS tb920893abfe53ed5418fbbbe8d8ec1d3 /* my_unique_lock_name */

The PostgreSQL implementation requires translating the user-specified
lock string into a pair of 32 bit numbers, which makes the lock and
unlock statements opaque in the SQL logs.

To aid debugging, add a SQL comment containing the original lock string.
The comment will be removed from the input by the PostgreSQL parser, but
it will still show up in logs.
@joshuaflanagan joshuaflanagan force-pushed the lock_name_comment_postgres branch 2 times, most recently from bfab0c3 to 1554628 Compare September 26, 2017 23:01
@mceachen
Copy link
Collaborator

mceachen commented Sep 27, 2017 via email

@joshuaflanagan
Copy link
Contributor Author

I'm not sure which SHA you are suggesting we replace.

The pg_try_advisory_lock function requires an integer, so it makes sense to continue to use the stable_hashcode of the lock name.

If you are referring to replacing the random unique_column_name with the lock name instead - I had considered that, but assumed that might cause the same AR caching issues the unique_column_name is trying to prevent, since the same value could be used repeatedly. It would also probably require even more scrubbing of the lock name to ensure a valid column identifier.

I think the SQL comment is a good enough compromise, but am open to suggestions if there is something else I'm not seeing.

@mceachen
Copy link
Collaborator

You're right, I was thinking of a different codepath that truncated lock strings with a sha--I'd forgotten postgres' requirement for numeric locks.

Thanks again for the PR!

@mceachen mceachen merged commit 46a89de into ClosureTree:master Sep 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants