I know this is very similar to other questions (e.g., this one), but I feel that I haven't found an answer that includes the specific question of how the SELECT part of the query affects the lock.
Specifically, I have a parent-child relationship between two tables, enforced via a foreign key on the child table that references the primary key of the parent. In a piece of software I'm working on, I want to take a "path" (parent key + child key) and return the child row only if it's a valid "path". So, the query will look something like:
SELECT Child.*
FROM Child
INNER JOIN Parent ON Parent.id = Child.parent_id
WHERE Parent.id = ? AND Child.id = ?
FOR UPDATE
I understand from the other question that I linked, that the Parent row would be locked if I did a SELECT *
. But I'm not sure if that changes when I don't actually return any of the Parent row's columns.
The documentation says (emphasis mine):
For SELECT ... FOR UPDATE or SELECT ... LOCK IN SHARE MODE, locks are acquired for scanned rows, and expected to be released for rows that do not qualify for inclusion in the result set (for example, if they do not meet the criteria given in the WHERE clause). However, in some cases, rows might not be unlocked immediately because the relationship between a result row and its original source is lost during query execution.
But I can kind of interpret that multiple ways. What does it mean to "qualify" for inclusion in the result set? Does the fact that I didn't include any of the columns in my SELECT
"disqualify" it? Or does "qualify" only refer to JOIN...ON
, WHERE
, and HAVING
clauses?
Thank you!