↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

Wiki / 表访问方法

heap

The built-in heap table access method integrates tuple storage, visibility, scans, index fetches and maintenance with PostgreSQL.

当前阅读 PG 18·选择有来源记录的版本

此版本暂无所选语言的定义,以下显示原始英文内容。

Interface family
Table access method
Handler or routine
heapam_methods
Recorded callbacks
43
evidence kind
source and documentation
manual path
/docs/18/tableam.html
runtime verified
false

版本定义 PG 18

Chapter 62. Table Access Method Interface Definition

This chapter explains the interface between the core PostgreSQL system and table access methods, which manage the storage for tables. The core system knows little about these access methods beyond what is specified here, so it is possible to develop entirely new access method types by writing add-on code.

Each table access method is described by a row in the pg_am system catalog. The pg_am entry specifies a name and a handler function for the table access method. These entries can be created and deleted using the CREATE ACCESS METHOD and DROP ACCESS METHOD SQL commands.

A table access method handler function must be declared to accept a single argument of type internal and to return the pseudo-type table_am_handler. The argument is a dummy value that simply serves to prevent handler functions from being called directly from SQL commands.

Here is how an extension SQL script file might create a table access method handler:

CREATE OR REPLACE FUNCTION my_tableam_handler(internal)
  RETURNS table_am_handler AS 'my_extension', 'my_tableam_handler'
  LANGUAGE C STRICT;

CREATE ACCESS METHOD myam TYPE TABLE HANDLER my_tableam_handler;

The result of the function must be a pointer to a struct of type TableAmRoutine, which contains everything that the core code needs to know to make use of the table access method. The return value needs to be of server lifetime, which is typically achieved by defining it as a static const variable in global scope.

Here is how a source file with the table access method handler might look like:

#include "postgres.h"

#include "access/tableam.h"
#include "fmgr.h"

PG_MODULE_MAGIC;

static const TableAmRoutine my_tableam_methods = {
    .type = T_TableAmRoutine,

    /* Methods of TableAmRoutine omitted from example, add them here. */
};

PG_FUNCTION_INFO_V1(my_tableam_handler);

Datum
my_tableam_handler(PG_FUNCTION_ARGS)
{
    PG_RETURN_POINTER(&my_tableam_methods);
}

The TableAmRoutine struct, also called the access method's API struct, defines the behavior of the access method using callbacks. These callbacks are pointers to plain C functions and are not visible or callable at the SQL level. All the callbacks and their behavior is defined in the TableAmRoutine structure (with comments inside the struct defining the requirements for callbacks). Most callbacks have wrapper functions, which are documented from the point of view of a user (rather than an implementor) of the table access method. For details, please refer to the src/include/access/tableam.h file.

To implement an access method, an implementor will typically need to implement an AM-specific type of tuple table slot (see src/include/executor/tuptable.h), which allows code outside the access method to hold references to tuples of the AM, and to access the columns of the tuple.

Currently, the way an AM actually stores data is fairly unconstrained. For example, it's possible, but not required, to use postgres' shared buffer cache. In case it is used, it likely makes sense to use PostgreSQL's standard page layout as described in Section 66.6.

One fairly large constraint of the table access method API is that, currently, if the AM wants to support modifications and/or indexes, it is necessary for each tuple to have a tuple identifier (TID) consisting of a block number and an item number (see also Section 66.6). It is not strictly necessary that the sub-parts of TIDs have the same meaning they e.g., have for heap, but if bitmap scan support is desired (it is optional), the block number needs to provide locality.

For crash safety, an AM can use postgres' WAL, or a custom implementation. If WAL is chosen, either Generic WAL Records can be used, or a Custom WAL Resource Manager can be implemented.

To implement transactional support in a manner that allows different table access methods be accessed within a single transaction, it likely is necessary to closely integrate with the machinery in src/backend/access/transam/xlog.c.

Any developer of a new table access method can refer to the existing heap implementation present in src/backend/access/heap/heapam_handler.c for details of its implementation.

Registered interface handlers

Interface operationSource observationCallbackImplementation
Tuple slotHandler registered; conditions applyslot_callbacksheapam_slot_callbacks
Begin scanHandler registered; conditions applyscan_beginheap_beginscan
Parallel scan initializationHandler registered; conditions applyparallelscan_initializetable_block_parallelscan_initialize
Begin index lookupHandler registered; conditions applyindex_fetch_beginheapam_index_fetch_begin
Fetch identified tupleHandler registered; conditions applyindex_fetch_tupleheapam_index_fetch_tuple
Insert tupleHandler registered; conditions applytuple_insertheapam_tuple_insert
Update tupleHandler registered; conditions applytuple_updateheapam_tuple_update
Delete tupleHandler registered; conditions applytuple_deleteheapam_tuple_delete
Vacuum relationHandler registered; conditions applyrelation_vacuumheap_vacuum_rel
Analyze blockHandler registered; conditions applyscan_analyze_next_blockheapam_scan_analyze_next_block
TOAST decisionHandler registered; conditions applyrelation_needs_toast_tableheapam_relation_needs_toast_table

比较版本

完整来源事实

callbacks

{"index_build_range_scan":"heapam_index_build_range_scan","index_delete_tuples":"heap_index_delete_tuples","index_fetch_begin":"heapam_index_fetch_begin","index_fetch_end":"heapam_index_fetch_end","index_fetch_reset":"heapam_index_fetch_reset","index_fetch_tuple":"heapam_index_fetch_tuple","index_validate_scan":"heapam_index_validate_scan","multi_insert":"heap_multi_insert","parallelscan_estimate":"table_block_parallelscan_estimate","parallelscan_initialize":"table_block_parallelscan_initialize","parallelscan_reinitialize":"table_block_parallelscan_reinitialize","relation_copy_data":"heapam_relation_copy_data","relation_copy_for_cluster":"heapam_relation_copy_for_cluster","relation_estimate_size":"heapam_estimate_rel_size","relation_fetch_toast_slice":"heap_fetch_toast_slice","relation_needs_toast_table":"heapam_relation_needs_toast_table","relation_nontransactional_truncate":"heapam_relation_nontransactional_truncate","relation_set_new_filelocator":"heapam_relation_set_new_filelocator","relation_size":"table_block_relation_size","relation_toast_am":"heapam_relation_toast_am","relation_vacuum":"heap_vacuum_rel","scan_analyze_next_block":"heapam_scan_analyze_next_block","scan_analyze_next_tuple":"heapam_scan_analyze_next_tuple","scan_begin":"heap_beginscan","scan_bitmap_next_tuple":"heapam_scan_bitmap_next_tuple","scan_end":"heap_endscan","scan_getnextslot":"heap_getnextslot","scan_getnextslot_tidrange":"heap_getnextslot_tidrange","scan_rescan":"heap_rescan","scan_sample_next_block":"heapam_scan_sample_next_block","scan_sample_next_tuple":"heapam_scan_sample_next_tuple","scan_set_tidrange":"heap_set_tidrange","slot_callbacks":"heapam_slot_callbacks","tuple_complete_speculative":"heapam_tuple_complete_speculative","tuple_delete":"heapam_tuple_delete","tuple_fetch_row_version":"heapam_fetch_row_version","tuple_get_latest_tid":"heap_get_latest_tid","tuple_insert":"heapam_tuple_insert","tuple_insert_speculative":"heapam_tuple_insert_speculative","tuple_lock":"heapam_tuple_lock","tuple_satisfies_snapshot":"heapam_tuple_satisfies_snapshot","tuple_tid_valid":"heapam_tuple_tid_valid","tuple_update":"heapam_tuple_update"}

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · 555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f

正文语言: en · 555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f