1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-06 18:42:54 +03:00

Increase width of RelFileNumbers from 32 bits to 56 bits.

RelFileNumbers are now assigned using a separate counter, instead of
being assigned from the OID counter. This counter never wraps around:
if all 2^56 possible RelFileNumbers are used, an internal error
occurs. As the cluster is limited to 2^64 total bytes of WAL, this
limitation should not cause a problem in practice.

If the counter were 64 bits wide rather than 56 bits wide, we would
need to increase the width of the BufferTag, which might adversely
impact buffer lookup performance. Also, this lets us use bigint for
pg_class.relfilenode and other places where these values are exposed
at the SQL level without worrying about overflow.

This should remove the need to keep "tombstone" files around until
the next checkpoint when relations are removed. We do that to keep
RelFileNumbers from being recycled, but now that won't happen
anyway. However, this patch doesn't actually change anything in
this area; it just makes it possible for a future patch to do so.

Dilip Kumar, based on an idea from Andres Freund, who also reviewed
some earlier versions of the patch. Further review and some
wordsmithing by me. Also reviewed at various points by Ashutosh
Sharma, Vignesh C, Amul Sul, Álvaro Herrera, and Tom Lane.

Discussion: http://postgr.es/m/CA+Tgmobp7+7kmi4gkq7Y+4AM9fTvL+O1oQ4-5gFTT+6Ng-dQ=g@mail.gmail.com
This commit is contained in:
Robert Haas
2022-09-27 13:25:21 -04:00
parent 2f47715cc8
commit 05d4cbf9b6
70 changed files with 693 additions and 289 deletions

View File

@@ -6,8 +6,8 @@ OBJS = \
pg_buffercache_pages.o
EXTENSION = pg_buffercache
DATA = pg_buffercache--1.2.sql pg_buffercache--1.2--1.3.sql \
pg_buffercache--1.1--1.2.sql pg_buffercache--1.0--1.1.sql
DATA = pg_buffercache--1.0--1.1.sql pg_buffercache--1.1--1.2.sql pg_buffercache--1.2.sql \
pg_buffercache--1.2--1.3.sql pg_buffercache--1.3--1.4.sql
PGFILEDESC = "pg_buffercache - monitoring of shared buffer cache in real-time"
REGRESS = pg_buffercache

View File

@@ -0,0 +1,30 @@
/* contrib/pg_buffercache/pg_buffercache--1.3--1.4.sql */
-- complain if script is sourced in psql, rather than via ALTER EXTENSION
\echo Use "ALTER EXTENSION pg_buffercache UPDATE TO '1.4'" to load this file. \quit
/* First we have to remove them from the extension */
ALTER EXTENSION pg_buffercache DROP VIEW pg_buffercache;
ALTER EXTENSION pg_buffercache DROP FUNCTION pg_buffercache_pages();
/* Then we can drop them */
DROP VIEW pg_buffercache;
DROP FUNCTION pg_buffercache_pages();
/* Now redefine */
CREATE FUNCTION pg_buffercache_pages()
RETURNS SETOF RECORD
AS 'MODULE_PATHNAME', 'pg_buffercache_pages_v1_4'
LANGUAGE C PARALLEL SAFE;
CREATE VIEW pg_buffercache AS
SELECT P.* FROM pg_buffercache_pages() AS P
(bufferid integer, relfilenode int8, reltablespace oid, reldatabase oid,
relforknumber int2, relblocknumber int8, isdirty bool, usagecount int2,
pinning_backends int4);
-- Don't want these to be available to public.
REVOKE ALL ON FUNCTION pg_buffercache_pages() FROM PUBLIC;
REVOKE ALL ON pg_buffercache FROM PUBLIC;
GRANT EXECUTE ON FUNCTION pg_buffercache_pages() TO pg_monitor;
GRANT SELECT ON pg_buffercache TO pg_monitor;

View File

@@ -1,5 +1,5 @@
# pg_buffercache extension
comment = 'examine the shared buffer cache'
default_version = '1.3'
default_version = '1.4'
module_pathname = '$libdir/pg_buffercache'
relocatable = true

View File

@@ -59,9 +59,10 @@ typedef struct
* relation node/tablespace/database/blocknum and dirty indicator.
*/
PG_FUNCTION_INFO_V1(pg_buffercache_pages);
PG_FUNCTION_INFO_V1(pg_buffercache_pages_v1_4);
Datum
pg_buffercache_pages(PG_FUNCTION_ARGS)
static Datum
pg_buffercache_pages_internal(PG_FUNCTION_ARGS, Oid rfn_typid)
{
FuncCallContext *funcctx;
Datum result;
@@ -103,7 +104,7 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
TupleDescInitEntry(tupledesc, (AttrNumber) 1, "bufferid",
INT4OID, -1, 0);
TupleDescInitEntry(tupledesc, (AttrNumber) 2, "relfilenode",
OIDOID, -1, 0);
rfn_typid, -1, 0);
TupleDescInitEntry(tupledesc, (AttrNumber) 3, "reltablespace",
OIDOID, -1, 0);
TupleDescInitEntry(tupledesc, (AttrNumber) 4, "reldatabase",
@@ -209,7 +210,24 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
}
else
{
values[1] = ObjectIdGetDatum(fctx->record[i].relfilenumber);
if (rfn_typid == INT8OID)
values[1] =
Int64GetDatum((int64) fctx->record[i].relfilenumber);
else
{
Assert(rfn_typid == OIDOID);
if (fctx->record[i].relfilenumber > OID_MAX)
ereport(ERROR,
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("relfilenode %llu is too large to be represented as an OID",
(unsigned long long) fctx->record[i].relfilenumber),
errhint("Upgrade the extension using ALTER EXTENSION pg_buffercache UPDATE"));
values[1] =
ObjectIdGetDatum((Oid) fctx->record[i].relfilenumber);
}
nulls[1] = false;
values[2] = ObjectIdGetDatum(fctx->record[i].reltablespace);
nulls[2] = false;
@@ -237,3 +255,16 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
else
SRF_RETURN_DONE(funcctx);
}
/* entry point for old extension version */
Datum
pg_buffercache_pages(PG_FUNCTION_ARGS)
{
return pg_buffercache_pages_internal(fcinfo, OIDOID);
}
Datum
pg_buffercache_pages_v1_4(PG_FUNCTION_ARGS)
{
return pg_buffercache_pages_internal(fcinfo, INT8OID);
}