Skip to content

Commit 05d4cbf

Browse files
committed
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
1 parent 2f47715 commit 05d4cbf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+694
-290
lines changed

contrib/pg_buffercache/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ OBJS = \
66
pg_buffercache_pages.o
77

88
EXTENSION = pg_buffercache
9-
DATA = pg_buffercache--1.2.sql pg_buffercache--1.2--1.3.sql \
10-
pg_buffercache--1.1--1.2.sql pg_buffercache--1.0--1.1.sql
9+
DATA = pg_buffercache--1.0--1.1.sql pg_buffercache--1.1--1.2.sql pg_buffercache--1.2.sql \
10+
pg_buffercache--1.2--1.3.sql pg_buffercache--1.3--1.4.sql
1111
PGFILEDESC = "pg_buffercache - monitoring of shared buffer cache in real-time"
1212

1313
REGRESS = pg_buffercache
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* contrib/pg_buffercache/pg_buffercache--1.3--1.4.sql */
2+
3+
-- complain if script is sourced in psql, rather than via ALTER EXTENSION
4+
\echo Use "ALTER EXTENSION pg_buffercache UPDATE TO '1.4'" to load this file. \quit
5+
6+
/* First we have to remove them from the extension */
7+
ALTER EXTENSION pg_buffercache DROP VIEW pg_buffercache;
8+
ALTER EXTENSION pg_buffercache DROP FUNCTION pg_buffercache_pages();
9+
10+
/* Then we can drop them */
11+
DROP VIEW pg_buffercache;
12+
DROP FUNCTION pg_buffercache_pages();
13+
14+
/* Now redefine */
15+
CREATE FUNCTION pg_buffercache_pages()
16+
RETURNS SETOF RECORD
17+
AS 'MODULE_PATHNAME', 'pg_buffercache_pages_v1_4'
18+
LANGUAGE C PARALLEL SAFE;
19+
20+
CREATE VIEW pg_buffercache AS
21+
SELECT P.* FROM pg_buffercache_pages() AS P
22+
(bufferid integer, relfilenode int8, reltablespace oid, reldatabase oid,
23+
relforknumber int2, relblocknumber int8, isdirty bool, usagecount int2,
24+
pinning_backends int4);
25+
26+
-- Don't want these to be available to public.
27+
REVOKE ALL ON FUNCTION pg_buffercache_pages() FROM PUBLIC;
28+
REVOKE ALL ON pg_buffercache FROM PUBLIC;
29+
GRANT EXECUTE ON FUNCTION pg_buffercache_pages() TO pg_monitor;
30+
GRANT SELECT ON pg_buffercache TO pg_monitor;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pg_buffercache extension
22
comment = 'examine the shared buffer cache'
3-
default_version = '1.3'
3+
default_version = '1.4'
44
module_pathname = '$libdir/pg_buffercache'
55
relocatable = true

contrib/pg_buffercache/pg_buffercache_pages.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ typedef struct
5959
* relation node/tablespace/database/blocknum and dirty indicator.
6060
*/
6161
PG_FUNCTION_INFO_V1(pg_buffercache_pages);
62+
PG_FUNCTION_INFO_V1(pg_buffercache_pages_v1_4);
6263

63-
Datum
64-
pg_buffercache_pages(PG_FUNCTION_ARGS)
64+
static Datum
65+
pg_buffercache_pages_internal(PG_FUNCTION_ARGS, Oid rfn_typid)
6566
{
6667
FuncCallContext *funcctx;
6768
Datum result;
@@ -103,7 +104,7 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
103104
TupleDescInitEntry(tupledesc, (AttrNumber) 1, "bufferid",
104105
INT4OID, -1, 0);
105106
TupleDescInitEntry(tupledesc, (AttrNumber) 2, "relfilenode",
106-
OIDOID, -1, 0);
107+
rfn_typid, -1, 0);
107108
TupleDescInitEntry(tupledesc, (AttrNumber) 3, "reltablespace",
108109
OIDOID, -1, 0);
109110
TupleDescInitEntry(tupledesc, (AttrNumber) 4, "reldatabase",
@@ -209,7 +210,24 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
209210
}
210211
else
211212
{
212-
values[1] = ObjectIdGetDatum(fctx->record[i].relfilenumber);
213+
if (rfn_typid == INT8OID)
214+
values[1] =
215+
Int64GetDatum((int64) fctx->record[i].relfilenumber);
216+
else
217+
{
218+
Assert(rfn_typid == OIDOID);
219+
220+
if (fctx->record[i].relfilenumber > OID_MAX)
221+
ereport(ERROR,
222+
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
223+
errmsg("relfilenode %llu is too large to be represented as an OID",
224+
(unsigned long long) fctx->record[i].relfilenumber),
225+
errhint("Upgrade the extension using ALTER EXTENSION pg_buffercache UPDATE"));
226+
227+
values[1] =
228+
ObjectIdGetDatum((Oid) fctx->record[i].relfilenumber);
229+
}
230+
213231
nulls[1] = false;
214232
values[2] = ObjectIdGetDatum(fctx->record[i].reltablespace);
215233
nulls[2] = false;
@@ -237,3 +255,16 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
237255
else
238256
SRF_RETURN_DONE(funcctx);
239257
}
258+
259+
/* entry point for old extension version */
260+
Datum
261+
pg_buffercache_pages(PG_FUNCTION_ARGS)
262+
{
263+
return pg_buffercache_pages_internal(fcinfo, OIDOID);
264+
}
265+
266+
Datum
267+
pg_buffercache_pages_v1_4(PG_FUNCTION_ARGS)
268+
{
269+
return pg_buffercache_pages_internal(fcinfo, INT8OID);
270+
}

