@@ -1425,8 +1425,7 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
1425
1425
1426
1426
for (i = 0 ; i < tupdesc -> natts ; i ++ )
1427
1427
{
1428
- Datum val ,
1429
- origval ;
1428
+ Datum val ;
1430
1429
bool isnull ;
1431
1430
char * attname ;
1432
1431
TYPCATEGORY tcategory ;
@@ -1445,7 +1444,7 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
1445
1444
escape_json (result , attname );
1446
1445
appendStringInfoChar (result , ':' );
1447
1446
1448
- origval = heap_getattr (tuple , i + 1 , tupdesc , & isnull );
1447
+ val = heap_getattr (tuple , i + 1 , tupdesc , & isnull );
1449
1448
1450
1449
getTypeOutputInfo (tupdesc -> attrs [i ]-> atttypid ,
1451
1450
& typoutput , & typisvarlena );
@@ -1480,20 +1479,7 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
1480
1479
else
1481
1480
tcategory = TypeCategory (tupdesc -> attrs [i ]-> atttypid );
1482
1481
1483
- /*
1484
- * If we have a toasted datum, forcibly detoast it here to avoid
1485
- * memory leakage inside the type's output routine.
1486
- */
1487
- if (typisvarlena && !isnull )
1488
- val = PointerGetDatum (PG_DETOAST_DATUM (origval ));
1489
- else
1490
- val = origval ;
1491
-
1492
1482
datum_to_json (val , isnull , result , tcategory , typoutput );
1493
-
1494
- /* Clean up detoasted copy, if any */
1495
- if (val != origval )
1496
- pfree (DatumGetPointer (val ));
1497
1483
}
1498
1484
1499
1485
appendStringInfoChar (result , '}' );
@@ -1572,10 +1558,9 @@ row_to_json_pretty(PG_FUNCTION_ARGS)
1572
1558
Datum
1573
1559
to_json (PG_FUNCTION_ARGS )
1574
1560
{
1561
+ Datum val = PG_GETARG_DATUM (0 );
1575
1562
Oid val_type = get_fn_expr_argtype (fcinfo -> flinfo , 0 );
1576
1563
StringInfo result ;
1577
- Datum orig_val ,
1578
- val ;
1579
1564
TYPCATEGORY tcategory ;
1580
1565
Oid typoutput ;
1581
1566
bool typisvarlena ;
@@ -1586,11 +1571,8 @@ to_json(PG_FUNCTION_ARGS)
1586
1571
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
1587
1572
errmsg ("could not determine input data type" )));
1588
1573
1589
-
1590
1574
result = makeStringInfo ();
1591
1575
1592
- orig_val = PG_ARGISNULL (0 ) ? (Datum ) 0 : PG_GETARG_DATUM (0 );
1593
-
1594
1576
getTypeOutputInfo (val_type , & typoutput , & typisvarlena );
1595
1577
1596
1578
if (val_type > FirstNormalObjectId )
@@ -1623,21 +1605,8 @@ to_json(PG_FUNCTION_ARGS)
1623
1605
else
1624
1606
tcategory = TypeCategory (val_type );
1625
1607
1626
- /*
1627
- * If we have a toasted datum, forcibly detoast it here to avoid memory
1628
- * leakage inside the type's output routine.
1629
- */
1630
- if (typisvarlena && orig_val != (Datum ) 0 )
1631
- val = PointerGetDatum (PG_DETOAST_DATUM (orig_val ));
1632
- else
1633
- val = orig_val ;
1634
-
1635
1608
datum_to_json (val , false, result , tcategory , typoutput );
1636
1609
1637
- /* Clean up detoasted copy, if any */
1638
- if (val != orig_val )
1639
- pfree (DatumGetPointer (val ));
1640
-
1641
1610
PG_RETURN_TEXT_P (cstring_to_text (result -> data ));
1642
1611
}
1643
1612
@@ -1651,8 +1620,7 @@ json_agg_transfn(PG_FUNCTION_ARGS)
1651
1620
MemoryContext aggcontext ,
1652
1621
oldcontext ;
1653
1622
StringInfo state ;
1654
- Datum orig_val ,
1655
- val ;
1623
+ Datum val ;
1656
1624
TYPCATEGORY tcategory ;
1657
1625
Oid typoutput ;
1658
1626
bool typisvarlena ;
@@ -1692,13 +1660,12 @@ json_agg_transfn(PG_FUNCTION_ARGS)
1692
1660
/* fast path for NULLs */
1693
1661
if (PG_ARGISNULL (1 ))
1694
1662
{
1695
- orig_val = (Datum ) 0 ;
1696
- datum_to_json (orig_val , true, state , 0 , InvalidOid );
1663
+ val = (Datum ) 0 ;
1664
+ datum_to_json (val , true, state , 0 , InvalidOid );
1697
1665
PG_RETURN_POINTER (state );
1698
1666
}
1699
1667
1700
-
1701
- orig_val = PG_GETARG_DATUM (1 );
1668
+ val = PG_GETARG_DATUM (1 );
1702
1669
1703
1670
getTypeOutputInfo (val_type , & typoutput , & typisvarlena );
1704
1671
@@ -1732,15 +1699,6 @@ json_agg_transfn(PG_FUNCTION_ARGS)
1732
1699
else
1733
1700
tcategory = TypeCategory (val_type );
1734
1701
1735
- /*
1736
- * If we have a toasted datum, forcibly detoast it here to avoid memory
1737
- * leakage inside the type's output routine.
1738
- */
1739
- if (typisvarlena )
1740
- val = PointerGetDatum (PG_DETOAST_DATUM (orig_val ));
1741
- else
1742
- val = orig_val ;
1743
-
1744
1702
if (!PG_ARGISNULL (0 ) &&
1745
1703
(tcategory == TYPCATEGORY_ARRAY || tcategory == TYPCATEGORY_COMPOSITE ))
1746
1704
{
@@ -1749,10 +1707,6 @@ json_agg_transfn(PG_FUNCTION_ARGS)
1749
1707
1750
1708
datum_to_json (val , false, state , tcategory , typoutput );
1751
1709
1752
- /* Clean up detoasted copy, if any */
1753
- if (val != orig_val )
1754
- pfree (DatumGetPointer (val ));
1755
-
1756
1710
/*
1757
1711
* The transition type for array_agg() is declared to be "internal", which
1758
1712
* is a pass-by-value type the same size as a pointer. So we can safely
0 commit comments