* BuildParamLogString * Return a string that represents the parameter list, for logging. * * If caller already knows textual representations for some parameters, it can * pass an array of exactly params->numParams values as knownTextValues, which * can contain NULLs for any unknown individual values. NULL can be given if * no parameters are known. * * If maxlen is >= 0, that's the maximu
| 332 | * longer. (Added quotes are not considered in this calculation.) |
| 333 | */ |
| 334 | char * |
| 335 | BuildParamLogString(ParamListInfo params, char **knownTextValues, int maxlen) |
| 336 | { |
| 337 | MemoryContext tmpCxt, |
| 338 | oldCxt; |
| 339 | StringInfoData buf; |
| 340 | |
| 341 | /* |
| 342 | * NB: think not of returning params->paramValuesStr! It may have been |
| 343 | * generated with a different maxlen, and so be unsuitable. Besides that, |
| 344 | * this is the function used to create that string. |
| 345 | */ |
| 346 | |
| 347 | /* |
| 348 | * No work if the param fetch hook is in use. Also, it's not possible to |
| 349 | * do this in an aborted transaction. (It might be possible to improve on |
| 350 | * this last point when some knownTextValues exist, but it seems tricky.) |
| 351 | */ |
| 352 | if (params->paramFetch != NULL || |
| 353 | IsAbortedTransactionBlockState()) |
| 354 | return NULL; |
| 355 | |
| 356 | /* Initialize the output stringinfo, in caller's memory context */ |
| 357 | initStringInfo(&buf); |
| 358 | |
| 359 | /* Use a temporary context to call output functions, just in case */ |
| 360 | tmpCxt = AllocSetContextCreate(CurrentMemoryContext, |
| 361 | "BuildParamLogString", |
| 362 | ALLOCSET_DEFAULT_SIZES); |
| 363 | oldCxt = MemoryContextSwitchTo(tmpCxt); |
| 364 | |
| 365 | for (int paramno = 0; paramno < params->numParams; paramno++) |
| 366 | { |
| 367 | ParamExternData *param = ¶ms->params[paramno]; |
| 368 | |
| 369 | appendStringInfo(&buf, |
| 370 | "%s$%d = ", |
| 371 | paramno > 0 ? ", " : "", |
| 372 | paramno + 1); |
| 373 | |
| 374 | if (param->isnull || !OidIsValid(param->ptype)) |
| 375 | appendStringInfoString(&buf, "NULL"); |
| 376 | else |
| 377 | { |
| 378 | if (knownTextValues != NULL && knownTextValues[paramno] != NULL) |
| 379 | appendStringInfoStringQuoted(&buf, knownTextValues[paramno], |
| 380 | maxlen); |
| 381 | else |
| 382 | { |
| 383 | Oid typoutput; |
| 384 | bool typisvarlena; |
| 385 | char *pstring; |
| 386 | |
| 387 | getTypeOutputInfo(param->ptype, &typoutput, &typisvarlena); |
| 388 | pstring = OidOutputFunctionCall(typoutput, param->value); |
| 389 | appendStringInfoStringQuoted(&buf, pstring, maxlen); |
| 390 | } |
| 391 | } |
no test coverage detected