Skip to content

Commit d87f8c8

Browse files
committed
[Doc] Improvements of tempbuffers doc
Clareifed calculation of temp relation size
1 parent de39723 commit d87f8c8

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

doc/src/sgml/func.sgml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18865,6 +18865,16 @@ postgres=# SELECT * FROM pg_xlogfile_name_offset(pg_stop_backup());
1886518865
Shorthand for <literal>pg_relation_size(..., 'main')</literal>
1886618866
</entry>
1886718867
</row>
18868+
<row>
18869+
<entry>
18870+
<literal><function>pg_temp_relation_size(<parameter>relation</parameter> <type>regclass</type>)</function></literal>
18871+
</entry>
18872+
<entry><type>bigint</type></entry>
18873+
<entry>
18874+
Total space used by <literal>'main'</literal> fork of
18875+
the specified temporary table.
18876+
</entry>
18877+
</row>
1886818878
<row>
1886918879
<entry>
1887018880
<literal><function>pg_size_bytes(<type>text</type>)</function></literal>
@@ -19004,6 +19014,15 @@ postgres=# SELECT * FROM pg_xlogfile_name_offset(pg_stop_backup());
1900419014
</itemizedlist>
1900519015
</para>
1900619016

19017+
<para>
19018+
<function>pg_temp_relation_size</> accepts the OID or name of a temporary table
19019+
and returns the total size in bytes of <literal>'main'</literal> fork of
19020+
that relation. We extend physical file for a temporary table when table doesn't
19021+
fit into <literal>temp_buffers</literal> cache anymore. So total and on-disk size of a temporary
19022+
table can differ. For other kinds of relations it will not throw any error,
19023+
just return NULL.
19024+
</para>
19025+
1900719026
<para>
1900819027
<function>pg_size_pretty</> can be used to format the result of one of
1900919028
the other functions in a human-readable way, using bytes, kB, MB, GB or TB

doc/src/sgml/ref/create_table.sgml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED | CONSTANT ] TABLE [
151151
table are automatically temporary as well.
152152
</para>
153153

154+
<para>
155+
Unlike for permanent tables, we extend physical file for a temporary table
156+
only when it doesn't fit into <literal>temp_buffers</> cache anymore.
157+
So total and on-disk size of a temporary table can differ. Blocks are
158+
not allocated in advance, so we may run out of free space on disk
159+
when trying to evict a temp buffer from cache. It will cause an error.
160+
This case is quite unusual, though.
161+
</para>
162+
154163
<para>
155164
The <link linkend="autovacuum">autovacuum daemon</link> cannot
156165
access and therefore cannot vacuum or analyze temporary tables.

src/backend/utils/adt/dbsize.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,19 @@ pg_temp_relation_size(PG_FUNCTION_ARGS)
348348
PG_RETURN_NULL();
349349

350350
size = 0;
351-
if (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
351+
if (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP
352+
&& rel->rd_rel->relkind == RELKIND_RELATION)
352353
{
353354
RelationOpenSmgr(rel);
354355

355356
if (rel->rd_smgr->smgr_main_nblocks != InvalidBlockNumber)
356357
size = BLCKSZ * rel->rd_smgr->smgr_main_nblocks;
357358
}
359+
else
360+
{
361+
relation_close(rel, AccessShareLock);
362+
PG_RETURN_NULL();
363+
}
358364

359365
relation_close(rel, AccessShareLock);
360366

0 commit comments

Comments
 (0)