|
54 | 54 | OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x"
|
55 | 55 |
|
56 | 56 | static const char *query_text;
|
| 57 | +static bool isQueryUsingSystemRelation(Query *query); |
| 58 | +static bool isQueryUsingSystemRelation_walker(Node *node, void *context); |
57 | 59 |
|
58 | 60 | /*
|
59 | 61 | * Saves query text into query_text variable.
|
@@ -108,7 +110,7 @@ aqo_planner(Query *parse,
|
108 | 110 | strlen(CREATE_EXTENSION_STARTSTRING_0)) == 0 ||
|
109 | 111 | strncmp(query_text, CREATE_EXTENSION_STARTSTRING_1,
|
110 | 112 | strlen(CREATE_EXTENSION_STARTSTRING_1)) == 0 ||
|
111 |
| - aqo_mode == AQO_MODE_DISABLED) |
| 113 | + aqo_mode == AQO_MODE_DISABLED || isQueryUsingSystemRelation(parse)) |
112 | 114 | {
|
113 | 115 | disable_aqo_for_query();
|
114 | 116 | return call_default_planner(parse, cursorOptions, boundParams);
|
@@ -212,3 +214,50 @@ disable_aqo_for_query(void)
|
212 | 214 | auto_tuning = false;
|
213 | 215 | collect_stat = false;
|
214 | 216 | }
|
| 217 | + |
| 218 | +/* |
| 219 | + * Examine a fully-parsed query, and return TRUE iff any relation underlying |
| 220 | + * the query is a system relation. |
| 221 | + */ |
| 222 | +bool |
| 223 | +isQueryUsingSystemRelation(Query *query) |
| 224 | +{ |
| 225 | + return isQueryUsingSystemRelation_walker((Node *) query, NULL); |
| 226 | +} |
| 227 | + |
| 228 | +bool |
| 229 | +isQueryUsingSystemRelation_walker(Node *node, void *context) |
| 230 | +{ |
| 231 | + if (node == NULL) |
| 232 | + return false; |
| 233 | + |
| 234 | + if (IsA(node, Query)) |
| 235 | + { |
| 236 | + Query *query = (Query *) node; |
| 237 | + ListCell *rtable; |
| 238 | + |
| 239 | + foreach(rtable, query->rtable) |
| 240 | + { |
| 241 | + RangeTblEntry *rte = lfirst(rtable); |
| 242 | + |
| 243 | + if (rte->rtekind == RTE_RELATION) |
| 244 | + { |
| 245 | + Relation rel = heap_open(rte->relid, AccessShareLock); |
| 246 | + bool is_catalog = IsCatalogRelation(rel); |
| 247 | + |
| 248 | + heap_close(rel, AccessShareLock); |
| 249 | + if (is_catalog) |
| 250 | + return true; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + return query_tree_walker(query, |
| 255 | + isQueryUsingSystemRelation_walker, |
| 256 | + context, |
| 257 | + 0); |
| 258 | + } |
| 259 | + |
| 260 | + return expression_tree_walker(node, |
| 261 | + isQueryUsingSystemRelation_walker, |
| 262 | + context); |
| 263 | +} |
0 commit comments