Re: [PATCHES] [HACKERS] get_relation_stats_hook()

2008-08-06 Thread Simon Riggs

On Thu, 2008-07-10 at 14:59 -0400, Tom Lane wrote:
 Simon Riggs [EMAIL PROTECTED] writes:
  On Thu, 2008-06-26 at 12:50 -0400, Tom Lane wrote:
  I think you need to move up a level, and perhaps refactor some of the
  existing code to make it easier to inject made-up stats.
 
  I've looked at ways of refactoring this, but the hooks provided here
  complement the existing functionality fairly well. 
 
 The hooks proposed here are completely useless --- it's impossible
 to return anything except actual catalog data, because whatever is
 returned is going to be subjected to ReleaseSysCache() later on.
 I'd think you at least need to be able to turn that into a no-op,
 or perhaps a pfree() call.  Also it's clear that some calls of
 examine_variable would still return regular syscache entries, so
 the cleanup behavior has to be determinable on a per-call basis.

I've introduced an additional plugin function 

typedef void (*release_relation_stats_hook_type) (HeapTuple statstup);

Simply pfree-ing things would prevent the plugin from sometimes
returning a syscache entry or keeping its own cache internally.

 I also still think that you need to reconsider the level at which
 the hook gets control.  It's impossible given these hook definitions
 to create fake data for an appendrel or sub-SELECT, which seems to
 me to be an important requirement, at least till such time as the
 core planner handles those cases better.

I agree with this, especially with regard to join relations.

However, I'm having difficulty seeing how to identify these structures
externally to allow any meaningful lookups of data. So if we have a 7
table join, then how do we specifically identify the join between tables
4 and 7, say.

We can say, its foo = foo, but that can easily occur multiple times in
a many way join, especially since join columns are frequently named the
same. How should we tell them apart? AFAICS we can't and yet we need to
if this is to be useful.

Overriding that is already reasonably easily possible anyway using
operators, so I'm not seeing this plugin as a way to introduce hints.
This is purely to provide access to alternate stats.

 My feeling is that the hooks ought to be at more or less the same
 semantic level as examine_variable and ReleaseVariableStats, and that
 rather than making special-case hooks for the other couple of direct
 accesses to STATRELATT, the right thing is to make sure that the
 examine_variable hook will work for them too.

In general, I agree, though this does cause more work in the plugin and
in other areas of selfuncs.c

btcostestimate() searches syscache directly rather than going through
examine_variable. I suspect refactoring that would cause changes to
internals of examine_variable, possibly also changing the API to cater
for index stats.

I have tried to follow this through as you suggest, but the thought that
examine_variable is the right level doesn't seem as clean as it sounded
when you first suggested.

With those problems in mind, can we stick to the original plan? It's
reasonably clear how it works, and it does work correctly now with the
new release call mentioned above.

Or can you suggest the refactoring you would like to see in that area?
Or better still commit something for me to use as a base.

 I think it might be a good idea to prepare an actual working example
 of a plug-in that does something with the hook in order to verify
 that you've got a useful definition.

I've tested this new patch with the plugin I showed you before, rewired
so it makes syscache calls via patch, as a null test case. 
All works, no problems.

I'll submit the fully working plugin once we've stabilised the API. It's
designed as a contrib module, so it can go in pgfoundry or contrib.

-- 
 Simon Riggs   www.2ndQuadrant.com
 PostgreSQL Training, Services and Support
Index: src/backend/utils/adt/selfuncs.c
===
RCS file: /home/sriggs/pg/REPOSITORY/pgsql/src/backend/utils/adt/selfuncs.c,v
retrieving revision 1.250
diff -c -r1.250 selfuncs.c
*** src/backend/utils/adt/selfuncs.c	7 Jul 2008 20:24:55 -	1.250
--- src/backend/utils/adt/selfuncs.c	6 Aug 2008 09:06:26 -
***
*** 103,108 
--- 103,111 
  #include utils/selfuncs.h
  #include utils/syscache.h
  
+ /* Hook for plugins to get control when we ask for stats */
+ get_relation_stats_hook_type get_relation_stats_hook = NULL;
+ release_relation_stats_hook_type release_relation_stats_hook = NULL;
  
  static double var_eq_const(VariableStatData *vardata, Oid operator,
  			 Datum constval, bool constisnull,
***
*** 3769,3775 
  		}
  		else if (rte-rtekind == RTE_RELATION)
  		{
! 			vardata-statsTuple = SearchSysCache(STATRELATT,
   ObjectIdGetDatum(rte-relid),
   Int16GetDatum(var-varattno),
   0, 0);
--- 3772,3783 
  		}
  		else if (rte-rtekind == RTE_RELATION)
  		{
! 			if (get_relation_stats_hook)

Re: [PATCHES] WIP: Transportable Optimizer Mode

2008-08-06 Thread Simon Riggs

On Sat, 2008-08-02 at 20:12 +0100, Simon Riggs wrote:
 Chris,
 
 Thanks for all of those changes... added as suggested (in next version)
 
 On Wed, 2008-07-30 at 14:58 -0400, chris wrote:
 
  It's not clear to me that the plugin is actually working.
  
  When I run EXPLAIN against tables in tomtestdb, I get results
  consistent with an unanalyzed table.  So possibly the hook isn't
  being used.  Perhaps I'm using it wrongly; perhaps what I have
  documented above may suggest to you what's broken.
 
 The plugin works, but it currently extracts 0 for number of tuples. So
 you need to override that to give a different answer.
 
 I was going to add an SQL function to estimate the number of tuples in
 the same way as the optimizer does. That way we get the same answer from
 the EXPLAIN as we would have got on the main server and we don't need to
 run select count(*) against each table (unless we want to).

New version submitted together with stats hook patch on other thread.

-- 
 Simon Riggs   www.2ndQuadrant.com
 PostgreSQL Training, Services and Support


-- 
Sent via pgsql-patches mailing list (pgsql-patches@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-patches