Type of pg_buffercache_pages()::forknum

From: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Subject: Type of pg_buffercache_pages()::forknum
Date: 2025-11-12 05:16:12
Message-ID: CAExHW5s2_qwSdhKpVnUzjRMf0cf1PvmhUHQDLaFM3QzKbP1OyQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi All,
The SQL definition of pg_buffercache_pages() and pg_buffercache
declares relforknum as int2

CREATE FUNCTION pg_buffercache_pages()
RETURNS SETOF RECORD
AS 'MODULE_PATHNAME', 'pg_buffercache_pages'
LANGUAGE C PARALLEL SAFE;

-- Create a view for convenient access.
CREATE VIEW pg_buffercache AS
SELECT P.* FROM pg_buffercache_pages() AS P
(bufferid integer, relfilenode oid, reltablespace oid, reldatabase oid,
relforknumber int2, relblocknumber int8, isdirty bool, usagecount int2,
^^^^^^^^^^^^^^^^^
pinning_backends int4);

But C implementation uses ObjectIdGetDatum() where ObjectId is 32 bit integer.
values[4] = ObjectIdGetDatum(fctx->record[i].forknum);

Since forknum values are within 16 bits and that's unlikely to change
in future, int2 seems to ok. Also casting it to a wider integer, OID,
also doesn't seem like a correctness issue. But it does look
inconsistent and a reader of C implementation may think that the SQL
datatype used is int4. Should we instead change the C Implementation
to be

values[4] = Int16GetDatum((int16) fctx->record[i].forknum);

The code has been there for 17 years, and I didn't find any previous
complaints. I propose to change just the master branch.

--
Best Wishes,
Ashutosh Bapat

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2025-11-12 05:22:06 Re: [PATCH] Fix translation consistency issues in Korean, Chinese Traditional, and Tamil
Previous Message shveta malik 2025-11-12 05:04:29 Re: Rename sync_error_count to tbl_sync_error_count in subscription statistics