contrib/pg_prewarm/autoprewarm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ apw_load_buffers(void)
345345
{
346346
unsigned forknum;
347347

348-
if (fscanf(file, "%u,%u,%u,%u,%u\n", &blkinfo[i].database,
348+
if (fscanf(file, "%u,%u," UINT64_FORMAT ",%u,%u\n", &blkinfo[i].database,
349349
&blkinfo[i].tablespace, &blkinfo[i].filenumber,
350350
&forknum, &blkinfo[i].blocknum) != 5)
351351
ereport(ERROR,
@@ -669,7 +669,7 @@ apw_dump_now(bool is_bgworker, bool dump_unlogged)
669669
{
670670
CHECK_FOR_INTERRUPTS();
671671

672-
ret = fprintf(file, "%u,%u,%u,%u,%u\n",
672+
ret = fprintf(file, "%u,%u," UINT64_FORMAT ",%u,%u\n",
673673
block_info_array[i].database,
674674
block_info_array[i].tablespace,
675675
block_info_array[i].filenumber,

contrib/pg_walinspect/expected/pg_walinspect.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ SELECT COUNT(*) >= 0 AS ok FROM pg_get_wal_stats_till_end_of_wal(:'wal_lsn1');
5454
-- ===================================================================
5555
-- Test for filtering out WAL records of a particular table
5656
-- ===================================================================
57-
SELECT oid AS sample_tbl_oid FROM pg_class WHERE relname = 'sample_tbl' \gset
57+
SELECT relfilenode AS sample_tbl_relfilenode FROM pg_class WHERE relname = 'sample_tbl' \gset
5858
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_records_info(:'wal_lsn1', :'wal_lsn2')
59-
WHERE block_ref LIKE concat('%', :'sample_tbl_oid', '%') AND resource_manager = 'Heap';
59+
WHERE block_ref LIKE concat('%', :'sample_tbl_relfilenode', '%') AND resource_manager = 'Heap';
6060
ok
6161
----
6262
t

contrib/pg_walinspect/sql/pg_walinspect.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ SELECT COUNT(*) >= 0 AS ok FROM pg_get_wal_stats_till_end_of_wal(:'wal_lsn1');
3939
-- Test for filtering out WAL records of a particular table
4040
-- ===================================================================
4141

42-
SELECT oid AS sample_tbl_oid FROM pg_class WHERE relname = 'sample_tbl' \gset
42+
SELECT relfilenode AS sample_tbl_relfilenode FROM pg_class WHERE relname = 'sample_tbl' \gset
4343

4444
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_records_info(:'wal_lsn1', :'wal_lsn2')
45-
WHERE block_ref LIKE concat('%', :'sample_tbl_oid', '%') AND resource_manager = 'Heap';
45+
WHERE block_ref LIKE concat('%', :'sample_tbl_relfilenode', '%') AND resource_manager = 'Heap';
4646

4747
-- ===================================================================
4848
-- Test for filtering out WAL records based on resource_manager and

doc/src/sgml/catalogs.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,7 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
19841984

19851985
<row>
19861986
<entry role="catalog_table_entry"><para role="column_definition">
1987-
<structfield>relfilenode</structfield> <type>oid</type>
1987+
<structfield>relfilenode</structfield> <type>int8</type>
19881988
</para>
19891989
<para>
19901990
Name of the on-disk file of this relation; zero means this

doc/src/sgml/func.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25210,6 +25210,11 @@ SELECT collation for ('foo' COLLATE "de_DE");
2521025210
<entry><type>timestamp with time zone</type></entry>
2521125211
</row>
2521225212

25213+
<row>
25214+
<entry><structfield>next_relfilenumber</structfield></entry>
25215+
<entry><type>timestamp with time zone</type></entry>
25216+
</row>
25217+
2521325218
</tbody>
2521425219
</tgroup>
2521525220
</table>

doc/src/sgml/pgbuffercache.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
<row>
6464
<entry role="catalog_table_entry"><para role="column_definition">
65-
<structfield>relfilenode</structfield> <type>oid</type>
65+
<structfield>relfilenode</structfield> <type>int8</type>
6666
(references <link linkend="catalog-pg-class"><structname>pg_class</structname></link>.<structfield>relfilenode</structfield>)
6767
</para>
6868
<para>

0 commit comments

Comments
 (0)