1. 13 3月, 2016 1 次提交
  2. 08 3月, 2016 1 次提交
    • T
      Make the upper part of the planner work by generating and comparing Paths. · 3fc6e2d7
      Tom Lane 提交于
      I've been saying we needed to do this for more than five years, and here it
      finally is.  This patch removes the ever-growing tangle of spaghetti logic
      that grouping_planner() used to use to try to identify the best plan for
      post-scan/join query steps.  Now, there is (nearly) independent
      consideration of each execution step, and entirely separate construction of
      Paths to represent each of the possible ways to do that step.  We choose
      the best Path or set of Paths using the same add_path() logic that's been
      used inside query_planner() for years.
      
      In addition, this patch removes the old restriction that subquery_planner()
      could return only a single Plan.  It now returns a RelOptInfo containing a
      set of Paths, just as query_planner() does, and the parent query level can
      use each of those Paths as the basis of a SubqueryScanPath at its level.
      This allows finding some optimizations that we missed before, wherein a
      subquery was capable of returning presorted data and thereby avoiding a
      sort in the parent level, making the overall cost cheaper even though
      delivering sorted output was not the cheapest plan for the subquery in
      isolation.  (A couple of regression test outputs change in consequence of
      that.  However, there is very little change in visible planner behavior
      overall, because the point of this patch is not to get immediate planning
      benefits but to create the infrastructure for future improvements.)
      
      There is a great deal left to do here.  This patch unblocks a lot of
      planner work that was basically impractical in the old code structure,
      such as allowing FDWs to implement remote aggregation, or rewriting
      plan_set_operations() to allow consideration of multiple implementation
      orders for set operations.  (The latter will likely require a full
      rewrite of plan_set_operations(); what I've done here is only to fix it
      to return Paths not Plans.)  I have also left unfinished some localized
      refactoring in createplan.c and planner.c, because it was not necessary
      to get this patch to a working state.
      
      Thanks to Robert Haas, David Rowley, and Amit Kapila for review.
      3fc6e2d7
  3. 08 2月, 2016 1 次提交
    • R
      Introduce a new GUC force_parallel_mode for testing purposes. · 7c944bd9
      Robert Haas 提交于
      When force_parallel_mode = true, we enable the parallel mode restrictions
      for all queries for which this is believed to be safe.  For the subset of
      those queries believed to be safe to run entirely within a worker, we spin
      up a worker and run the query there instead of running it in the
      original process.  When force_parallel_mode = regress, make additional
      changes to allow the regression tests to run cleanly even though parallel
      workers have been injected under the hood.
      
      Taken together, this facilitates both better user testing and better
      regression testing of the parallelism code.
      
      Robert Haas, with help from Amit Kapila and Rushabh Lathia.
      7c944bd9
  4. 21 1月, 2016 1 次提交
    • R
      Support multi-stage aggregation. · a7de3dc5
      Robert Haas 提交于
      Aggregate nodes now have two new modes: a "partial" mode where they
      output the unfinalized transition state, and a "finalize" mode where
      they accept unfinalized transition states rather than individual
      values as input.
      
      These new modes are not used anywhere yet, but they will be necessary
      for parallel aggregation.  The infrastructure also figures to be
      useful for cases where we want to aggregate local data and remote
      data via the FDW interface, and want to bring back partial aggregates
      from the remote side that can then be combined with locally generated
      partial aggregates to produce the final value.  It may also be useful
      even when neither FDWs nor parallelism are in play, as explained in
      the comments in nodeAgg.c.
      
      David Rowley and Simon Riggs, reviewed by KaiGai Kohei, Heikki
      Linnakangas, Haribabu Kommi, and me.
      a7de3dc5
  5. 03 1月, 2016 1 次提交
  6. 09 12月, 2015 1 次提交
    • R
      Allow foreign and custom joins to handle EvalPlanQual rechecks. · 385f337c
      Robert Haas 提交于
      Commit e7cb7ee1 provided basic
      infrastructure for allowing a foreign data wrapper or custom scan
      provider to replace a join of one or more tables with a scan.
      However, this infrastructure failed to take into account the need
      for possible EvalPlanQual rechecks, and ExecScanFetch would fail
      an assertion (or just overwrite memory) if such a check was attempted
      for a plan containing a pushed-down join.  To fix, adjust the EPQ
      machinery to skip some processing steps when scanrelid == 0, making
      those the responsibility of scan's recheck method, which also has
      the responsibility in this case of correctly populating the relevant
      slot.
      
      To allow foreign scans to gain control in the right place to make
      use of this new facility, add a new, optional RecheckForeignScan
      method.  Also, allow a foreign scan to have a child plan, which can
      be used to correctly populate the slot (or perhaps for something
      else, but this is the only use currently envisioned).
      
      KaiGai Kohei, reviewed by Robert Haas, Etsuro Fujita, and Kyotaro
      Horiguchi.
      385f337c
  7. 16 10月, 2015 1 次提交
    • R
      Allow FDWs to push down quals without breaking EvalPlanQual rechecks. · 5fc4c26d
      Robert Haas 提交于
      This fixes a long-standing bug which was discovered while investigating
      the interaction between the new join pushdown code and the EvalPlanQual
      machinery: if a ForeignScan appears on the inner side of a paramaterized
      nestloop, an EPQ recheck would re-return the original tuple even if
      it no longer satisfied the pushed-down quals due to changed parameter
      values.
      
      This fix adds a new member to ForeignScan and ForeignScanState and a
      new argument to make_foreignscan, and requires changes to FDWs which
      push down quals to populate that new argument with a list of quals they
      have chosen to push down.  Therefore, I'm only back-patching to 9.5,
      even though the bug is not new in 9.5.
      
      Etsuro Fujita, reviewed by me and by Kyotaro Horiguchi.
      5fc4c26d
  8. 16 5月, 2015 1 次提交
    • A
      Support GROUPING SETS, CUBE and ROLLUP. · f3d31185
      Andres Freund 提交于
      This SQL standard functionality allows to aggregate data by different
      GROUP BY clauses at once. Each grouping set returns rows with columns
      grouped by in other sets set to NULL.
      
      This could previously be achieved by doing each grouping as a separate
      query, conjoined by UNION ALLs. Besides being considerably more concise,
      grouping sets will in many cases be faster, requiring only one scan over
      the underlying data.
      
      The current implementation of grouping sets only supports using sorting
      for input. Individual sets that share a sort order are computed in one
      pass. If there are sets that don't share a sort order, additional sort &
      aggregation steps are performed. These additional passes are sourced by
      the previous sort step; thus avoiding repeated scans of the source data.
      
      The code is structured in a way that adding support for purely using
      hash aggregation or a mix of hashing and sorting is possible. Sorting
      was chosen to be supported first, as it is the most generic method of
      implementation.
      
      Instead of, as in an earlier versions of the patch, representing the
      chain of sort and aggregation steps as full blown planner and executor
      nodes, all but the first sort are performed inside the aggregation node
      itself. This avoids the need to do some unusual gymnastics to handle
      having to return aggregated and non-aggregated tuples from underlying
      nodes, as well as having to shut down underlying nodes early to limit
      memory usage.  The optimizer still builds Sort/Agg node to describe each
      phase, but they're not part of the plan tree, but instead additional
      data for the aggregation node. They're a convenient and preexisting way
      to describe aggregation and sorting.  The first (and possibly only) sort
      step is still performed as a separate execution step. That retains
      similarity with existing group by plans, makes rescans fairly simple,
      avoids very deep plans (leading to slow explains) and easily allows to
      avoid the sorting step if the underlying data is sorted by other means.
      
      A somewhat ugly side of this patch is having to deal with a grammar
      ambiguity between the new CUBE keyword and the cube extension/functions
      named cube (and rollup). To avoid breaking existing deployments of the
      cube extension it has not been renamed, neither has cube been made a
      reserved keyword. Instead precedence hacking is used to make GROUP BY
      cube(..) refer to the CUBE grouping sets feature, and not the function
      cube(). To actually group by a function cube(), unlikely as that might
      be, the function name has to be quoted.
      
      Needs a catversion bump because stored rules may change.
      
      Author: Andrew Gierth and Atri Sharma, with contributions from Andres Freund
      Reviewed-By: Andres Freund, Noah Misch, Tom Lane, Svenne Krap, Tomas
          Vondra, Erik Rijkers, Marti Raudsepp, Pavel Stehule
      Discussion: CAOeZVidmVRe2jU6aMk_5qkxnB7dfmPROzM7Ur8JPW5j8Y5X-Lw@mail.gmail.com
      f3d31185
  9. 11 5月, 2015 1 次提交
    • T
      Code review for foreign/custom join pushdown patch. · 1a8a4e5c
      Tom Lane 提交于
      Commit e7cb7ee1 included some design
      decisions that seem pretty questionable to me, and there was quite a lot
      of stuff not to like about the documentation and comments.  Clean up
      as follows:
      
      * Consider foreign joins only between foreign tables on the same server,
      rather than between any two foreign tables with the same underlying FDW
      handler function.  In most if not all cases, the FDW would simply have had
      to apply the same-server restriction itself (far more expensively, both for
      lack of caching and because it would be repeated for each combination of
      input sub-joins), or else risk nasty bugs.  Anyone who's really intent on
      doing something outside this restriction can always use the
      set_join_pathlist_hook.
      
      * Rename fdw_ps_tlist/custom_ps_tlist to fdw_scan_tlist/custom_scan_tlist
      to better reflect what they're for, and allow these custom scan tlists
      to be used even for base relations.
      
      * Change make_foreignscan() API to include passing the fdw_scan_tlist
      value, since the FDW is required to set that.  Backwards compatibility
      doesn't seem like an adequate reason to expect FDWs to set it in some
      ad-hoc extra step, and anyway existing FDWs can just pass NIL.
      
      * Change the API of path-generating subroutines of add_paths_to_joinrel,
      and in particular that of GetForeignJoinPaths and set_join_pathlist_hook,
      so that various less-used parameters are passed in a struct rather than
      as separate parameter-list entries.  The objective here is to reduce the
      probability that future additions to those parameter lists will result in
      source-level API breaks for users of these hooks.  It's possible that this
      is even a small win for the core code, since most CPU architectures can't
      pass more than half a dozen parameters efficiently anyway.  I kept root,
      joinrel, outerrel, innerrel, and jointype as separate parameters to reduce
      code churn in joinpath.c --- in particular, putting jointype into the
      struct would have been problematic because of the subroutines' habit of
      changing their local copies of that variable.
      
      * Avoid ad-hocery in ExecAssignScanProjectionInfo.  It was probably all
      right for it to know about IndexOnlyScan, but if the list is to grow
      we should refactor the knowledge out to the callers.
      
      * Restore nodeForeignscan.c's previous use of the relcache to avoid
      extra GetFdwRoutine lookups for base-relation scans.
      
      * Lots of cleanup of documentation and missed comments.  Re-order some
      code additions into more logical places.
      1a8a4e5c
  10. 08 5月, 2015 1 次提交
    • A
      Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE. · 168d5805
      Andres Freund 提交于
      The newly added ON CONFLICT clause allows to specify an alternative to
      raising a unique or exclusion constraint violation error when inserting.
      ON CONFLICT refers to constraints that can either be specified using a
      inference clause (by specifying the columns of a unique constraint) or
      by naming a unique or exclusion constraint.  DO NOTHING avoids the
      constraint violation, without touching the pre-existing row.  DO UPDATE
      SET ... [WHERE ...] updates the pre-existing tuple, and has access to
      both the tuple proposed for insertion and the existing tuple; the
      optional WHERE clause can be used to prevent an update from being
      executed.  The UPDATE SET and WHERE clauses have access to the tuple
      proposed for insertion using the "magic" EXCLUDED alias, and to the
      pre-existing tuple using the table name or its alias.
      
      This feature is often referred to as upsert.
      
      This is implemented using a new infrastructure called "speculative
      insertion". It is an optimistic variant of regular insertion that first
      does a pre-check for existing tuples and then attempts an insert.  If a
      violating tuple was inserted concurrently, the speculatively inserted
      tuple is deleted and a new attempt is made.  If the pre-check finds a
      matching tuple the alternative DO NOTHING or DO UPDATE action is taken.
      If the insertion succeeds without detecting a conflict, the tuple is
      deemed inserted.
      
      To handle the possible ambiguity between the excluded alias and a table
      named excluded, and for convenience with long relation names, INSERT
      INTO now can alias its target table.
      
      Bumps catversion as stored rules change.
      
      Author: Peter Geoghegan, with significant contributions from Heikki
          Linnakangas and Andres Freund. Testing infrastructure by Jeff Janes.
      Reviewed-By: Heikki Linnakangas, Andres Freund, Robert Haas, Simon Riggs,
          Dean Rasheed, Stephen Frost and many others.
      168d5805
  11. 01 5月, 2015 1 次提交
    • R
      Allow FDWs and custom scan providers to replace joins with scans. · e7cb7ee1
      Robert Haas 提交于
      Foreign data wrappers can use this capability for so-called "join
      pushdown"; that is, instead of executing two separate foreign scans
      and then joining the results locally, they can generate a path which
      performs the join on the remote server and then is scanned locally.
      This commit does not extend postgres_fdw to take advantage of this
      capability; it just provides the infrastructure.
      
      Custom scan providers can use this in a similar way.  Previously,
      it was only possible for a custom scan provider to scan a single
      relation.  Now, it can scan an entire join tree, provided of course
      that it knows how to produce the same results that the join would
      have produced if executed normally.
      
      KaiGai Kohei, reviewed by Shigeru Hanada, Ashutosh Bapat, and me.
      e7cb7ee1
  12. 18 2月, 2015 1 次提交
    • T
      Fix EXPLAIN output for cases where parent table is excluded by constraints. · abe45a9b
      Tom Lane 提交于
      The previous coding in EXPLAIN always labeled a ModifyTable node with the
      name of the target table affected by its first child plan.  When originally
      written, this was necessarily the parent table of the inheritance tree,
      so everything was unconfusing.  But when we added NO INHERIT constraints,
      it became possible for the parent table to be deleted from the plan by
      constraint exclusion while still leaving child tables present.  This led to
      the ModifyTable plan node being labeled with the first surviving child,
      which was deemed confusing.  Fix it by retaining the parent table's RT
      index in a new field in ModifyTable.
      
      Etsuro Fujita, reviewed by Ashutosh Bapat and myself
      abe45a9b
  13. 07 1月, 2015 1 次提交
  14. 22 11月, 2014 1 次提交
    • T
      Rearrange CustomScan API. · 44777040
      Tom Lane 提交于
      Make it work more like FDW plans do: instead of assuming that there are
      expressions in a CustomScan plan node that the core code doesn't know
      about, insist that all subexpressions that need planner attention be in
      a "custom_exprs" list in the Plan representation.  (Of course, the
      custom plugin can break the list apart again at executor initialization.)
      This lets us revert the parts of the patch that exposed setrefs.c and
      subselect.c processing to the outside world.
      
      Also revert the GetSpecialCustomVar stuff in ruleutils.c; that concept
      may work in future, but it's far from fully baked right now.
      44777040
  15. 21 11月, 2014 1 次提交
    • T
      Initial code review for CustomScan patch. · a34fa8ee
      Tom Lane 提交于
      Get rid of the pernicious entanglement between planner and executor headers
      introduced by commit 0b03e595.
      
      Also, rearrange the CustomFoo struct/typedef definitions so that all the
      typedef names are seen as used by the compiler.  Without this pgindent
      will mess things up a bit, which is not so important perhaps, but it also
      removes a bizarre discrepancy between the declaration arrangement used for
      CustomExecMethods and that used for CustomScanMethods and
      CustomPathMethods.
      
      Clean up the commentary around ExecSupportsMarkRestore to reflect the
      rather large change in its API.
      
      Const-ify register_custom_path_provider's argument.  This necessitates
      casting away const in the function, but that seems better than forcing
      callers of the function to do so (or else not const-ify their method
      pointer structs, which was sort of the whole point).
      
      De-export fix_expr_common.  I don't like the exporting of fix_scan_expr
      or replace_nestloop_params either, but this one surely has got little
      excuse.
      a34fa8ee
  16. 08 11月, 2014 1 次提交
    • R
      Introduce custom path and scan providers. · 0b03e595
      Robert Haas 提交于
      This allows extension modules to define their own methods for
      scanning a relation, and get the core code to use them.  It's
      unclear as yet how much use this capability will find, but we
      won't find out if we never commit it.
      
      KaiGai Kohei, reviewed at various times and in various levels
      of detail by Shigeru Hanada, Tom Lane, Andres Freund, Álvaro
      Herrera, and myself.
      0b03e595
  17. 19 9月, 2014 1 次提交
    • S
      Row-Level Security Policies (RLS) · 491c029d
      Stephen Frost 提交于
      Building on the updatable security-barrier views work, add the
      ability to define policies on tables to limit the set of rows
      which are returned from a query and which are allowed to be added
      to a table.  Expressions defined by the policy for filtering are
      added to the security barrier quals of the query, while expressions
      defined to check records being added to a table are added to the
      with-check options of the query.
      
      New top-level commands are CREATE/ALTER/DROP POLICY and are
      controlled by the table owner.  Row Security is able to be enabled
      and disabled by the owner on a per-table basis using
      ALTER TABLE .. ENABLE/DISABLE ROW SECURITY.
      
      Per discussion, ROW SECURITY is disabled on tables by default and
      must be enabled for policies on the table to be used.  If no
      policies exist on a table with ROW SECURITY enabled, a default-deny
      policy is used and no records will be visible.
      
      By default, row security is applied at all times except for the
      table owner and the superuser.  A new GUC, row_security, is added
      which can be set to ON, OFF, or FORCE.  When set to FORCE, row
      security will be applied even for the table owner and superusers.
      When set to OFF, row security will be disabled when allowed and an
      error will be thrown if the user does not have rights to bypass row
      security.
      
      Per discussion, pg_dump sets row_security = OFF by default to ensure
      that exports and backups will have all data in the table or will
      error if there are insufficient privileges to bypass row security.
      A new option has been added to pg_dump, --enable-row-security, to
      ask pg_dump to export with row security enabled.
      
      A new role capability, BYPASSRLS, which can only be set by the
      superuser, is added to allow other users to be able to bypass row
      security using row_security = OFF.
      
      Many thanks to the various individuals who have helped with the
      design, particularly Robert Haas for his feedback.
      
      Authors include Craig Ringer, KaiGai Kohei, Adam Brightwell, Dean
      Rasheed, with additional changes and rework by me.
      
      Reviewers have included all of the above, Greg Smith,
      Jeff McCormick, and Robert Haas.
      491c029d
  18. 16 7月, 2014 1 次提交
    • T
      Allow join removal in some cases involving a left join to a subquery. · f15821ee
      Tom Lane 提交于
      We can remove a left join to a relation if the relation's output is
      provably distinct for the columns involved in the join clause (considering
      only equijoin clauses) and the relation supplies no variables needed above
      the join.  Previously, the join removal logic could only prove distinctness
      by reference to unique indexes of a table.  This patch extends the logic
      to consider subquery relations, wherein distinctness might be proven by
      reference to GROUP BY, DISTINCT, etc.
      
      We actually already had some code to check that a subquery's output was
      provably distinct, but it was hidden inside pathnode.c; which was a pretty
      bad place for it really, since that file is mostly boilerplate Path
      construction and comparison.  Move that code to analyzejoins.c, which is
      arguably a more appropriate location, and is certainly the site of the
      new usage for it.
      
      David Rowley, reviewed by Simon Riggs
      f15821ee
  19. 08 1月, 2014 1 次提交
  20. 06 8月, 2013 1 次提交
    • T
      Simplify query_planner's API by having it return the top-level RelOptInfo. · 3ced8837
      Tom Lane 提交于
      Formerly, query_planner returned one or possibly two Paths for the topmost
      join relation, so that grouping_planner didn't see the join RelOptInfo
      (at least not directly; it didn't have any hesitation about examining
      cheapest_path->parent, though).  However, correct selection of the Paths
      involved a significant amount of coupling between query_planner and
      grouping_planner, a problem which has gotten worse over time.  It seems
      best to give up on this API choice and instead return the topmost
      RelOptInfo explicitly.  Then grouping_planner can pull out the Paths it
      wants from the rel's path list.  In this way we can remove all knowledge
      of grouping behaviors from query_planner.
      
      The only real benefit of the old way is that in the case of an empty
      FROM clause, we never made any RelOptInfos at all, just a Path.  Now
      we have to gin up a dummy RelOptInfo to represent the empty FROM clause.
      That's not a very big deal though.
      
      While at it, simplify query_planner's API a bit more by having the caller
      set up root->tuple_fraction and root->limit_tuples, rather than passing
      those values as separate parameters.  Since query_planner no longer does
      anything with either value, requiring it to fill the PlannerInfo fields
      seemed pretty arbitrary.
      
      This patch just rearranges code; it doesn't (intentionally) change any
      behaviors.  Followup patches will do more interesting things.
      3ced8837
  21. 19 7月, 2013 1 次提交
    • S
      WITH CHECK OPTION support for auto-updatable VIEWs · 4cbe3ac3
      Stephen Frost 提交于
      For simple views which are automatically updatable, this patch allows
      the user to specify what level of checking should be done on records
      being inserted or updated.  For 'LOCAL CHECK', new tuples are validated
      against the conditionals of the view they are being inserted into, while
      for 'CASCADED CHECK' the new tuples are validated against the
      conditionals for all views involved (from the top down).
      
      This option is part of the SQL specification.
      
      Dean Rasheed, reviewed by Pavel Stehule
      4cbe3ac3
  22. 30 4月, 2013 1 次提交
    • T
      Postpone creation of pathkeys lists to fix bug #8049. · db9f0e1d
      Tom Lane 提交于
      This patch gets rid of the concept of, and infrastructure for,
      non-canonical PathKeys; we now only ever create canonical pathkey lists.
      
      The need for non-canonical pathkeys came from the desire to have
      grouping_planner initialize query_pathkeys and related pathkey lists before
      calling query_planner.  However, since query_planner didn't actually *do*
      anything with those lists before they'd been made canonical, we can get rid
      of the whole mess by just not creating the lists at all until the point
      where we formerly canonicalized them.
      
      There are several ways in which we could implement that without making
      query_planner itself deal with grouping/sorting features (which are
      supposed to be the province of grouping_planner).  I chose to add a
      callback function to query_planner's API; other alternatives would have
      required adding more fields to PlannerInfo, which while not bad in itself
      would create an ABI break for planner-related plugins in the 9.2 release
      series.  This still breaks ABI for anything that calls query_planner
      directly, but it seems somewhat unlikely that there are any such plugins.
      
      I had originally conceived of this change as merely a step on the way to
      fixing bug #8049 from Teun Hoogendoorn; but it turns out that this fixes
      that bug all by itself, as per the added regression test.  The reason is
      that now get_eclass_for_sort_expr is adding the ORDER BY expression at the
      end of EquivalenceClass creation not the start, and so anything that is in
      a multi-member EquivalenceClass has already been created with correct
      em_nullable_relids.  I am suspicious that there are related scenarios in
      which we still need to teach get_eclass_for_sort_expr to compute correct
      nullable_relids, but am not eager to risk destabilizing either 9.2 or 9.3
      to fix bugs that are only hypothetical.  So for the moment, do this and
      stop here.
      
      Back-patch to 9.2 but not to earlier branches, since they don't exhibit
      this bug for lack of join-clause-movement logic that depends on
      em_nullable_relids being correct.  (We might have to revisit that choice
      if any related bugs turn up.)  In 9.2, don't change the signature of
      make_pathkeys_for_sortclauses nor remove canonicalize_pathkeys, so as
      not to risk more plugin breakage than we have to.
      db9f0e1d
  23. 11 3月, 2013 1 次提交
    • T
      Support writable foreign tables. · 21734d2f
      Tom Lane 提交于
      This patch adds the core-system infrastructure needed to support updates
      on foreign tables, and extends contrib/postgres_fdw to allow updates
      against remote Postgres servers.  There's still a great deal of room for
      improvement in optimization of remote updates, but at least there's basic
      functionality there now.
      
      KaiGai Kohei, reviewed by Alexander Korotkov and Laurenz Albe, and rather
      heavily revised by Tom Lane.
      21734d2f
  24. 02 1月, 2013 1 次提交
  25. 19 10月, 2012 1 次提交
    • T
      Fix planning of non-strict equivalence clauses above outer joins. · 72a4231f
      Tom Lane 提交于
      If a potential equivalence clause references a variable from the nullable
      side of an outer join, the planner needs to take care that derived clauses
      are not pushed to below the outer join; else they may use the wrong value
      for the variable.  (The problem arises only with non-strict clauses, since
      if an upper clause can be proven strict then the outer join will get
      simplified to a plain join.)  The planner attempted to prevent this type
      of error by checking that potential equivalence clauses aren't
      outerjoin-delayed as a whole, but actually we have to check each side
      separately, since the two sides of the clause will get moved around
      separately if it's treated as an equivalence.  Bugs of this type can be
      demonstrated as far back as 7.4, even though releases before 8.3 had only
      a very ad-hoc notion of equivalence clauses.
      
      In addition, we neglected to account for the possibility that such clauses
      might have nonempty nullable_relids even when not outerjoin-delayed; so the
      equivalence-class machinery lacked logic to compute correct nullable_relids
      values for clauses it constructs.  This oversight was harmless before 9.2
      because we were only using RestrictInfo.nullable_relids for OR clauses;
      but as of 9.2 it could result in pushing constructed equivalence clauses
      to incorrect places.  (This accounts for bug #7604 from Bill MacArthur.)
      
      Fix the first problem by adding a new test check_equivalence_delay() in
      distribute_qual_to_rels, and fix the second one by adding code in
      equivclass.c and called functions to set correct nullable_relids for
      generated clauses.  Although I believe the second part of this is not
      currently necessary before 9.2, I chose to back-patch it anyway, partly to
      keep the logic similar across branches and partly because it seems possible
      we might find other reasons why we need valid values of nullable_relids in
      the older branches.
      
      Add regression tests illustrating these problems.  In 9.0 and up, also
      add test cases checking that we can push constants through outer joins,
      since we've broken that optimization before and I nearly broke it again
      with an overly simplistic patch for this problem.
      72a4231f
  26. 27 8月, 2012 1 次提交
    • T
      Fix up planner infrastructure to support LATERAL properly. · 9ff79b9d
      Tom Lane 提交于
      This patch takes care of a number of problems having to do with failure
      to choose valid join orders and incorrect handling of lateral references
      pulled up from subqueries.  Notable changes:
      
      * Add a LateralJoinInfo data structure similar to SpecialJoinInfo, to
      represent join ordering constraints created by lateral references.
      (I first considered extending the SpecialJoinInfo structure, but the
      semantics are different enough that a separate data structure seems
      better.)  Extend join_is_legal() and related functions to prevent trying
      to form unworkable joins, and to ensure that we will consider joins that
      satisfy lateral references even if the joins would be clauseless.
      
      * Fill in the infrastructure needed for the last few types of relation scan
      paths to support parameterization.  We'd have wanted this eventually
      anyway, but it is necessary now because a relation that gets pulled up out
      of a UNION ALL subquery may acquire a reltargetlist containing lateral
      references, meaning that its paths *have* to be parameterized whether or
      not we have any code that can push join quals down into the scan.
      
      * Compute data about lateral references early in query_planner(), and save
      in RelOptInfo nodes, to avoid repetitive calculations later.
      
      * Assorted corner-case bug fixes.
      
      There's probably still some bugs left, but this is a lot closer to being
      real than it was before.
      9ff79b9d
  27. 26 4月, 2012 1 次提交
    • T
      Fix planner's handling of RETURNING lists in writable CTEs. · 9fa82c98
      Tom Lane 提交于
      setrefs.c failed to do "rtoffset" adjustment of Vars in RETURNING lists,
      which meant they were left with the wrong varnos when the RETURNING list
      was in a subquery.  That was never possible before writable CTEs, of
      course, but now it's broken.  The executor fails to notice any problem
      because ExecEvalVar just references the ecxt_scantuple for any normal
      varno; but EXPLAIN breaks when the varno is wrong, as illustrated in a
      recent complaint from Bartosz Dmytrak.
      
      Since the eventual rtoffset of the subquery is not known at the time
      we are preparing its plan node, the previous scheme of executing
      set_returning_clause_references() at that time cannot handle this
      adjustment.  Fortunately, it turns out that we don't really need to do it
      that way, because all the needed information is available during normal
      setrefs.c execution; we just have to dig it out of the ModifyTable node.
      So, do that, and get rid of the kluge of early setrefs processing of
      RETURNING lists.  (This is a little bit of a cheat in the case of inherited
      UPDATE/DELETE, because we are not passing a "root" struct that corresponds
      exactly to what the subplan was built with.  But that doesn't matter, and
      anyway this is less ugly than early setrefs processing was.)
      
      Back-patch to 9.1, where the problem became possible to hit.
      9fa82c98
  28. 10 3月, 2012 1 次提交
    • T
      Revise FDW planning API, again. · b1495393
      Tom Lane 提交于
      Further reflection shows that a single callback isn't very workable if we
      desire to let FDWs generate multiple Paths, because that forces the FDW to
      do all work necessary to generate a valid Plan node for each Path.  Instead
      split the former PlanForeignScan API into three steps: GetForeignRelSize,
      GetForeignPaths, GetForeignPlan.  We had already bit the bullet of breaking
      the 9.1 FDW API for 9.2, so this shouldn't cause very much additional pain,
      and it's substantially more flexible for complex FDWs.
      
      Add an fdw_private field to RelOptInfo so that the new functions can save
      state there rather than possibly having to recalculate information two or
      three times.
      
      In addition, we'd not thought through what would be needed to allow an FDW
      to set up subexpressions of its choice for runtime execution.  We could
      treat ForeignScan.fdw_private as an executable expression but that seems
      likely to break existing FDWs unnecessarily (in particular, it would
      restrict the set of node types allowable in fdw_private to those supported
      by expression_tree_walker).  Instead, invent a separate field fdw_exprs
      which will receive the postprocessing appropriate for expression trees.
      (One field is enough since it can be a list of expressions; also, we assume
      the corresponding expression state tree(s) will be held within fdw_state,
      so we don't need to add anything to ForeignScanState.)
      
      Per review of Hanada Shigeru's pgsql_fdw patch.  We may need to tweak this
      further as we continue to work on that patch, but to me it feels a lot
      closer to being right now.
      b1495393
  29. 02 1月, 2012 1 次提交
  30. 04 9月, 2011 1 次提交
    • T
      Rearrange planner to save the whole PlannerInfo (subroot) for a subquery. · b3aaf908
      Tom Lane 提交于
      Formerly, set_subquery_pathlist and other creators of plans for subqueries
      saved only the rangetable and rowMarks lists from the lower-level
      PlannerInfo.  But there's no reason not to remember the whole PlannerInfo,
      and indeed this turns out to simplify matters in a number of places.
      
      The immediate reason for doing this was so that the subroot will still be
      accessible when we're trying to extract column statistics out of an
      already-planned subquery.  But now that I've done it, it seems like a good
      code-beautification effort in its own right.
      
      I also chose to get rid of the transient subrtable and subrowmark fields in
      SubqueryScan nodes, in favor of having setrefs.c look up the subquery's
      RelOptInfo.  That required changing all the APIs in setrefs.c to pass
      PlannerInfo not PlannerGlobal, which was a large but quite mechanical
      transformation.
      
      One side-effect not foreseen at the beginning is that this finally broke
      inheritance_planner's assumption that replanning the same subquery RTE N
      times would necessarily give interchangeable results each time.  That
      assumption was always pretty risky, but now we really have to make a
      separate RTE for each instance so that there's a place to carry the
      separate subroots.
      b3aaf908
  31. 09 8月, 2011 1 次提交
    • T
      Fix nested PlaceHolderVar expressions that appear only in targetlists. · 77ba2325
      Tom Lane 提交于
      A PlaceHolderVar's expression might contain another, lower-level
      PlaceHolderVar.  If the outer PlaceHolderVar is used, the inner one
      certainly will be also, and so we have to make sure that both of them get
      into the placeholder_list with correct ph_may_need values during the
      initial pre-scan of the query (before deconstruct_jointree starts).
      We did this correctly for PlaceHolderVars appearing in the query quals,
      but overlooked the issue for those appearing in the top-level targetlist;
      with the result that nested placeholders referenced only in the targetlist
      did not work correctly, as illustrated in bug #6154.
      
      While at it, add some error checking to find_placeholder_info to ensure
      that we don't try to create new placeholders after it's too late to do so;
      they have to all be created before deconstruct_jointree starts.
      
      Back-patch to 8.4 where the PlaceHolderVar mechanism was introduced.
      77ba2325
  32. 25 4月, 2011 1 次提交
    • T
      Improve cost estimation for aggregates and window functions. · e6a30a8c
      Tom Lane 提交于
      The previous coding failed to account properly for the costs of evaluating
      the input expressions of aggregates and window functions, as seen in a
      recent gripe from Claudio Freire.  (I said at the time that it wasn't
      counting these costs at all; but on closer inspection, it was effectively
      charging these costs once per output tuple.  That is completely wrong for
      aggregates, and not exactly right for window functions either.)
      
      There was also a hard-wired assumption that aggregates and window functions
      had procost 1.0, which is now fixed to respect the actual cataloged costs.
      
      The costing of WindowAgg is still pretty bogus, since it doesn't try to
      estimate the effects of spilling data to disk, but that seems like a
      separate issue.
      e6a30a8c
  33. 20 3月, 2011 1 次提交
    • T
      Revise collation derivation method and expression-tree representation. · b310b6e3
      Tom Lane 提交于
      All expression nodes now have an explicit output-collation field, unless
      they are known to only return a noncollatable data type (such as boolean
      or record).  Also, nodes that can invoke collation-aware functions store
      a separate field that is the collation value to pass to the function.
      This avoids confusion that arises when a function has collatable inputs
      and noncollatable output type, or vice versa.
      
      Also, replace the parser's on-the-fly collation assignment method with
      a post-pass over the completed expression tree.  This allows us to use
      a more complex (and hopefully more nearly spec-compliant) assignment
      rule without paying for it in extra storage in every expression node.
      
      Fix assorted bugs in the planner's handling of collations by making
      collation one of the defining properties of an EquivalenceClass and
      by converting CollateExprs into discardable RelabelType nodes during
      expression preprocessing.
      b310b6e3
  34. 26 2月, 2011 1 次提交
    • T
      Support data-modifying commands (INSERT/UPDATE/DELETE) in WITH. · 389af951
      Tom Lane 提交于
      This patch implements data-modifying WITH queries according to the
      semantics that the updates all happen with the same command counter value,
      and in an unspecified order.  Therefore one WITH clause can't see the
      effects of another, nor can the outer query see the effects other than
      through the RETURNING values.  And attempts to do conflicting updates will
      have unpredictable results.  We'll need to document all that.
      
      This commit just fixes the code; documentation updates are waiting on
      author.
      
      Marko Tiikkaja and Hitoshi Harada
      389af951
  35. 02 1月, 2011 1 次提交
  36. 05 11月, 2010 1 次提交
    • T
      Reimplement planner's handling of MIN/MAX aggregate optimization. · 034967bd
      Tom Lane 提交于
      Per my recent proposal, get rid of all the direct inspection of indexes
      and manual generation of paths in planagg.c.  Instead, set up
      EquivalenceClasses for the aggregate argument expressions, and let the
      regular path generation logic deal with creating paths that can satisfy
      those sort orders.  This makes planagg.c a bit more visible to the rest
      of the planner than it was originally, but the approach is basically a lot
      cleaner than before.  A major advantage of doing it this way is that we get
      MIN/MAX optimization on inheritance trees (using MergeAppend of indexscans)
      practically for free, whereas in the old way we'd have had to add a whole
      lot more duplicative logic.
      
      One small disadvantage of this approach is that MIN/MAX aggregates can no
      longer exploit partial indexes having an "x IS NOT NULL" predicate, unless
      that restriction or something that implies it is specified in the query.
      The previous implementation was able to use the added "x IS NOT NULL"
      condition as an extra predicate proof condition, but in this version we
      rely entirely on indexes that are considered usable by the main planning
      process.  That seems a fair tradeoff for the simplicity and functionality
      gained.
      034967bd
  37. 21 9月, 2010 1 次提交
  38. 29 3月, 2010 1 次提交
  39. 26 2月, 2010 1 次提交
  40. 13 2月, 2010 1 次提交
    • T
      Extend the set of frame options supported for window functions. · ec4be2ee
      Tom Lane 提交于
      This patch allows the frame to start from CURRENT ROW (in either RANGE or
      ROWS mode), and it also adds support for ROWS n PRECEDING and ROWS n FOLLOWING
      start and end points.  (RANGE value PRECEDING/FOLLOWING isn't there yet ---
      the grammar works, but that's all.)
      
      Hitoshi Harada, reviewed by Pavel Stehule
      ec4be2ee