Re: BUG #19508: pg_buffercache_pages() crashes the backend with an incompatible caller-supplied record definition

Поиск
Список
Период
Сортировка
Искать

Re: BUG #19508: pg_buffercache_pages() crashes the backend with an incompatible caller-supplied record definition

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
Вложения:
On Fri, Jun 5, 2026 at 12:49 AM PG Bug reporting form
 wrote:
>
> The following bug has been logged on the website:
>
> Bug reference:      19508
> Logged by:          Nikita Kalinin
> Email address:      n.kalinin@postgrespro.ru
> PostgreSQL version: 19beta1
> Operating system:   Fedora 44
> Description:
>
> Hello,
>
> It appears that pg_buffercache_pages() trusts a caller-supplied record
> descriptor without verifying that the declared column types match the actual
> values returned by the function.
>
> The crash is reproducible on the current master branch with a fresh cluster
> after installing the extension:

Thanks for the report! I could reproduce this as well.


> git blame points to the following commit:
>
> commit 257c8231bf97a77378f6fedb826b1243f0a41612 (HEAD)
> Author: Heikki Linnakangas 
> Date:   Tue Apr 7 16:04:48 2026 +0300
>
>     Modernize and optimize pg_buffercache_pages()

Commit 257c8231bf9 changed pg_buffercache_pages() to materialize rows directly
into a tuplestore. As a result, the function started using the caller-supplied
RECORD descriptor as rsinfo->setDesc, so a mismatched column definition list
could cause tuplestore_putvalues() to interpret returned Datums with incorrect
types.

Before that change, pg_buffercache_pages() exposed its actual tuple descriptor
to the executor, allowing the executor's existing rowtype checks to reject
incompatible definitions with a normal error.

The attached patch restores that behavior while keeping the materialized-SRF
implementation. Thoughts?

Regards,

-- 
Fujii Masao

Re: BUG #19508: pg_buffercache_pages() crashes the backend with an incompatible caller-supplied record definition

От:
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Дата:
Hi,

On Fri, 5 Jun 2026 at 08:49, Fujii Masao <masao.fujii@gmail.com> wrote:

Commit 257c8231bf9 changed pg_buffercache_pages() to materialize rows directly
into a tuplestore. As a result, the function started using the caller-supplied
RECORD descriptor as rsinfo->setDesc, so a mismatched column definition list
could cause tuplestore_putvalues() to interpret returned Datums with incorrect
types.

Before that change, pg_buffercache_pages() exposed its actual tuple descriptor
to the executor, allowing the executor's existing rowtype checks to reject
incompatible definitions with a normal error.

The attached patch restores that behavior while keeping the materialized-SRF
implementation. Thoughts?

Thanks for the patch, Fujii-san!

I was looking into the bug last night, and the approach looks right to me.

This still means InitMaterializedSRF() briefly creates the caller-derived
descriptor before rsinfo->setDesc is replaced. That seems acceptable here:
the descriptor lives only in the per-query context, and avoiding a local
copy of InitMaterializedSRF() keeps the fix much smaller and less fragile.

One small nit: build_buffercache_pages_tupledesc() names attribute 8
"usage_count", while the existing pg_buffercache view and the test use
"usagecount". This probably does not affect the tupledesc_match() check,
but I think it would be better to keep the existing spelling for
consistency.

Regards,
Ayush

Re: BUG #19508: pg_buffercache_pages() crashes the backend with an incompatible caller-supplied record definition

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
Вложения:
On Fri, Jun 5, 2026 at 12:42 PM Ayush Tiwari
 wrote:
> One small nit: build_buffercache_pages_tupledesc() names attribute 8
> "usage_count", while the existing pg_buffercache view and the test use
> "usagecount". This probably does not affect the tupledesc_match() check,
> but I think it would be better to keep the existing spelling for
> consistency.

Agreed. I've fixed that and attached an updated version of the patch.

Regards,

-- 
Fujii Masao

Re: BUG #19508: pg_buffercache_pages() crashes the backend with an incompatible caller-supplied record definition

От:
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Дата:
Hi,

On Fri, 5 Jun, 2026, 13:28 Fujii Masao, <masao.fujii@gmail.com> wrote:
On Fri, Jun 5, 2026 at 12:42 PM Ayush Tiwari
<ayushtiwari.slg01@gmail.com> wrote:
> One small nit: build_buffercache_pages_tupledesc() names attribute 8
> "usage_count", while the existing pg_buffercache view and the test use
> "usagecount". This probably does not affect the tupledesc_match() check,
> but I think it would be better to keep the existing spelling for
> consistency.

Agreed. I've fixed that and attached an updated version of the patch.

LGTM.

Regards,
Ayush

Re: BUG #19508: pg_buffercache_pages() crashes the backend with an incompatible caller-supplied record definition

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Sat, Jun 6, 2026 at 12:29 AM Ashutosh Sharma  wrote:
> pg_buffercache_pages uses RETURNS SETOF RECORD whereas other
> extensions like pgstattuple define explicit IN/OUT parameters at the
> SQL level. Is there a specific reason this pattern was kept, or is it
> simply a legacy design that hasn't been modernized? Had we followed
> the IN/OUT parameter style, this sort of issue could have been
> avoided, no?

Probably yes. But if we do that, we would likely need to bump pg_buffercache
version. I'm not sure that's worthwhile just for this change.

Regards,

-- 
Fujii Masao


Re: BUG #19508: pg_buffercache_pages() crashes the backend with an incompatible caller-supplied record definition

От:
Ashutosh Sharma <ashu.coek88@gmail.com>
Дата:
On Fri, Jun 5, 2026 at 9:08 PM Fujii Masao  wrote:
>
> On Sat, Jun 6, 2026 at 12:29 AM Ashutosh Sharma  wrote:
> > pg_buffercache_pages uses RETURNS SETOF RECORD whereas other
> > extensions like pgstattuple define explicit IN/OUT parameters at the
> > SQL level. Is there a specific reason this pattern was kept, or is it
> > simply a legacy design that hasn't been modernized? Had we followed
> > the IN/OUT parameter style, this sort of issue could have been
> > avoided, no?
>
> Probably yes. But if we do that, we would likely need to bump pg_buffercache
> version. I'm not sure that's worthwhile just for this change.
>

Okay, that makes perfect sense, thanks for the confirmation.

--
With Regards,
Ashutosh Sharma.


Re: BUG #19508: pg_buffercache_pages() crashes the backend with an incompatible caller-supplied record definition

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Tue, Jun 9, 2026 at 8:44 AM Fujii Masao  wrote:
>
> On Fri, Jun 5, 2026 at 5:48 PM Ayush Tiwari  wrote:
> >> Agreed. I've fixed that and attached an updated version of the patch.
> >
> >
> > LGTM.
>
> Barring any objections, I will commit the patch.

I've pushed the patch. Thanks!

Regards,

-- 
Fujii Masao


FAQ