From ea1fd27765962aaef33949c2f7340da887d27771 Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Mon, 30 Mar 2020 15:27:54 +0200 Subject: [PATCH] Fix some compatibility issue with pg12. Based on 3bb72b74920210229504e31255611702d38da07c and 4e6fce31c78f4a1290a87bfe1c3e3846e1dd673f. A lot more is still needed to get the hypothetical partitionning part compatible with pg12. --- hypopg.c | 15 +++++++----- hypopg_analyze.c | 20 +++++++++++----- hypopg_index.c | 45 +++++++++++++++++++++++++++++++----- hypopg_table.c | 9 ++++++++ import/hypopg_import_index.c | 10 ++++++-- include/hypopg.h | 19 +++++++++++++++ 6 files changed, 98 insertions(+), 20 deletions(-) diff --git a/hypopg.c b/hypopg.c index 210c1e8..65dc033 100644 --- a/hypopg.c +++ b/hypopg.c @@ -21,6 +21,9 @@ #if PG_VERSION_NUM >= 90300 #include "access/htup_details.h" #endif +#if PG_VERSION_NUM >= 120000 +#include "access/table.h" +#endif #if PG_VERSION_NUM >= 100000 #include "access/xact.h" #endif @@ -208,22 +211,22 @@ hypo_getNewOid(Oid relid) char relpersistence; /* Open the relation on which we want a new OID */ - relation = heap_open(relid, AccessShareLock); + relation = table_open(relid, AccessShareLock); reltablespace = relation->rd_rel->reltablespace; relpersistence = relation->rd_rel->relpersistence; /* Close the relation and release the lock now */ - heap_close(relation, AccessShareLock); + table_close(relation, AccessShareLock); /* Open pg_class to aks a new OID */ - pg_class = heap_open(RelationRelationId, RowExclusiveLock); + pg_class = table_open(RelationRelationId, RowExclusiveLock); /* ask for a new relfilenode */ newoid = GetNewRelFileNode(reltablespace, pg_class, relpersistence); /* Close pg_class and release the lock now */ - heap_close(pg_class, RowExclusiveLock); + table_close(pg_class, RowExclusiveLock); return newoid; } @@ -542,7 +545,7 @@ hypo_get_relation_info_hook(PlannerInfo *root, #endif /* Open the current relation */ - relation = heap_open(relationObjectId, AccessShareLock); + relation = table_open(relationObjectId, AccessShareLock); if (relation->rd_rel->relkind == RELKIND_RELATION #if PG_VERSION_NUM >= 90300 @@ -632,7 +635,7 @@ hypo_get_relation_info_hook(PlannerInfo *root, } } /* Close the relation and keep the lock, it might be reopened later */ - heap_close(relation, NoLock); + table_close(relation, NoLock); } if (prev_get_relation_info_hook) prev_get_relation_info_hook(root, relationObjectId, inhparent, rel); diff --git a/hypopg_analyze.c b/hypopg_analyze.c index be49ca4..fbb08f3 100644 --- a/hypopg_analyze.c +++ b/hypopg_analyze.c @@ -29,10 +29,18 @@ #if PG_VERSION_NUM >= 90300 #include "access/htup_details.h" #endif +#if PG_VERSION_NUM >= 120000 +#include "access/table.h" +#endif #include "commands/vacuum.h" #include "executor/spi.h" +#if PG_VERSION_NUM < 120000 #include "optimizer/clauses.h" #include "optimizer/cost.h" +#else +#include"nodes/makefuncs.h" +#include"optimizer/optimizer.h" +#endif #include "parser/parsetree.h" #include "rewrite/rewriteManip.h" #include "utils/attoptcache.h" @@ -617,15 +625,15 @@ HYPO_PARTITION_NOT_SUPPORTED(); HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT); } - onerel = heap_open(root_tableid, AccessShareLock); - pgstats = heap_open(StatisticRelationId, AccessShareLock); + onerel = table_open(root_tableid, AccessShareLock); + pgstats = table_open(StatisticRelationId, AccessShareLock); hypo_do_analyze_tree(onerel, pgstats, fraction, root_entry); /* release SPI related resources (and return to caller's context) */ SPI_finish(); - relation_close(onerel, AccessShareLock); - relation_close(pgstats, AccessShareLock); + table_close(onerel, AccessShareLock); + table_close(pgstats, AccessShareLock); return (Datum) 0; @@ -682,7 +690,7 @@ HYPO_PARTITION_NOT_SUPPORTED(); if (!hypoStatsHash) return (Datum) 0; - pgstats = heap_open(StatisticRelationId, AccessShareLock); + pgstats = table_open(StatisticRelationId, AccessShareLock); hash_seq_init(&hash_seq, hypoStatsHash); while ((entry = hash_seq_search(&hash_seq)) != NULL) @@ -699,7 +707,7 @@ HYPO_PARTITION_NOT_SUPPORTED(); tuplestore_putvalues(tupstore, tupdesc, values, nulls); } - relation_close(pgstats, AccessShareLock); + table_close(pgstats, AccessShareLock); /* clean up and return the tuplestore */ tuplestore_donestoring(tupstore); diff --git a/hypopg_index.c b/hypopg_index.c index 595610f..8c4b705 100644 --- a/hypopg_index.c +++ b/hypopg_index.c @@ -32,6 +32,10 @@ #include "access/htup_details.h" #endif #include "access/nbtree.h" +#if PG_VERSION_NUM >= 120000 +#include "access/relation.h" +#include "access/table.h" +#endif #include "access/reloptions.h" #include "access/spgist.h" #include "access/spgist_private.h" @@ -44,11 +48,19 @@ #include "catalog/pg_opclass.h" #include "catalog/pg_type.h" #include "commands/defrem.h" +#if PG_VERSION_NUM < 120000 #include "nodes/relation.h" +#else +#include "nodes/makefuncs.h" +#endif #include "optimizer/clauses.h" #include "optimizer/cost.h" #include "optimizer/pathnode.h" +#if PG_VERSION_NUM < 120000 #include "optimizer/var.h" +#else +#include "optimizer/optimizer.h" +#endif #include "parser/parse_utilcmd.h" #include "parser/parser.h" #include "parser/parsetree.h" @@ -128,6 +140,7 @@ hypo_newIndex(Oid relid, char *accessMethod, int nkeycolumns, int ninccolumns, hypoIndex *volatile entry; MemoryContext oldcontext; HeapTuple tuple; + Oid oid; #if PG_VERSION_NUM >= 90600 IndexAmRoutine *amroutine; @@ -146,13 +159,19 @@ hypo_newIndex(Oid relid, char *accessMethod, int nkeycolumns, int ninccolumns, accessMethod))); } - hypo_discover_am(accessMethod, HeapTupleGetOid(tuple)); +#if PG_VERSION_NUM < 120000 + oid = HeapTupleGetOid(tuple); +#else + oid = ((Form_pg_am) GETSTRUCT(tuple))->oid; +#endif + + hypo_discover_am(accessMethod, oid); oldcontext = MemoryContextSwitchTo(HypoMemoryContext); entry = palloc0(sizeof(hypoIndex)); - entry->relam = HeapTupleGetOid(tuple); + entry->relam = oid; #if PG_VERSION_NUM >= 90600 @@ -664,10 +683,17 @@ hypo_index_store_parsetree(IndexStmt *node, const char *queryString) } /* get the opclass */ +#if PG_VERSION_NUM < 100000 + opclass= GetIndexOpClass(attribute->opclass, + atttype, + node->accessMethod, + entry->relam); +#else opclass = ResolveOpClass(attribute->opclass, atttype, node->accessMethod, entry->relam); +#endif entry->opclass[attn] = opclass; /* setup the opfamily */ entry->opfamily[attn] = get_opclass_family(opclass); @@ -726,7 +752,11 @@ hypo_index_store_parsetree(IndexStmt *node, const char *queryString) { AttrNumber attno = entry->indexkeys[attn]; - if (attno < 0 && attno != ObjectIdAttributeNumber) + if (attno < 0 +#if PG_VERSION_NUM < 120000 + && attno != ObjectIdAttributeNumber +#endif + ) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("hypopg: index creation on system columns is not supported"))); @@ -824,7 +854,10 @@ hypo_index_store_parsetree(IndexStmt *node, const char *queryString) for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++) { - if (i != ObjectIdAttributeNumber && + if ( +#if PG_VERSION_NUM < 120000 + i != ObjectIdAttributeNumber && +#endif bms_is_member(i - FirstLowInvalidHeapAttributeNumber, indexattrs)) ereport(ERROR, @@ -1759,7 +1792,7 @@ hypo_estimate_index_simple(hypoIndex *entry, BlockNumber *pages, double *tuples) rel = makeNode(RelOptInfo); /* Open the hypo index' relation */ - relation = heap_open(entry->relid, AccessShareLock); + relation = table_open(entry->relid, AccessShareLock); if (!RelationNeedsWAL(relation) && RecoveryInProgress()) ereport(ERROR, @@ -1787,7 +1820,7 @@ hypo_estimate_index_simple(hypoIndex *entry, BlockNumber *pages, double *tuples) &rel->pages, &rel->tuples, &rel->allvisfrac); /* Close the relation and release the lock now */ - heap_close(relation, AccessShareLock); + table_close(relation, AccessShareLock); hypo_estimate_index(entry, rel, NULL); *pages = entry->pages; diff --git a/hypopg_table.c b/hypopg_table.c index 8d5ee6d..5caa420 100644 --- a/hypopg_table.c +++ b/hypopg_table.c @@ -36,16 +36,22 @@ #include "catalog/pg_opclass.h" #include "catalog/pg_type.h" #include "nodes/makefuncs.h" +#if PG_VERSION_NUM < 120000 #include "nodes/relation.h" +#endif #include "nodes/nodes.h" #include "nodes/pg_list.h" #include "optimizer/clauses.h" #include "optimizer/cost.h" #include "optimizer/pathnode.h" +#if PG_VERSION_NUM < 120000 #include "optimizer/predtest.h" +#endif #include "optimizer/prep.h" #include "optimizer/restrictinfo.h" +#if PG_VERSION_NUM < 120000 #include "optimizer/var.h" +#endif #include "parser/parsetree.h" #include "parser/parse_utilcmd.h" #include "rewrite/rewriteManip.h" @@ -58,6 +64,9 @@ #include "utils/partcache.h" #include "partitioning/partbounds.h" #endif +#if PG_VERSION_NUM >= 120000 +#include "partitioning/partdesc.h" +#endif #include "utils/ruleutils.h" #include "utils/syscache.h" #endif /* pg10+ */ diff --git a/import/hypopg_import_index.c b/import/hypopg_import_index.c index 7c58a02..9cbc29d 100644 --- a/import/hypopg_import_index.c +++ b/import/hypopg_import_index.c @@ -22,6 +22,9 @@ #include "nodes/makefuncs.h" #include "nodes/pg_list.h" #include "optimizer/clauses.h" +#if PG_VERSION_NUM >= 120000 +#include "optimizer/optimizer.h" +#endif #include "optimizer/planner.h" #include "optimizer/pathnode.h" #if PG_VERSION_NUM >= 110000 @@ -63,8 +66,11 @@ build_index_tlist(PlannerInfo *root, IndexOptInfo *index, const FormData_pg_attribute *att_tup; if (indexkey < 0) - att_tup = SystemAttributeDefinition(indexkey, - heapRelation->rd_rel->relhasoids); + att_tup = SystemAttributeDefinition(indexkey +#if PG_VERSION_NUM < 120000 + ,heapRelation->rd_rel->relhasoids +#endif + ); else #if PG_VERSION_NUM >= 110000 att_tup = TupleDescAttr(heapRelation->rd_att, indexkey - 1); diff --git a/include/hypopg.h b/include/hypopg.h index fc2a3cd..1282f43 100644 --- a/include/hypopg.h +++ b/include/hypopg.h @@ -19,6 +19,25 @@ #include "include/hypopg_import.h" +/* Provide backward compatibility macros for table.c API on pre v12 versions */ +#if PG_VERSION_NUM < 120000 +#define table_open(r, l) heap_open(r, l) +#define table_close(r, l) heap_close(r, l) +#endif + +/* + * Hacky macro to provide backward compatibility with either 1 or 2 arg lnext() + * on pre v13 versions + */ +#if PG_VERSION_NUM < 130000 +#define LNEXT(_1, _2, NAME, ...) NAME +#undef lnext +#define lnext(...) LNEXT(__VA_ARGS__, LNEXT2, LNEXT1) (__VA_ARGS__) +#define LNEXT1(lc) ((lc)->next) +#define LNEXT2(list, lc) ((lc)->next) +#endif + + extern bool isExplain; /* GUC for enabling / disabling hypopg during EXPLAIN */