diff options
author | Nathan Bossart <nathan@postgresql.org> | 2024-03-14 10:58:00 -0500 |
---|---|---|
committer | Nathan Bossart <nathan@postgresql.org> | 2024-03-14 10:58:00 -0500 |
commit | d1162cfda885c5a8cb9cebfc8eed9f1d76855e83 (patch) | |
tree | 7211679289e248d36e3129aa19f6518ac53ec584 /src/backend | |
parent | 84c18acaf690e438e953e387caf1c13298d4ecb4 (diff) | |
download | postgresql-d1162cfda885c5a8cb9cebfc8eed9f1d76855e83.tar.gz postgresql-d1162cfda885c5a8cb9cebfc8eed9f1d76855e83.zip |
Add pg_column_toast_chunk_id().
This function returns the chunk_id of an on-disk TOASTed value. If
the value is un-TOASTed or not on-disk, it returns NULL. This is
useful for identifying which values are actually TOASTed and for
investigating "unexpected chunk number" errors.
Bumps catversion.
Author: Yugo Nagata
Reviewed-by: Jian He
Discussion: https://postgr.es/m/20230329105507.d764497456eeac1ca491b5bd%40sraoss.co.jp
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/utils/adt/varlena.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 543afb66e58..8d28dd42ce1 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -5106,6 +5106,47 @@ pg_column_compression(PG_FUNCTION_ARGS) } /* + * Return the chunk_id of the on-disk TOASTed value. Return NULL if the value + * is un-TOASTed or not on-disk. + */ +Datum +pg_column_toast_chunk_id(PG_FUNCTION_ARGS) +{ + int typlen; + struct varlena *attr; + struct varatt_external toast_pointer; + + /* On first call, get the input type's typlen, and save at *fn_extra */ + if (fcinfo->flinfo->fn_extra == NULL) + { + /* Lookup the datatype of the supplied argument */ + Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0); + + typlen = get_typlen(argtypeid); + if (typlen == 0) /* should not happen */ + elog(ERROR, "cache lookup failed for type %u", argtypeid); + + fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, + sizeof(int)); + *((int *) fcinfo->flinfo->fn_extra) = typlen; + } + else + typlen = *((int *) fcinfo->flinfo->fn_extra); + + if (typlen != -1) + PG_RETURN_NULL(); + + attr = (struct varlena *) DatumGetPointer(PG_GETARG_DATUM(0)); + + if (!VARATT_IS_EXTERNAL_ONDISK(attr)) + PG_RETURN_NULL(); + + VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr); + + PG_RETURN_OID(toast_pointer.va_valueid); +} + +/* * string_agg - Concatenates values and returns string. * * Syntax: string_agg(value text, delimiter text) RETURNS text |