Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 691e8b2

Browse files
pageinspect: Fix types used for bt_metap() columns.
The data types that contrib/pageinspect's bt_metap() function were declared to return as OUT arguments were wrong in some cases. For example, the oldest_xact column (a TransactionId/xid field) was declared integer/int4 within the pageinspect extension's sql file. This led to errors when an oldest_xact value that exceeded 2^31-1 was encountered. Some of the other columns were defined incorrectly ever since pageinspect was first introduced, though they were far less likely to produce problems in practice. Fix these issues by changing the declaration of bt_metap() to consistently use data types that can reliably represent all possible values. This fixes things on HEAD only. No backpatch, since it doesn't seem like there is a safe way to fix the issue without including a new version of the pageinspect extension (HEAD/Postgres 13 already introduced a new version of the extension). Besides, the oldest_xact issue has been around since the release of Postgres 11, and we haven't heard any complaints about it before now. Also, throw an error when we detect a bt_metap() declaration that must be from an old version of the pageinspect extension by examining the number of attributes from the tuple descriptor for the return tuples. It seems better to throw an error in a reliable and obvious way following a Postgres upgrade, rather than letting bt_metap() fail unpredictably. The problem is fundamentally with the CREATE FUNCTION declared data types themselves, so I see no sensible alternative. Reported-By: Victor Yegorov Bug: #16285 Discussion: https://postgr.es/m/16285-df8fc1000ab3d5fc@postgresql.org
1 parent b9c3de6 commit 691e8b2

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

contrib/pageinspect/btreefuncs.c

+23-4
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,8 @@ bt_page_items_bytea(PG_FUNCTION_ARGS)
597597
}
598598
}
599599

600+
/* Number of output arguments (columns) for bt_metap() */
601+
#define BT_METAP_COLS_V1_8 9
600602

601603
/* ------------------------------------------------
602604
* bt_metap()
@@ -653,13 +655,30 @@ bt_metap(PG_FUNCTION_ARGS)
653655
if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
654656
elog(ERROR, "return type must be a row type");
655657

658+
/*
659+
* We need a kluge here to detect API versions prior to 1.8. Earlier
660+
* versions incorrectly used int4 for certain columns. This caused
661+
* various problems. For example, an int4 version of the "oldest_xact"
662+
* column would not work with TransactionId values that happened to exceed
663+
* PG_INT32_MAX.
664+
*
665+
* There is no way to reliably avoid the problems created by the old
666+
* function definition at this point, so insist that the user update the
667+
* extension.
668+
*/
669+
if (tupleDesc->natts < BT_METAP_COLS_V1_8)
670+
ereport(ERROR,
671+
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
672+
errmsg("function has wrong number of declared columns"),
673+
errhint("To resolve the problem, update the \"pageinspect\" extension to the latest version.")));
674+
656675
j = 0;
657676
values[j++] = psprintf("%d", metad->btm_magic);
658677
values[j++] = psprintf("%d", metad->btm_version);
659-
values[j++] = psprintf("%d", metad->btm_root);
660-
values[j++] = psprintf("%d", metad->btm_level);
661-
values[j++] = psprintf("%d", metad->btm_fastroot);
662-
values[j++] = psprintf("%d", metad->btm_fastlevel);
678+
values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_root);
679+
values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_level);
680+
values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_fastroot);
681+
values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_fastlevel);
663682

664683
/*
665684
* Get values of extended metadata if available, use default values

contrib/pageinspect/pageinspect--1.7--1.8.sql

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ DROP FUNCTION bt_metap(text);
2222
CREATE FUNCTION bt_metap(IN relname text,
2323
OUT magic int4,
2424
OUT version int4,
25-
OUT root int4,
26-
OUT level int4,
27-
OUT fastroot int4,
28-
OUT fastlevel int4,
29-
OUT oldest_xact int4,
30-
OUT last_cleanup_num_tuples real,
25+
OUT root int8,
26+
OUT level int8,
27+
OUT fastroot int8,
28+
OUT fastlevel int8,
29+
OUT oldest_xact xid,
30+
OUT last_cleanup_num_tuples float8,
3131
OUT allequalimage boolean)
3232
AS 'MODULE_PATHNAME', 'bt_metap'
3333
LANGUAGE C STRICT PARALLEL SAFE;

0 commit comments

Comments
 (0)