Line data Source code
1 : /*------------------------------------------------------------------------- 2 : * 3 : * pg_ndistinct.c 4 : * pg_ndistinct data type support. 5 : * 6 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group 7 : * Portions Copyright (c) 1994, Regents of the University of California 8 : * 9 : * IDENTIFICATION 10 : * src/backend/utils/adt/pg_ndistinct.c 11 : * 12 : *------------------------------------------------------------------------- 13 : */ 14 : 15 : #include "postgres.h" 16 : 17 : #include "lib/stringinfo.h" 18 : #include "statistics/extended_stats_internal.h" 19 : #include "utils/fmgrprotos.h" 20 : 21 : 22 : /* 23 : * pg_ndistinct_in 24 : * input routine for type pg_ndistinct 25 : * 26 : * pg_ndistinct is real enough to be a table column, but it has no 27 : * operations of its own, and disallows input (just like pg_node_tree). 28 : */ 29 : Datum 30 0 : pg_ndistinct_in(PG_FUNCTION_ARGS) 31 : { 32 0 : ereport(ERROR, 33 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), 34 : errmsg("cannot accept a value of type %s", "pg_ndistinct"))); 35 : 36 : PG_RETURN_VOID(); /* keep compiler quiet */ 37 : } 38 : 39 : /* 40 : * pg_ndistinct_out 41 : * output routine for type pg_ndistinct 42 : * 43 : * Produces a human-readable representation of the value. 44 : */ 45 : Datum 46 24 : pg_ndistinct_out(PG_FUNCTION_ARGS) 47 : { 48 24 : bytea *data = PG_GETARG_BYTEA_PP(0); 49 24 : MVNDistinct *ndist = statext_ndistinct_deserialize(data); 50 : int i; 51 : StringInfoData str; 52 : 53 24 : initStringInfo(&str); 54 24 : appendStringInfoChar(&str, '{'); 55 : 56 120 : for (i = 0; i < ndist->nitems; i++) 57 : { 58 : int j; 59 96 : MVNDistinctItem item = ndist->items[i]; 60 : 61 96 : if (i > 0) 62 72 : appendStringInfoString(&str, ", "); 63 : 64 312 : for (j = 0; j < item.nattributes; j++) 65 : { 66 216 : AttrNumber attnum = item.attributes[j]; 67 : 68 216 : appendStringInfo(&str, "%s%d", (j == 0) ? "\"" : ", ", attnum); 69 : } 70 96 : appendStringInfo(&str, "\": %d", (int) item.ndistinct); 71 : } 72 : 73 24 : appendStringInfoChar(&str, '}'); 74 : 75 24 : PG_RETURN_CSTRING(str.data); 76 : } 77 : 78 : /* 79 : * pg_ndistinct_recv 80 : * binary input routine for type pg_ndistinct 81 : */ 82 : Datum 83 0 : pg_ndistinct_recv(PG_FUNCTION_ARGS) 84 : { 85 0 : ereport(ERROR, 86 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), 87 : errmsg("cannot accept a value of type %s", "pg_ndistinct"))); 88 : 89 : PG_RETURN_VOID(); /* keep compiler quiet */ 90 : } 91 : 92 : /* 93 : * pg_ndistinct_send 94 : * binary output routine for type pg_ndistinct 95 : * 96 : * n-distinct is serialized into a bytea value, so let's send that. 97 : */ 98 : Datum 99 0 : pg_ndistinct_send(PG_FUNCTION_ARGS) 100 : { 101 0 : return byteasend(fcinfo); 102 : }