-
Notifications
You must be signed in to change notification settings - Fork 66
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 sanity checks for ENR entries. #500
Merged
xhfanhe
merged 5 commits into
babelfish-for-postgresql:BABEL_4_X_DEV__PG_16_X
from
amazon-aurora:BABEL-4776
Dec 18, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
043f242
[BABEL-4776] Add sanity checks for ENR entries.
timchang514 41b94dc
Throw an error instead of Assert when checking pg_depend tuples.
timchang514 26e95db
Correct error messages, changed Asserts to errors
timchang514 4ea592f
Minor naming refactor
timchang514 2dfb7dd
Correct ENR_OP_DROP check to ENR_OP_UPDATE.
timchang514 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,9 @@ | |
#include "catalog/catalog.h" | ||
#include "catalog/dependency.h" | ||
#include "catalog/indexing.h" | ||
#include "catalog/pg_authid.h" | ||
#include "catalog/pg_collation.h" | ||
#include "catalog/pg_opclass.h" | ||
#include "catalog/pg_constraint.h" | ||
#include "catalog/pg_statistic.h" | ||
#include "catalog/pg_statistic_ext.h" | ||
|
@@ -49,6 +52,8 @@ | |
#include "utils/queryenvironment.h" | ||
#include "utils/rel.h" | ||
|
||
#define NUM_ENR_CATALOGS 11 | ||
|
||
pltsql_get_tsql_enr_from_oid_hook_type pltsql_get_tsql_enr_from_oid_hook = NULL; | ||
|
||
/* | ||
|
@@ -63,6 +68,27 @@ struct QueryEnvironment | |
MemoryContext memctx; | ||
}; | ||
|
||
/* | ||
* This list must match ENRCatalogTupleType in queryenvironment.h. | ||
* | ||
* These are the pg catalogs which we are placing in | ||
* ENR. We cannot mix any entries in non-ENR (ie on disk) catalogs. | ||
*/ | ||
static Oid ENRCatalogOids[NUM_ENR_CATALOGS] = | ||
{ | ||
RelationRelationId, /* pg_class */ | ||
TypeRelationId, /* pg_type */ | ||
AttributeRelationId, /* pg_attribute */ | ||
ConstraintRelationId, /* pg_constraint */ | ||
StatisticRelationId, /* pg_statistic */ | ||
StatisticExtRelationId, /* pg_statistic_ext */ | ||
DependRelationId, /* pg_depend */ | ||
SharedDependRelationId, /* pg_shdepend */ | ||
IndexRelationId, /* pg_index */ | ||
SequenceRelationId, /* pg_sequence */ | ||
AttrDefaultRelationId /* pg_attrdef */ | ||
}; | ||
|
||
struct QueryEnvironment topLevelQueryEnvData; | ||
struct QueryEnvironment *topLevelQueryEnv = &topLevelQueryEnvData; | ||
struct QueryEnvironment *currentQueryEnv = NULL; | ||
|
@@ -74,6 +100,7 @@ static bool _ENR_tuple_operation(Relation catalog_rel, HeapTuple tup, ENRTupleOp | |
static void ENRAddUncommittedTupleData(EphemeralNamedRelation enr, Oid catoid, ENRTupleOperationType op, HeapTuple tup, bool in_enr_rollback); | ||
static void ENRDeleteUncommittedTupleData(SubTransactionId subid, EphemeralNamedRelation enr); | ||
static void ENRRollbackUncommittedTuple(QueryEnvironment *queryEnv, ENRUncommittedTuple uncommitted_tup); | ||
static bool IsCatalogOidENR(Oid reloid, bool extended); | ||
static EphemeralNamedRelation find_enr(Form_pg_depend entry); | ||
|
||
QueryEnvironment * | ||
|
@@ -326,6 +353,37 @@ ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd) | |
return tupdesc; | ||
} | ||
|
||
/* Simple check to see if the provided OID is the OID of an ENR'd catalog. */ | ||
static bool IsCatalogOidENR(Oid reloid, bool extended) | ||
{ | ||
/* This should always be a catalog relation we're checking. */ | ||
if (!IsCatalogRelationOid(reloid)) | ||
return false; | ||
|
||
for (int i = 0; i < NUM_ENR_CATALOGS; i++) | ||
{ | ||
if (reloid == ENRCatalogOids[i]) | ||
return true; | ||
} | ||
|
||
/* | ||
* These are catalogs that aren't in ENR but can be dependencies of | ||
* temp tables. | ||
* | ||
* There could be some potential for misuse here, involving creating | ||
* and dropping collations or roles in while having temp tables | ||
* that depend on them on a different session. | ||
*/ | ||
if (extended) | ||
{ | ||
if (reloid == CollationRelationId | ||
|| reloid == AuthIdRelationId | ||
|| reloid == OperatorClassRelationId) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/* | ||
* Get the starting tuple (or more precisely, a ListCell that contains the tuple) | ||
* for systable scan functions based on the given keys. | ||
|
@@ -361,17 +419,7 @@ bool ENRGetSystableScan(Relation rel, Oid indexId, int nkeys, ScanKey key, List | |
return false; | ||
} | ||
|
||
if (reloid != RelationRelationId && | ||
reloid != TypeRelationId && | ||
reloid != AttributeRelationId && | ||
reloid != ConstraintRelationId && | ||
reloid != StatisticRelationId && | ||
reloid != StatisticExtRelationId && | ||
reloid != DependRelationId && | ||
reloid != SharedDependRelationId && | ||
reloid != IndexRelationId && | ||
reloid != SequenceRelationId && | ||
reloid != AttrDefaultRelationId) | ||
if (!IsCatalogOidENR(reloid, false)) | ||
return false; | ||
|
||
switch (nkeys) { | ||
|
@@ -748,6 +796,24 @@ static bool _ENR_tuple_operation(Relation catalog_rel, HeapTuple tup, ENRTupleOp | |
list_ptr = &enr->md.cattups[ENR_CATTUP_CLASS]; | ||
lc = list_head(enr->md.cattups[ENR_CATTUP_CLASS]); | ||
ret = true; | ||
|
||
/* | ||
* All of the below asserts should hold true for TSQL temp tables. | ||
* | ||
* This helps ensure that we don't have any dependencies pointing to | ||
* non-ENR catalogs. | ||
*/ | ||
if ((op == ENR_OP_ADD || op == ENR_OP_UPDATE) && HeapTupleIsValid(tup)) | ||
{ | ||
Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tup); | ||
|
||
if ((classForm->relisshared) || | ||
(classForm->relpersistence != RELPERSISTENCE_TEMP) || | ||
(classForm->relhastriggers) || | ||
(classForm->relrowsecurity) || | ||
(classForm->relispartition)) | ||
elog(ERROR, "Invalid pg_class ENR entry."); | ||
} | ||
} | ||
break; | ||
case DependRelationId: | ||
|
@@ -764,6 +830,19 @@ static bool _ENR_tuple_operation(Relation catalog_rel, HeapTuple tup, ENRTupleOp | |
tf1->objid == tf2->objid && | ||
tf1->objsubid == tf2->objsubid) { | ||
lc = curlc; | ||
|
||
/* | ||
* When adding entries to pg_depend, do an additional sanity check | ||
* to verify we aren't creating links to non-ENR catalogs. | ||
*/ | ||
if (op == ENR_OP_ADD) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this adds value but will not detect cases where there is dependency between temp and main objects There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above comment. |
||
if (!IsCatalogOidENR(tf1->classid, true)) | ||
elog(ERROR, "Unexpected catalog OID %d referenced in ENR.", tf1->classid); | ||
else if (!IsCatalogOidENR(tf1->refclassid, true)) | ||
elog(ERROR, "Unexpected catalog OID %d referenced in ENR.", tf1->refclassid); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
@@ -787,6 +866,18 @@ static bool _ENR_tuple_operation(Relation catalog_rel, HeapTuple tup, ENRTupleOp | |
tf1->objid == tf2->objid && | ||
tf1->objsubid == tf2->objsubid) { | ||
lc = curlc; | ||
|
||
/* | ||
* When adding entries to pg_shdepend, do an additional check | ||
* to verify we aren't creating links to non-ENR catalogs. | ||
*/ | ||
if (op == ENR_OP_ADD) | ||
{ | ||
if (!IsCatalogOidENR(tf1->classid, true)) | ||
elog(ERROR, "Unexpected catalog OID %d referenced in ENR.", tf1->classid); | ||
else if (!IsCatalogOidENR(tf1->refclassid, true)) | ||
elog(ERROR, "Unexpected catalog OID %d referenced in ENR.", tf1->refclassid); | ||
} | ||
break; | ||
} | ||
} | ||
|
@@ -800,6 +891,14 @@ static bool _ENR_tuple_operation(Relation catalog_rel, HeapTuple tup, ENRTupleOp | |
list_ptr = &enr->md.cattups[ENR_CATTUP_INDEX]; | ||
lc = list_head(enr->md.cattups[ENR_CATTUP_INDEX]); | ||
ret = true; | ||
|
||
if ((op == ENR_OP_ADD || op == ENR_OP_UPDATE) && HeapTupleIsValid(tup)) | ||
{ | ||
Form_pg_index indexForm = (Form_pg_index) GETSTRUCT(tup); | ||
|
||
if(indexForm->indisreplident) | ||
elog(ERROR, "Invalid pg_index ENR entry."); | ||
} | ||
} | ||
break; | ||
case TypeRelationId: | ||
|
@@ -962,14 +1061,14 @@ static bool _ENR_tuple_operation(Relation catalog_rel, HeapTuple tup, ENRTupleOp | |
break; | ||
case ENR_OP_UPDATE: | ||
/* | ||
* Invalidate the tuple before updating / removing it from the List. | ||
* Consider the case when we remove the tuple and cache invalidation | ||
* failed, then error handling would try to remove it again but would | ||
* crash because entry is gone from the List but we could still find it in the syscache. | ||
* If we failed to drop because we failed to invalidate, then subsequent | ||
* creation of the same table would fail saying the tuple exists already | ||
* which is much better than crashing. | ||
*/ | ||
* Invalidate the tuple before updating / removing it from the List. | ||
* Consider the case when we remove the tuple and cache invalidation | ||
* failed, then error handling would try to remove it again but would | ||
* crash because entry is gone from the List but we could still find it in the syscache. | ||
* If we failed to drop because we failed to invalidate, then subsequent | ||
* creation of the same table would fail saying the tuple exists already | ||
* which is much better than crashing. | ||
*/ | ||
oldtup = lfirst(lc); | ||
if (enr->md.is_bbf_temp_table && temp_table_xact_support) | ||
ENRAddUncommittedTupleData(enr, catalog_oid, op, oldtup, in_enr_rollback); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what value these conditions add? for example temp tables cannot have row level security attributes. But what we really want to check is that any part of an ENR table should not be stored in PG catalog. It means that I cannot have an entry in PG catalog to store row level security information. Either I store that in ENR or whole table goes into PG catalog. But are these checks right candidates to validate above assumptions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's at least some amount of validation on the pg_class entry. You're correct that this doesn't check whether there is an entry for an ENR table in the PG catalog for row level security information. But that row is still tied to a pg_class entry with the flag set, so this will still catch that case and throw an error.
As I mentioned in the description of the PR:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will start a followup discussion re: the incompatible catalogs as well as the problems with checking for ENR entries in pg catalogs. I wouldn't consider either of those blockers for this change.