From be2bec0b61e5a943df0f32cfd52ace9ef4dafa6c Mon Sep 17 00:00:00 2001 From: naisila Date: Tue, 9 Jul 2024 17:19:21 +0200 Subject: [PATCH] attstattarget is nullable, define pg compatible functions for it Relevant PG commit: 4f622503d6de975ac87448aea5cea7de4bc140d5 https://github.com/postgres/postgres/commit/4f622503d6de975ac87448aea5cea7de4bc140d5 --- src/backend/distributed/commands/statistics.c | 5 ++-- .../distributed/deparser/citus_ruleutils.c | 9 ++++++-- src/include/pg_version_compat.h | 23 +++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/backend/distributed/commands/statistics.c b/src/backend/distributed/commands/statistics.c index 45d79afe4..223e8f612 100644 --- a/src/backend/distributed/commands/statistics.c +++ b/src/backend/distributed/commands/statistics.c @@ -651,14 +651,15 @@ GetAlterIndexStatisticsCommands(Oid indexOid) } 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 *command = GenerateAlterIndexColumnSetStatsCommand(indexNameWithSchema, targetAttr->attnum, - targetAttr->attstattarget); + targetAttstattarget); alterIndexStatisticsCommandList = lappend(alterIndexStatisticsCommandList, diff --git a/src/backend/distributed/deparser/citus_ruleutils.c b/src/backend/distributed/deparser/citus_ruleutils.c index 3b387799b..87868238c 100644 --- a/src/backend/distributed/deparser/citus_ruleutils.c +++ b/src/backend/distributed/deparser/citus_ruleutils.c @@ -738,7 +738,12 @@ pg_get_tablecolumnoptionsdef_string(Oid tableRelationId) * If the user changed the column's statistics target, create * 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 }; initStringInfo(&statement); @@ -746,7 +751,7 @@ pg_get_tablecolumnoptionsdef_string(Oid tableRelationId) appendStringInfo(&statement, "ALTER COLUMN %s ", quote_identifier(attributeName)); appendStringInfo(&statement, "SET STATISTICS %d", - attributeForm->attstattarget); + targetAttstattarget); columnOptionList = lappend(columnOptionList, statement.data); } diff --git a/src/include/pg_version_compat.h b/src/include/pg_version_compat.h index 1407eccef..919c1f7bc 100644 --- a/src/include/pg_version_compat.h +++ b/src/include/pg_version_compat.h @@ -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 #if PG_VERSION_NUM >= PG_VERSION_16