attstattarget is nullable, define pg compatible functions for it

Relevant PG commit:
4f622503d6de975ac87448aea5cea7de4bc140d5
4f622503d6
pg17_kickoff
naisila 2024-07-09 17:19:21 +02:00
parent 4eafd3a51b
commit be2bec0b61
No known key found for this signature in database
GPG Key ID: A824BA9862D73E6D
3 changed files with 33 additions and 4 deletions

View File

@ -651,14 +651,15 @@ GetAlterIndexStatisticsCommands(Oid indexOid)
} }
Form_pg_attribute targetAttr = (Form_pg_attribute) GETSTRUCT(attTuple); Form_pg_attribute targetAttr = (Form_pg_attribute) GETSTRUCT(attTuple);
if (targetAttr->attstattarget != DEFAULT_STATISTICS_TARGET) int32 targetAttstattarget = getAttstattarget_compat(attTuple);
if (targetAttstattarget != DEFAULT_STATISTICS_TARGET)
{ {
char *indexNameWithSchema = generate_qualified_relation_name(indexOid); char *indexNameWithSchema = generate_qualified_relation_name(indexOid);
char *command = char *command =
GenerateAlterIndexColumnSetStatsCommand(indexNameWithSchema, GenerateAlterIndexColumnSetStatsCommand(indexNameWithSchema,
targetAttr->attnum, targetAttr->attnum,
targetAttr->attstattarget); targetAttstattarget);
alterIndexStatisticsCommandList = alterIndexStatisticsCommandList =
lappend(alterIndexStatisticsCommandList, lappend(alterIndexStatisticsCommandList,

View File

@ -738,7 +738,12 @@ pg_get_tablecolumnoptionsdef_string(Oid tableRelationId)
* If the user changed the column's statistics target, create * If the user changed the column's statistics target, create
* alter statement and add statement to a list for later processing. * alter statement and add statement to a list for later processing.
*/ */
if (attributeForm->attstattarget >= 0) HeapTuple tp = SearchSysCache2(ATTNUM,
ObjectIdGetDatum(tableRelationId),
Int16GetDatum(attributeForm->attnum));
int32 targetAttstattarget = getAttstattarget_compat(tp);
ReleaseSysCache(tp);
if (targetAttstattarget >= 0)
{ {
StringInfoData statement = { NULL, 0, 0, 0 }; StringInfoData statement = { NULL, 0, 0, 0 };
initStringInfo(&statement); initStringInfo(&statement);
@ -746,7 +751,7 @@ pg_get_tablecolumnoptionsdef_string(Oid tableRelationId)
appendStringInfo(&statement, "ALTER COLUMN %s ", appendStringInfo(&statement, "ALTER COLUMN %s ",
quote_identifier(attributeName)); quote_identifier(attributeName));
appendStringInfo(&statement, "SET STATISTICS %d", appendStringInfo(&statement, "SET STATISTICS %d",
attributeForm->attstattarget); targetAttstattarget);
columnOptionList = lappend(columnOptionList, statement.data); columnOptionList = lappend(columnOptionList, statement.data);
} }

View File

@ -70,6 +70,29 @@ RangeVarCallbackOwnsTable(const RangeVar *relation,
} }
#include "catalog/pg_attribute.h"
#include "utils/syscache.h"
static inline int32
getAttstattarget_compat(HeapTuple attTuple)
{
bool isnull;
Datum dat = SysCacheGetAttr(ATTNUM, attTuple,
Anum_pg_attribute_attstattarget, &isnull);
return (isnull ? -1 : DatumGetInt16(dat));
}
#else
#include "access/htup_details.h"
static inline int32
getAttstattarget_compat(HeapTuple attTuple)
{
return ((Form_pg_attribute) GETSTRUCT(attTuple))->attstattarget;
}
#endif #endif
#if PG_VERSION_NUM >= PG_VERSION_16 #if PG_VERSION_NUM >= PG_VERSION_16