00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include "postgres.h"
00049
00050 #include <limits.h>
00051
00052 #include "access/transam.h"
00053 #include "catalog/namespace.h"
00054 #include "executor/executor.h"
00055 #include "executor/spi.h"
00056 #include "nodes/nodeFuncs.h"
00057 #include "optimizer/planmain.h"
00058 #include "optimizer/prep.h"
00059 #include "parser/analyze.h"
00060 #include "parser/parsetree.h"
00061 #include "storage/lmgr.h"
00062 #include "tcop/pquery.h"
00063 #include "tcop/utility.h"
00064 #include "utils/inval.h"
00065 #include "utils/memutils.h"
00066 #include "utils/resowner_private.h"
00067 #include "utils/snapmgr.h"
00068 #include "utils/syscache.h"
00069
00070
00071
00072
00073
00074
00075 #define IsTransactionStmtPlan(plansource) \
00076 ((plansource)->raw_parse_tree && \
00077 IsA((plansource)->raw_parse_tree, TransactionStmt))
00078
00079
00080
00081
00082
00083
00084
00085 static CachedPlanSource *first_saved_plan = NULL;
00086
00087 static void ReleaseGenericPlan(CachedPlanSource *plansource);
00088 static List *RevalidateCachedQuery(CachedPlanSource *plansource);
00089 static bool CheckCachedPlan(CachedPlanSource *plansource);
00090 static CachedPlan *BuildCachedPlan(CachedPlanSource *plansource, List *qlist,
00091 ParamListInfo boundParams);
00092 static bool choose_custom_plan(CachedPlanSource *plansource,
00093 ParamListInfo boundParams);
00094 static double cached_plan_cost(CachedPlan *plan);
00095 static void AcquireExecutorLocks(List *stmt_list, bool acquire);
00096 static void AcquirePlannerLocks(List *stmt_list, bool acquire);
00097 static void ScanQueryForLocks(Query *parsetree, bool acquire);
00098 static bool ScanQueryWalker(Node *node, bool *acquire);
00099 static bool plan_list_is_transient(List *stmt_list);
00100 static TupleDesc PlanCacheComputeResultDesc(List *stmt_list);
00101 static void PlanCacheRelCallback(Datum arg, Oid relid);
00102 static void PlanCacheFuncCallback(Datum arg, int cacheid, uint32 hashvalue);
00103 static void PlanCacheSysCallback(Datum arg, int cacheid, uint32 hashvalue);
00104
00105
00106
00107
00108
00109
00110
00111 void
00112 InitPlanCache(void)
00113 {
00114 CacheRegisterRelcacheCallback(PlanCacheRelCallback, (Datum) 0);
00115 CacheRegisterSyscacheCallback(PROCOID, PlanCacheFuncCallback, (Datum) 0);
00116 CacheRegisterSyscacheCallback(NAMESPACEOID, PlanCacheSysCallback, (Datum) 0);
00117 CacheRegisterSyscacheCallback(OPEROID, PlanCacheSysCallback, (Datum) 0);
00118 CacheRegisterSyscacheCallback(AMOPOPID, PlanCacheSysCallback, (Datum) 0);
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 CachedPlanSource *
00146 CreateCachedPlan(Node *raw_parse_tree,
00147 const char *query_string,
00148 const char *commandTag)
00149 {
00150 CachedPlanSource *plansource;
00151 MemoryContext source_context;
00152 MemoryContext oldcxt;
00153
00154 Assert(query_string != NULL);
00155
00156
00157
00158
00159
00160
00161
00162
00163 source_context = AllocSetContextCreate(CurrentMemoryContext,
00164 "CachedPlanSource",
00165 ALLOCSET_SMALL_MINSIZE,
00166 ALLOCSET_SMALL_INITSIZE,
00167 ALLOCSET_DEFAULT_MAXSIZE);
00168
00169
00170
00171
00172
00173 oldcxt = MemoryContextSwitchTo(source_context);
00174
00175 plansource = (CachedPlanSource *) palloc0(sizeof(CachedPlanSource));
00176 plansource->magic = CACHEDPLANSOURCE_MAGIC;
00177 plansource->raw_parse_tree = copyObject(raw_parse_tree);
00178 plansource->query_string = pstrdup(query_string);
00179 plansource->commandTag = commandTag;
00180 plansource->param_types = NULL;
00181 plansource->num_params = 0;
00182 plansource->parserSetup = NULL;
00183 plansource->parserSetupArg = NULL;
00184 plansource->cursor_options = 0;
00185 plansource->fixed_result = false;
00186 plansource->resultDesc = NULL;
00187 plansource->context = source_context;
00188 plansource->query_list = NIL;
00189 plansource->relationOids = NIL;
00190 plansource->invalItems = NIL;
00191 plansource->search_path = NULL;
00192 plansource->query_context = NULL;
00193 plansource->gplan = NULL;
00194 plansource->is_oneshot = false;
00195 plansource->is_complete = false;
00196 plansource->is_saved = false;
00197 plansource->is_valid = false;
00198 plansource->generation = 0;
00199 plansource->next_saved = NULL;
00200 plansource->generic_cost = -1;
00201 plansource->total_custom_cost = 0;
00202 plansource->num_custom_plans = 0;
00203
00204 MemoryContextSwitchTo(oldcxt);
00205
00206 return plansource;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 CachedPlanSource *
00228 CreateOneShotCachedPlan(Node *raw_parse_tree,
00229 const char *query_string,
00230 const char *commandTag)
00231 {
00232 CachedPlanSource *plansource;
00233
00234 Assert(query_string != NULL);
00235
00236
00237
00238
00239
00240 plansource = (CachedPlanSource *) palloc0(sizeof(CachedPlanSource));
00241 plansource->magic = CACHEDPLANSOURCE_MAGIC;
00242 plansource->raw_parse_tree = raw_parse_tree;
00243 plansource->query_string = query_string;
00244 plansource->commandTag = commandTag;
00245 plansource->param_types = NULL;
00246 plansource->num_params = 0;
00247 plansource->parserSetup = NULL;
00248 plansource->parserSetupArg = NULL;
00249 plansource->cursor_options = 0;
00250 plansource->fixed_result = false;
00251 plansource->resultDesc = NULL;
00252 plansource->context = CurrentMemoryContext;
00253 plansource->query_list = NIL;
00254 plansource->relationOids = NIL;
00255 plansource->invalItems = NIL;
00256 plansource->search_path = NULL;
00257 plansource->query_context = NULL;
00258 plansource->gplan = NULL;
00259 plansource->is_oneshot = true;
00260 plansource->is_complete = false;
00261 plansource->is_saved = false;
00262 plansource->is_valid = false;
00263 plansource->generation = 0;
00264 plansource->next_saved = NULL;
00265 plansource->generic_cost = -1;
00266 plansource->total_custom_cost = 0;
00267 plansource->num_custom_plans = 0;
00268
00269 return plansource;
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 void
00315 CompleteCachedPlan(CachedPlanSource *plansource,
00316 List *querytree_list,
00317 MemoryContext querytree_context,
00318 Oid *param_types,
00319 int num_params,
00320 ParserSetupHook parserSetup,
00321 void *parserSetupArg,
00322 int cursor_options,
00323 bool fixed_result)
00324 {
00325 MemoryContext source_context = plansource->context;
00326 MemoryContext oldcxt = CurrentMemoryContext;
00327
00328
00329 Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
00330 Assert(!plansource->is_complete);
00331
00332
00333
00334
00335
00336
00337
00338
00339 if (plansource->is_oneshot)
00340 {
00341 querytree_context = CurrentMemoryContext;
00342 }
00343 else if (querytree_context != NULL)
00344 {
00345 MemoryContextSetParent(querytree_context, source_context);
00346 MemoryContextSwitchTo(querytree_context);
00347 }
00348 else
00349 {
00350
00351 querytree_context = AllocSetContextCreate(source_context,
00352 "CachedPlanQuery",
00353 ALLOCSET_SMALL_MINSIZE,
00354 ALLOCSET_SMALL_INITSIZE,
00355 ALLOCSET_DEFAULT_MAXSIZE);
00356 MemoryContextSwitchTo(querytree_context);
00357 querytree_list = (List *) copyObject(querytree_list);
00358 }
00359
00360 plansource->query_context = querytree_context;
00361 plansource->query_list = querytree_list;
00362
00363 if (!plansource->is_oneshot && !IsTransactionStmtPlan(plansource))
00364 {
00365
00366
00367
00368
00369
00370
00371 extract_query_dependencies((Node *) querytree_list,
00372 &plansource->relationOids,
00373 &plansource->invalItems);
00374
00375
00376
00377
00378
00379
00380
00381
00382 plansource->search_path = GetOverrideSearchPath(querytree_context);
00383 }
00384
00385
00386
00387
00388
00389
00390 MemoryContextSwitchTo(source_context);
00391
00392 if (num_params > 0)
00393 {
00394 plansource->param_types = (Oid *) palloc(num_params * sizeof(Oid));
00395 memcpy(plansource->param_types, param_types, num_params * sizeof(Oid));
00396 }
00397 else
00398 plansource->param_types = NULL;
00399 plansource->num_params = num_params;
00400 plansource->parserSetup = parserSetup;
00401 plansource->parserSetupArg = parserSetupArg;
00402 plansource->cursor_options = cursor_options;
00403 plansource->fixed_result = fixed_result;
00404 plansource->resultDesc = PlanCacheComputeResultDesc(querytree_list);
00405
00406 MemoryContextSwitchTo(oldcxt);
00407
00408 plansource->is_complete = true;
00409 plansource->is_valid = true;
00410 }
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 void
00428 SaveCachedPlan(CachedPlanSource *plansource)
00429 {
00430
00431 Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
00432 Assert(plansource->is_complete);
00433 Assert(!plansource->is_saved);
00434
00435
00436 if (plansource->is_oneshot)
00437 elog(ERROR, "cannot save one-shot cached plan");
00438
00439
00440
00441
00442
00443
00444
00445
00446 ReleaseGenericPlan(plansource);
00447
00448
00449
00450
00451
00452
00453 MemoryContextSetParent(plansource->context, CacheMemoryContext);
00454
00455
00456
00457
00458 plansource->next_saved = first_saved_plan;
00459 first_saved_plan = plansource;
00460
00461 plansource->is_saved = true;
00462 }
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472 void
00473 DropCachedPlan(CachedPlanSource *plansource)
00474 {
00475 Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
00476
00477
00478 if (plansource->is_saved)
00479 {
00480 if (first_saved_plan == plansource)
00481 first_saved_plan = plansource->next_saved;
00482 else
00483 {
00484 CachedPlanSource *psrc;
00485
00486 for (psrc = first_saved_plan; psrc; psrc = psrc->next_saved)
00487 {
00488 if (psrc->next_saved == plansource)
00489 {
00490 psrc->next_saved = plansource->next_saved;
00491 break;
00492 }
00493 }
00494 }
00495 plansource->is_saved = false;
00496 }
00497
00498
00499 ReleaseGenericPlan(plansource);
00500
00501
00502 plansource->magic = 0;
00503
00504
00505
00506
00507
00508 if (!plansource->is_oneshot)
00509 MemoryContextDelete(plansource->context);
00510 }
00511
00512
00513
00514
00515 static void
00516 ReleaseGenericPlan(CachedPlanSource *plansource)
00517 {
00518
00519 if (plansource->gplan)
00520 {
00521 CachedPlan *plan = plansource->gplan;
00522
00523 Assert(plan->magic == CACHEDPLAN_MAGIC);
00524 plansource->gplan = NULL;
00525 ReleaseCachedPlan(plan, false);
00526 }
00527 }
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 static List *
00544 RevalidateCachedQuery(CachedPlanSource *plansource)
00545 {
00546 bool snapshot_set;
00547 Node *rawtree;
00548 List *tlist;
00549 List *qlist;
00550 TupleDesc resultDesc;
00551 MemoryContext querytree_context;
00552 MemoryContext oldcxt;
00553
00554
00555
00556
00557
00558
00559
00560
00561 if (plansource->is_oneshot || IsTransactionStmtPlan(plansource))
00562 {
00563 Assert(plansource->is_valid);
00564 return NIL;
00565 }
00566
00567
00568
00569
00570
00571
00572 if (plansource->is_valid)
00573 {
00574 Assert(plansource->search_path != NULL);
00575 if (!OverrideSearchPathMatchesCurrent(plansource->search_path))
00576 {
00577
00578 plansource->is_valid = false;
00579 if (plansource->gplan)
00580 plansource->gplan->is_valid = false;
00581 }
00582 }
00583
00584
00585
00586
00587
00588
00589 if (plansource->is_valid)
00590 {
00591 AcquirePlannerLocks(plansource->query_list, true);
00592
00593
00594
00595
00596
00597 if (plansource->is_valid)
00598 {
00599
00600 return NIL;
00601 }
00602
00603
00604 AcquirePlannerLocks(plansource->query_list, false);
00605 }
00606
00607
00608
00609
00610
00611
00612 plansource->is_valid = false;
00613 plansource->query_list = NIL;
00614 plansource->relationOids = NIL;
00615 plansource->invalItems = NIL;
00616 plansource->search_path = NULL;
00617
00618
00619
00620
00621
00622
00623
00624 if (plansource->query_context)
00625 {
00626 MemoryContext qcxt = plansource->query_context;
00627
00628 plansource->query_context = NULL;
00629 MemoryContextDelete(qcxt);
00630 }
00631
00632
00633 ReleaseGenericPlan(plansource);
00634
00635
00636
00637
00638
00639 Assert(plansource->is_complete);
00640
00641
00642
00643
00644
00645
00646
00647
00648 snapshot_set = false;
00649 if (!ActiveSnapshotSet())
00650 {
00651 PushActiveSnapshot(GetTransactionSnapshot());
00652 snapshot_set = true;
00653 }
00654
00655
00656
00657
00658
00659
00660 rawtree = copyObject(plansource->raw_parse_tree);
00661 if (plansource->parserSetup != NULL)
00662 tlist = pg_analyze_and_rewrite_params(rawtree,
00663 plansource->query_string,
00664 plansource->parserSetup,
00665 plansource->parserSetupArg);
00666 else
00667 tlist = pg_analyze_and_rewrite(rawtree,
00668 plansource->query_string,
00669 plansource->param_types,
00670 plansource->num_params);
00671
00672
00673 if (snapshot_set)
00674 PopActiveSnapshot();
00675
00676
00677
00678
00679
00680
00681
00682
00683 resultDesc = PlanCacheComputeResultDesc(tlist);
00684 if (resultDesc == NULL && plansource->resultDesc == NULL)
00685 {
00686
00687 }
00688 else if (resultDesc == NULL || plansource->resultDesc == NULL ||
00689 !equalTupleDescs(resultDesc, plansource->resultDesc))
00690 {
00691
00692 if (plansource->fixed_result)
00693 ereport(ERROR,
00694 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
00695 errmsg("cached plan must not change result type")));
00696 oldcxt = MemoryContextSwitchTo(plansource->context);
00697 if (resultDesc)
00698 resultDesc = CreateTupleDescCopy(resultDesc);
00699 if (plansource->resultDesc)
00700 FreeTupleDesc(plansource->resultDesc);
00701 plansource->resultDesc = resultDesc;
00702 MemoryContextSwitchTo(oldcxt);
00703 }
00704
00705
00706
00707
00708
00709 querytree_context = AllocSetContextCreate(CurrentMemoryContext,
00710 "CachedPlanQuery",
00711 ALLOCSET_SMALL_MINSIZE,
00712 ALLOCSET_SMALL_INITSIZE,
00713 ALLOCSET_DEFAULT_MAXSIZE);
00714 oldcxt = MemoryContextSwitchTo(querytree_context);
00715
00716 qlist = (List *) copyObject(tlist);
00717
00718
00719
00720
00721
00722
00723 extract_query_dependencies((Node *) qlist,
00724 &plansource->relationOids,
00725 &plansource->invalItems);
00726
00727
00728
00729
00730
00731
00732 plansource->search_path = GetOverrideSearchPath(querytree_context);
00733
00734 MemoryContextSwitchTo(oldcxt);
00735
00736
00737 MemoryContextSetParent(querytree_context, plansource->context);
00738
00739 plansource->query_context = querytree_context;
00740 plansource->query_list = qlist;
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751 plansource->is_valid = true;
00752
00753
00754 return tlist;
00755 }
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766 static bool
00767 CheckCachedPlan(CachedPlanSource *plansource)
00768 {
00769 CachedPlan *plan = plansource->gplan;
00770
00771
00772 Assert(plansource->is_valid);
00773
00774
00775 if (!plan)
00776 return false;
00777
00778 Assert(plan->magic == CACHEDPLAN_MAGIC);
00779
00780 Assert(!plan->is_oneshot);
00781
00782
00783
00784
00785
00786 if (plan->is_valid)
00787 {
00788
00789
00790
00791
00792 Assert(plan->refcount > 0);
00793
00794 AcquireExecutorLocks(plan->stmt_list, true);
00795
00796
00797
00798
00799
00800 if (plan->is_valid &&
00801 TransactionIdIsValid(plan->saved_xmin) &&
00802 !TransactionIdEquals(plan->saved_xmin, TransactionXmin))
00803 plan->is_valid = false;
00804
00805
00806
00807
00808
00809 if (plan->is_valid)
00810 {
00811
00812 return true;
00813 }
00814
00815
00816 AcquireExecutorLocks(plan->stmt_list, false);
00817 }
00818
00819
00820
00821
00822 ReleaseGenericPlan(plansource);
00823
00824 return false;
00825 }
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843 static CachedPlan *
00844 BuildCachedPlan(CachedPlanSource *plansource, List *qlist,
00845 ParamListInfo boundParams)
00846 {
00847 CachedPlan *plan;
00848 List *plist;
00849 bool snapshot_set;
00850 bool spi_pushed;
00851 MemoryContext plan_context;
00852 MemoryContext oldcxt = CurrentMemoryContext;
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867 if (!plansource->is_valid)
00868 qlist = RevalidateCachedQuery(plansource);
00869
00870
00871
00872
00873
00874
00875 if (qlist == NIL)
00876 {
00877 if (!plansource->is_oneshot)
00878 qlist = (List *) copyObject(plansource->query_list);
00879 else
00880 qlist = plansource->query_list;
00881 }
00882
00883
00884
00885
00886
00887 snapshot_set = false;
00888 if (!ActiveSnapshotSet() &&
00889 analyze_requires_snapshot(plansource->raw_parse_tree))
00890 {
00891 PushActiveSnapshot(GetTransactionSnapshot());
00892 snapshot_set = true;
00893 }
00894
00895
00896
00897
00898
00899
00900
00901 spi_pushed = SPI_push_conditional();
00902
00903
00904
00905
00906 plist = pg_plan_queries(qlist, plansource->cursor_options, boundParams);
00907
00908
00909 SPI_pop_conditional(spi_pushed);
00910
00911
00912 if (snapshot_set)
00913 PopActiveSnapshot();
00914
00915
00916
00917
00918
00919
00920
00921
00922 if (!plansource->is_oneshot)
00923 {
00924 plan_context = AllocSetContextCreate(CurrentMemoryContext,
00925 "CachedPlan",
00926 ALLOCSET_SMALL_MINSIZE,
00927 ALLOCSET_SMALL_INITSIZE,
00928 ALLOCSET_DEFAULT_MAXSIZE);
00929
00930
00931
00932
00933 MemoryContextSwitchTo(plan_context);
00934
00935 plist = (List *) copyObject(plist);
00936 }
00937 else
00938 plan_context = CurrentMemoryContext;
00939
00940
00941
00942
00943 plan = (CachedPlan *) palloc(sizeof(CachedPlan));
00944 plan->magic = CACHEDPLAN_MAGIC;
00945 plan->stmt_list = plist;
00946 if (plan_list_is_transient(plist))
00947 {
00948 Assert(TransactionIdIsNormal(TransactionXmin));
00949 plan->saved_xmin = TransactionXmin;
00950 }
00951 else
00952 plan->saved_xmin = InvalidTransactionId;
00953 plan->refcount = 0;
00954 plan->context = plan_context;
00955 plan->is_oneshot = plansource->is_oneshot;
00956 plan->is_saved = false;
00957 plan->is_valid = true;
00958
00959
00960 plan->generation = ++(plansource->generation);
00961
00962 MemoryContextSwitchTo(oldcxt);
00963
00964 return plan;
00965 }
00966
00967
00968
00969
00970
00971
00972 static bool
00973 choose_custom_plan(CachedPlanSource *plansource, ParamListInfo boundParams)
00974 {
00975 double avg_custom_cost;
00976
00977
00978 if (plansource->is_oneshot)
00979 return true;
00980
00981
00982 if (boundParams == NULL)
00983 return false;
00984
00985 if (IsTransactionStmtPlan(plansource))
00986 return false;
00987
00988
00989 if (plansource->cursor_options & CURSOR_OPT_GENERIC_PLAN)
00990 return false;
00991 if (plansource->cursor_options & CURSOR_OPT_CUSTOM_PLAN)
00992 return true;
00993
00994
00995 if (plansource->num_custom_plans < 5)
00996 return true;
00997
00998 avg_custom_cost = plansource->total_custom_cost / plansource->num_custom_plans;
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009 if (plansource->generic_cost < avg_custom_cost * 1.1)
01010 return false;
01011
01012 return true;
01013 }
01014
01015
01016
01017
01018 static double
01019 cached_plan_cost(CachedPlan *plan)
01020 {
01021 double result = 0;
01022 ListCell *lc;
01023
01024 foreach(lc, plan->stmt_list)
01025 {
01026 PlannedStmt *plannedstmt = (PlannedStmt *) lfirst(lc);
01027
01028 if (!IsA(plannedstmt, PlannedStmt))
01029 continue;
01030
01031 result += plannedstmt->planTree->total_cost;
01032 }
01033
01034 return result;
01035 }
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055 CachedPlan *
01056 GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams,
01057 bool useResOwner)
01058 {
01059 CachedPlan *plan;
01060 List *qlist;
01061 bool customplan;
01062
01063
01064 Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
01065 Assert(plansource->is_complete);
01066
01067 if (useResOwner && !plansource->is_saved)
01068 elog(ERROR, "cannot apply ResourceOwner to non-saved cached plan");
01069
01070
01071 qlist = RevalidateCachedQuery(plansource);
01072
01073
01074 customplan = choose_custom_plan(plansource, boundParams);
01075
01076 if (!customplan)
01077 {
01078 if (CheckCachedPlan(plansource))
01079 {
01080
01081 plan = plansource->gplan;
01082 Assert(plan->magic == CACHEDPLAN_MAGIC);
01083 }
01084 else
01085 {
01086
01087 plan = BuildCachedPlan(plansource, qlist, NULL);
01088
01089 ReleaseGenericPlan(plansource);
01090
01091 plansource->gplan = plan;
01092 plan->refcount++;
01093
01094 if (plansource->is_saved)
01095 {
01096
01097 MemoryContextSetParent(plan->context, CacheMemoryContext);
01098 plan->is_saved = true;
01099 }
01100 else
01101 {
01102
01103 MemoryContextSetParent(plan->context,
01104 MemoryContextGetParent(plansource->context));
01105 }
01106
01107 plansource->generic_cost = cached_plan_cost(plan);
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118 customplan = choose_custom_plan(plansource, boundParams);
01119
01120
01121
01122
01123
01124
01125 qlist = NIL;
01126 }
01127 }
01128
01129 if (customplan)
01130 {
01131
01132 plan = BuildCachedPlan(plansource, qlist, boundParams);
01133
01134 if (plansource->num_custom_plans < INT_MAX)
01135 {
01136 plansource->total_custom_cost += cached_plan_cost(plan);
01137 plansource->num_custom_plans++;
01138 }
01139 }
01140
01141
01142 if (useResOwner)
01143 ResourceOwnerEnlargePlanCacheRefs(CurrentResourceOwner);
01144 plan->refcount++;
01145 if (useResOwner)
01146 ResourceOwnerRememberPlanCacheRef(CurrentResourceOwner, plan);
01147
01148
01149
01150
01151
01152
01153
01154 if (customplan && plansource->is_saved)
01155 {
01156 MemoryContextSetParent(plan->context, CacheMemoryContext);
01157 plan->is_saved = true;
01158 }
01159
01160 return plan;
01161 }
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174 void
01175 ReleaseCachedPlan(CachedPlan *plan, bool useResOwner)
01176 {
01177 Assert(plan->magic == CACHEDPLAN_MAGIC);
01178 if (useResOwner)
01179 {
01180 Assert(plan->is_saved);
01181 ResourceOwnerForgetPlanCacheRef(CurrentResourceOwner, plan);
01182 }
01183 Assert(plan->refcount > 0);
01184 plan->refcount--;
01185 if (plan->refcount == 0)
01186 {
01187
01188 plan->magic = 0;
01189
01190
01191 if (!plan->is_oneshot)
01192 MemoryContextDelete(plan->context);
01193 }
01194 }
01195
01196
01197
01198
01199
01200
01201
01202 void
01203 CachedPlanSetParentContext(CachedPlanSource *plansource,
01204 MemoryContext newcontext)
01205 {
01206
01207 Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
01208 Assert(plansource->is_complete);
01209
01210
01211 if (plansource->is_saved)
01212 elog(ERROR, "cannot move a saved cached plan to another context");
01213 if (plansource->is_oneshot)
01214 elog(ERROR, "cannot move a one-shot cached plan to another context");
01215
01216
01217 MemoryContextSetParent(plansource->context, newcontext);
01218
01219
01220
01221
01222
01223
01224 if (plansource->gplan)
01225 {
01226 Assert(plansource->gplan->magic == CACHEDPLAN_MAGIC);
01227 MemoryContextSetParent(plansource->gplan->context, newcontext);
01228 }
01229 }
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240 CachedPlanSource *
01241 CopyCachedPlan(CachedPlanSource *plansource)
01242 {
01243 CachedPlanSource *newsource;
01244 MemoryContext source_context;
01245 MemoryContext querytree_context;
01246 MemoryContext oldcxt;
01247
01248 Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
01249 Assert(plansource->is_complete);
01250
01251
01252
01253
01254
01255 if (plansource->is_oneshot)
01256 elog(ERROR, "cannot copy a one-shot cached plan");
01257
01258 source_context = AllocSetContextCreate(CurrentMemoryContext,
01259 "CachedPlanSource",
01260 ALLOCSET_SMALL_MINSIZE,
01261 ALLOCSET_SMALL_INITSIZE,
01262 ALLOCSET_DEFAULT_MAXSIZE);
01263
01264 oldcxt = MemoryContextSwitchTo(source_context);
01265
01266 newsource = (CachedPlanSource *) palloc0(sizeof(CachedPlanSource));
01267 newsource->magic = CACHEDPLANSOURCE_MAGIC;
01268 newsource->raw_parse_tree = copyObject(plansource->raw_parse_tree);
01269 newsource->query_string = pstrdup(plansource->query_string);
01270 newsource->commandTag = plansource->commandTag;
01271 if (plansource->num_params > 0)
01272 {
01273 newsource->param_types = (Oid *)
01274 palloc(plansource->num_params * sizeof(Oid));
01275 memcpy(newsource->param_types, plansource->param_types,
01276 plansource->num_params * sizeof(Oid));
01277 }
01278 else
01279 newsource->param_types = NULL;
01280 newsource->num_params = plansource->num_params;
01281 newsource->parserSetup = plansource->parserSetup;
01282 newsource->parserSetupArg = plansource->parserSetupArg;
01283 newsource->cursor_options = plansource->cursor_options;
01284 newsource->fixed_result = plansource->fixed_result;
01285 if (plansource->resultDesc)
01286 newsource->resultDesc = CreateTupleDescCopy(plansource->resultDesc);
01287 else
01288 newsource->resultDesc = NULL;
01289 newsource->context = source_context;
01290
01291 querytree_context = AllocSetContextCreate(source_context,
01292 "CachedPlanQuery",
01293 ALLOCSET_SMALL_MINSIZE,
01294 ALLOCSET_SMALL_INITSIZE,
01295 ALLOCSET_DEFAULT_MAXSIZE);
01296 MemoryContextSwitchTo(querytree_context);
01297 newsource->query_list = (List *) copyObject(plansource->query_list);
01298 newsource->relationOids = (List *) copyObject(plansource->relationOids);
01299 newsource->invalItems = (List *) copyObject(plansource->invalItems);
01300 if (plansource->search_path)
01301 newsource->search_path = CopyOverrideSearchPath(plansource->search_path);
01302 newsource->query_context = querytree_context;
01303
01304 newsource->gplan = NULL;
01305
01306 newsource->is_oneshot = false;
01307 newsource->is_complete = true;
01308 newsource->is_saved = false;
01309 newsource->is_valid = plansource->is_valid;
01310 newsource->generation = plansource->generation;
01311 newsource->next_saved = NULL;
01312
01313
01314 newsource->generic_cost = plansource->generic_cost;
01315 newsource->total_custom_cost = plansource->total_custom_cost;
01316 newsource->num_custom_plans = plansource->num_custom_plans;
01317
01318 MemoryContextSwitchTo(oldcxt);
01319
01320 return newsource;
01321 }
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331 bool
01332 CachedPlanIsValid(CachedPlanSource *plansource)
01333 {
01334 Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
01335 return plansource->is_valid;
01336 }
01337
01338
01339
01340
01341
01342
01343
01344 List *
01345 CachedPlanGetTargetList(CachedPlanSource *plansource)
01346 {
01347 Node *pstmt;
01348
01349
01350 Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
01351 Assert(plansource->is_complete);
01352
01353
01354
01355
01356
01357 if (plansource->resultDesc == NULL)
01358 return NIL;
01359
01360
01361 RevalidateCachedQuery(plansource);
01362
01363
01364 pstmt = PortalListGetPrimaryStmt(plansource->query_list);
01365
01366 return FetchStatementTargetList(pstmt);
01367 }
01368
01369
01370
01371
01372
01373 static void
01374 AcquireExecutorLocks(List *stmt_list, bool acquire)
01375 {
01376 ListCell *lc1;
01377
01378 foreach(lc1, stmt_list)
01379 {
01380 PlannedStmt *plannedstmt = (PlannedStmt *) lfirst(lc1);
01381 int rt_index;
01382 ListCell *lc2;
01383
01384 Assert(!IsA(plannedstmt, Query));
01385 if (!IsA(plannedstmt, PlannedStmt))
01386 {
01387
01388
01389
01390
01391
01392
01393
01394 Query *query = UtilityContainsQuery((Node *) plannedstmt);
01395
01396 if (query)
01397 ScanQueryForLocks(query, acquire);
01398 continue;
01399 }
01400
01401 rt_index = 0;
01402 foreach(lc2, plannedstmt->rtable)
01403 {
01404 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc2);
01405 LOCKMODE lockmode;
01406 PlanRowMark *rc;
01407
01408 rt_index++;
01409
01410 if (rte->rtekind != RTE_RELATION)
01411 continue;
01412
01413
01414
01415
01416
01417
01418
01419 if (list_member_int(plannedstmt->resultRelations, rt_index))
01420 lockmode = RowExclusiveLock;
01421 else if ((rc = get_plan_rowmark(plannedstmt->rowMarks, rt_index)) != NULL &&
01422 RowMarkRequiresRowShareLock(rc->markType))
01423 lockmode = RowShareLock;
01424 else
01425 lockmode = AccessShareLock;
01426
01427 if (acquire)
01428 LockRelationOid(rte->relid, lockmode);
01429 else
01430 UnlockRelationOid(rte->relid, lockmode);
01431 }
01432 }
01433 }
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443 static void
01444 AcquirePlannerLocks(List *stmt_list, bool acquire)
01445 {
01446 ListCell *lc;
01447
01448 foreach(lc, stmt_list)
01449 {
01450 Query *query = (Query *) lfirst(lc);
01451
01452 Assert(IsA(query, Query));
01453
01454 if (query->commandType == CMD_UTILITY)
01455 {
01456
01457 query = UtilityContainsQuery(query->utilityStmt);
01458 if (query)
01459 ScanQueryForLocks(query, acquire);
01460 continue;
01461 }
01462
01463 ScanQueryForLocks(query, acquire);
01464 }
01465 }
01466
01467
01468
01469
01470 static void
01471 ScanQueryForLocks(Query *parsetree, bool acquire)
01472 {
01473 ListCell *lc;
01474 int rt_index;
01475
01476
01477 Assert(parsetree->commandType != CMD_UTILITY);
01478
01479
01480
01481
01482 rt_index = 0;
01483 foreach(lc, parsetree->rtable)
01484 {
01485 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
01486 LOCKMODE lockmode;
01487
01488 rt_index++;
01489 switch (rte->rtekind)
01490 {
01491 case RTE_RELATION:
01492
01493 if (rt_index == parsetree->resultRelation)
01494 lockmode = RowExclusiveLock;
01495 else if (get_parse_rowmark(parsetree, rt_index) != NULL)
01496 lockmode = RowShareLock;
01497 else
01498 lockmode = AccessShareLock;
01499 if (acquire)
01500 LockRelationOid(rte->relid, lockmode);
01501 else
01502 UnlockRelationOid(rte->relid, lockmode);
01503 break;
01504
01505 case RTE_SUBQUERY:
01506
01507 ScanQueryForLocks(rte->subquery, acquire);
01508 break;
01509
01510 default:
01511
01512 break;
01513 }
01514 }
01515
01516
01517 foreach(lc, parsetree->cteList)
01518 {
01519 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
01520
01521 ScanQueryForLocks((Query *) cte->ctequery, acquire);
01522 }
01523
01524
01525
01526
01527
01528 if (parsetree->hasSubLinks)
01529 {
01530 query_tree_walker(parsetree, ScanQueryWalker,
01531 (void *) &acquire,
01532 QTW_IGNORE_RC_SUBQUERIES);
01533 }
01534 }
01535
01536
01537
01538
01539 static bool
01540 ScanQueryWalker(Node *node, bool *acquire)
01541 {
01542 if (node == NULL)
01543 return false;
01544 if (IsA(node, SubLink))
01545 {
01546 SubLink *sub = (SubLink *) node;
01547
01548
01549 ScanQueryForLocks((Query *) sub->subselect, *acquire);
01550
01551 }
01552
01553
01554
01555
01556
01557 return expression_tree_walker(node, ScanQueryWalker,
01558 (void *) acquire);
01559 }
01560
01561
01562
01563
01564 static bool
01565 plan_list_is_transient(List *stmt_list)
01566 {
01567 ListCell *lc;
01568
01569 foreach(lc, stmt_list)
01570 {
01571 PlannedStmt *plannedstmt = (PlannedStmt *) lfirst(lc);
01572
01573 if (!IsA(plannedstmt, PlannedStmt))
01574 continue;
01575
01576 if (plannedstmt->transientPlan)
01577 return true;
01578 }
01579
01580 return false;
01581 }
01582
01583
01584
01585
01586
01587
01588
01589
01590 static TupleDesc
01591 PlanCacheComputeResultDesc(List *stmt_list)
01592 {
01593 Query *query;
01594
01595 switch (ChoosePortalStrategy(stmt_list))
01596 {
01597 case PORTAL_ONE_SELECT:
01598 case PORTAL_ONE_MOD_WITH:
01599 query = (Query *) linitial(stmt_list);
01600 Assert(IsA(query, Query));
01601 return ExecCleanTypeFromTL(query->targetList, false);
01602
01603 case PORTAL_ONE_RETURNING:
01604 query = (Query *) PortalListGetPrimaryStmt(stmt_list);
01605 Assert(IsA(query, Query));
01606 Assert(query->returningList);
01607 return ExecCleanTypeFromTL(query->returningList, false);
01608
01609 case PORTAL_UTIL_SELECT:
01610 query = (Query *) linitial(stmt_list);
01611 Assert(IsA(query, Query));
01612 Assert(query->utilityStmt);
01613 return UtilityTupleDescriptor(query->utilityStmt);
01614
01615 case PORTAL_MULTI_QUERY:
01616
01617 break;
01618 }
01619 return NULL;
01620 }
01621
01622
01623
01624
01625
01626
01627
01628
01629 static void
01630 PlanCacheRelCallback(Datum arg, Oid relid)
01631 {
01632 CachedPlanSource *plansource;
01633
01634 for (plansource = first_saved_plan; plansource; plansource = plansource->next_saved)
01635 {
01636 Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
01637
01638
01639 if (!plansource->is_valid)
01640 continue;
01641
01642
01643 if (IsTransactionStmtPlan(plansource))
01644 continue;
01645
01646
01647
01648
01649 if ((relid == InvalidOid) ? plansource->relationOids != NIL :
01650 list_member_oid(plansource->relationOids, relid))
01651 {
01652
01653 plansource->is_valid = false;
01654 if (plansource->gplan)
01655 plansource->gplan->is_valid = false;
01656 }
01657
01658
01659
01660
01661
01662 if (plansource->gplan && plansource->gplan->is_valid)
01663 {
01664 ListCell *lc;
01665
01666 foreach(lc, plansource->gplan->stmt_list)
01667 {
01668 PlannedStmt *plannedstmt = (PlannedStmt *) lfirst(lc);
01669
01670 Assert(!IsA(plannedstmt, Query));
01671 if (!IsA(plannedstmt, PlannedStmt))
01672 continue;
01673 if ((relid == InvalidOid) ? plannedstmt->relationOids != NIL :
01674 list_member_oid(plannedstmt->relationOids, relid))
01675 {
01676
01677 plansource->gplan->is_valid = false;
01678 break;
01679 }
01680 }
01681 }
01682 }
01683 }
01684
01685
01686
01687
01688
01689
01690
01691
01692
01693
01694
01695 static void
01696 PlanCacheFuncCallback(Datum arg, int cacheid, uint32 hashvalue)
01697 {
01698 CachedPlanSource *plansource;
01699
01700 for (plansource = first_saved_plan; plansource; plansource = plansource->next_saved)
01701 {
01702 ListCell *lc;
01703
01704 Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
01705
01706
01707 if (!plansource->is_valid)
01708 continue;
01709
01710
01711 if (IsTransactionStmtPlan(plansource))
01712 continue;
01713
01714
01715
01716
01717 foreach(lc, plansource->invalItems)
01718 {
01719 PlanInvalItem *item = (PlanInvalItem *) lfirst(lc);
01720
01721 if (item->cacheId != cacheid)
01722 continue;
01723 if (hashvalue == 0 ||
01724 item->hashValue == hashvalue)
01725 {
01726
01727 plansource->is_valid = false;
01728 if (plansource->gplan)
01729 plansource->gplan->is_valid = false;
01730 break;
01731 }
01732 }
01733
01734
01735
01736
01737
01738 if (plansource->gplan && plansource->gplan->is_valid)
01739 {
01740 foreach(lc, plansource->gplan->stmt_list)
01741 {
01742 PlannedStmt *plannedstmt = (PlannedStmt *) lfirst(lc);
01743 ListCell *lc3;
01744
01745 Assert(!IsA(plannedstmt, Query));
01746 if (!IsA(plannedstmt, PlannedStmt))
01747 continue;
01748 foreach(lc3, plannedstmt->invalItems)
01749 {
01750 PlanInvalItem *item = (PlanInvalItem *) lfirst(lc3);
01751
01752 if (item->cacheId != cacheid)
01753 continue;
01754 if (hashvalue == 0 ||
01755 item->hashValue == hashvalue)
01756 {
01757
01758 plansource->gplan->is_valid = false;
01759 break;
01760 }
01761 }
01762 if (!plansource->gplan->is_valid)
01763 break;
01764 }
01765 }
01766 }
01767 }
01768
01769
01770
01771
01772
01773
01774
01775 static void
01776 PlanCacheSysCallback(Datum arg, int cacheid, uint32 hashvalue)
01777 {
01778 ResetPlanCache();
01779 }
01780
01781
01782
01783
01784 void
01785 ResetPlanCache(void)
01786 {
01787 CachedPlanSource *plansource;
01788
01789 for (plansource = first_saved_plan; plansource; plansource = plansource->next_saved)
01790 {
01791 ListCell *lc;
01792
01793 Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
01794
01795
01796 if (!plansource->is_valid)
01797 continue;
01798
01799
01800
01801
01802
01803
01804 if (IsTransactionStmtPlan(plansource))
01805 continue;
01806
01807
01808
01809
01810
01811
01812
01813
01814 foreach(lc, plansource->query_list)
01815 {
01816 Query *query = (Query *) lfirst(lc);
01817
01818 Assert(IsA(query, Query));
01819 if (query->commandType != CMD_UTILITY ||
01820 UtilityContainsQuery(query->utilityStmt))
01821 {
01822
01823 plansource->is_valid = false;
01824 if (plansource->gplan)
01825 plansource->gplan->is_valid = false;
01826
01827 break;
01828 }
01829 }
01830 }
01831 }