Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ibhaskar2
Copy link
Contributor

  1. Saves from race condition while doing table scan in object acess hook, which arises when the current drop object is pg_ivm_immv's toast table, which looks up for primary key which already got dropped earlier.
  2. Use RangeVarGetRelidExtended(... AccessShareLock ...) instead of get_relname_relid(). That way, DROP EXTENSION can't cause errors in concurrent sessions that are in the middle of running the hook.

Tested in GCE VM with postgres 15

  • Drop extension failure in pg_ivm -
Screenshot 2024-08-23 at 12 12 47 PM
  • Drop extension working after fix -
Screenshot 2024-08-23 at 12 12 01 PM

1. Saves from race condition while doing table scan in object acess hook, which arises when the current drop object is pg_ivm_immv's toast table, which looks up for primary key which already got dropped earlier.
2. Use RangeVarGetRelidExtended(... AccessShareLock ...) instead of get_relname_relid().  That way, DROP EXTENSION can't cause errors in concurrent sessions that are in the middle of running the hook.
@ibhaskar2 ibhaskar2 mentioned this pull request Aug 23, 2024
@ibhaskar2
Copy link
Contributor Author

@yugo-n can you review this? This is a critical bug which breaks basic functionality of extension (CREATE DROP) and users can get stuck with this extension installed with no way out.

@yugo-n yugo-n self-requested a review August 29, 2024 05:14
@ibhaskar2
Copy link
Contributor Author

@yugo-n did you get a chance to take a look at it?

@yugo-n
Copy link
Collaborator

yugo-n commented Oct 15, 2024

@ibhaskar2 Thank you for your PR, and sorry for late response.
This is a definitely a bug, and I'm looking into this now.

@yugo-n
Copy link
Collaborator

yugo-n commented Oct 15, 2024

@ibhaskar2

Use RangeVarGetRelidExtended(... AccessShareLock ...) instead of get_relname_relid(). That way, DROP EXTENSION can't cause errors in concurrent sessions that are in the middle of running the hook.

The fix in PgIvmObjectAccessHook seems reasonable to me, but I've not understood why we should use RangeVarGetRelidExtended rather than get_relname_relid since it is not clear for mewhat errors occur in what situations.... I'll investigate it a bit more.

@yugo-n
Copy link
Collaborator

yugo-n commented Oct 15, 2024

I've not understood why we should use RangeVarGetRelidExtended rather than get_relname_relid since it is not clear for mewhat errors occur in what situations.... I'll investigate it a bit more.

OK, I've understood that if we don't acquire the lock before getting relid of pg_ivm_immv, "DROP TABLE" command could raise an error ERROR: could not open relation with OID .... when a concurrent session called "DROP EXTENSION pg_ivm".

return RangeVarGetRelidExtended(
makeRangeVar("pg_catalog", "pg_ivm_immv", -1),
AccessShareLock, RVR_MISSING_OK, NULL, NULL);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've understood that RangeVarGetRelid is required to avoid the could not open relation ERROR, but PgIvmImmvRelationId is called from another places in pg_ivm, so I would not like to acquire a lock in this function for avoiding unnecessary behaviour changes. Instead, I would like to call RangeVarGetRelid() directly in PgIvmObjectAccessHook. (I think it is better to use RangeVarGetRelid rather than RangeVarGetRelidExtended for making the function call simpler.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not like to acquire a lock in this function for avoiding unnecessary behaviour changes.

After some considering, I came to think that this is not problem because the lock only conflicts with DROP EXTENSION. Moreover, where PgIvmImmvRelationId is called, AccessShareLock or more stronger lock is acquired right after it, so taking a lock in this function would not cause additional conflicts including dead locks.

return RangeVarGetRelidExtended(
makeRangeVar("pg_catalog", "pg_ivm_immv_pkey", -1),
AccessShareLock, RVR_MISSING_OK, NULL, NULL);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same discussion as PgIvmImmvRelationId is applied here, too.

Copy link
Collaborator

@yugo-n yugo-n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After all, it seems good to me.
I'll merge it in a few days.

@yugo-n yugo-n merged commit edde972 into sraoss:main Oct 21, 2024
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