* get_typavgwidth * * Given a type OID and a typmod value (pass -1 if typmod is unknown), * estimate the average width of values of the type. This is used by * the planner, which doesn't require absolutely correct results; * it's OK (and expected) to guess if we don't know for sure. */
| 3176 | * it's OK (and expected) to guess if we don't know for sure. |
| 3177 | */ |
| 3178 | int32 |
| 3179 | get_typavgwidth(Oid typid, int32 typmod) |
| 3180 | { |
| 3181 | int typlen = get_typlen(typid); |
| 3182 | int32 maxwidth; |
| 3183 | |
| 3184 | /* |
| 3185 | * Easy if it's a fixed-width type |
| 3186 | */ |
| 3187 | if (typlen > 0) |
| 3188 | return typlen; |
| 3189 | |
| 3190 | /* |
| 3191 | * type_maximum_size knows the encoding of typmod for some datatypes; |
| 3192 | * don't duplicate that knowledge here. |
| 3193 | */ |
| 3194 | maxwidth = type_maximum_size(typid, typmod); |
| 3195 | if (maxwidth > 0) |
| 3196 | { |
| 3197 | /* |
| 3198 | * For BPCHAR, the max width is also the only width. Otherwise we |
| 3199 | * need to guess about the typical data width given the max. A sliding |
| 3200 | * scale for percentage of max width seems reasonable. |
| 3201 | */ |
| 3202 | if (typid == BPCHAROID) |
| 3203 | return maxwidth; |
| 3204 | if (maxwidth <= 32) |
| 3205 | return maxwidth; /* assume full width */ |
| 3206 | if (maxwidth < 1000) |
| 3207 | return 32 + (maxwidth - 32) / 2; /* assume 50% */ |
| 3208 | |
| 3209 | /* |
| 3210 | * Beyond 1000, assume we're looking at something like |
| 3211 | * "varchar(10000)" where the limit isn't actually reached often, and |
| 3212 | * use a fixed estimate. |
| 3213 | */ |
| 3214 | return 32 + (1000 - 32) / 2; |
| 3215 | } |
| 3216 | |
| 3217 | /* |
| 3218 | * Oops, we have no idea ... wild guess time. |
| 3219 | */ |
| 3220 | return 32; |
| 3221 | } |
| 3222 | |
| 3223 | /* |
| 3224 | * get_typtype |
no test coverage detected