* QueryRewrite - * Primary entry point to the query rewriter. * Rewrite one query via query rewrite system, possibly returning 0 * or many queries. * * NOTE: the parsetree must either have come straight from the parser, * or have been scanned by AcquireRewriteLocks to acquire suitable locks. */
| 4184 | * or have been scanned by AcquireRewriteLocks to acquire suitable locks. |
| 4185 | */ |
| 4186 | List * |
| 4187 | QueryRewrite(Query *parsetree) |
| 4188 | { |
| 4189 | uint64 input_query_id = parsetree->queryId; |
| 4190 | List *querylist; |
| 4191 | List *results; |
| 4192 | ListCell *l; |
| 4193 | CmdType origCmdType; |
| 4194 | bool foundOriginalQuery; |
| 4195 | Query *lastInstead; |
| 4196 | |
| 4197 | /* |
| 4198 | * This function is only applied to top-level original queries |
| 4199 | */ |
| 4200 | Assert(parsetree->querySource == QSRC_ORIGINAL); |
| 4201 | Assert(parsetree->canSetTag); |
| 4202 | |
| 4203 | /* |
| 4204 | * Step 1 |
| 4205 | * |
| 4206 | * Apply all non-SELECT rules possibly getting 0 or many queries |
| 4207 | */ |
| 4208 | querylist = RewriteQuery(parsetree, NIL); |
| 4209 | |
| 4210 | /* |
| 4211 | * Step 2 |
| 4212 | * |
| 4213 | * Apply all the RIR rules on each query |
| 4214 | * |
| 4215 | * This is also a handy place to mark each query with the original queryId |
| 4216 | */ |
| 4217 | results = NIL; |
| 4218 | foreach(l, querylist) |
| 4219 | { |
| 4220 | Query *query = (Query *) lfirst(l); |
| 4221 | |
| 4222 | query = fireRIRrules(query, NIL); |
| 4223 | |
| 4224 | query->queryId = input_query_id; |
| 4225 | |
| 4226 | results = lappend(results, query); |
| 4227 | } |
| 4228 | |
| 4229 | /* |
| 4230 | * Step 3 |
| 4231 | * |
| 4232 | * Determine which, if any, of the resulting queries is supposed to set |
| 4233 | * the command-result tag; and update the canSetTag fields accordingly. |
| 4234 | * |
| 4235 | * If the original query is still in the list, it sets the command tag. |
| 4236 | * Otherwise, the last INSTEAD query of the same kind as the original is |
| 4237 | * allowed to set the tag. (Note these rules can leave us with no query |
| 4238 | * setting the tag. The tcop code has to cope with this by setting up a |
| 4239 | * default tag based on the original un-rewritten query.) |
| 4240 | * |
| 4241 | * The Asserts verify that at most one query in the result list is marked |
| 4242 | * canSetTag. If we aren't checking asserts, we can fall out of the loop |
| 4243 | * as soon as we find the original query. |
no test coverage detected