pathnodes.h 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737
  1. /*-------------------------------------------------------------------------
  2. *
  3. * pathnodes.h
  4. * Definitions for planner's internal data structures, especially Paths.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/nodes/pathnodes.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef PATHNODES_H
  15. #define PATHNODES_H
  16. #include "access/sdir.h"
  17. #include "lib/stringinfo.h"
  18. #include "nodes/params.h"
  19. #include "nodes/parsenodes.h"
  20. #include "storage/block.h"
  21. /*
  22. * Relids
  23. * Set of relation identifiers (indexes into the rangetable).
  24. */
  25. typedef Bitmapset *Relids;
  26. /*
  27. * When looking for a "cheapest path", this enum specifies whether we want
  28. * cheapest startup cost or cheapest total cost.
  29. */
  30. typedef enum CostSelector
  31. {
  32. STARTUP_COST, TOTAL_COST
  33. } CostSelector;
  34. /*
  35. * The cost estimate produced by cost_qual_eval() includes both a one-time
  36. * (startup) cost, and a per-tuple cost.
  37. */
  38. typedef struct QualCost
  39. {
  40. Cost startup; /* one-time cost */
  41. Cost per_tuple; /* per-evaluation cost */
  42. } QualCost;
  43. /*
  44. * Costing aggregate function execution requires these statistics about
  45. * the aggregates to be executed by a given Agg node. Note that the costs
  46. * include the execution costs of the aggregates' argument expressions as
  47. * well as the aggregate functions themselves. Also, the fields must be
  48. * defined so that initializing the struct to zeroes with memset is correct.
  49. */
  50. typedef struct AggClauseCosts
  51. {
  52. QualCost transCost; /* total per-input-row execution costs */
  53. QualCost finalCost; /* total per-aggregated-row costs */
  54. Size transitionSpace; /* space for pass-by-ref transition data */
  55. } AggClauseCosts;
  56. /*
  57. * This enum identifies the different types of "upper" (post-scan/join)
  58. * relations that we might deal with during planning.
  59. */
  60. typedef enum UpperRelationKind
  61. {
  62. UPPERREL_SETOP, /* result of UNION/INTERSECT/EXCEPT, if any */
  63. UPPERREL_PARTIAL_GROUP_AGG, /* result of partial grouping/aggregation, if
  64. * any */
  65. UPPERREL_GROUP_AGG, /* result of grouping/aggregation, if any */
  66. UPPERREL_WINDOW, /* result of window functions, if any */
  67. UPPERREL_PARTIAL_DISTINCT, /* result of partial "SELECT DISTINCT", if any */
  68. UPPERREL_DISTINCT, /* result of "SELECT DISTINCT", if any */
  69. UPPERREL_ORDERED, /* result of ORDER BY, if any */
  70. UPPERREL_FINAL /* result of any remaining top-level actions */
  71. /* NB: UPPERREL_FINAL must be last enum entry; it's used to size arrays */
  72. } UpperRelationKind;
  73. /*----------
  74. * PlannerGlobal
  75. * Global information for planning/optimization
  76. *
  77. * PlannerGlobal holds state for an entire planner invocation; this state
  78. * is shared across all levels of sub-Queries that exist in the command being
  79. * planned.
  80. *----------
  81. */
  82. typedef struct PlannerGlobal
  83. {
  84. NodeTag type;
  85. ParamListInfo boundParams; /* Param values provided to planner() */
  86. List *subplans; /* Plans for SubPlan nodes */
  87. List *subroots; /* PlannerInfos for SubPlan nodes */
  88. Bitmapset *rewindPlanIDs; /* indices of subplans that require REWIND */
  89. List *finalrtable; /* "flat" rangetable for executor */
  90. List *finalrowmarks; /* "flat" list of PlanRowMarks */
  91. List *resultRelations; /* "flat" list of integer RT indexes */
  92. List *appendRelations; /* "flat" list of AppendRelInfos */
  93. List *relationOids; /* OIDs of relations the plan depends on */
  94. List *invalItems; /* other dependencies, as PlanInvalItems */
  95. List *paramExecTypes; /* type OIDs for PARAM_EXEC Params */
  96. Index lastPHId; /* highest PlaceHolderVar ID assigned */
  97. Index lastRowMarkId; /* highest PlanRowMark ID assigned */
  98. int lastPlanNodeId; /* highest plan node ID assigned */
  99. bool transientPlan; /* redo plan when TransactionXmin changes? */
  100. bool dependsOnRole; /* is plan specific to current role? */
  101. bool parallelModeOK; /* parallel mode potentially OK? */
  102. bool parallelModeNeeded; /* parallel mode actually required? */
  103. char maxParallelHazard; /* worst PROPARALLEL hazard level */
  104. PartitionDirectory partition_directory; /* partition descriptors */
  105. } PlannerGlobal;
  106. /* macro for fetching the Plan associated with a SubPlan node */
  107. #define planner_subplan_get_plan(root, subplan) \
  108. ((Plan *) list_nth((root)->glob->subplans, (subplan)->plan_id - 1))
  109. /*----------
  110. * PlannerInfo
  111. * Per-query information for planning/optimization
  112. *
  113. * This struct is conventionally called "root" in all the planner routines.
  114. * It holds links to all of the planner's working state, in addition to the
  115. * original Query. Note that at present the planner extensively modifies
  116. * the passed-in Query data structure; someday that should stop.
  117. *
  118. * For reasons explained in optimizer/optimizer.h, we define the typedef
  119. * either here or in that header, whichever is read first.
  120. *----------
  121. */
  122. #ifndef HAVE_PLANNERINFO_TYPEDEF
  123. typedef struct PlannerInfo PlannerInfo;
  124. #define HAVE_PLANNERINFO_TYPEDEF 1
  125. #endif
  126. struct PlannerInfo
  127. {
  128. NodeTag type;
  129. Query *parse; /* the Query being planned */
  130. PlannerGlobal *glob; /* global info for current planner run */
  131. Index query_level; /* 1 at the outermost Query */
  132. PlannerInfo *parent_root; /* NULL at outermost Query */
  133. /*
  134. * plan_params contains the expressions that this query level needs to
  135. * make available to a lower query level that is currently being planned.
  136. * outer_params contains the paramIds of PARAM_EXEC Params that outer
  137. * query levels will make available to this query level.
  138. */
  139. List *plan_params; /* list of PlannerParamItems, see below */
  140. Bitmapset *outer_params;
  141. /*
  142. * simple_rel_array holds pointers to "base rels" and "other rels" (see
  143. * comments for RelOptInfo for more info). It is indexed by rangetable
  144. * index (so entry 0 is always wasted). Entries can be NULL when an RTE
  145. * does not correspond to a base relation, such as a join RTE or an
  146. * unreferenced view RTE; or if the RelOptInfo hasn't been made yet.
  147. */
  148. struct RelOptInfo **simple_rel_array; /* All 1-rel RelOptInfos */
  149. int simple_rel_array_size; /* allocated size of array */
  150. /*
  151. * simple_rte_array is the same length as simple_rel_array and holds
  152. * pointers to the associated rangetable entries. Using this is a shade
  153. * faster than using rt_fetch(), mostly due to fewer indirections.
  154. */
  155. RangeTblEntry **simple_rte_array; /* rangetable as an array */
  156. /*
  157. * append_rel_array is the same length as the above arrays, and holds
  158. * pointers to the corresponding AppendRelInfo entry indexed by
  159. * child_relid, or NULL if the rel is not an appendrel child. The array
  160. * itself is not allocated if append_rel_list is empty.
  161. */
  162. struct AppendRelInfo **append_rel_array;
  163. /*
  164. * all_baserels is a Relids set of all base relids (but not "other"
  165. * relids) in the query; that is, the Relids identifier of the final join
  166. * we need to form. This is computed in make_one_rel, just before we
  167. * start making Paths.
  168. */
  169. Relids all_baserels;
  170. /*
  171. * nullable_baserels is a Relids set of base relids that are nullable by
  172. * some outer join in the jointree; these are rels that are potentially
  173. * nullable below the WHERE clause, SELECT targetlist, etc. This is
  174. * computed in deconstruct_jointree.
  175. */
  176. Relids nullable_baserels;
  177. /*
  178. * join_rel_list is a list of all join-relation RelOptInfos we have
  179. * considered in this planning run. For small problems we just scan the
  180. * list to do lookups, but when there are many join relations we build a
  181. * hash table for faster lookups. The hash table is present and valid
  182. * when join_rel_hash is not NULL. Note that we still maintain the list
  183. * even when using the hash table for lookups; this simplifies life for
  184. * GEQO.
  185. */
  186. List *join_rel_list; /* list of join-relation RelOptInfos */
  187. struct HTAB *join_rel_hash; /* optional hashtable for join relations */
  188. /*
  189. * When doing a dynamic-programming-style join search, join_rel_level[k]
  190. * is a list of all join-relation RelOptInfos of level k, and
  191. * join_cur_level is the current level. New join-relation RelOptInfos are
  192. * automatically added to the join_rel_level[join_cur_level] list.
  193. * join_rel_level is NULL if not in use.
  194. */
  195. List **join_rel_level; /* lists of join-relation RelOptInfos */
  196. int join_cur_level; /* index of list being extended */
  197. List *init_plans; /* init SubPlans for query */
  198. List *cte_plan_ids; /* per-CTE-item list of subplan IDs (or -1 if
  199. * no subplan was made for that CTE) */
  200. List *multiexpr_params; /* List of Lists of Params for MULTIEXPR
  201. * subquery outputs */
  202. List *eq_classes; /* list of active EquivalenceClasses */
  203. bool ec_merging_done; /* set true once ECs are canonical */
  204. List *canon_pathkeys; /* list of "canonical" PathKeys */
  205. List *left_join_clauses; /* list of RestrictInfos for mergejoinable
  206. * outer join clauses w/nonnullable var on
  207. * left */
  208. List *right_join_clauses; /* list of RestrictInfos for mergejoinable
  209. * outer join clauses w/nonnullable var on
  210. * right */
  211. List *full_join_clauses; /* list of RestrictInfos for mergejoinable
  212. * full join clauses */
  213. List *join_info_list; /* list of SpecialJoinInfos */
  214. /*
  215. * all_result_relids is empty for SELECT, otherwise it contains at least
  216. * parse->resultRelation. For UPDATE/DELETE/MERGE across an inheritance
  217. * or partitioning tree, the result rel's child relids are added. When
  218. * using multi-level partitioning, intermediate partitioned rels are
  219. * included. leaf_result_relids is similar except that only actual result
  220. * tables, not partitioned tables, are included in it.
  221. */
  222. Relids all_result_relids; /* set of all result relids */
  223. Relids leaf_result_relids; /* set of all leaf relids */
  224. /*
  225. * Note: for AppendRelInfos describing partitions of a partitioned table,
  226. * we guarantee that partitions that come earlier in the partitioned
  227. * table's PartitionDesc will appear earlier in append_rel_list.
  228. */
  229. List *append_rel_list; /* list of AppendRelInfos */
  230. List *row_identity_vars; /* list of RowIdentityVarInfos */
  231. List *rowMarks; /* list of PlanRowMarks */
  232. List *placeholder_list; /* list of PlaceHolderInfos */
  233. List *fkey_list; /* list of ForeignKeyOptInfos */
  234. List *query_pathkeys; /* desired pathkeys for query_planner() */
  235. List *group_pathkeys; /* groupClause pathkeys, if any */
  236. List *window_pathkeys; /* pathkeys of bottom window, if any */
  237. List *distinct_pathkeys; /* distinctClause pathkeys, if any */
  238. List *sort_pathkeys; /* sortClause pathkeys, if any */
  239. List *part_schemes; /* Canonicalised partition schemes used in the
  240. * query. */
  241. List *initial_rels; /* RelOptInfos we are now trying to join */
  242. /* Use fetch_upper_rel() to get any particular upper rel */
  243. List *upper_rels[UPPERREL_FINAL + 1]; /* upper-rel RelOptInfos */
  244. /* Result tlists chosen by grouping_planner for upper-stage processing */
  245. struct PathTarget *upper_targets[UPPERREL_FINAL + 1];
  246. /*
  247. * The fully-processed targetlist is kept here. It differs from
  248. * parse->targetList in that (for INSERT) it's been reordered to match the
  249. * target table, and defaults have been filled in. Also, additional
  250. * resjunk targets may be present. preprocess_targetlist() does most of
  251. * that work, but note that more resjunk targets can get added during
  252. * appendrel expansion. (Hence, upper_targets mustn't get set up till
  253. * after that.)
  254. */
  255. List *processed_tlist;
  256. /*
  257. * For UPDATE, this list contains the target table's attribute numbers to
  258. * which the first N entries of processed_tlist are to be assigned. (Any
  259. * additional entries in processed_tlist must be resjunk.) DO NOT use the
  260. * resnos in processed_tlist to identify the UPDATE target columns.
  261. */
  262. List *update_colnos;
  263. /* Fields filled during create_plan() for use in setrefs.c */
  264. AttrNumber *grouping_map; /* for GroupingFunc fixup */
  265. List *minmax_aggs; /* List of MinMaxAggInfos */
  266. MemoryContext planner_cxt; /* context holding PlannerInfo */
  267. Cardinality total_table_pages; /* # of pages in all non-dummy tables of
  268. * query */
  269. Selectivity tuple_fraction; /* tuple_fraction passed to query_planner */
  270. Cardinality limit_tuples; /* limit_tuples passed to query_planner */
  271. Index qual_security_level; /* minimum security_level for quals */
  272. /* Note: qual_security_level is zero if there are no securityQuals */
  273. bool hasJoinRTEs; /* true if any RTEs are RTE_JOIN kind */
  274. bool hasLateralRTEs; /* true if any RTEs are marked LATERAL */
  275. bool hasHavingQual; /* true if havingQual was non-null */
  276. bool hasPseudoConstantQuals; /* true if any RestrictInfo has
  277. * pseudoconstant = true */
  278. bool hasAlternativeSubPlans; /* true if we've made any of those */
  279. bool hasRecursion; /* true if planning a recursive WITH item */
  280. /*
  281. * Information about aggregates. Filled by preprocess_aggrefs().
  282. */
  283. List *agginfos; /* AggInfo structs */
  284. List *aggtransinfos; /* AggTransInfo structs */
  285. int numOrderedAggs; /* number w/ DISTINCT/ORDER BY/WITHIN GROUP */
  286. bool hasNonPartialAggs; /* does any agg not support partial mode? */
  287. bool hasNonSerialAggs; /* is any partial agg non-serializable? */
  288. /* These fields are used only when hasRecursion is true: */
  289. int wt_param_id; /* PARAM_EXEC ID for the work table */
  290. struct Path *non_recursive_path; /* a path for non-recursive term */
  291. /* These fields are workspace for createplan.c */
  292. Relids curOuterRels; /* outer rels above current node */
  293. List *curOuterParams; /* not-yet-assigned NestLoopParams */
  294. /* These fields are workspace for setrefs.c */
  295. bool *isAltSubplan; /* array corresponding to glob->subplans */
  296. bool *isUsedSubplan; /* array corresponding to glob->subplans */
  297. /* optional private data for join_search_hook, e.g., GEQO */
  298. void *join_search_private;
  299. /* Does this query modify any partition key columns? */
  300. bool partColsUpdated;
  301. };
  302. /*
  303. * In places where it's known that simple_rte_array[] must have been prepared
  304. * already, we just index into it to fetch RTEs. In code that might be
  305. * executed before or after entering query_planner(), use this macro.
  306. */
  307. #define planner_rt_fetch(rti, root) \
  308. ((root)->simple_rte_array ? (root)->simple_rte_array[rti] : \
  309. rt_fetch(rti, (root)->parse->rtable))
  310. /*
  311. * If multiple relations are partitioned the same way, all such partitions
  312. * will have a pointer to the same PartitionScheme. A list of PartitionScheme
  313. * objects is attached to the PlannerInfo. By design, the partition scheme
  314. * incorporates only the general properties of the partition method (LIST vs.
  315. * RANGE, number of partitioning columns and the type information for each)
  316. * and not the specific bounds.
  317. *
  318. * We store the opclass-declared input data types instead of the partition key
  319. * datatypes since the former rather than the latter are used to compare
  320. * partition bounds. Since partition key data types and the opclass declared
  321. * input data types are expected to be binary compatible (per ResolveOpClass),
  322. * both of those should have same byval and length properties.
  323. */
  324. typedef struct PartitionSchemeData
  325. {
  326. char strategy; /* partition strategy */
  327. int16 partnatts; /* number of partition attributes */
  328. Oid *partopfamily; /* OIDs of operator families */
  329. Oid *partopcintype; /* OIDs of opclass declared input data types */
  330. Oid *partcollation; /* OIDs of partitioning collations */
  331. /* Cached information about partition key data types. */
  332. int16 *parttyplen;
  333. bool *parttypbyval;
  334. /* Cached information about partition comparison functions. */
  335. struct FmgrInfo *partsupfunc;
  336. } PartitionSchemeData;
  337. typedef struct PartitionSchemeData *PartitionScheme;
  338. /*----------
  339. * RelOptInfo
  340. * Per-relation information for planning/optimization
  341. *
  342. * For planning purposes, a "base rel" is either a plain relation (a table)
  343. * or the output of a sub-SELECT or function that appears in the range table.
  344. * In either case it is uniquely identified by an RT index. A "joinrel"
  345. * is the joining of two or more base rels. A joinrel is identified by
  346. * the set of RT indexes for its component baserels. We create RelOptInfo
  347. * nodes for each baserel and joinrel, and store them in the PlannerInfo's
  348. * simple_rel_array and join_rel_list respectively.
  349. *
  350. * Note that there is only one joinrel for any given set of component
  351. * baserels, no matter what order we assemble them in; so an unordered
  352. * set is the right datatype to identify it with.
  353. *
  354. * We also have "other rels", which are like base rels in that they refer to
  355. * single RT indexes; but they are not part of the join tree, and are given
  356. * a different RelOptKind to identify them.
  357. * Currently the only kind of otherrels are those made for member relations
  358. * of an "append relation", that is an inheritance set or UNION ALL subquery.
  359. * An append relation has a parent RTE that is a base rel, which represents
  360. * the entire append relation. The member RTEs are otherrels. The parent
  361. * is present in the query join tree but the members are not. The member
  362. * RTEs and otherrels are used to plan the scans of the individual tables or
  363. * subqueries of the append set; then the parent baserel is given Append
  364. * and/or MergeAppend paths comprising the best paths for the individual
  365. * member rels. (See comments for AppendRelInfo for more information.)
  366. *
  367. * At one time we also made otherrels to represent join RTEs, for use in
  368. * handling join alias Vars. Currently this is not needed because all join
  369. * alias Vars are expanded to non-aliased form during preprocess_expression.
  370. *
  371. * We also have relations representing joins between child relations of
  372. * different partitioned tables. These relations are not added to
  373. * join_rel_level lists as they are not joined directly by the dynamic
  374. * programming algorithm.
  375. *
  376. * There is also a RelOptKind for "upper" relations, which are RelOptInfos
  377. * that describe post-scan/join processing steps, such as aggregation.
  378. * Many of the fields in these RelOptInfos are meaningless, but their Path
  379. * fields always hold Paths showing ways to do that processing step.
  380. *
  381. * Lastly, there is a RelOptKind for "dead" relations, which are base rels
  382. * that we have proven we don't need to join after all.
  383. *
  384. * Parts of this data structure are specific to various scan and join
  385. * mechanisms. It didn't seem worth creating new node types for them.
  386. *
  387. * relids - Set of base-relation identifiers; it is a base relation
  388. * if there is just one, a join relation if more than one
  389. * rows - estimated number of tuples in the relation after restriction
  390. * clauses have been applied (ie, output rows of a plan for it)
  391. * consider_startup - true if there is any value in keeping plain paths for
  392. * this rel on the basis of having cheap startup cost
  393. * consider_param_startup - the same for parameterized paths
  394. * reltarget - Default Path output tlist for this rel; normally contains
  395. * Var and PlaceHolderVar nodes for the values we need to
  396. * output from this relation.
  397. * List is in no particular order, but all rels of an
  398. * appendrel set must use corresponding orders.
  399. * NOTE: in an appendrel child relation, may contain
  400. * arbitrary expressions pulled up from a subquery!
  401. * pathlist - List of Path nodes, one for each potentially useful
  402. * method of generating the relation
  403. * ppilist - ParamPathInfo nodes for parameterized Paths, if any
  404. * cheapest_startup_path - the pathlist member with lowest startup cost
  405. * (regardless of ordering) among the unparameterized paths;
  406. * or NULL if there is no unparameterized path
  407. * cheapest_total_path - the pathlist member with lowest total cost
  408. * (regardless of ordering) among the unparameterized paths;
  409. * or if there is no unparameterized path, the path with lowest
  410. * total cost among the paths with minimum parameterization
  411. * cheapest_unique_path - for caching cheapest path to produce unique
  412. * (no duplicates) output from relation; NULL if not yet requested
  413. * cheapest_parameterized_paths - best paths for their parameterizations;
  414. * always includes cheapest_total_path, even if that's unparameterized
  415. * direct_lateral_relids - rels this rel has direct LATERAL references to
  416. * lateral_relids - required outer rels for LATERAL, as a Relids set
  417. * (includes both direct and indirect lateral references)
  418. *
  419. * If the relation is a base relation it will have these fields set:
  420. *
  421. * relid - RTE index (this is redundant with the relids field, but
  422. * is provided for convenience of access)
  423. * rtekind - copy of RTE's rtekind field
  424. * min_attr, max_attr - range of valid AttrNumbers for rel
  425. * attr_needed - array of bitmapsets indicating the highest joinrel
  426. * in which each attribute is needed; if bit 0 is set then
  427. * the attribute is needed as part of final targetlist
  428. * attr_widths - cache space for per-attribute width estimates;
  429. * zero means not computed yet
  430. * lateral_vars - lateral cross-references of rel, if any (list of
  431. * Vars and PlaceHolderVars)
  432. * lateral_referencers - relids of rels that reference this one laterally
  433. * (includes both direct and indirect lateral references)
  434. * indexlist - list of IndexOptInfo nodes for relation's indexes
  435. * (always NIL if it's not a table)
  436. * pages - number of disk pages in relation (zero if not a table)
  437. * tuples - number of tuples in relation (not considering restrictions)
  438. * allvisfrac - fraction of disk pages that are marked all-visible
  439. * eclass_indexes - EquivalenceClasses that mention this rel (filled
  440. * only after EC merging is complete)
  441. * subroot - PlannerInfo for subquery (NULL if it's not a subquery)
  442. * subplan_params - list of PlannerParamItems to be passed to subquery
  443. *
  444. * Note: for a subquery, tuples and subroot are not set immediately
  445. * upon creation of the RelOptInfo object; they are filled in when
  446. * set_subquery_pathlist processes the object.
  447. *
  448. * For otherrels that are appendrel members, these fields are filled
  449. * in just as for a baserel, except we don't bother with lateral_vars.
  450. *
  451. * If the relation is either a foreign table or a join of foreign tables that
  452. * all belong to the same foreign server and are assigned to the same user to
  453. * check access permissions as (cf checkAsUser), these fields will be set:
  454. *
  455. * serverid - OID of foreign server, if foreign table (else InvalidOid)
  456. * userid - OID of user to check access as (InvalidOid means current user)
  457. * useridiscurrent - we've assumed that userid equals current user
  458. * fdwroutine - function hooks for FDW, if foreign table (else NULL)
  459. * fdw_private - private state for FDW, if foreign table (else NULL)
  460. *
  461. * Two fields are used to cache knowledge acquired during the join search
  462. * about whether this rel is provably unique when being joined to given other
  463. * relation(s), ie, it can have at most one row matching any given row from
  464. * that join relation. Currently we only attempt such proofs, and thus only
  465. * populate these fields, for base rels; but someday they might be used for
  466. * join rels too:
  467. *
  468. * unique_for_rels - list of Relid sets, each one being a set of other
  469. * rels for which this one has been proven unique
  470. * non_unique_for_rels - list of Relid sets, each one being a set of
  471. * other rels for which we have tried and failed to prove
  472. * this one unique
  473. *
  474. * The presence of the following fields depends on the restrictions
  475. * and joins that the relation participates in:
  476. *
  477. * baserestrictinfo - List of RestrictInfo nodes, containing info about
  478. * each non-join qualification clause in which this relation
  479. * participates (only used for base rels)
  480. * baserestrictcost - Estimated cost of evaluating the baserestrictinfo
  481. * clauses at a single tuple (only used for base rels)
  482. * baserestrict_min_security - Smallest security_level found among
  483. * clauses in baserestrictinfo
  484. * joininfo - List of RestrictInfo nodes, containing info about each
  485. * join clause in which this relation participates (but
  486. * note this excludes clauses that might be derivable from
  487. * EquivalenceClasses)
  488. * has_eclass_joins - flag that EquivalenceClass joins are possible
  489. *
  490. * Note: Keeping a restrictinfo list in the RelOptInfo is useful only for
  491. * base rels, because for a join rel the set of clauses that are treated as
  492. * restrict clauses varies depending on which sub-relations we choose to join.
  493. * (For example, in a 3-base-rel join, a clause relating rels 1 and 2 must be
  494. * treated as a restrictclause if we join {1} and {2 3} to make {1 2 3}; but
  495. * if we join {1 2} and {3} then that clause will be a restrictclause in {1 2}
  496. * and should not be processed again at the level of {1 2 3}.) Therefore,
  497. * the restrictinfo list in the join case appears in individual JoinPaths
  498. * (field joinrestrictinfo), not in the parent relation. But it's OK for
  499. * the RelOptInfo to store the joininfo list, because that is the same
  500. * for a given rel no matter how we form it.
  501. *
  502. * We store baserestrictcost in the RelOptInfo (for base relations) because
  503. * we know we will need it at least once (to price the sequential scan)
  504. * and may need it multiple times to price index scans.
  505. *
  506. * A join relation is considered to be partitioned if it is formed from a
  507. * join of two relations that are partitioned, have matching partitioning
  508. * schemes, and are joined on an equijoin of the partitioning columns.
  509. * Under those conditions we can consider the join relation to be partitioned
  510. * by either relation's partitioning keys, though some care is needed if
  511. * either relation can be forced to null by outer-joining. For example, an
  512. * outer join like (A LEFT JOIN B ON A.a = B.b) may produce rows with B.b
  513. * NULL. These rows may not fit the partitioning conditions imposed on B.
  514. * Hence, strictly speaking, the join is not partitioned by B.b and thus
  515. * partition keys of an outer join should include partition key expressions
  516. * from the non-nullable side only. However, if a subsequent join uses
  517. * strict comparison operators (and all commonly-used equijoin operators are
  518. * strict), the presence of nulls doesn't cause a problem: such rows couldn't
  519. * match anything on the other side and thus they don't create a need to do
  520. * any cross-partition sub-joins. Hence we can treat such values as still
  521. * partitioning the join output for the purpose of additional partitionwise
  522. * joining, so long as a strict join operator is used by the next join.
  523. *
  524. * If the relation is partitioned, these fields will be set:
  525. *
  526. * part_scheme - Partitioning scheme of the relation
  527. * nparts - Number of partitions
  528. * boundinfo - Partition bounds
  529. * partbounds_merged - true if partition bounds are merged ones
  530. * partition_qual - Partition constraint if not the root
  531. * part_rels - RelOptInfos for each partition
  532. * all_partrels - Relids set of all partition relids
  533. * partexprs, nullable_partexprs - Partition key expressions
  534. *
  535. * The partexprs and nullable_partexprs arrays each contain
  536. * part_scheme->partnatts elements. Each of the elements is a list of
  537. * partition key expressions. For partitioned base relations, there is one
  538. * expression in each partexprs element, and nullable_partexprs is empty.
  539. * For partitioned join relations, each base relation within the join
  540. * contributes one partition key expression per partitioning column;
  541. * that expression goes in the partexprs[i] list if the base relation
  542. * is not nullable by this join or any lower outer join, or in the
  543. * nullable_partexprs[i] list if the base relation is nullable.
  544. * Furthermore, FULL JOINs add extra nullable_partexprs expressions
  545. * corresponding to COALESCE expressions of the left and right join columns,
  546. * to simplify matching join clauses to those lists.
  547. *----------
  548. */
  549. /* Bitmask of flags supported by table AMs */
  550. #define AMFLAG_HAS_TID_RANGE (1 << 0)
  551. typedef enum RelOptKind
  552. {
  553. RELOPT_BASEREL,
  554. RELOPT_JOINREL,
  555. RELOPT_OTHER_MEMBER_REL,
  556. RELOPT_OTHER_JOINREL,
  557. RELOPT_UPPER_REL,
  558. RELOPT_OTHER_UPPER_REL,
  559. RELOPT_DEADREL
  560. } RelOptKind;
  561. /*
  562. * Is the given relation a simple relation i.e a base or "other" member
  563. * relation?
  564. */
  565. #define IS_SIMPLE_REL(rel) \
  566. ((rel)->reloptkind == RELOPT_BASEREL || \
  567. (rel)->reloptkind == RELOPT_OTHER_MEMBER_REL)
  568. /* Is the given relation a join relation? */
  569. #define IS_JOIN_REL(rel) \
  570. ((rel)->reloptkind == RELOPT_JOINREL || \
  571. (rel)->reloptkind == RELOPT_OTHER_JOINREL)
  572. /* Is the given relation an upper relation? */
  573. #define IS_UPPER_REL(rel) \
  574. ((rel)->reloptkind == RELOPT_UPPER_REL || \
  575. (rel)->reloptkind == RELOPT_OTHER_UPPER_REL)
  576. /* Is the given relation an "other" relation? */
  577. #define IS_OTHER_REL(rel) \
  578. ((rel)->reloptkind == RELOPT_OTHER_MEMBER_REL || \
  579. (rel)->reloptkind == RELOPT_OTHER_JOINREL || \
  580. (rel)->reloptkind == RELOPT_OTHER_UPPER_REL)
  581. typedef struct RelOptInfo
  582. {
  583. NodeTag type;
  584. RelOptKind reloptkind;
  585. /* all relations included in this RelOptInfo */
  586. Relids relids; /* set of base relids (rangetable indexes) */
  587. /* size estimates generated by planner */
  588. Cardinality rows; /* estimated number of result tuples */
  589. /* per-relation planner control flags */
  590. bool consider_startup; /* keep cheap-startup-cost paths? */
  591. bool consider_param_startup; /* ditto, for parameterized paths? */
  592. bool consider_parallel; /* consider parallel paths? */
  593. /* default result targetlist for Paths scanning this relation */
  594. struct PathTarget *reltarget; /* list of Vars/Exprs, cost, width */
  595. /* materialization information */
  596. List *pathlist; /* Path structures */
  597. List *ppilist; /* ParamPathInfos used in pathlist */
  598. List *partial_pathlist; /* partial Paths */
  599. struct Path *cheapest_startup_path;
  600. struct Path *cheapest_total_path;
  601. struct Path *cheapest_unique_path;
  602. List *cheapest_parameterized_paths;
  603. /* parameterization information needed for both base rels and join rels */
  604. /* (see also lateral_vars and lateral_referencers) */
  605. Relids direct_lateral_relids; /* rels directly laterally referenced */
  606. Relids lateral_relids; /* minimum parameterization of rel */
  607. /* information about a base rel (not set for join rels!) */
  608. Index relid;
  609. Oid reltablespace; /* containing tablespace */
  610. RTEKind rtekind; /* RELATION, SUBQUERY, FUNCTION, etc */
  611. AttrNumber min_attr; /* smallest attrno of rel (often <0) */
  612. AttrNumber max_attr; /* largest attrno of rel */
  613. Relids *attr_needed; /* array indexed [min_attr .. max_attr] */
  614. int32 *attr_widths; /* array indexed [min_attr .. max_attr] */
  615. List *lateral_vars; /* LATERAL Vars and PHVs referenced by rel */
  616. Relids lateral_referencers; /* rels that reference me laterally */
  617. List *indexlist; /* list of IndexOptInfo */
  618. List *statlist; /* list of StatisticExtInfo */
  619. BlockNumber pages; /* size estimates derived from pg_class */
  620. Cardinality tuples;
  621. double allvisfrac;
  622. Bitmapset *eclass_indexes; /* Indexes in PlannerInfo's eq_classes list of
  623. * ECs that mention this rel */
  624. PlannerInfo *subroot; /* if subquery */
  625. List *subplan_params; /* if subquery */
  626. int rel_parallel_workers; /* wanted number of parallel workers */
  627. uint32 amflags; /* Bitmask of optional features supported by
  628. * the table AM */
  629. /* Information about foreign tables and foreign joins */
  630. Oid serverid; /* identifies server for the table or join */
  631. Oid userid; /* identifies user to check access as */
  632. bool useridiscurrent; /* join is only valid for current user */
  633. /* use "struct FdwRoutine" to avoid including fdwapi.h here */
  634. struct FdwRoutine *fdwroutine;
  635. void *fdw_private;
  636. /* cache space for remembering if we have proven this relation unique */
  637. List *unique_for_rels; /* known unique for these other relid
  638. * set(s) */
  639. List *non_unique_for_rels; /* known not unique for these set(s) */
  640. /* used by various scans and joins: */
  641. List *baserestrictinfo; /* RestrictInfo structures (if base rel) */
  642. QualCost baserestrictcost; /* cost of evaluating the above */
  643. Index baserestrict_min_security; /* min security_level found in
  644. * baserestrictinfo */
  645. List *joininfo; /* RestrictInfo structures for join clauses
  646. * involving this rel */
  647. bool has_eclass_joins; /* T means joininfo is incomplete */
  648. /* used by partitionwise joins: */
  649. bool consider_partitionwise_join; /* consider partitionwise join
  650. * paths? (if partitioned rel) */
  651. Relids top_parent_relids; /* Relids of topmost parents (if "other"
  652. * rel) */
  653. /* used for partitioned relations: */
  654. PartitionScheme part_scheme; /* Partitioning scheme */
  655. int nparts; /* Number of partitions; -1 if not yet set; in
  656. * case of a join relation 0 means it's
  657. * considered unpartitioned */
  658. struct PartitionBoundInfoData *boundinfo; /* Partition bounds */
  659. bool partbounds_merged; /* True if partition bounds were created
  660. * by partition_bounds_merge() */
  661. List *partition_qual; /* Partition constraint, if not the root */
  662. struct RelOptInfo **part_rels; /* Array of RelOptInfos of partitions,
  663. * stored in the same order as bounds */
  664. Bitmapset *live_parts; /* Bitmap with members acting as indexes into
  665. * the part_rels[] array to indicate which
  666. * partitions survived partition pruning. */
  667. Relids all_partrels; /* Relids set of all partition relids */
  668. List **partexprs; /* Non-nullable partition key expressions */
  669. List **nullable_partexprs; /* Nullable partition key expressions */
  670. } RelOptInfo;
  671. /*
  672. * Is given relation partitioned?
  673. *
  674. * It's not enough to test whether rel->part_scheme is set, because it might
  675. * be that the basic partitioning properties of the input relations matched
  676. * but the partition bounds did not. Also, if we are able to prove a rel
  677. * dummy (empty), we should henceforth treat it as unpartitioned.
  678. */
  679. #define IS_PARTITIONED_REL(rel) \
  680. ((rel)->part_scheme && (rel)->boundinfo && (rel)->nparts > 0 && \
  681. (rel)->part_rels && !IS_DUMMY_REL(rel))
  682. /*
  683. * Convenience macro to make sure that a partitioned relation has all the
  684. * required members set.
  685. */
  686. #define REL_HAS_ALL_PART_PROPS(rel) \
  687. ((rel)->part_scheme && (rel)->boundinfo && (rel)->nparts > 0 && \
  688. (rel)->part_rels && (rel)->partexprs && (rel)->nullable_partexprs)
  689. /*
  690. * IndexOptInfo
  691. * Per-index information for planning/optimization
  692. *
  693. * indexkeys[], indexcollations[] each have ncolumns entries.
  694. * opfamily[], and opcintype[] each have nkeycolumns entries. They do
  695. * not contain any information about included attributes.
  696. *
  697. * sortopfamily[], reverse_sort[], and nulls_first[] have
  698. * nkeycolumns entries, if the index is ordered; but if it is unordered,
  699. * those pointers are NULL.
  700. *
  701. * Zeroes in the indexkeys[] array indicate index columns that are
  702. * expressions; there is one element in indexprs for each such column.
  703. *
  704. * For an ordered index, reverse_sort[] and nulls_first[] describe the
  705. * sort ordering of a forward indexscan; we can also consider a backward
  706. * indexscan, which will generate the reverse ordering.
  707. *
  708. * The indexprs and indpred expressions have been run through
  709. * prepqual.c and eval_const_expressions() for ease of matching to
  710. * WHERE clauses. indpred is in implicit-AND form.
  711. *
  712. * indextlist is a TargetEntry list representing the index columns.
  713. * It provides an equivalent base-relation Var for each simple column,
  714. * and links to the matching indexprs element for each expression column.
  715. *
  716. * While most of these fields are filled when the IndexOptInfo is created
  717. * (by plancat.c), indrestrictinfo and predOK are set later, in
  718. * check_index_predicates().
  719. */
  720. #ifndef HAVE_INDEXOPTINFO_TYPEDEF
  721. typedef struct IndexOptInfo IndexOptInfo;
  722. #define HAVE_INDEXOPTINFO_TYPEDEF 1
  723. #endif
  724. struct IndexOptInfo
  725. {
  726. NodeTag type;
  727. Oid indexoid; /* OID of the index relation */
  728. Oid reltablespace; /* tablespace of index (not table) */
  729. RelOptInfo *rel; /* back-link to index's table */
  730. /* index-size statistics (from pg_class and elsewhere) */
  731. BlockNumber pages; /* number of disk pages in index */
  732. Cardinality tuples; /* number of index tuples in index */
  733. int tree_height; /* index tree height, or -1 if unknown */
  734. /* index descriptor information */
  735. int ncolumns; /* number of columns in index */
  736. int nkeycolumns; /* number of key columns in index */
  737. int *indexkeys; /* column numbers of index's attributes both
  738. * key and included columns, or 0 */
  739. Oid *indexcollations; /* OIDs of collations of index columns */
  740. Oid *opfamily; /* OIDs of operator families for columns */
  741. Oid *opcintype; /* OIDs of opclass declared input data types */
  742. Oid *sortopfamily; /* OIDs of btree opfamilies, if orderable */
  743. bool *reverse_sort; /* is sort order descending? */
  744. bool *nulls_first; /* do NULLs come first in the sort order? */
  745. bytea **opclassoptions; /* opclass-specific options for columns */
  746. bool *canreturn; /* which index cols can be returned in an
  747. * index-only scan? */
  748. Oid relam; /* OID of the access method (in pg_am) */
  749. List *indexprs; /* expressions for non-simple index columns */
  750. List *indpred; /* predicate if a partial index, else NIL */
  751. List *indextlist; /* targetlist representing index columns */
  752. List *indrestrictinfo; /* parent relation's baserestrictinfo
  753. * list, less any conditions implied by
  754. * the index's predicate (unless it's a
  755. * target rel, see comments in
  756. * check_index_predicates()) */
  757. bool predOK; /* true if index predicate matches query */
  758. bool unique; /* true if a unique index */
  759. bool immediate; /* is uniqueness enforced immediately? */
  760. bool hypothetical; /* true if index doesn't really exist */
  761. /* Remaining fields are copied from the index AM's API struct: */
  762. bool amcanorderbyop; /* does AM support order by operator result? */
  763. bool amoptionalkey; /* can query omit key for the first column? */
  764. bool amsearcharray; /* can AM handle ScalarArrayOpExpr quals? */
  765. bool amsearchnulls; /* can AM search for NULL/NOT NULL entries? */
  766. bool amhasgettuple; /* does AM have amgettuple interface? */
  767. bool amhasgetbitmap; /* does AM have amgetbitmap interface? */
  768. bool amcanparallel; /* does AM support parallel scan? */
  769. bool amcanmarkpos; /* does AM support mark/restore? */
  770. /* Rather than include amapi.h here, we declare amcostestimate like this */
  771. void (*amcostestimate) (); /* AM's cost estimator */
  772. };
  773. /*
  774. * ForeignKeyOptInfo
  775. * Per-foreign-key information for planning/optimization
  776. *
  777. * The per-FK-column arrays can be fixed-size because we allow at most
  778. * INDEX_MAX_KEYS columns in a foreign key constraint. Each array has
  779. * nkeys valid entries.
  780. */
  781. typedef struct ForeignKeyOptInfo
  782. {
  783. NodeTag type;
  784. /* Basic data about the foreign key (fetched from catalogs): */
  785. Index con_relid; /* RT index of the referencing table */
  786. Index ref_relid; /* RT index of the referenced table */
  787. int nkeys; /* number of columns in the foreign key */
  788. AttrNumber conkey[INDEX_MAX_KEYS]; /* cols in referencing table */
  789. AttrNumber confkey[INDEX_MAX_KEYS]; /* cols in referenced table */
  790. Oid conpfeqop[INDEX_MAX_KEYS]; /* PK = FK operator OIDs */
  791. /* Derived info about whether FK's equality conditions match the query: */
  792. int nmatched_ec; /* # of FK cols matched by ECs */
  793. int nconst_ec; /* # of these ECs that are ec_has_const */
  794. int nmatched_rcols; /* # of FK cols matched by non-EC rinfos */
  795. int nmatched_ri; /* total # of non-EC rinfos matched to FK */
  796. /* Pointer to eclass matching each column's condition, if there is one */
  797. struct EquivalenceClass *eclass[INDEX_MAX_KEYS];
  798. /* Pointer to eclass member for the referencing Var, if there is one */
  799. struct EquivalenceMember *fk_eclass_member[INDEX_MAX_KEYS];
  800. /* List of non-EC RestrictInfos matching each column's condition */
  801. List *rinfos[INDEX_MAX_KEYS];
  802. } ForeignKeyOptInfo;
  803. /*
  804. * StatisticExtInfo
  805. * Information about extended statistics for planning/optimization
  806. *
  807. * Each pg_statistic_ext row is represented by one or more nodes of this
  808. * type, or even zero if ANALYZE has not computed them.
  809. */
  810. typedef struct StatisticExtInfo
  811. {
  812. NodeTag type;
  813. Oid statOid; /* OID of the statistics row */
  814. bool inherit; /* includes child relations */
  815. RelOptInfo *rel; /* back-link to statistic's table */
  816. char kind; /* statistics kind of this entry */
  817. Bitmapset *keys; /* attnums of the columns covered */
  818. List *exprs; /* expressions */
  819. } StatisticExtInfo;
  820. /*
  821. * EquivalenceClasses
  822. *
  823. * Whenever we can determine that a mergejoinable equality clause A = B is
  824. * not delayed by any outer join, we create an EquivalenceClass containing
  825. * the expressions A and B to record this knowledge. If we later find another
  826. * equivalence B = C, we add C to the existing EquivalenceClass; this may
  827. * require merging two existing EquivalenceClasses. At the end of the qual
  828. * distribution process, we have sets of values that are known all transitively
  829. * equal to each other, where "equal" is according to the rules of the btree
  830. * operator family(s) shown in ec_opfamilies, as well as the collation shown
  831. * by ec_collation. (We restrict an EC to contain only equalities whose
  832. * operators belong to the same set of opfamilies. This could probably be
  833. * relaxed, but for now it's not worth the trouble, since nearly all equality
  834. * operators belong to only one btree opclass anyway. Similarly, we suppose
  835. * that all or none of the input datatypes are collatable, so that a single
  836. * collation value is sufficient.)
  837. *
  838. * We also use EquivalenceClasses as the base structure for PathKeys, letting
  839. * us represent knowledge about different sort orderings being equivalent.
  840. * Since every PathKey must reference an EquivalenceClass, we will end up
  841. * with single-member EquivalenceClasses whenever a sort key expression has
  842. * not been equivalenced to anything else. It is also possible that such an
  843. * EquivalenceClass will contain a volatile expression ("ORDER BY random()"),
  844. * which is a case that can't arise otherwise since clauses containing
  845. * volatile functions are never considered mergejoinable. We mark such
  846. * EquivalenceClasses specially to prevent them from being merged with
  847. * ordinary EquivalenceClasses. Also, for volatile expressions we have
  848. * to be careful to match the EquivalenceClass to the correct targetlist
  849. * entry: consider SELECT random() AS a, random() AS b ... ORDER BY b,a.
  850. * So we record the SortGroupRef of the originating sort clause.
  851. *
  852. * We allow equality clauses appearing below the nullable side of an outer join
  853. * to form EquivalenceClasses, but these have a slightly different meaning:
  854. * the included values might be all NULL rather than all the same non-null
  855. * values. See src/backend/optimizer/README for more on that point.
  856. *
  857. * NB: if ec_merged isn't NULL, this class has been merged into another, and
  858. * should be ignored in favor of using the pointed-to class.
  859. */
  860. typedef struct EquivalenceClass
  861. {
  862. NodeTag type;
  863. List *ec_opfamilies; /* btree operator family OIDs */
  864. Oid ec_collation; /* collation, if datatypes are collatable */
  865. List *ec_members; /* list of EquivalenceMembers */
  866. List *ec_sources; /* list of generating RestrictInfos */
  867. List *ec_derives; /* list of derived RestrictInfos */
  868. Relids ec_relids; /* all relids appearing in ec_members, except
  869. * for child members (see below) */
  870. bool ec_has_const; /* any pseudoconstants in ec_members? */
  871. bool ec_has_volatile; /* the (sole) member is a volatile expr */
  872. bool ec_below_outer_join; /* equivalence applies below an OJ */
  873. bool ec_broken; /* failed to generate needed clauses? */
  874. Index ec_sortref; /* originating sortclause label, or 0 */
  875. Index ec_min_security; /* minimum security_level in ec_sources */
  876. Index ec_max_security; /* maximum security_level in ec_sources */
  877. struct EquivalenceClass *ec_merged; /* set if merged into another EC */
  878. } EquivalenceClass;
  879. /*
  880. * If an EC contains a const and isn't below-outer-join, any PathKey depending
  881. * on it must be redundant, since there's only one possible value of the key.
  882. */
  883. #define EC_MUST_BE_REDUNDANT(eclass) \
  884. ((eclass)->ec_has_const && !(eclass)->ec_below_outer_join)
  885. /*
  886. * EquivalenceMember - one member expression of an EquivalenceClass
  887. *
  888. * em_is_child signifies that this element was built by transposing a member
  889. * for an appendrel parent relation to represent the corresponding expression
  890. * for an appendrel child. These members are used for determining the
  891. * pathkeys of scans on the child relation and for explicitly sorting the
  892. * child when necessary to build a MergeAppend path for the whole appendrel
  893. * tree. An em_is_child member has no impact on the properties of the EC as a
  894. * whole; in particular the EC's ec_relids field does NOT include the child
  895. * relation. An em_is_child member should never be marked em_is_const nor
  896. * cause ec_has_const or ec_has_volatile to be set, either. Thus, em_is_child
  897. * members are not really full-fledged members of the EC, but just reflections
  898. * or doppelgangers of real members. Most operations on EquivalenceClasses
  899. * should ignore em_is_child members, and those that don't should test
  900. * em_relids to make sure they only consider relevant members.
  901. *
  902. * em_datatype is usually the same as exprType(em_expr), but can be
  903. * different when dealing with a binary-compatible opfamily; in particular
  904. * anyarray_ops would never work without this. Use em_datatype when
  905. * looking up a specific btree operator to work with this expression.
  906. */
  907. typedef struct EquivalenceMember
  908. {
  909. NodeTag type;
  910. Expr *em_expr; /* the expression represented */
  911. Relids em_relids; /* all relids appearing in em_expr */
  912. Relids em_nullable_relids; /* nullable by lower outer joins */
  913. bool em_is_const; /* expression is pseudoconstant? */
  914. bool em_is_child; /* derived version for a child relation? */
  915. Oid em_datatype; /* the "nominal type" used by the opfamily */
  916. } EquivalenceMember;
  917. /*
  918. * PathKeys
  919. *
  920. * The sort ordering of a path is represented by a list of PathKey nodes.
  921. * An empty list implies no known ordering. Otherwise the first item
  922. * represents the primary sort key, the second the first secondary sort key,
  923. * etc. The value being sorted is represented by linking to an
  924. * EquivalenceClass containing that value and including pk_opfamily among its
  925. * ec_opfamilies. The EquivalenceClass tells which collation to use, too.
  926. * This is a convenient method because it makes it trivial to detect
  927. * equivalent and closely-related orderings. (See optimizer/README for more
  928. * information.)
  929. *
  930. * Note: pk_strategy is either BTLessStrategyNumber (for ASC) or
  931. * BTGreaterStrategyNumber (for DESC). We assume that all ordering-capable
  932. * index types will use btree-compatible strategy numbers.
  933. */
  934. typedef struct PathKey
  935. {
  936. NodeTag type;
  937. EquivalenceClass *pk_eclass; /* the value that is ordered */
  938. Oid pk_opfamily; /* btree opfamily defining the ordering */
  939. int pk_strategy; /* sort direction (ASC or DESC) */
  940. bool pk_nulls_first; /* do NULLs come before normal values? */
  941. } PathKey;
  942. /*
  943. * VolatileFunctionStatus -- allows nodes to cache their
  944. * contain_volatile_functions properties. VOLATILITY_UNKNOWN means not yet
  945. * determined.
  946. */
  947. typedef enum VolatileFunctionStatus
  948. {
  949. VOLATILITY_UNKNOWN = 0,
  950. VOLATILITY_VOLATILE,
  951. VOLATILITY_NOVOLATILE
  952. } VolatileFunctionStatus;
  953. /*
  954. * PathTarget
  955. *
  956. * This struct contains what we need to know during planning about the
  957. * targetlist (output columns) that a Path will compute. Each RelOptInfo
  958. * includes a default PathTarget, which its individual Paths may simply
  959. * reference. However, in some cases a Path may compute outputs different
  960. * from other Paths, and in that case we make a custom PathTarget for it.
  961. * For example, an indexscan might return index expressions that would
  962. * otherwise need to be explicitly calculated. (Note also that "upper"
  963. * relations generally don't have useful default PathTargets.)
  964. *
  965. * exprs contains bare expressions; they do not have TargetEntry nodes on top,
  966. * though those will appear in finished Plans.
  967. *
  968. * sortgrouprefs[] is an array of the same length as exprs, containing the
  969. * corresponding sort/group refnos, or zeroes for expressions not referenced
  970. * by sort/group clauses. If sortgrouprefs is NULL (which it generally is in
  971. * RelOptInfo.reltarget targets; only upper-level Paths contain this info),
  972. * we have not identified sort/group columns in this tlist. This allows us to
  973. * deal with sort/group refnos when needed with less expense than including
  974. * TargetEntry nodes in the exprs list.
  975. */
  976. typedef struct PathTarget
  977. {
  978. NodeTag type;
  979. List *exprs; /* list of expressions to be computed */
  980. Index *sortgrouprefs; /* corresponding sort/group refnos, or 0 */
  981. QualCost cost; /* cost of evaluating the expressions */
  982. int width; /* estimated avg width of result tuples */
  983. VolatileFunctionStatus has_volatile_expr; /* indicates if exprs contain
  984. * any volatile functions. */
  985. } PathTarget;
  986. /* Convenience macro to get a sort/group refno from a PathTarget */
  987. #define get_pathtarget_sortgroupref(target, colno) \
  988. ((target)->sortgrouprefs ? (target)->sortgrouprefs[colno] : (Index) 0)
  989. /*
  990. * ParamPathInfo
  991. *
  992. * All parameterized paths for a given relation with given required outer rels
  993. * link to a single ParamPathInfo, which stores common information such as
  994. * the estimated rowcount for this parameterization. We do this partly to
  995. * avoid recalculations, but mostly to ensure that the estimated rowcount
  996. * is in fact the same for every such path.
  997. *
  998. * Note: ppi_clauses is only used in ParamPathInfos for base relation paths;
  999. * in join cases it's NIL because the set of relevant clauses varies depending
  1000. * on how the join is formed. The relevant clauses will appear in each
  1001. * parameterized join path's joinrestrictinfo list, instead.
  1002. */
  1003. typedef struct ParamPathInfo
  1004. {
  1005. NodeTag type;
  1006. Relids ppi_req_outer; /* rels supplying parameters used by path */
  1007. Cardinality ppi_rows; /* estimated number of result tuples */
  1008. List *ppi_clauses; /* join clauses available from outer rels */
  1009. } ParamPathInfo;
  1010. /*
  1011. * Type "Path" is used as-is for sequential-scan paths, as well as some other
  1012. * simple plan types that we don't need any extra information in the path for.
  1013. * For other path types it is the first component of a larger struct.
  1014. *
  1015. * "pathtype" is the NodeTag of the Plan node we could build from this Path.
  1016. * It is partially redundant with the Path's NodeTag, but allows us to use
  1017. * the same Path type for multiple Plan types when there is no need to
  1018. * distinguish the Plan type during path processing.
  1019. *
  1020. * "parent" identifies the relation this Path scans, and "pathtarget"
  1021. * describes the precise set of output columns the Path would compute.
  1022. * In simple cases all Paths for a given rel share the same targetlist,
  1023. * which we represent by having path->pathtarget equal to parent->reltarget.
  1024. *
  1025. * "param_info", if not NULL, links to a ParamPathInfo that identifies outer
  1026. * relation(s) that provide parameter values to each scan of this path.
  1027. * That means this path can only be joined to those rels by means of nestloop
  1028. * joins with this path on the inside. Also note that a parameterized path
  1029. * is responsible for testing all "movable" joinclauses involving this rel
  1030. * and the specified outer rel(s).
  1031. *
  1032. * "rows" is the same as parent->rows in simple paths, but in parameterized
  1033. * paths and UniquePaths it can be less than parent->rows, reflecting the
  1034. * fact that we've filtered by extra join conditions or removed duplicates.
  1035. *
  1036. * "pathkeys" is a List of PathKey nodes (see above), describing the sort
  1037. * ordering of the path's output rows.
  1038. */
  1039. typedef struct Path
  1040. {
  1041. NodeTag type;
  1042. NodeTag pathtype; /* tag identifying scan/join method */
  1043. RelOptInfo *parent; /* the relation this path can build */
  1044. PathTarget *pathtarget; /* list of Vars/Exprs, cost, width */
  1045. ParamPathInfo *param_info; /* parameterization info, or NULL if none */
  1046. bool parallel_aware; /* engage parallel-aware logic? */
  1047. bool parallel_safe; /* OK to use as part of parallel plan? */
  1048. int parallel_workers; /* desired # of workers; 0 = not parallel */
  1049. /* estimated size/costs for path (see costsize.c for more info) */
  1050. Cardinality rows; /* estimated number of result tuples */
  1051. Cost startup_cost; /* cost expended before fetching any tuples */
  1052. Cost total_cost; /* total cost (assuming all tuples fetched) */
  1053. List *pathkeys; /* sort ordering of path's output */
  1054. /* pathkeys is a List of PathKey nodes; see above */
  1055. } Path;
  1056. /* Macro for extracting a path's parameterization relids; beware double eval */
  1057. #define PATH_REQ_OUTER(path) \
  1058. ((path)->param_info ? (path)->param_info->ppi_req_outer : (Relids) NULL)
  1059. /*----------
  1060. * IndexPath represents an index scan over a single index.
  1061. *
  1062. * This struct is used for both regular indexscans and index-only scans;
  1063. * path.pathtype is T_IndexScan or T_IndexOnlyScan to show which is meant.
  1064. *
  1065. * 'indexinfo' is the index to be scanned.
  1066. *
  1067. * 'indexclauses' is a list of IndexClause nodes, each representing one
  1068. * index-checkable restriction, with implicit AND semantics across the list.
  1069. * An empty list implies a full index scan.
  1070. *
  1071. * 'indexorderbys', if not NIL, is a list of ORDER BY expressions that have
  1072. * been found to be usable as ordering operators for an amcanorderbyop index.
  1073. * The list must match the path's pathkeys, ie, one expression per pathkey
  1074. * in the same order. These are not RestrictInfos, just bare expressions,
  1075. * since they generally won't yield booleans. It's guaranteed that each
  1076. * expression has the index key on the left side of the operator.
  1077. *
  1078. * 'indexorderbycols' is an integer list of index column numbers (zero-based)
  1079. * of the same length as 'indexorderbys', showing which index column each
  1080. * ORDER BY expression is meant to be used with. (There is no restriction
  1081. * on which index column each ORDER BY can be used with.)
  1082. *
  1083. * 'indexscandir' is one of:
  1084. * ForwardScanDirection: forward scan of an ordered index
  1085. * BackwardScanDirection: backward scan of an ordered index
  1086. * NoMovementScanDirection: scan of an unordered index, or don't care
  1087. * (The executor doesn't care whether it gets ForwardScanDirection or
  1088. * NoMovementScanDirection for an indexscan, but the planner wants to
  1089. * distinguish ordered from unordered indexes for building pathkeys.)
  1090. *
  1091. * 'indextotalcost' and 'indexselectivity' are saved in the IndexPath so that
  1092. * we need not recompute them when considering using the same index in a
  1093. * bitmap index/heap scan (see BitmapHeapPath). The costs of the IndexPath
  1094. * itself represent the costs of an IndexScan or IndexOnlyScan plan type.
  1095. *----------
  1096. */
  1097. typedef struct IndexPath
  1098. {
  1099. Path path;
  1100. IndexOptInfo *indexinfo;
  1101. List *indexclauses;
  1102. List *indexorderbys;
  1103. List *indexorderbycols;
  1104. ScanDirection indexscandir;
  1105. Cost indextotalcost;
  1106. Selectivity indexselectivity;
  1107. } IndexPath;
  1108. /*
  1109. * Each IndexClause references a RestrictInfo node from the query's WHERE
  1110. * or JOIN conditions, and shows how that restriction can be applied to
  1111. * the particular index. We support both indexclauses that are directly
  1112. * usable by the index machinery, which are typically of the form
  1113. * "indexcol OP pseudoconstant", and those from which an indexable qual
  1114. * can be derived. The simplest such transformation is that a clause
  1115. * of the form "pseudoconstant OP indexcol" can be commuted to produce an
  1116. * indexable qual (the index machinery expects the indexcol to be on the
  1117. * left always). Another example is that we might be able to extract an
  1118. * indexable range condition from a LIKE condition, as in "x LIKE 'foo%bar'"
  1119. * giving rise to "x >= 'foo' AND x < 'fop'". Derivation of such lossy
  1120. * conditions is done by a planner support function attached to the
  1121. * indexclause's top-level function or operator.
  1122. *
  1123. * indexquals is a list of RestrictInfos for the directly-usable index
  1124. * conditions associated with this IndexClause. In the simplest case
  1125. * it's a one-element list whose member is iclause->rinfo. Otherwise,
  1126. * it contains one or more directly-usable indexqual conditions extracted
  1127. * from the given clause. The 'lossy' flag indicates whether the
  1128. * indexquals are semantically equivalent to the original clause, or
  1129. * represent a weaker condition.
  1130. *
  1131. * Normally, indexcol is the index of the single index column the clause
  1132. * works on, and indexcols is NIL. But if the clause is a RowCompareExpr,
  1133. * indexcol is the index of the leading column, and indexcols is a list of
  1134. * all the affected columns. (Note that indexcols matches up with the
  1135. * columns of the actual indexable RowCompareExpr in indexquals, which
  1136. * might be different from the original in rinfo.)
  1137. *
  1138. * An IndexPath's IndexClause list is required to be ordered by index
  1139. * column, i.e. the indexcol values must form a nondecreasing sequence.
  1140. * (The order of multiple clauses for the same index column is unspecified.)
  1141. */
  1142. typedef struct IndexClause
  1143. {
  1144. NodeTag type;
  1145. struct RestrictInfo *rinfo; /* original restriction or join clause */
  1146. List *indexquals; /* indexqual(s) derived from it */
  1147. bool lossy; /* are indexquals a lossy version of clause? */
  1148. AttrNumber indexcol; /* index column the clause uses (zero-based) */
  1149. List *indexcols; /* multiple index columns, if RowCompare */
  1150. } IndexClause;
  1151. /*
  1152. * BitmapHeapPath represents one or more indexscans that generate TID bitmaps
  1153. * instead of directly accessing the heap, followed by AND/OR combinations
  1154. * to produce a single bitmap, followed by a heap scan that uses the bitmap.
  1155. * Note that the output is always considered unordered, since it will come
  1156. * out in physical heap order no matter what the underlying indexes did.
  1157. *
  1158. * The individual indexscans are represented by IndexPath nodes, and any
  1159. * logic on top of them is represented by a tree of BitmapAndPath and
  1160. * BitmapOrPath nodes. Notice that we can use the same IndexPath node both
  1161. * to represent a regular (or index-only) index scan plan, and as the child
  1162. * of a BitmapHeapPath that represents scanning the same index using a
  1163. * BitmapIndexScan. The startup_cost and total_cost figures of an IndexPath
  1164. * always represent the costs to use it as a regular (or index-only)
  1165. * IndexScan. The costs of a BitmapIndexScan can be computed using the
  1166. * IndexPath's indextotalcost and indexselectivity.
  1167. */
  1168. typedef struct BitmapHeapPath
  1169. {
  1170. Path path;
  1171. Path *bitmapqual; /* IndexPath, BitmapAndPath, BitmapOrPath */
  1172. } BitmapHeapPath;
  1173. /*
  1174. * BitmapAndPath represents a BitmapAnd plan node; it can only appear as
  1175. * part of the substructure of a BitmapHeapPath. The Path structure is
  1176. * a bit more heavyweight than we really need for this, but for simplicity
  1177. * we make it a derivative of Path anyway.
  1178. */
  1179. typedef struct BitmapAndPath
  1180. {
  1181. Path path;
  1182. List *bitmapquals; /* IndexPaths and BitmapOrPaths */
  1183. Selectivity bitmapselectivity;
  1184. } BitmapAndPath;
  1185. /*
  1186. * BitmapOrPath represents a BitmapOr plan node; it can only appear as
  1187. * part of the substructure of a BitmapHeapPath. The Path structure is
  1188. * a bit more heavyweight than we really need for this, but for simplicity
  1189. * we make it a derivative of Path anyway.
  1190. */
  1191. typedef struct BitmapOrPath
  1192. {
  1193. Path path;
  1194. List *bitmapquals; /* IndexPaths and BitmapAndPaths */
  1195. Selectivity bitmapselectivity;
  1196. } BitmapOrPath;
  1197. /*
  1198. * TidPath represents a scan by TID
  1199. *
  1200. * tidquals is an implicitly OR'ed list of qual expressions of the form
  1201. * "CTID = pseudoconstant", or "CTID = ANY(pseudoconstant_array)",
  1202. * or a CurrentOfExpr for the relation.
  1203. */
  1204. typedef struct TidPath
  1205. {
  1206. Path path;
  1207. List *tidquals; /* qual(s) involving CTID = something */
  1208. } TidPath;
  1209. /*
  1210. * TidRangePath represents a scan by a contiguous range of TIDs
  1211. *
  1212. * tidrangequals is an implicitly AND'ed list of qual expressions of the form
  1213. * "CTID relop pseudoconstant", where relop is one of >,>=,<,<=.
  1214. */
  1215. typedef struct TidRangePath
  1216. {
  1217. Path path;
  1218. List *tidrangequals;
  1219. } TidRangePath;
  1220. /*
  1221. * SubqueryScanPath represents a scan of an unflattened subquery-in-FROM
  1222. *
  1223. * Note that the subpath comes from a different planning domain; for example
  1224. * RTE indexes within it mean something different from those known to the
  1225. * SubqueryScanPath. path.parent->subroot is the planning context needed to
  1226. * interpret the subpath.
  1227. */
  1228. typedef struct SubqueryScanPath
  1229. {
  1230. Path path;
  1231. Path *subpath; /* path representing subquery execution */
  1232. } SubqueryScanPath;
  1233. /*
  1234. * ForeignPath represents a potential scan of a foreign table, foreign join
  1235. * or foreign upper-relation.
  1236. *
  1237. * fdw_private stores FDW private data about the scan. While fdw_private is
  1238. * not actually touched by the core code during normal operations, it's
  1239. * generally a good idea to use a representation that can be dumped by
  1240. * nodeToString(), so that you can examine the structure during debugging
  1241. * with tools like pprint().
  1242. */
  1243. typedef struct ForeignPath
  1244. {
  1245. Path path;
  1246. Path *fdw_outerpath;
  1247. List *fdw_private;
  1248. } ForeignPath;
  1249. /*
  1250. * CustomPath represents a table scan or a table join done by some out-of-core
  1251. * extension.
  1252. *
  1253. * We provide a set of hooks here - which the provider must take care to set
  1254. * up correctly - to allow extensions to supply their own methods of scanning
  1255. * a relation or joing relations. For example, a provider might provide GPU
  1256. * acceleration, a cache-based scan, or some other kind of logic we haven't
  1257. * dreamed up yet.
  1258. *
  1259. * CustomPaths can be injected into the planning process for a base or join
  1260. * relation by set_rel_pathlist_hook or set_join_pathlist_hook functions,
  1261. * respectively.
  1262. *
  1263. * Core code must avoid assuming that the CustomPath is only as large as
  1264. * the structure declared here; providers are allowed to make it the first
  1265. * element in a larger structure. (Since the planner never copies Paths,
  1266. * this doesn't add any complication.) However, for consistency with the
  1267. * FDW case, we provide a "custom_private" field in CustomPath; providers
  1268. * may prefer to use that rather than define another struct type.
  1269. */
  1270. struct CustomPathMethods;
  1271. typedef struct CustomPath
  1272. {
  1273. Path path;
  1274. uint32 flags; /* mask of CUSTOMPATH_* flags, see
  1275. * nodes/extensible.h */
  1276. List *custom_paths; /* list of child Path nodes, if any */
  1277. List *custom_private;
  1278. const struct CustomPathMethods *methods;
  1279. } CustomPath;
  1280. /*
  1281. * AppendPath represents an Append plan, ie, successive execution of
  1282. * several member plans.
  1283. *
  1284. * For partial Append, 'subpaths' contains non-partial subpaths followed by
  1285. * partial subpaths.
  1286. *
  1287. * Note: it is possible for "subpaths" to contain only one, or even no,
  1288. * elements. These cases are optimized during create_append_plan.
  1289. * In particular, an AppendPath with no subpaths is a "dummy" path that
  1290. * is created to represent the case that a relation is provably empty.
  1291. * (This is a convenient representation because it means that when we build
  1292. * an appendrel and find that all its children have been excluded, no extra
  1293. * action is needed to recognize the relation as dummy.)
  1294. */
  1295. typedef struct AppendPath
  1296. {
  1297. Path path;
  1298. List *subpaths; /* list of component Paths */
  1299. /* Index of first partial path in subpaths; list_length(subpaths) if none */
  1300. int first_partial_path;
  1301. Cardinality limit_tuples; /* hard limit on output tuples, or -1 */
  1302. } AppendPath;
  1303. #define IS_DUMMY_APPEND(p) \
  1304. (IsA((p), AppendPath) && ((AppendPath *) (p))->subpaths == NIL)
  1305. /*
  1306. * A relation that's been proven empty will have one path that is dummy
  1307. * (but might have projection paths on top). For historical reasons,
  1308. * this is provided as a macro that wraps is_dummy_rel().
  1309. */
  1310. #define IS_DUMMY_REL(r) is_dummy_rel(r)
  1311. extern bool is_dummy_rel(RelOptInfo *rel);
  1312. /*
  1313. * MergeAppendPath represents a MergeAppend plan, ie, the merging of sorted
  1314. * results from several member plans to produce similarly-sorted output.
  1315. */
  1316. typedef struct MergeAppendPath
  1317. {
  1318. Path path;
  1319. List *subpaths; /* list of component Paths */
  1320. Cardinality limit_tuples; /* hard limit on output tuples, or -1 */
  1321. } MergeAppendPath;
  1322. /*
  1323. * GroupResultPath represents use of a Result plan node to compute the
  1324. * output of a degenerate GROUP BY case, wherein we know we should produce
  1325. * exactly one row, which might then be filtered by a HAVING qual.
  1326. *
  1327. * Note that quals is a list of bare clauses, not RestrictInfos.
  1328. */
  1329. typedef struct GroupResultPath
  1330. {
  1331. Path path;
  1332. List *quals;
  1333. } GroupResultPath;
  1334. /*
  1335. * MaterialPath represents use of a Material plan node, i.e., caching of
  1336. * the output of its subpath. This is used when the subpath is expensive
  1337. * and needs to be scanned repeatedly, or when we need mark/restore ability
  1338. * and the subpath doesn't have it.
  1339. */
  1340. typedef struct MaterialPath
  1341. {
  1342. Path path;
  1343. Path *subpath;
  1344. } MaterialPath;
  1345. /*
  1346. * MemoizePath represents a Memoize plan node, i.e., a cache that caches
  1347. * tuples from parameterized paths to save the underlying node from having to
  1348. * be rescanned for parameter values which are already cached.
  1349. */
  1350. typedef struct MemoizePath
  1351. {
  1352. Path path;
  1353. Path *subpath; /* outerpath to cache tuples from */
  1354. List *hash_operators; /* OIDs of hash equality ops for cache keys */
  1355. List *param_exprs; /* expressions that are cache keys */
  1356. bool singlerow; /* true if the cache entry is to be marked as
  1357. * complete after caching the first record. */
  1358. bool binary_mode; /* true when cache key should be compared bit
  1359. * by bit, false when using hash equality ops */
  1360. Cardinality calls; /* expected number of rescans */
  1361. uint32 est_entries; /* The maximum number of entries that the
  1362. * planner expects will fit in the cache, or 0
  1363. * if unknown */
  1364. } MemoizePath;
  1365. /*
  1366. * UniquePath represents elimination of distinct rows from the output of
  1367. * its subpath.
  1368. *
  1369. * This can represent significantly different plans: either hash-based or
  1370. * sort-based implementation, or a no-op if the input path can be proven
  1371. * distinct already. The decision is sufficiently localized that it's not
  1372. * worth having separate Path node types. (Note: in the no-op case, we could
  1373. * eliminate the UniquePath node entirely and just return the subpath; but
  1374. * it's convenient to have a UniquePath in the path tree to signal upper-level
  1375. * routines that the input is known distinct.)
  1376. */
  1377. typedef enum UniquePathMethod
  1378. {
  1379. UNIQUE_PATH_NOOP, /* input is known unique already */
  1380. UNIQUE_PATH_HASH, /* use hashing */
  1381. UNIQUE_PATH_SORT /* use sorting */
  1382. } UniquePathMethod;
  1383. typedef struct UniquePath
  1384. {
  1385. Path path;
  1386. Path *subpath;
  1387. UniquePathMethod umethod;
  1388. List *in_operators; /* equality operators of the IN clause */
  1389. List *uniq_exprs; /* expressions to be made unique */
  1390. } UniquePath;
  1391. /*
  1392. * GatherPath runs several copies of a plan in parallel and collects the
  1393. * results. The parallel leader may also execute the plan, unless the
  1394. * single_copy flag is set.
  1395. */
  1396. typedef struct GatherPath
  1397. {
  1398. Path path;
  1399. Path *subpath; /* path for each worker */
  1400. bool single_copy; /* don't execute path more than once */
  1401. int num_workers; /* number of workers sought to help */
  1402. } GatherPath;
  1403. /*
  1404. * GatherMergePath runs several copies of a plan in parallel and collects
  1405. * the results, preserving their common sort order.
  1406. */
  1407. typedef struct GatherMergePath
  1408. {
  1409. Path path;
  1410. Path *subpath; /* path for each worker */
  1411. int num_workers; /* number of workers sought to help */
  1412. } GatherMergePath;
  1413. /*
  1414. * All join-type paths share these fields.
  1415. */
  1416. typedef struct JoinPath
  1417. {
  1418. Path path;
  1419. JoinType jointype;
  1420. bool inner_unique; /* each outer tuple provably matches no more
  1421. * than one inner tuple */
  1422. Path *outerjoinpath; /* path for the outer side of the join */
  1423. Path *innerjoinpath; /* path for the inner side of the join */
  1424. List *joinrestrictinfo; /* RestrictInfos to apply to join */
  1425. /*
  1426. * See the notes for RelOptInfo and ParamPathInfo to understand why
  1427. * joinrestrictinfo is needed in JoinPath, and can't be merged into the
  1428. * parent RelOptInfo.
  1429. */
  1430. } JoinPath;
  1431. /*
  1432. * A nested-loop path needs no special fields.
  1433. */
  1434. typedef struct NestPath
  1435. {
  1436. JoinPath jpath;
  1437. } NestPath;
  1438. /*
  1439. * A mergejoin path has these fields.
  1440. *
  1441. * Unlike other path types, a MergePath node doesn't represent just a single
  1442. * run-time plan node: it can represent up to four. Aside from the MergeJoin
  1443. * node itself, there can be a Sort node for the outer input, a Sort node
  1444. * for the inner input, and/or a Material node for the inner input. We could
  1445. * represent these nodes by separate path nodes, but considering how many
  1446. * different merge paths are investigated during a complex join problem,
  1447. * it seems better to avoid unnecessary palloc overhead.
  1448. *
  1449. * path_mergeclauses lists the clauses (in the form of RestrictInfos)
  1450. * that will be used in the merge.
  1451. *
  1452. * Note that the mergeclauses are a subset of the parent relation's
  1453. * restriction-clause list. Any join clauses that are not mergejoinable
  1454. * appear only in the parent's restrict list, and must be checked by a
  1455. * qpqual at execution time.
  1456. *
  1457. * outersortkeys (resp. innersortkeys) is NIL if the outer path
  1458. * (resp. inner path) is already ordered appropriately for the
  1459. * mergejoin. If it is not NIL then it is a PathKeys list describing
  1460. * the ordering that must be created by an explicit Sort node.
  1461. *
  1462. * skip_mark_restore is true if the executor need not do mark/restore calls.
  1463. * Mark/restore overhead is usually required, but can be skipped if we know
  1464. * that the executor need find only one match per outer tuple, and that the
  1465. * mergeclauses are sufficient to identify a match. In such cases the
  1466. * executor can immediately advance the outer relation after processing a
  1467. * match, and therefore it need never back up the inner relation.
  1468. *
  1469. * materialize_inner is true if a Material node should be placed atop the
  1470. * inner input. This may appear with or without an inner Sort step.
  1471. */
  1472. typedef struct MergePath
  1473. {
  1474. JoinPath jpath;
  1475. List *path_mergeclauses; /* join clauses to be used for merge */
  1476. List *outersortkeys; /* keys for explicit sort, if any */
  1477. List *innersortkeys; /* keys for explicit sort, if any */
  1478. bool skip_mark_restore; /* can executor skip mark/restore? */
  1479. bool materialize_inner; /* add Materialize to inner? */
  1480. } MergePath;
  1481. /*
  1482. * A hashjoin path has these fields.
  1483. *
  1484. * The remarks above for mergeclauses apply for hashclauses as well.
  1485. *
  1486. * Hashjoin does not care what order its inputs appear in, so we have
  1487. * no need for sortkeys.
  1488. */
  1489. typedef struct HashPath
  1490. {
  1491. JoinPath jpath;
  1492. List *path_hashclauses; /* join clauses used for hashing */
  1493. int num_batches; /* number of batches expected */
  1494. Cardinality inner_rows_total; /* total inner rows expected */
  1495. } HashPath;
  1496. /*
  1497. * ProjectionPath represents a projection (that is, targetlist computation)
  1498. *
  1499. * Nominally, this path node represents using a Result plan node to do a
  1500. * projection step. However, if the input plan node supports projection,
  1501. * we can just modify its output targetlist to do the required calculations
  1502. * directly, and not need a Result. In some places in the planner we can just
  1503. * jam the desired PathTarget into the input path node (and adjust its cost
  1504. * accordingly), so we don't need a ProjectionPath. But in other places
  1505. * it's necessary to not modify the input path node, so we need a separate
  1506. * ProjectionPath node, which is marked dummy to indicate that we intend to
  1507. * assign the work to the input plan node. The estimated cost for the
  1508. * ProjectionPath node will account for whether a Result will be used or not.
  1509. */
  1510. typedef struct ProjectionPath
  1511. {
  1512. Path path;
  1513. Path *subpath; /* path representing input source */
  1514. bool dummypp; /* true if no separate Result is needed */
  1515. } ProjectionPath;
  1516. /*
  1517. * ProjectSetPath represents evaluation of a targetlist that includes
  1518. * set-returning function(s), which will need to be implemented by a
  1519. * ProjectSet plan node.
  1520. */
  1521. typedef struct ProjectSetPath
  1522. {
  1523. Path path;
  1524. Path *subpath; /* path representing input source */
  1525. } ProjectSetPath;
  1526. /*
  1527. * SortPath represents an explicit sort step
  1528. *
  1529. * The sort keys are, by definition, the same as path.pathkeys.
  1530. *
  1531. * Note: the Sort plan node cannot project, so path.pathtarget must be the
  1532. * same as the input's pathtarget.
  1533. */
  1534. typedef struct SortPath
  1535. {
  1536. Path path;
  1537. Path *subpath; /* path representing input source */
  1538. } SortPath;
  1539. /*
  1540. * IncrementalSortPath represents an incremental sort step
  1541. *
  1542. * This is like a regular sort, except some leading key columns are assumed
  1543. * to be ordered already.
  1544. */
  1545. typedef struct IncrementalSortPath
  1546. {
  1547. SortPath spath;
  1548. int nPresortedCols; /* number of presorted columns */
  1549. } IncrementalSortPath;
  1550. /*
  1551. * GroupPath represents grouping (of presorted input)
  1552. *
  1553. * groupClause represents the columns to be grouped on; the input path
  1554. * must be at least that well sorted.
  1555. *
  1556. * We can also apply a qual to the grouped rows (equivalent of HAVING)
  1557. */
  1558. typedef struct GroupPath
  1559. {
  1560. Path path;
  1561. Path *subpath; /* path representing input source */
  1562. List *groupClause; /* a list of SortGroupClause's */
  1563. List *qual; /* quals (HAVING quals), if any */
  1564. } GroupPath;
  1565. /*
  1566. * UpperUniquePath represents adjacent-duplicate removal (in presorted input)
  1567. *
  1568. * The columns to be compared are the first numkeys columns of the path's
  1569. * pathkeys. The input is presumed already sorted that way.
  1570. */
  1571. typedef struct UpperUniquePath
  1572. {
  1573. Path path;
  1574. Path *subpath; /* path representing input source */
  1575. int numkeys; /* number of pathkey columns to compare */
  1576. } UpperUniquePath;
  1577. /*
  1578. * AggPath represents generic computation of aggregate functions
  1579. *
  1580. * This may involve plain grouping (but not grouping sets), using either
  1581. * sorted or hashed grouping; for the AGG_SORTED case, the input must be
  1582. * appropriately presorted.
  1583. */
  1584. typedef struct AggPath
  1585. {
  1586. Path path;
  1587. Path *subpath; /* path representing input source */
  1588. AggStrategy aggstrategy; /* basic strategy, see nodes.h */
  1589. AggSplit aggsplit; /* agg-splitting mode, see nodes.h */
  1590. Cardinality numGroups; /* estimated number of groups in input */
  1591. uint64 transitionSpace; /* for pass-by-ref transition data */
  1592. List *groupClause; /* a list of SortGroupClause's */
  1593. List *qual; /* quals (HAVING quals), if any */
  1594. } AggPath;
  1595. /*
  1596. * Various annotations used for grouping sets in the planner.
  1597. */
  1598. typedef struct GroupingSetData
  1599. {
  1600. NodeTag type;
  1601. List *set; /* grouping set as list of sortgrouprefs */
  1602. Cardinality numGroups; /* est. number of result groups */
  1603. } GroupingSetData;
  1604. typedef struct RollupData
  1605. {
  1606. NodeTag type;
  1607. List *groupClause; /* applicable subset of parse->groupClause */
  1608. List *gsets; /* lists of integer indexes into groupClause */
  1609. List *gsets_data; /* list of GroupingSetData */
  1610. Cardinality numGroups; /* est. number of result groups */
  1611. bool hashable; /* can be hashed */
  1612. bool is_hashed; /* to be implemented as a hashagg */
  1613. } RollupData;
  1614. /*
  1615. * GroupingSetsPath represents a GROUPING SETS aggregation
  1616. */
  1617. typedef struct GroupingSetsPath
  1618. {
  1619. Path path;
  1620. Path *subpath; /* path representing input source */
  1621. AggStrategy aggstrategy; /* basic strategy */
  1622. List *rollups; /* list of RollupData */
  1623. List *qual; /* quals (HAVING quals), if any */
  1624. uint64 transitionSpace; /* for pass-by-ref transition data */
  1625. } GroupingSetsPath;
  1626. /*
  1627. * MinMaxAggPath represents computation of MIN/MAX aggregates from indexes
  1628. */
  1629. typedef struct MinMaxAggPath
  1630. {
  1631. Path path;
  1632. List *mmaggregates; /* list of MinMaxAggInfo */
  1633. List *quals; /* HAVING quals, if any */
  1634. } MinMaxAggPath;
  1635. /*
  1636. * WindowAggPath represents generic computation of window functions
  1637. */
  1638. typedef struct WindowAggPath
  1639. {
  1640. Path path;
  1641. Path *subpath; /* path representing input source */
  1642. WindowClause *winclause; /* WindowClause we'll be using */
  1643. List *qual; /* lower-level WindowAgg runconditions */
  1644. bool topwindow; /* false for all apart from the WindowAgg
  1645. * that's closest to the root of the plan */
  1646. } WindowAggPath;
  1647. /*
  1648. * SetOpPath represents a set-operation, that is INTERSECT or EXCEPT
  1649. */
  1650. typedef struct SetOpPath
  1651. {
  1652. Path path;
  1653. Path *subpath; /* path representing input source */
  1654. SetOpCmd cmd; /* what to do, see nodes.h */
  1655. SetOpStrategy strategy; /* how to do it, see nodes.h */
  1656. List *distinctList; /* SortGroupClauses identifying target cols */
  1657. AttrNumber flagColIdx; /* where is the flag column, if any */
  1658. int firstFlag; /* flag value for first input relation */
  1659. Cardinality numGroups; /* estimated number of groups in input */
  1660. } SetOpPath;
  1661. /*
  1662. * RecursiveUnionPath represents a recursive UNION node
  1663. */
  1664. typedef struct RecursiveUnionPath
  1665. {
  1666. Path path;
  1667. Path *leftpath; /* paths representing input sources */
  1668. Path *rightpath;
  1669. List *distinctList; /* SortGroupClauses identifying target cols */
  1670. int wtParam; /* ID of Param representing work table */
  1671. Cardinality numGroups; /* estimated number of groups in input */
  1672. } RecursiveUnionPath;
  1673. /*
  1674. * LockRowsPath represents acquiring row locks for SELECT FOR UPDATE/SHARE
  1675. */
  1676. typedef struct LockRowsPath
  1677. {
  1678. Path path;
  1679. Path *subpath; /* path representing input source */
  1680. List *rowMarks; /* a list of PlanRowMark's */
  1681. int epqParam; /* ID of Param for EvalPlanQual re-eval */
  1682. } LockRowsPath;
  1683. /*
  1684. * ModifyTablePath represents performing INSERT/UPDATE/DELETE/MERGE
  1685. *
  1686. * We represent most things that will be in the ModifyTable plan node
  1687. * literally, except we have a child Path not Plan. But analysis of the
  1688. * OnConflictExpr is deferred to createplan.c, as is collection of FDW data.
  1689. */
  1690. typedef struct ModifyTablePath
  1691. {
  1692. Path path;
  1693. Path *subpath; /* Path producing source data */
  1694. CmdType operation; /* INSERT, UPDATE, DELETE, or MERGE */
  1695. bool canSetTag; /* do we set the command tag/es_processed? */
  1696. Index nominalRelation; /* Parent RT index for use of EXPLAIN */
  1697. Index rootRelation; /* Root RT index, if target is partitioned */
  1698. bool partColsUpdated; /* some part key in hierarchy updated? */
  1699. List *resultRelations; /* integer list of RT indexes */
  1700. List *updateColnosLists; /* per-target-table update_colnos lists */
  1701. List *withCheckOptionLists; /* per-target-table WCO lists */
  1702. List *returningLists; /* per-target-table RETURNING tlists */
  1703. List *rowMarks; /* PlanRowMarks (non-locking only) */
  1704. OnConflictExpr *onconflict; /* ON CONFLICT clause, or NULL */
  1705. int epqParam; /* ID of Param for EvalPlanQual re-eval */
  1706. List *mergeActionLists; /* per-target-table lists of actions for
  1707. * MERGE */
  1708. } ModifyTablePath;
  1709. /*
  1710. * LimitPath represents applying LIMIT/OFFSET restrictions
  1711. */
  1712. typedef struct LimitPath
  1713. {
  1714. Path path;
  1715. Path *subpath; /* path representing input source */
  1716. Node *limitOffset; /* OFFSET parameter, or NULL if none */
  1717. Node *limitCount; /* COUNT parameter, or NULL if none */
  1718. LimitOption limitOption; /* FETCH FIRST with ties or exact number */
  1719. } LimitPath;
  1720. /*
  1721. * Restriction clause info.
  1722. *
  1723. * We create one of these for each AND sub-clause of a restriction condition
  1724. * (WHERE or JOIN/ON clause). Since the restriction clauses are logically
  1725. * ANDed, we can use any one of them or any subset of them to filter out
  1726. * tuples, without having to evaluate the rest. The RestrictInfo node itself
  1727. * stores data used by the optimizer while choosing the best query plan.
  1728. *
  1729. * If a restriction clause references a single base relation, it will appear
  1730. * in the baserestrictinfo list of the RelOptInfo for that base rel.
  1731. *
  1732. * If a restriction clause references more than one base rel, it will
  1733. * appear in the joininfo list of every RelOptInfo that describes a strict
  1734. * subset of the base rels mentioned in the clause. The joininfo lists are
  1735. * used to drive join tree building by selecting plausible join candidates.
  1736. * The clause cannot actually be applied until we have built a join rel
  1737. * containing all the base rels it references, however.
  1738. *
  1739. * When we construct a join rel that includes all the base rels referenced
  1740. * in a multi-relation restriction clause, we place that clause into the
  1741. * joinrestrictinfo lists of paths for the join rel, if neither left nor
  1742. * right sub-path includes all base rels referenced in the clause. The clause
  1743. * will be applied at that join level, and will not propagate any further up
  1744. * the join tree. (Note: the "predicate migration" code was once intended to
  1745. * push restriction clauses up and down the plan tree based on evaluation
  1746. * costs, but it's dead code and is unlikely to be resurrected in the
  1747. * foreseeable future.)
  1748. *
  1749. * Note that in the presence of more than two rels, a multi-rel restriction
  1750. * might reach different heights in the join tree depending on the join
  1751. * sequence we use. So, these clauses cannot be associated directly with
  1752. * the join RelOptInfo, but must be kept track of on a per-join-path basis.
  1753. *
  1754. * RestrictInfos that represent equivalence conditions (i.e., mergejoinable
  1755. * equalities that are not outerjoin-delayed) are handled a bit differently.
  1756. * Initially we attach them to the EquivalenceClasses that are derived from
  1757. * them. When we construct a scan or join path, we look through all the
  1758. * EquivalenceClasses and generate derived RestrictInfos representing the
  1759. * minimal set of conditions that need to be checked for this particular scan
  1760. * or join to enforce that all members of each EquivalenceClass are in fact
  1761. * equal in all rows emitted by the scan or join.
  1762. *
  1763. * When dealing with outer joins we have to be very careful about pushing qual
  1764. * clauses up and down the tree. An outer join's own JOIN/ON conditions must
  1765. * be evaluated exactly at that join node, unless they are "degenerate"
  1766. * conditions that reference only Vars from the nullable side of the join.
  1767. * Quals appearing in WHERE or in a JOIN above the outer join cannot be pushed
  1768. * down below the outer join, if they reference any nullable Vars.
  1769. * RestrictInfo nodes contain a flag to indicate whether a qual has been
  1770. * pushed down to a lower level than its original syntactic placement in the
  1771. * join tree would suggest. If an outer join prevents us from pushing a qual
  1772. * down to its "natural" semantic level (the level associated with just the
  1773. * base rels used in the qual) then we mark the qual with a "required_relids"
  1774. * value including more than just the base rels it actually uses. By
  1775. * pretending that the qual references all the rels required to form the outer
  1776. * join, we prevent it from being evaluated below the outer join's joinrel.
  1777. * When we do form the outer join's joinrel, we still need to distinguish
  1778. * those quals that are actually in that join's JOIN/ON condition from those
  1779. * that appeared elsewhere in the tree and were pushed down to the join rel
  1780. * because they used no other rels. That's what the is_pushed_down flag is
  1781. * for; it tells us that a qual is not an OUTER JOIN qual for the set of base
  1782. * rels listed in required_relids. A clause that originally came from WHERE
  1783. * or an INNER JOIN condition will *always* have its is_pushed_down flag set.
  1784. * It's possible for an OUTER JOIN clause to be marked is_pushed_down too,
  1785. * if we decide that it can be pushed down into the nullable side of the join.
  1786. * In that case it acts as a plain filter qual for wherever it gets evaluated.
  1787. * (In short, is_pushed_down is only false for non-degenerate outer join
  1788. * conditions. Possibly we should rename it to reflect that meaning? But
  1789. * see also the comments for RINFO_IS_PUSHED_DOWN, below.)
  1790. *
  1791. * RestrictInfo nodes also contain an outerjoin_delayed flag, which is true
  1792. * if the clause's applicability must be delayed due to any outer joins
  1793. * appearing below it (ie, it has to be postponed to some join level higher
  1794. * than the set of relations it actually references).
  1795. *
  1796. * There is also an outer_relids field, which is NULL except for outer join
  1797. * clauses; for those, it is the set of relids on the outer side of the
  1798. * clause's outer join. (These are rels that the clause cannot be applied to
  1799. * in parameterized scans, since pushing it into the join's outer side would
  1800. * lead to wrong answers.)
  1801. *
  1802. * There is also a nullable_relids field, which is the set of rels the clause
  1803. * references that can be forced null by some outer join below the clause.
  1804. *
  1805. * outerjoin_delayed = true is subtly different from nullable_relids != NULL:
  1806. * a clause might reference some nullable rels and yet not be
  1807. * outerjoin_delayed because it also references all the other rels of the
  1808. * outer join(s). A clause that is not outerjoin_delayed can be enforced
  1809. * anywhere it is computable.
  1810. *
  1811. * To handle security-barrier conditions efficiently, we mark RestrictInfo
  1812. * nodes with a security_level field, in which higher values identify clauses
  1813. * coming from less-trusted sources. The exact semantics are that a clause
  1814. * cannot be evaluated before another clause with a lower security_level value
  1815. * unless the first clause is leakproof. As with outer-join clauses, this
  1816. * creates a reason for clauses to sometimes need to be evaluated higher in
  1817. * the join tree than their contents would suggest; and even at a single plan
  1818. * node, this rule constrains the order of application of clauses.
  1819. *
  1820. * In general, the referenced clause might be arbitrarily complex. The
  1821. * kinds of clauses we can handle as indexscan quals, mergejoin clauses,
  1822. * or hashjoin clauses are limited (e.g., no volatile functions). The code
  1823. * for each kind of path is responsible for identifying the restrict clauses
  1824. * it can use and ignoring the rest. Clauses not implemented by an indexscan,
  1825. * mergejoin, or hashjoin will be placed in the plan qual or joinqual field
  1826. * of the finished Plan node, where they will be enforced by general-purpose
  1827. * qual-expression-evaluation code. (But we are still entitled to count
  1828. * their selectivity when estimating the result tuple count, if we
  1829. * can guess what it is...)
  1830. *
  1831. * When the referenced clause is an OR clause, we generate a modified copy
  1832. * in which additional RestrictInfo nodes are inserted below the top-level
  1833. * OR/AND structure. This is a convenience for OR indexscan processing:
  1834. * indexquals taken from either the top level or an OR subclause will have
  1835. * associated RestrictInfo nodes.
  1836. *
  1837. * The can_join flag is set true if the clause looks potentially useful as
  1838. * a merge or hash join clause, that is if it is a binary opclause with
  1839. * nonoverlapping sets of relids referenced in the left and right sides.
  1840. * (Whether the operator is actually merge or hash joinable isn't checked,
  1841. * however.)
  1842. *
  1843. * The pseudoconstant flag is set true if the clause contains no Vars of
  1844. * the current query level and no volatile functions. Such a clause can be
  1845. * pulled out and used as a one-time qual in a gating Result node. We keep
  1846. * pseudoconstant clauses in the same lists as other RestrictInfos so that
  1847. * the regular clause-pushing machinery can assign them to the correct join
  1848. * level, but they need to be treated specially for cost and selectivity
  1849. * estimates. Note that a pseudoconstant clause can never be an indexqual
  1850. * or merge or hash join clause, so it's of no interest to large parts of
  1851. * the planner.
  1852. *
  1853. * When join clauses are generated from EquivalenceClasses, there may be
  1854. * several equally valid ways to enforce join equivalence, of which we need
  1855. * apply only one. We mark clauses of this kind by setting parent_ec to
  1856. * point to the generating EquivalenceClass. Multiple clauses with the same
  1857. * parent_ec in the same join are redundant.
  1858. */
  1859. typedef struct RestrictInfo
  1860. {
  1861. NodeTag type;
  1862. Expr *clause; /* the represented clause of WHERE or JOIN */
  1863. bool is_pushed_down; /* true if clause was pushed down in level */
  1864. bool outerjoin_delayed; /* true if delayed by lower outer join */
  1865. bool can_join; /* see comment above */
  1866. bool pseudoconstant; /* see comment above */
  1867. bool leakproof; /* true if known to contain no leaked Vars */
  1868. VolatileFunctionStatus has_volatile; /* to indicate if clause contains
  1869. * any volatile functions. */
  1870. Index security_level; /* see comment above */
  1871. /* The set of relids (varnos) actually referenced in the clause: */
  1872. Relids clause_relids;
  1873. /* The set of relids required to evaluate the clause: */
  1874. Relids required_relids;
  1875. /* If an outer-join clause, the outer-side relations, else NULL: */
  1876. Relids outer_relids;
  1877. /* The relids used in the clause that are nullable by lower outer joins: */
  1878. Relids nullable_relids;
  1879. /* These fields are set for any binary opclause: */
  1880. Relids left_relids; /* relids in left side of clause */
  1881. Relids right_relids; /* relids in right side of clause */
  1882. /* This field is NULL unless clause is an OR clause: */
  1883. Expr *orclause; /* modified clause with RestrictInfos */
  1884. /* This field is NULL unless clause is potentially redundant: */
  1885. EquivalenceClass *parent_ec; /* generating EquivalenceClass */
  1886. /* cache space for cost and selectivity */
  1887. QualCost eval_cost; /* eval cost of clause; -1 if not yet set */
  1888. Selectivity norm_selec; /* selectivity for "normal" (JOIN_INNER)
  1889. * semantics; -1 if not yet set; >1 means a
  1890. * redundant clause */
  1891. Selectivity outer_selec; /* selectivity for outer join semantics; -1 if
  1892. * not yet set */
  1893. /* valid if clause is mergejoinable, else NIL */
  1894. List *mergeopfamilies; /* opfamilies containing clause operator */
  1895. /* cache space for mergeclause processing; NULL if not yet set */
  1896. EquivalenceClass *left_ec; /* EquivalenceClass containing lefthand */
  1897. EquivalenceClass *right_ec; /* EquivalenceClass containing righthand */
  1898. EquivalenceMember *left_em; /* EquivalenceMember for lefthand */
  1899. EquivalenceMember *right_em; /* EquivalenceMember for righthand */
  1900. List *scansel_cache; /* list of MergeScanSelCache structs */
  1901. /* transient workspace for use while considering a specific join path */
  1902. bool outer_is_left; /* T = outer var on left, F = on right */
  1903. /* valid if clause is hashjoinable, else InvalidOid: */
  1904. Oid hashjoinoperator; /* copy of clause operator */
  1905. /* cache space for hashclause processing; -1 if not yet set */
  1906. Selectivity left_bucketsize; /* avg bucketsize of left side */
  1907. Selectivity right_bucketsize; /* avg bucketsize of right side */
  1908. Selectivity left_mcvfreq; /* left side's most common val's freq */
  1909. Selectivity right_mcvfreq; /* right side's most common val's freq */
  1910. /* hash equality operators used for memoize nodes, else InvalidOid */
  1911. Oid left_hasheqoperator;
  1912. Oid right_hasheqoperator;
  1913. } RestrictInfo;
  1914. /*
  1915. * This macro embodies the correct way to test whether a RestrictInfo is
  1916. * "pushed down" to a given outer join, that is, should be treated as a filter
  1917. * clause rather than a join clause at that outer join. This is certainly so
  1918. * if is_pushed_down is true; but examining that is not sufficient anymore,
  1919. * because outer-join clauses will get pushed down to lower outer joins when
  1920. * we generate a path for the lower outer join that is parameterized by the
  1921. * LHS of the upper one. We can detect such a clause by noting that its
  1922. * required_relids exceed the scope of the join.
  1923. */
  1924. #define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids) \
  1925. ((rinfo)->is_pushed_down || \
  1926. !bms_is_subset((rinfo)->required_relids, joinrelids))
  1927. /*
  1928. * Since mergejoinscansel() is a relatively expensive function, and would
  1929. * otherwise be invoked many times while planning a large join tree,
  1930. * we go out of our way to cache its results. Each mergejoinable
  1931. * RestrictInfo carries a list of the specific sort orderings that have
  1932. * been considered for use with it, and the resulting selectivities.
  1933. */
  1934. typedef struct MergeScanSelCache
  1935. {
  1936. /* Ordering details (cache lookup key) */
  1937. Oid opfamily; /* btree opfamily defining the ordering */
  1938. Oid collation; /* collation for the ordering */
  1939. int strategy; /* sort direction (ASC or DESC) */
  1940. bool nulls_first; /* do NULLs come before normal values? */
  1941. /* Results */
  1942. Selectivity leftstartsel; /* first-join fraction for clause left side */
  1943. Selectivity leftendsel; /* last-join fraction for clause left side */
  1944. Selectivity rightstartsel; /* first-join fraction for clause right side */
  1945. Selectivity rightendsel; /* last-join fraction for clause right side */
  1946. } MergeScanSelCache;
  1947. /*
  1948. * Placeholder node for an expression to be evaluated below the top level
  1949. * of a plan tree. This is used during planning to represent the contained
  1950. * expression. At the end of the planning process it is replaced by either
  1951. * the contained expression or a Var referring to a lower-level evaluation of
  1952. * the contained expression. Typically the evaluation occurs below an outer
  1953. * join, and Var references above the outer join might thereby yield NULL
  1954. * instead of the expression value.
  1955. *
  1956. * Although the planner treats this as an expression node type, it is not
  1957. * recognized by the parser or executor, so we declare it here rather than
  1958. * in primnodes.h.
  1959. */
  1960. typedef struct PlaceHolderVar
  1961. {
  1962. Expr xpr;
  1963. Expr *phexpr; /* the represented expression */
  1964. Relids phrels; /* base relids syntactically within expr src */
  1965. Index phid; /* ID for PHV (unique within planner run) */
  1966. Index phlevelsup; /* > 0 if PHV belongs to outer query */
  1967. } PlaceHolderVar;
  1968. /*
  1969. * "Special join" info.
  1970. *
  1971. * One-sided outer joins constrain the order of joining partially but not
  1972. * completely. We flatten such joins into the planner's top-level list of
  1973. * relations to join, but record information about each outer join in a
  1974. * SpecialJoinInfo struct. These structs are kept in the PlannerInfo node's
  1975. * join_info_list.
  1976. *
  1977. * Similarly, semijoins and antijoins created by flattening IN (subselect)
  1978. * and EXISTS(subselect) clauses create partial constraints on join order.
  1979. * These are likewise recorded in SpecialJoinInfo structs.
  1980. *
  1981. * We make SpecialJoinInfos for FULL JOINs even though there is no flexibility
  1982. * of planning for them, because this simplifies make_join_rel()'s API.
  1983. *
  1984. * min_lefthand and min_righthand are the sets of base relids that must be
  1985. * available on each side when performing the special join. lhs_strict is
  1986. * true if the special join's condition cannot succeed when the LHS variables
  1987. * are all NULL (this means that an outer join can commute with upper-level
  1988. * outer joins even if it appears in their RHS). We don't bother to set
  1989. * lhs_strict for FULL JOINs, however.
  1990. *
  1991. * It is not valid for either min_lefthand or min_righthand to be empty sets;
  1992. * if they were, this would break the logic that enforces join order.
  1993. *
  1994. * syn_lefthand and syn_righthand are the sets of base relids that are
  1995. * syntactically below this special join. (These are needed to help compute
  1996. * min_lefthand and min_righthand for higher joins.)
  1997. *
  1998. * delay_upper_joins is set true if we detect a pushed-down clause that has
  1999. * to be evaluated after this join is formed (because it references the RHS).
  2000. * Any outer joins that have such a clause and this join in their RHS cannot
  2001. * commute with this join, because that would leave noplace to check the
  2002. * pushed-down clause. (We don't track this for FULL JOINs, either.)
  2003. *
  2004. * For a semijoin, we also extract the join operators and their RHS arguments
  2005. * and set semi_operators, semi_rhs_exprs, semi_can_btree, and semi_can_hash.
  2006. * This is done in support of possibly unique-ifying the RHS, so we don't
  2007. * bother unless at least one of semi_can_btree and semi_can_hash can be set
  2008. * true. (You might expect that this information would be computed during
  2009. * join planning; but it's helpful to have it available during planning of
  2010. * parameterized table scans, so we store it in the SpecialJoinInfo structs.)
  2011. *
  2012. * jointype is never JOIN_RIGHT; a RIGHT JOIN is handled by switching
  2013. * the inputs to make it a LEFT JOIN. So the allowed values of jointype
  2014. * in a join_info_list member are only LEFT, FULL, SEMI, or ANTI.
  2015. *
  2016. * For purposes of join selectivity estimation, we create transient
  2017. * SpecialJoinInfo structures for regular inner joins; so it is possible
  2018. * to have jointype == JOIN_INNER in such a structure, even though this is
  2019. * not allowed within join_info_list. We also create transient
  2020. * SpecialJoinInfos with jointype == JOIN_INNER for outer joins, since for
  2021. * cost estimation purposes it is sometimes useful to know the join size under
  2022. * plain innerjoin semantics. Note that lhs_strict, delay_upper_joins, and
  2023. * of course the semi_xxx fields are not set meaningfully within such structs.
  2024. */
  2025. #ifndef HAVE_SPECIALJOININFO_TYPEDEF
  2026. typedef struct SpecialJoinInfo SpecialJoinInfo;
  2027. #define HAVE_SPECIALJOININFO_TYPEDEF 1
  2028. #endif
  2029. struct SpecialJoinInfo
  2030. {
  2031. NodeTag type;
  2032. Relids min_lefthand; /* base relids in minimum LHS for join */
  2033. Relids min_righthand; /* base relids in minimum RHS for join */
  2034. Relids syn_lefthand; /* base relids syntactically within LHS */
  2035. Relids syn_righthand; /* base relids syntactically within RHS */
  2036. JoinType jointype; /* always INNER, LEFT, FULL, SEMI, or ANTI */
  2037. bool lhs_strict; /* joinclause is strict for some LHS rel */
  2038. bool delay_upper_joins; /* can't commute with upper RHS */
  2039. /* Remaining fields are set only for JOIN_SEMI jointype: */
  2040. bool semi_can_btree; /* true if semi_operators are all btree */
  2041. bool semi_can_hash; /* true if semi_operators are all hash */
  2042. List *semi_operators; /* OIDs of equality join operators */
  2043. List *semi_rhs_exprs; /* righthand-side expressions of these ops */
  2044. };
  2045. /*
  2046. * Append-relation info.
  2047. *
  2048. * When we expand an inheritable table or a UNION-ALL subselect into an
  2049. * "append relation" (essentially, a list of child RTEs), we build an
  2050. * AppendRelInfo for each child RTE. The list of AppendRelInfos indicates
  2051. * which child RTEs must be included when expanding the parent, and each node
  2052. * carries information needed to translate between columns of the parent and
  2053. * columns of the child.
  2054. *
  2055. * These structs are kept in the PlannerInfo node's append_rel_list, with
  2056. * append_rel_array[] providing a convenient lookup method for the struct
  2057. * associated with a particular child relid (there can be only one, though
  2058. * parent rels may have many entries in append_rel_list).
  2059. *
  2060. * Note: after completion of the planner prep phase, any given RTE is an
  2061. * append parent having entries in append_rel_list if and only if its
  2062. * "inh" flag is set. We clear "inh" for plain tables that turn out not
  2063. * to have inheritance children, and (in an abuse of the original meaning
  2064. * of the flag) we set "inh" for subquery RTEs that turn out to be
  2065. * flattenable UNION ALL queries. This lets us avoid useless searches
  2066. * of append_rel_list.
  2067. *
  2068. * Note: the data structure assumes that append-rel members are single
  2069. * baserels. This is OK for inheritance, but it prevents us from pulling
  2070. * up a UNION ALL member subquery if it contains a join. While that could
  2071. * be fixed with a more complex data structure, at present there's not much
  2072. * point because no improvement in the plan could result.
  2073. */
  2074. typedef struct AppendRelInfo
  2075. {
  2076. NodeTag type;
  2077. /*
  2078. * These fields uniquely identify this append relationship. There can be
  2079. * (in fact, always should be) multiple AppendRelInfos for the same
  2080. * parent_relid, but never more than one per child_relid, since a given
  2081. * RTE cannot be a child of more than one append parent.
  2082. */
  2083. Index parent_relid; /* RT index of append parent rel */
  2084. Index child_relid; /* RT index of append child rel */
  2085. /*
  2086. * For an inheritance appendrel, the parent and child are both regular
  2087. * relations, and we store their rowtype OIDs here for use in translating
  2088. * whole-row Vars. For a UNION-ALL appendrel, the parent and child are
  2089. * both subqueries with no named rowtype, and we store InvalidOid here.
  2090. */
  2091. Oid parent_reltype; /* OID of parent's composite type */
  2092. Oid child_reltype; /* OID of child's composite type */
  2093. /*
  2094. * The N'th element of this list is a Var or expression representing the
  2095. * child column corresponding to the N'th column of the parent. This is
  2096. * used to translate Vars referencing the parent rel into references to
  2097. * the child. A list element is NULL if it corresponds to a dropped
  2098. * column of the parent (this is only possible for inheritance cases, not
  2099. * UNION ALL). The list elements are always simple Vars for inheritance
  2100. * cases, but can be arbitrary expressions in UNION ALL cases.
  2101. *
  2102. * Notice we only store entries for user columns (attno > 0). Whole-row
  2103. * Vars are special-cased, and system columns (attno < 0) need no special
  2104. * translation since their attnos are the same for all tables.
  2105. *
  2106. * Caution: the Vars have varlevelsup = 0. Be careful to adjust as needed
  2107. * when copying into a subquery.
  2108. */
  2109. List *translated_vars; /* Expressions in the child's Vars */
  2110. /*
  2111. * This array simplifies translations in the reverse direction, from
  2112. * child's column numbers to parent's. The entry at [ccolno - 1] is the
  2113. * 1-based parent column number for child column ccolno, or zero if that
  2114. * child column is dropped or doesn't exist in the parent.
  2115. */
  2116. int num_child_cols; /* length of array */
  2117. AttrNumber *parent_colnos; /* array of parent attnos, or zeroes */
  2118. /*
  2119. * We store the parent table's OID here for inheritance, or InvalidOid for
  2120. * UNION ALL. This is only needed to help in generating error messages if
  2121. * an attempt is made to reference a dropped parent column.
  2122. */
  2123. Oid parent_reloid; /* OID of parent relation */
  2124. } AppendRelInfo;
  2125. /*
  2126. * Information about a row-identity "resjunk" column in UPDATE/DELETE/MERGE.
  2127. *
  2128. * In partitioned UPDATE/DELETE/MERGE it's important for child partitions to
  2129. * share row-identity columns whenever possible, so as not to chew up too many
  2130. * targetlist columns. We use these structs to track which identity columns
  2131. * have been requested. In the finished plan, each of these will give rise
  2132. * to one resjunk entry in the targetlist of the ModifyTable's subplan node.
  2133. *
  2134. * All the Vars stored in RowIdentityVarInfos must have varno ROWID_VAR, for
  2135. * convenience of detecting duplicate requests. We'll replace that, in the
  2136. * final plan, with the varno of the generating rel.
  2137. *
  2138. * Outside this list, a Var with varno ROWID_VAR and varattno k is a reference
  2139. * to the k-th element of the row_identity_vars list (k counting from 1).
  2140. * We add such a reference to root->processed_tlist when creating the entry,
  2141. * and it propagates into the plan tree from there.
  2142. */
  2143. typedef struct RowIdentityVarInfo
  2144. {
  2145. NodeTag type;
  2146. Var *rowidvar; /* Var to be evaluated (but varno=ROWID_VAR) */
  2147. int32 rowidwidth; /* estimated average width */
  2148. char *rowidname; /* name of the resjunk column */
  2149. Relids rowidrels; /* RTE indexes of target rels using this */
  2150. } RowIdentityVarInfo;
  2151. /*
  2152. * For each distinct placeholder expression generated during planning, we
  2153. * store a PlaceHolderInfo node in the PlannerInfo node's placeholder_list.
  2154. * This stores info that is needed centrally rather than in each copy of the
  2155. * PlaceHolderVar. The phid fields identify which PlaceHolderInfo goes with
  2156. * each PlaceHolderVar. Note that phid is unique throughout a planner run,
  2157. * not just within a query level --- this is so that we need not reassign ID's
  2158. * when pulling a subquery into its parent.
  2159. *
  2160. * The idea is to evaluate the expression at (only) the ph_eval_at join level,
  2161. * then allow it to bubble up like a Var until the ph_needed join level.
  2162. * ph_needed has the same definition as attr_needed for a regular Var.
  2163. *
  2164. * The PlaceHolderVar's expression might contain LATERAL references to vars
  2165. * coming from outside its syntactic scope. If so, those rels are *not*
  2166. * included in ph_eval_at, but they are recorded in ph_lateral.
  2167. *
  2168. * Notice that when ph_eval_at is a join rather than a single baserel, the
  2169. * PlaceHolderInfo may create constraints on join order: the ph_eval_at join
  2170. * has to be formed below any outer joins that should null the PlaceHolderVar.
  2171. *
  2172. * We create a PlaceHolderInfo only after determining that the PlaceHolderVar
  2173. * is actually referenced in the plan tree, so that unreferenced placeholders
  2174. * don't result in unnecessary constraints on join order.
  2175. */
  2176. typedef struct PlaceHolderInfo
  2177. {
  2178. NodeTag type;
  2179. Index phid; /* ID for PH (unique within planner run) */
  2180. PlaceHolderVar *ph_var; /* copy of PlaceHolderVar tree */
  2181. Relids ph_eval_at; /* lowest level we can evaluate value at */
  2182. Relids ph_lateral; /* relids of contained lateral refs, if any */
  2183. Relids ph_needed; /* highest level the value is needed at */
  2184. int32 ph_width; /* estimated attribute width */
  2185. } PlaceHolderInfo;
  2186. /*
  2187. * This struct describes one potentially index-optimizable MIN/MAX aggregate
  2188. * function. MinMaxAggPath contains a list of these, and if we accept that
  2189. * path, the list is stored into root->minmax_aggs for use during setrefs.c.
  2190. */
  2191. typedef struct MinMaxAggInfo
  2192. {
  2193. NodeTag type;
  2194. Oid aggfnoid; /* pg_proc Oid of the aggregate */
  2195. Oid aggsortop; /* Oid of its sort operator */
  2196. Expr *target; /* expression we are aggregating on */
  2197. PlannerInfo *subroot; /* modified "root" for planning the subquery */
  2198. Path *path; /* access path for subquery */
  2199. Cost pathcost; /* estimated cost to fetch first row */
  2200. Param *param; /* param for subplan's output */
  2201. } MinMaxAggInfo;
  2202. /*
  2203. * At runtime, PARAM_EXEC slots are used to pass values around from one plan
  2204. * node to another. They can be used to pass values down into subqueries (for
  2205. * outer references in subqueries), or up out of subqueries (for the results
  2206. * of a subplan), or from a NestLoop plan node into its inner relation (when
  2207. * the inner scan is parameterized with values from the outer relation).
  2208. * The planner is responsible for assigning nonconflicting PARAM_EXEC IDs to
  2209. * the PARAM_EXEC Params it generates.
  2210. *
  2211. * Outer references are managed via root->plan_params, which is a list of
  2212. * PlannerParamItems. While planning a subquery, each parent query level's
  2213. * plan_params contains the values required from it by the current subquery.
  2214. * During create_plan(), we use plan_params to track values that must be
  2215. * passed from outer to inner sides of NestLoop plan nodes.
  2216. *
  2217. * The item a PlannerParamItem represents can be one of three kinds:
  2218. *
  2219. * A Var: the slot represents a variable of this level that must be passed
  2220. * down because subqueries have outer references to it, or must be passed
  2221. * from a NestLoop node to its inner scan. The varlevelsup value in the Var
  2222. * will always be zero.
  2223. *
  2224. * A PlaceHolderVar: this works much like the Var case, except that the
  2225. * entry is a PlaceHolderVar node with a contained expression. The PHV
  2226. * will have phlevelsup = 0, and the contained expression is adjusted
  2227. * to match in level.
  2228. *
  2229. * An Aggref (with an expression tree representing its argument): the slot
  2230. * represents an aggregate expression that is an outer reference for some
  2231. * subquery. The Aggref itself has agglevelsup = 0, and its argument tree
  2232. * is adjusted to match in level.
  2233. *
  2234. * Note: we detect duplicate Var and PlaceHolderVar parameters and coalesce
  2235. * them into one slot, but we do not bother to do that for Aggrefs.
  2236. * The scope of duplicate-elimination only extends across the set of
  2237. * parameters passed from one query level into a single subquery, or for
  2238. * nestloop parameters across the set of nestloop parameters used in a single
  2239. * query level. So there is no possibility of a PARAM_EXEC slot being used
  2240. * for conflicting purposes.
  2241. *
  2242. * In addition, PARAM_EXEC slots are assigned for Params representing outputs
  2243. * from subplans (values that are setParam items for those subplans). These
  2244. * IDs need not be tracked via PlannerParamItems, since we do not need any
  2245. * duplicate-elimination nor later processing of the represented expressions.
  2246. * Instead, we just record the assignment of the slot number by appending to
  2247. * root->glob->paramExecTypes.
  2248. */
  2249. typedef struct PlannerParamItem
  2250. {
  2251. NodeTag type;
  2252. Node *item; /* the Var, PlaceHolderVar, or Aggref */
  2253. int paramId; /* its assigned PARAM_EXEC slot number */
  2254. } PlannerParamItem;
  2255. /*
  2256. * When making cost estimates for a SEMI/ANTI/inner_unique join, there are
  2257. * some correction factors that are needed in both nestloop and hash joins
  2258. * to account for the fact that the executor can stop scanning inner rows
  2259. * as soon as it finds a match to the current outer row. These numbers
  2260. * depend only on the selected outer and inner join relations, not on the
  2261. * particular paths used for them, so it's worthwhile to calculate them
  2262. * just once per relation pair not once per considered path. This struct
  2263. * is filled by compute_semi_anti_join_factors and must be passed along
  2264. * to the join cost estimation functions.
  2265. *
  2266. * outer_match_frac is the fraction of the outer tuples that are
  2267. * expected to have at least one match.
  2268. * match_count is the average number of matches expected for
  2269. * outer tuples that have at least one match.
  2270. */
  2271. typedef struct SemiAntiJoinFactors
  2272. {
  2273. Selectivity outer_match_frac;
  2274. Selectivity match_count;
  2275. } SemiAntiJoinFactors;
  2276. /*
  2277. * Struct for extra information passed to subroutines of add_paths_to_joinrel
  2278. *
  2279. * restrictlist contains all of the RestrictInfo nodes for restriction
  2280. * clauses that apply to this join
  2281. * mergeclause_list is a list of RestrictInfo nodes for available
  2282. * mergejoin clauses in this join
  2283. * inner_unique is true if each outer tuple provably matches no more
  2284. * than one inner tuple
  2285. * sjinfo is extra info about special joins for selectivity estimation
  2286. * semifactors is as shown above (only valid for SEMI/ANTI/inner_unique joins)
  2287. * param_source_rels are OK targets for parameterization of result paths
  2288. */
  2289. typedef struct JoinPathExtraData
  2290. {
  2291. List *restrictlist;
  2292. List *mergeclause_list;
  2293. bool inner_unique;
  2294. SpecialJoinInfo *sjinfo;
  2295. SemiAntiJoinFactors semifactors;
  2296. Relids param_source_rels;
  2297. } JoinPathExtraData;
  2298. /*
  2299. * Various flags indicating what kinds of grouping are possible.
  2300. *
  2301. * GROUPING_CAN_USE_SORT should be set if it's possible to perform
  2302. * sort-based implementations of grouping. When grouping sets are in use,
  2303. * this will be true if sorting is potentially usable for any of the grouping
  2304. * sets, even if it's not usable for all of them.
  2305. *
  2306. * GROUPING_CAN_USE_HASH should be set if it's possible to perform
  2307. * hash-based implementations of grouping.
  2308. *
  2309. * GROUPING_CAN_PARTIAL_AGG should be set if the aggregation is of a type
  2310. * for which we support partial aggregation (not, for example, grouping sets).
  2311. * It says nothing about parallel-safety or the availability of suitable paths.
  2312. */
  2313. #define GROUPING_CAN_USE_SORT 0x0001
  2314. #define GROUPING_CAN_USE_HASH 0x0002
  2315. #define GROUPING_CAN_PARTIAL_AGG 0x0004
  2316. /*
  2317. * What kind of partitionwise aggregation is in use?
  2318. *
  2319. * PARTITIONWISE_AGGREGATE_NONE: Not used.
  2320. *
  2321. * PARTITIONWISE_AGGREGATE_FULL: Aggregate each partition separately, and
  2322. * append the results.
  2323. *
  2324. * PARTITIONWISE_AGGREGATE_PARTIAL: Partially aggregate each partition
  2325. * separately, append the results, and then finalize aggregation.
  2326. */
  2327. typedef enum
  2328. {
  2329. PARTITIONWISE_AGGREGATE_NONE,
  2330. PARTITIONWISE_AGGREGATE_FULL,
  2331. PARTITIONWISE_AGGREGATE_PARTIAL
  2332. } PartitionwiseAggregateType;
  2333. /*
  2334. * Struct for extra information passed to subroutines of create_grouping_paths
  2335. *
  2336. * flags indicating what kinds of grouping are possible.
  2337. * partial_costs_set is true if the agg_partial_costs and agg_final_costs
  2338. * have been initialized.
  2339. * agg_partial_costs gives partial aggregation costs.
  2340. * agg_final_costs gives finalization costs.
  2341. * target_parallel_safe is true if target is parallel safe.
  2342. * havingQual gives list of quals to be applied after aggregation.
  2343. * targetList gives list of columns to be projected.
  2344. * patype is the type of partitionwise aggregation that is being performed.
  2345. */
  2346. typedef struct
  2347. {
  2348. /* Data which remains constant once set. */
  2349. int flags;
  2350. bool partial_costs_set;
  2351. AggClauseCosts agg_partial_costs;
  2352. AggClauseCosts agg_final_costs;
  2353. /* Data which may differ across partitions. */
  2354. bool target_parallel_safe;
  2355. Node *havingQual;
  2356. List *targetList;
  2357. PartitionwiseAggregateType patype;
  2358. } GroupPathExtraData;
  2359. /*
  2360. * Struct for extra information passed to subroutines of grouping_planner
  2361. *
  2362. * limit_needed is true if we actually need a Limit plan node.
  2363. * limit_tuples is an estimated bound on the number of output tuples,
  2364. * or -1 if no LIMIT or couldn't estimate.
  2365. * count_est and offset_est are the estimated values of the LIMIT and OFFSET
  2366. * expressions computed by preprocess_limit() (see comments for
  2367. * preprocess_limit() for more information).
  2368. */
  2369. typedef struct
  2370. {
  2371. bool limit_needed;
  2372. Cardinality limit_tuples;
  2373. int64 count_est;
  2374. int64 offset_est;
  2375. } FinalPathExtraData;
  2376. /*
  2377. * For speed reasons, cost estimation for join paths is performed in two
  2378. * phases: the first phase tries to quickly derive a lower bound for the
  2379. * join cost, and then we check if that's sufficient to reject the path.
  2380. * If not, we come back for a more refined cost estimate. The first phase
  2381. * fills a JoinCostWorkspace struct with its preliminary cost estimates
  2382. * and possibly additional intermediate values. The second phase takes
  2383. * these values as inputs to avoid repeating work.
  2384. *
  2385. * (Ideally we'd declare this in cost.h, but it's also needed in pathnode.h,
  2386. * so seems best to put it here.)
  2387. */
  2388. typedef struct JoinCostWorkspace
  2389. {
  2390. /* Preliminary cost estimates --- must not be larger than final ones! */
  2391. Cost startup_cost; /* cost expended before fetching any tuples */
  2392. Cost total_cost; /* total cost (assuming all tuples fetched) */
  2393. /* Fields below here should be treated as private to costsize.c */
  2394. Cost run_cost; /* non-startup cost components */
  2395. /* private for cost_nestloop code */
  2396. Cost inner_run_cost; /* also used by cost_mergejoin code */
  2397. Cost inner_rescan_run_cost;
  2398. /* private for cost_mergejoin code */
  2399. Cardinality outer_rows;
  2400. Cardinality inner_rows;
  2401. Cardinality outer_skip_rows;
  2402. Cardinality inner_skip_rows;
  2403. /* private for cost_hashjoin code */
  2404. int numbuckets;
  2405. int numbatches;
  2406. Cardinality inner_rows_total;
  2407. } JoinCostWorkspace;
  2408. /*
  2409. * AggInfo holds information about an aggregate that needs to be computed.
  2410. * Multiple Aggrefs in a query can refer to the same AggInfo by having the
  2411. * same 'aggno' value, so that the aggregate is computed only once.
  2412. */
  2413. typedef struct AggInfo
  2414. {
  2415. /*
  2416. * Link to an Aggref expr this state value is for.
  2417. *
  2418. * There can be multiple identical Aggref's sharing the same per-agg. This
  2419. * points to the first one of them.
  2420. */
  2421. Aggref *representative_aggref;
  2422. int transno;
  2423. /*
  2424. * "shareable" is false if this agg cannot share state values with other
  2425. * aggregates because the final function is read-write.
  2426. */
  2427. bool shareable;
  2428. /* Oid of the final function or InvalidOid */
  2429. Oid finalfn_oid;
  2430. } AggInfo;
  2431. /*
  2432. * AggTransInfo holds information about transition state that is used by one
  2433. * or more aggregates in the query. Multiple aggregates can share the same
  2434. * transition state, if they have the same inputs and the same transition
  2435. * function. Aggrefs that share the same transition info have the same
  2436. * 'aggtransno' value.
  2437. */
  2438. typedef struct AggTransInfo
  2439. {
  2440. List *args;
  2441. Expr *aggfilter;
  2442. /* Oid of the state transition function */
  2443. Oid transfn_oid;
  2444. /* Oid of the serialization function or InvalidOid */
  2445. Oid serialfn_oid;
  2446. /* Oid of the deserialization function or InvalidOid */
  2447. Oid deserialfn_oid;
  2448. /* Oid of the combine function or InvalidOid */
  2449. Oid combinefn_oid;
  2450. /* Oid of state value's datatype */
  2451. Oid aggtranstype;
  2452. int32 aggtranstypmod;
  2453. int transtypeLen;
  2454. bool transtypeByVal;
  2455. int32 aggtransspace;
  2456. /*
  2457. * initial value from pg_aggregate entry
  2458. */
  2459. Datum initValue;
  2460. bool initValueIsNull;
  2461. } AggTransInfo;
  2462. #endif /* PATHNODES_H */