stxstattarget is nullable in PG17, write compat functions for it

Relevant PG commit:
012460ee93c304fbc7220e5b55d9d0577fc766ab
012460ee93
pg17_kickoff
naisila 2024-07-09 18:57:31 +02:00
parent be2bec0b61
commit 01769744b7
No known key found for this signature in database
GPG Key ID: A824BA9862D73E6D
3 changed files with 33 additions and 4 deletions

View File

@ -774,9 +774,10 @@ CreateAlterCommandIfTargetNotDefault(Oid statsOid)
} }
Form_pg_statistic_ext statisticsForm = (Form_pg_statistic_ext) GETSTRUCT(tup); Form_pg_statistic_ext statisticsForm = (Form_pg_statistic_ext) GETSTRUCT(tup);
int16 currentStxstattarget = getStxstattarget_compat(tup);
ReleaseSysCache(tup); ReleaseSysCache(tup);
if (statisticsForm->stxstattarget == -1) if (currentStxstattarget == -1)
{ {
return NULL; return NULL;
} }
@ -786,7 +787,8 @@ CreateAlterCommandIfTargetNotDefault(Oid statsOid)
char *schemaName = get_namespace_name(statisticsForm->stxnamespace); char *schemaName = get_namespace_name(statisticsForm->stxnamespace);
char *statName = NameStr(statisticsForm->stxname); char *statName = NameStr(statisticsForm->stxname);
alterStatsStmt->stxstattarget = statisticsForm->stxstattarget; alterStatsStmt->stxstattarget = getAlterStatsStxstattarget_compat(
currentStxstattarget);
alterStatsStmt->defnames = list_make2(makeString(schemaName), makeString(statName)); alterStatsStmt->defnames = list_make2(makeString(schemaName), makeString(statName));
return DeparseAlterStatisticsStmt((Node *) alterStatsStmt); return DeparseAlterStatisticsStmt((Node *) alterStatsStmt);

View File

@ -177,8 +177,9 @@ AppendAlterStatisticsSchemaStmt(StringInfo buf, AlterObjectSchemaStmt *stmt)
static void static void
AppendAlterStatisticsStmt(StringInfo buf, AlterStatsStmt *stmt) AppendAlterStatisticsStmt(StringInfo buf, AlterStatsStmt *stmt)
{ {
appendStringInfo(buf, "ALTER STATISTICS %s SET STATISTICS %d", NameListToQuotedString( appendStringInfo(buf, "ALTER STATISTICS %s SET STATISTICS %d",
stmt->defnames), stmt->stxstattarget); NameListToQuotedString(stmt->defnames),
getIntStxstattarget_compat(stmt->stxstattarget));
} }

View File

@ -83,6 +83,21 @@ getAttstattarget_compat(HeapTuple attTuple)
} }
#include "catalog/pg_statistic_ext.h"
static inline int16
getStxstattarget_compat(HeapTuple tup)
{
bool isnull;
Datum dat = SysCacheGetAttr(STATEXTOID, tup,
Anum_pg_statistic_ext_stxstattarget, &isnull);
return (isnull ? -1 : DatumGetInt16(dat));
}
#define getAlterStatsStxstattarget_compat(a) ((Node *) makeInteger(a))
#define getIntStxstattarget_compat(a) (intVal(a))
#else #else
#include "access/htup_details.h" #include "access/htup_details.h"
@ -93,6 +108,17 @@ getAttstattarget_compat(HeapTuple attTuple)
} }
#include "catalog/pg_statistic_ext.h"
static inline int32
getStxstattarget_compat(HeapTuple tup)
{
return ((Form_pg_statistic_ext) GETSTRUCT(tup))->stxstattarget;
}
#define getAlterStatsStxstattarget_compat(a) (a)
#define getIntStxstattarget_compat(a) (a)
#endif #endif
#if PG_VERSION_NUM >= PG_VERSION_16 #if PG_VERSION_NUM >= PG_VERSION_16