8000 Avoid an Assert failure in deconstruct_array() by making get_attstats… · danielcode/postgres@dae1190 · GitHub
[go: up one dir, main page]

Skip to content

Commit dae1190

Browse files
committed
Avoid an Assert failure in deconstruct_array() by making get_attstatsslot()
use the actual element type of the array it's disassembling, rather than trusting the type OID passed in by its caller. This is needed because sometimes the planner passes in a type OID that's only binary-compatible with the target column's type, rather than being an exact match. Per an example from Bernd Helmle. Possibly we should refactor get_attstatsslot/free_attstatsslot to not expect the caller to supply type ID data at all, but for now I'll just do the minimum-change fix. Back-patch to 7.4. Bernd's test case only crashes back to 8.0, but since these subroutines are the same in 7.4, I suspect there may be variant cases that would crash 7.4 as well.
1 parent accabac commit dae1190

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/backend/utils/cache/lsyscache.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/utils/cache/lsyscache.c,v 1.129.2.1 2007/10/13 15:55:58 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/cache/lsyscache.c,v 1.129.2.2 2010/07/09 22:58:12 tgl Exp $
1111
*
1212
* NOTES
1313
* Eventually, the index information should go through here, too.
@@ -1881,6 +1881,10 @@ get_attavgwidth(Oid relid, AttrNumber attnum)
18811881
* If the attribute type is pass-by-reference, the values referenced by
18821882
* the values array are themselves palloc'd. The palloc'd stuff can be
18831883
* freed by calling free_attstatsslot.
1884+
*
1885+
* Note: at present, atttype/atttypmod aren't actually used here at all.
1886+
* But the caller must have the correct (or at least binary-compatible)
1887+
* type ID to pass to free_attstatsslot later.
18841888
*/
18851889
bool
18861890
get_attstatsslot(HeapTuple statstuple,
@@ -1895,6 +1899,7 @@ get_attstatsslot(HeapTuple statstuple,
18951899
Datum val;
18961900
bool isnull;
18971901
ArrayType *statarray;
1902+
Oid arrayelemtype;
18981903
int narrayelem;
18991904
HeapTuple typeTuple;
19001905
Form_pg_type typeForm;
@@ -1917,17 +1922,24 @@ get_attstatsslot(HeapTuple statstuple,
19171922
elog(ERROR, "stavalues is null");
19181923
statarray = DatumGetArrayTypeP(val);
19191924

1920-
/* Need to get info about the array element type */
1925+
/*
1926+
* Need to get info about the array element type. We look at the
1927+
* actual element type embedded in the array, which might be only
1928+
* binary-compatible with the passed-in atttype. The info we
1929+
* extract here should be the same either way, but deconstruct_array
1930+
* is picky about having an exact type OID match.
1931+
*/
1932+
arrayelemtype = ARR_ELEMTYPE(statarray);
19211933
typeTuple = SearchSysCache(TYPEOID,
1922-
ObjectIdGetDatum(atttype),
1934+
ObjectIdGetDatum(arrayelemtype),
19231935
0, 0, 0);
19241936
if (!HeapTupleIsValid(typeTuple))
1925-
elog(ERROR, "cache lookup failed for type %u", atttype);
1937+
elog(ERROR, "cache lookup failed for type %u", arrayelemtype);
19261938
typeForm = (Form_pg_type) GETSTRUCT(typeTuple);
19271939

19281940
/* Deconstruct array into Datum elements */
19291941
deconstruct_array(statarray,
1930-
atttype,
1942+
arrayelemtype,
19311943
typeForm->typlen,
19321944
typeForm->typbyval,
19331945
typeForm->typalign,

0 commit comments

Comments
 (0)
0