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

Skip to content
8000

Commit 678e0d9

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 1cf269c commit 678e0d9

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-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.108.2.1 2003/12/03 17:45:37 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.108.2.2 2010/07/09 22:58:25 tgl Exp $
1111
*
1212
* NOTES
1313
* Eventually, the index information should go through here, too.
@@ -1796,6 +1796,10 @@ get_attavgwidth(Oid relid, AttrNumber attnum)
17961796
* If the attribute type is pass-by-reference, the values referenced by
17971797
* the values array are themselves palloc'd. The palloc'd stuff can be
17981798
* freed by calling free_attstatsslot.
1799+
*
1800+
* Note: at present, atttype/atttypmod aren't actually used here at all.
1801+
* But the caller must have the correct (or at least binary-compatible)
1802+
* type ID to pass to free_attstatsslot later.
17991803
*/
18001804
bool
18011805
get_attstatsslot(HeapTuple statstuple,
@@ -1810,6 +1814,7 @@ get_attstatsslot(HeapTuple statstuple,
18101814
Datum val;
18111815
bool isnull;
18121816
ArrayType *statarray;
1817+
Oid arrayelemtype;
18131818
int narrayelem;
18141819
HeapTuple typeTuple;
18151820
Form_pg_type typeForm;
@@ -1832,17 +1837,24 @@ get_attstatsslot(HeapTuple statstuple,
18321837
elog(ERROR, "stavalues is null");
18331838
statarray = DatumGetArrayTypeP(val);
18341839

1835-
/* Need to get info about the array element type */
1840+
/*
1841+
* Need to get info about the array element type. We look at the
1842+
* actual element type embedded in the array, which might be only
1843+
* binary-compatible with the passed-in atttype. The info we
1844+
* extract here should be the same either way, but deconstruct_array
1845+
* is picky about having an exact type OID match.
1846+
*/
1847+
arrayelemtype = ARR_ELEMTYPE(statarray);
18361848
typeTuple = SearchSysCache(TYPEOID,
1837-
ObjectIdGetDatum(atttype),
83FB 1849+
ObjectIdGetDatum(arrayelemtype),
18381850
0, 0, 0);
18391851
if (!HeapTupleIsValid(typeTuple))
1840-
elog(ERROR, "cache lookup failed for type %u", atttype);
1852+
elog(ERROR, "cache lookup failed for type %u", arrayelemtype);
18411853
typeForm = (Form_pg_type) GETSTRUCT(typeTuple);
18421854

18431855
/* Deconstruct array into Datum elements */
18441856
deconstruct_array(statarray,
1845-
atttype,
1857+
arrayelemtype,
18461858
typeForm->typlen,
18471859
typeForm->typbyval,
18481860
typeForm->typalign,

0 commit comments

Comments
 (0)
0