Rename structs and functions to mention index instead of entry.

This is preliminary work for further support of hypothetical
partitioning.

(cherry picked from commit cac17141f5)
This commit is contained in:
Julien Rouhaud 2018-03-02 21:30:48 +01:00
parent 31a2fbd104
commit f9144a79e0

166
hypopg.c
View file

@ -71,8 +71,8 @@
PG_MODULE_MAGIC;
#define HYPO_NB_COLS 12 /* # of column hypopg() returns */
#define HYPO_CREATE_COLS 2 /* # of column hypopg_create_index() returns */
#define HYPO_INDEX_NB_COLS 12 /* # of column hypopg() returns */
#define HYPO_INDEX_CREATE_COLS 2 /* # of column hypopg_create_index() returns */
bool isExplain = false;
@ -95,7 +95,7 @@ static MemoryContext HypoMemoryContext = NULL;
* Some dynamic informations such as pages and lines are not stored but
* computed when the hypothetical index is used.
*/
typedef struct hypoEntry
typedef struct hypoIndex
{
Oid oid; /* hypothetical index unique identifier */
Oid relid; /* related relation Oid */
@ -158,10 +158,10 @@ typedef struct hypoEntry
List *options; /* WITH clause options: a list of DefElem */
bool amcanorder; /* does AM support order by column value? */
} hypoEntry;
} hypoIndex;
/* List of hypothetic indexes for current backend */
List *entries = NIL;
List *hypoIndexes = NIL;
/*--- Functions --- */
@ -183,17 +183,17 @@ PG_FUNCTION_INFO_V1(hypopg_drop_index);
PG_FUNCTION_INFO_V1(hypopg_relation_size);
PG_FUNCTION_INFO_V1(hypopg_get_indexdef);
static hypoEntry *hypo_newEntry(Oid relid, char *accessMethod, int nkeycolumns,
static hypoIndex *hypo_newIndex(Oid relid, char *accessMethod, int nkeycolumns,
int ninccolumns,
List *options);
static Oid hypo_getNewOid(Oid relid);
static void hypo_addEntry(hypoEntry *entry);
static void hypo_addIndex(hypoIndex *entry);
static void hypo_entry_reset(void);
static const hypoEntry *hypo_entry_store_parsetree(IndexStmt *node,
static void hypo_index_reset(void);
static const hypoIndex *hypo_index_store_parsetree(IndexStmt *node,
const char *queryString);
static bool hypo_entry_remove(Oid indexid);
static void hypo_entry_pfree(hypoEntry *entry);
static bool hypo_index_remove(Oid indexid);
static void hypo_index_pfree(hypoIndex *entry);
static void
hypo_utility_hook(
@ -235,15 +235,15 @@ static void hypo_injectHypotheticalIndex(PlannerInfo *root,
bool inhparent,
RelOptInfo *rel,
Relation relation,
hypoEntry *entry);
hypoIndex *entry);
static bool hypo_query_walker(Node *node);
static void hypo_set_indexname(hypoEntry *entry, char *indexname);
static void hypo_estimate_index_simple(hypoEntry *entry,
static void hypo_set_indexname(hypoIndex *entry, char *indexname);
static void hypo_estimate_index_simple(hypoIndex *entry,
BlockNumber *pages, double *tuples);
static void hypo_estimate_index(hypoEntry *entry, RelOptInfo *rel);
static int hypo_estimate_index_colsize(hypoEntry *entry, int col);
static bool hypo_can_return(hypoEntry *entry, Oid atttype, int i, char *amname);
static void hypo_estimate_index(hypoIndex *entry, RelOptInfo *rel);
static int hypo_estimate_index_colsize(hypoIndex *entry, int col);
static bool hypo_can_return(hypoIndex *entry, Oid atttype, int i, char *amname);
static void hypo_discover_am(char *amname, Oid oid);
void
@ -298,16 +298,16 @@ _PG_fini(void)
}
/*
* palloc a new hypoEntry, and give it a new OID, and some other global stuff.
* palloc a new hypoIndex, and give it a new OID, and some other global stuff.
* This function also parse index storage options (if any) to check if they're
* valid.
*/
static hypoEntry *
hypo_newEntry(Oid relid, char *accessMethod, int nkeycolumns, int ninccolumns,
static hypoIndex *
hypo_newIndex(Oid relid, char *accessMethod, int nkeycolumns, int ninccolumns,
List *options)
{
/* must be declared "volatile", because used in a PG_CATCH() */
hypoEntry *volatile entry;
hypoIndex *volatile entry;
MemoryContext oldcontext;
HeapTuple tuple;
@ -332,7 +332,7 @@ hypo_newEntry(Oid relid, char *accessMethod, int nkeycolumns, int ninccolumns,
oldcontext = MemoryContextSwitchTo(HypoMemoryContext);
entry = palloc0(sizeof(hypoEntry));
entry = palloc0(sizeof(hypoIndex));
entry->relam = HeapTupleGetOid(tuple);
@ -450,7 +450,7 @@ hypo_newEntry(Oid relid, char *accessMethod, int nkeycolumns, int ninccolumns,
PG_CATCH();
{
/* Free what was palloc'd in HypoMemoryContext */
hypo_entry_pfree(entry);
hypo_index_pfree(entry);
PG_RE_THROW();
}
@ -493,42 +493,42 @@ hypo_getNewOid(Oid relid)
return newoid;
}
/* Add an hypoEntry to hypoEntries */
/* Add an hypoIndex to hypoIndexes */
static void
hypo_addEntry(hypoEntry *entry)
hypo_addIndex(hypoIndex *entry)
{
MemoryContext oldcontext;
oldcontext = MemoryContextSwitchTo(HypoMemoryContext);
entries = lappend(entries, entry);
hypoIndexes = lappend(hypoIndexes, entry);
MemoryContextSwitchTo(oldcontext);
}
/*
* Remove cleanly all hypothetical indexes by calling hypo_entry_remove() on
* each entry. hypo_entry_remove() function pfree all allocated memory
* Remove cleanly all hypothetical indexes by calling hypo_index_remove() on
* each entry. hypo_index_remove() function pfree all allocated memory
*/
static void
hypo_entry_reset(void)
hypo_index_reset(void)
{
ListCell *lc;
/*
* The cell is removed in hypo_entry_remove(), so we can't iterate using
* The cell is removed in hypo_index_remove(), so we can't iterate using
* standard foreach / lnext macros.
*/
while ((lc = list_head(entries)) != NULL)
while ((lc = list_head(hypoIndexes)) != NULL)
{
hypoEntry *entry = (hypoEntry *) lfirst(lc);
hypoIndex *entry = (hypoIndex *) lfirst(lc);
hypo_entry_remove(entry->oid);
hypo_index_remove(entry->oid);
}
list_free(entries);
entries = NIL;
list_free(hypoIndexes);
hypoIndexes = NIL;
return;
}
@ -537,11 +537,11 @@ hypo_entry_reset(void)
* is where all the hypothetic index creation is done, except the index size
* estimation.
*/
static const hypoEntry *
hypo_entry_store_parsetree(IndexStmt *node, const char *queryString)
static const hypoIndex *
hypo_index_store_parsetree(IndexStmt *node, const char *queryString)
{
/* must be declared "volatile", because used in a PG_CATCH() */
hypoEntry *volatile entry;
hypoIndex *volatile entry;
Form_pg_attribute attform;
Oid relid;
StringInfoData indexRelationName;
@ -586,7 +586,7 @@ hypo_entry_store_parsetree(IndexStmt *node, const char *queryString)
appendStringInfo(&indexRelationName, "%s", node->relation->relname);
/* now create the hypothetical index entry */
entry = hypo_newEntry(relid, node->accessMethod, nkeycolumns, ninccolumns,
entry = hypo_newIndex(relid, node->accessMethod, nkeycolumns, ninccolumns,
node->options);
PG_TRY();
@ -967,7 +967,7 @@ hypo_entry_store_parsetree(IndexStmt *node, const char *queryString)
PG_CATCH();
{
/* Free what was palloc'd in HypoMemoryContext */
hypo_entry_pfree(entry);
hypo_index_pfree(entry);
PG_RE_THROW();
}
@ -1032,37 +1032,37 @@ hypo_entry_store_parsetree(IndexStmt *node, const char *queryString)
hypo_set_indexname(entry, indexRelationName.data);
hypo_addEntry(entry);
hypo_addIndex(entry);
return entry;
}
/*
* Remove an hypothetical index from the list of hypothetical indexes.
* pfree (by calling hypo_entry_pfree) all memory that has been allocated.
* pfree (by calling hypo_index_pfree) all memory that has been allocated.
*/
static bool
hypo_entry_remove(Oid indexid)
hypo_index_remove(Oid indexid)
{
ListCell *lc;
foreach(lc, entries)
foreach(lc, hypoIndexes)
{
hypoEntry *entry = (hypoEntry *) lfirst(lc);
hypoIndex *entry = (hypoIndex *) lfirst(lc);
if (entry->oid == indexid)
{
entries = list_delete_ptr(entries, entry);
hypo_entry_pfree(entry);
hypoIndexes = list_delete_ptr(hypoIndexes, entry);
hypo_index_pfree(entry);
return true;
}
}
return false;
}
/* pfree all allocated memory for within an hypoEntry and the entry itself. */
/* pfree all allocated memory for within an hypoIndex and the entry itself. */
static void
hypo_entry_pfree(hypoEntry *entry)
hypo_index_pfree(hypoIndex *entry)
{
/* pfree all memory that has been allocated */
pfree(entry->indexname);
@ -1215,11 +1215,11 @@ hypo_executorEnd_hook(QueryDesc *queryDesc)
/*--------------------------------------------------
* Add an hypothetical index to the list of indexes.
* Caller should have check that the specified hypoEntry does belong to the
* Caller should have check that the specified hypoIndex does belong to the
* specified relation. This function also assume that the specified entry
* already contains every needed information, so we just basically need to copy
* it from the hypoEntry to the new IndexOptInfo. Every specific handling is
* done at store time (ie. hypo_entry_store_parsetree). The only exception is
* it from the hypoIndex to the new IndexOptInfo. Every specific handling is
* done at store time (ie. hypo_index_store_parsetree). The only exception is
* the size estimation, recomputed verytime, as it needs up to date statistics.
*/
static void
@ -1228,7 +1228,7 @@ hypo_injectHypotheticalIndex(PlannerInfo *root,
bool inhparent,
RelOptInfo *rel,
Relation relation,
hypoEntry *entry)
hypoIndex *entry)
{
IndexOptInfo *index;
int ncolumns,
@ -1299,7 +1299,7 @@ hypo_injectHypotheticalIndex(PlannerInfo *root,
/*
* Fetch the ordering information for the index, if any. This is handled
* in hypo_entry_store_parsetree(). Again, adapted from plancat.c -
* in hypo_index_store_parsetree(). Again, adapted from plancat.c -
* get_relation_info()
*/
if (entry->relam == BTREE_AM_OID)
@ -1352,7 +1352,7 @@ hypo_injectHypotheticalIndex(PlannerInfo *root,
index->amcanparallel = entry->amcanparallel;
#endif
/* these has already been handled in hypo_entry_store_parsetree() if any */
/* these has already been handled in hypo_index_store_parsetree() if any */
index->indexprs = list_copy(entry->indexprs);
index->indpred = list_copy(entry->indpred);
index->predOK = false; /* will be set later in indxpath.c */
@ -1411,9 +1411,9 @@ hypo_get_relation_info_hook(PlannerInfo *root,
{
ListCell *lc;
foreach(lc, entries)
foreach(lc, hypoIndexes)
{
hypoEntry *entry = (hypoEntry *) lfirst(lc);
hypoIndex *entry = (hypoIndex *) lfirst(lc);
if (entry->relid == relationObjectId)
{
@ -1451,9 +1451,9 @@ hypo_explain_get_index_name_hook(Oid indexId)
*/
ListCell *lc;
foreach(lc, entries)
foreach(lc, hypoIndexes)
{
hypoEntry *entry = (hypoEntry *) lfirst(lc);
hypoIndex *entry = (hypoIndex *) lfirst(lc);
if (entry->oid == indexId)
{
@ -1470,7 +1470,7 @@ hypo_explain_get_index_name_hook(Oid indexId)
Datum
hypopg_reset(PG_FUNCTION_ARGS)
{
hypo_entry_reset();
hypo_index_reset();
PG_RETURN_VOID();
}
@ -1513,11 +1513,11 @@ hypopg(PG_FUNCTION_ARGS)
MemoryContextSwitchTo(oldcontext);
foreach(lc, entries)
foreach(lc, hypoIndexes)
{
hypoEntry *entry = (hypoEntry *) lfirst(lc);
Datum values[HYPO_NB_COLS];
bool nulls[HYPO_NB_COLS];
hypoIndex *entry = (hypoIndex *) lfirst(lc);
Datum values[HYPO_INDEX_NB_COLS];
bool nulls[HYPO_INDEX_NB_COLS];
ListCell *lc2;
StringInfoData exprsString;
int i = 0;
@ -1567,7 +1567,7 @@ hypopg(PG_FUNCTION_ARGS)
nulls[i++] = true;
values[i++] = ObjectIdGetDatum(entry->relam);
Assert(i == HYPO_NB_COLS);
Assert(i == HYPO_INDEX_NB_COLS);
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
}
@ -1624,9 +1624,9 @@ hypopg_create_index(PG_FUNCTION_ARGS)
foreach(parsetree_item, parsetree_list)
{
Node *parsetree = (Node *) lfirst(parsetree_item);
Datum values[HYPO_CREATE_COLS];
bool nulls[HYPO_CREATE_COLS];
const hypoEntry *entry;
Datum values[HYPO_INDEX_CREATE_COLS];
bool nulls[HYPO_INDEX_CREATE_COLS];
const hypoIndex *entry;
memset(values, 0, sizeof(values));
memset(nulls, 0, sizeof(nulls));
@ -1642,7 +1642,7 @@ hypopg_create_index(PG_FUNCTION_ARGS)
}
else
{
entry = hypo_entry_store_parsetree((IndexStmt *) parsetree, sql);
entry = hypo_index_store_parsetree((IndexStmt *) parsetree, sql);
if (entry != NULL)
{
values[0] = ObjectIdGetDatum(entry->oid);
@ -1668,7 +1668,7 @@ hypopg_drop_index(PG_FUNCTION_ARGS)
{
Oid indexid = PG_GETARG_OID(0);
PG_RETURN_BOOL(hypo_entry_remove(indexid));
PG_RETURN_BOOL(hypo_index_remove(indexid));
}
/*
@ -1684,9 +1684,9 @@ hypopg_relation_size(PG_FUNCTION_ARGS)
pages = 0;
tuples = 0;
foreach(lc, entries)
foreach(lc, hypoIndexes)
{
hypoEntry *entry = (hypoEntry *) lfirst(lc);
hypoIndex *entry = (hypoIndex *) lfirst(lc);
if (entry->oid == indexid)
{
@ -1698,7 +1698,7 @@ hypopg_relation_size(PG_FUNCTION_ARGS)
}
/*
* Deparse an hypoEntry, indentified by its indexid to the actual CREATE INDEX
* Deparse an hypoIndex, indentified by its indexid to the actual CREATE INDEX
* command.
*
* Heavilty inspired on pg_get_indexdef_worker()
@ -1710,14 +1710,14 @@ hypopg_get_indexdef(PG_FUNCTION_ARGS)
Oid indexid = PG_GETARG_OID(0);
ListCell *indexpr_item;
StringInfoData buf;
hypoEntry *entry = NULL;
hypoIndex *entry = NULL;
ListCell *lc;
List *context;
int keyno, cpt = 0;
foreach(lc, entries)
foreach(lc, hypoIndexes)
{
entry = (hypoEntry *) lfirst(lc);
entry = (hypoIndex *) lfirst(lc);
if (entry->oid == indexid)
break;
@ -1873,7 +1873,7 @@ hypopg_get_indexdef(PG_FUNCTION_ARGS)
* ending \0
*/
static void
hypo_set_indexname(hypoEntry *entry, char *indexname)
hypo_set_indexname(hypoIndex *entry, char *indexname)
{
char oid[12]; /* store <oid>, oid shouldn't be more than
* 9999999999 */
@ -1894,10 +1894,10 @@ hypo_set_indexname(hypoEntry *entry, char *indexname)
}
/*
* Fill the pages and tuples information for a given hypoentry.
* Fill the pages and tuples information for a given hypoIndex.
*/
static void
hypo_estimate_index_simple(hypoEntry *entry, BlockNumber *pages, double *tuples)
hypo_estimate_index_simple(hypoIndex *entry, BlockNumber *pages, double *tuples)
{
RelOptInfo *rel;
Relation relation;
@ -1940,11 +1940,11 @@ hypo_estimate_index_simple(hypoEntry *entry, BlockNumber *pages, double *tuples)
/*
* Fill the pages and tuples information for a given hypoentry and a given
* Fill the pages and tuples information for a given hypoIndex and a given
* RelOptInfo
*/
static void
hypo_estimate_index(hypoEntry *entry, RelOptInfo *rel)
hypo_estimate_index(hypoIndex *entry, RelOptInfo *rel)
{
int i,
ind_avg_width = 0;
@ -2170,7 +2170,7 @@ hypo_estimate_index(hypoEntry *entry, RelOptInfo *rel)
* Estimate a single index's column of an hypothetical index.
*/
static int
hypo_estimate_index_colsize(hypoEntry *entry, int col)
hypo_estimate_index_colsize(hypoIndex *entry, int col)
{
int i, pos;
Node *expr;
@ -2233,7 +2233,7 @@ hypo_estimate_index_colsize(hypoEntry *entry, int col)
* can't be done without a real Relation, so try to find it out
*/
static bool
hypo_can_return(hypoEntry *entry, Oid atttype, int i, char *amname)
hypo_can_return(hypoIndex *entry, Oid atttype, int i, char *amname)
{
/* no amcanreturn entry, am does not handle IOS */
#if PG_VERSION_NUM >= 90600