SET_END in statcollect()

After all rows are processed, statcollect() must transfer the statistics data from its statistics-collection structure into the predefined opaque type, stat. It is stat data that the UPDATE STATISTICS statement stores in the encdat column of the sysdistrib system catalog table.

The stat data type is a multirepresentational opaque data type; that is, it holds statistics data within its internal structure until the data reaches a predefined threshold. If the statistics data exceeds this threshold, the stat data type stores the data in a smart large object. In support of the multirepresentational data, the stat data type provides the following functions:
  • An assign() support function, which is responsible for determining whether or not the statistics data is to be stored in a smart large object

    If the data exceeds the predefined threshold, this assign() function creates the smart large object and increments its reference count. The database server calls this assign() function just before it inserts the mi_statret structure into the encdat column of the sysdistrib table.

  • A destroy() support function, which is responsible for deleting any smart large object that might exist to hold the statistics data

    The database server calls this destroy() function just before it deletes a row from the sysdistrib system catalog table in response to the DROP DISTRIBUTION clause of the UPDATE STATISTICS statement.

For UPDATE STATISTICS to be able to store the distribution data in the encdat column, the statcollect() function must copy its statistics-collection structure into the stat data type.

The internal structure of the stat opaque type is a C language structure named mi_statret. The stat support functions handle most of the interaction with the mi_statret structure; however, your statcollect() function must fill in the mi_statret multirepresentational fields.

For an exact declaration of mi_statret, see the milo.h header file. This header file also provides the following useful declarations.
mi_stat_buf
#define for the statdata.buffer field
mi_stat_mr
#define for the statdata.mr field
MI_STATMAXLEN
Constant for the size of the statdata.buffer field
mi_stat_hdrsize
Size of the information in the mi_statret structure that is not holding the statistics data (size of all fields except the statdata field)

The assign() and destroy() support functions of the stat opaque type determine whether to store the distribution data directly in the encdat column or in a smart large object. In the latter case, the encdat column stores the LO handle of the smart large object. Your statcollect() function can use the MI_STATMAXLEN constant to determine whether it needs to handle multirepresentational data.

The MI_STATMAXLEN constant is the maximum size that the encdat column of sysdistrib can hold. Therefore, it is the maximum size of the statdata.buffer array. If your distribution data has a size less than MI_STATMAXLEN, you can take the following actions:
  • Copy the data from the statistics-collection structure directly into the statdata.buffer field.
  • Set the statdata.szind field to MI_MULTIREP_SMALL to indicate that the multirepresentational data is not stored in a smart large object but is in the mi_statret structure.

The assign() and destroy() support functions of the stat opaque type take care of determining whether to store the distribution data directly in the encdat column or in a smart large object whose LO handle is stored in the encdat column.

If your distribution data exceeds MI_STATMAXLEN, your statcollect() function must handle the multirepresentational data itself, with the following steps:
  1. Create a new smart large object.
  2. Copy the data from the statistics-collection structure into the new smart large object.
  3. Copy the LO handle of this smart large object into the statdata.mr.mr_lo_struct.mr_s_lo field.
  4. Set the statdata.szind field to MI_MULTIREP_LARGE to indicate that the multirepresentational data is stored in a smart large object.