Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

In PostgreSQL, the storage layer that people usually mean by a “database engine” is the table access method. Every ordinary table uses one, the built-in default is heap, and an existing table can be moved to a different method with ALTER TABLE ... SET ACCESS METHOD. That command rewrites the table’s data, so the change is a planned data migration rather than a setting you flip. Only methods that are installed and registered on the server can be used, and registering a new one is a superuser task that normally comes from an extension.

What “database engine” means in PostgreSQL

Outside PostgreSQL, “engine” is used loosely. Some products let you choose a storage engine per table, others have one engine underneath everything. PostgreSQL does not use the word that way. Its documentation separates two jobs that are easy to confuse:

  • Table access methods decide how the rows of a table are physically stored and read.
  • Index access methods decide how an index structure is built and searched.

Both are recorded in the pg_am system catalog, and each entry is marked as either a table method or an index method. That marker is the quickest way to see which kind of method you are looking at.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When you choose a “storage engine” for a PostgreSQL table, you are choosing a table access method. B-tree, hash, GiST, GIN, SP-GiST and BRIN are index access methods. They speed up lookups on a table but do not replace where the table’s rows are kept, so they are not alternatives to heap.

How a table access method works

PostgreSQL core does not hard-code how table rows are stored. It talks to storage through the table access method interface. A method supplies a TableAmRoutine structure, a set of callback functions that tell the core system how to scan, insert, update, delete and fetch tuples. An extension’s handler function returns that structure. The official PostgreSQL 18 documentation describes the chapter as explaining “the interface between the core PostgreSQL system and table access methods, which manage the storage for tables.”

The interface leaves real design choices to the implementer:

  • Buffering. A method may use PostgreSQL shared buffers, but it is not required to.
  • Tuple identifiers. A method that supports modifications or indexes must identify each tuple with a TID, which the documentation defines as a block number plus an item number.
  • Crash safety. A method can write to the PostgreSQL write-ahead log (WAL) or use its own mechanism.
  • Transactions. Letting different table methods take part in one transaction can require close integration with PostgreSQL’s transaction machinery.

These constraints explain why a table method is a substantial piece of C code, not a configuration option. They also mean two methods that both “work” can differ in what they support, which is why you should check documented capabilities before switching anything.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Heap: the baseline you already have

heap is the built-in table access method and the reference implementation that developers study when they write a new one. It is the default for new tables. Unless your server or a session has changed default_table_access_method, the tables you create use heap. That makes heap the right point of comparison: any other method is a deliberate departure from it.

Some general database background helps when judging a switch. Storage engines differ in how they lay out rows (row-oriented versus column-oriented layouts are the most common contrast), how they handle updates and deletes, how they keep data safe after a crash, and how they interact with indexes. Those are the axes on which a table method can differ, and they are the questions to ask of any alternative you consider.

Finding the methods installed on your server

Before you plan anything, list which table methods the server actually has. Connect with psql and run:

SELECT amname, amtype
FROM pg_am
WHERE amtype = 't';

Table methods are marked t in amtype, and index methods are marked i. If heap is the only row returned, your server has no additional table methods registered. To see which method a specific table uses, run d+ table_name in psql; the output includes an access method line for tables.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Also check the default for new tables:

SHOW default_table_access_method;

Registering a new table access method

CREATE ACCESS METHOD registers a method with the server. It does not convert any existing table. In PostgreSQL 18 the command accepts TABLE and INDEX as access method types, and only superusers can define new methods. A table method needs a handler function that returns a TableAmRoutine, which in practice means the extension that provides the method has already been built and installed on the server.

CREATE ACCESS METHOD method_name TYPE TABLE HANDLER handler_function;

In most cases you will not type this yourself. An extension’s installation script normally runs it, so the usual step is to install the extension according to its own documentation and then confirm the method appears in pg_am. If you are registering a method by hand, the handler function name must match the one the extension provides.

Swapping the method on an existing table

Changing a table’s method uses ALTER TABLE:

ALTER TABLE table_name SET ACCESS METHOD method_name;

Use DEFAULT in place of a method name to switch the table back to whatever default_table_access_method currently specifies. Because the statement rewrites the table into the new method’s layout, treat it as a migration. A reasonable sequence is:

  1. Confirm the target method is registered (the pg_am query above) and that the extension providing it is installed on the same server version you run in production.
  2. Read that extension’s documentation for its supported PostgreSQL versions, its crash-safety and WAL behavior, and any restrictions on indexes, replication or extensions that touch the table.
  3. Rehearse on a copy of the table or a restored backup, and time the rewrite on realistic data volume. The PostgreSQL documentation does not give a duration or downtime figure for this operation, so measure it yourself.
  4. Make sure there is enough free disk space for a second copy of the table during the rewrite and for any indexes that are rebuilt along with it.
  5. Run the ALTER TABLE statement in a maintenance window. Treat the table as unavailable to normal work until you have confirmed the rewrite finished.
  6. Verify the result with d+ table_name and check row counts and key queries against the values you recorded before the change.

To return to the previous method, run the same statement with the old method name, or with DEFAULT if the old method was the default. That is another full rewrite, so plan the rollback with the same care as the forward change.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Partitioned tables

A partitioned table holds no rows of its own, so there is no data to rewrite at the parent. For a partitioned parent, the access method setting determines the method for future partitions, unless a specific partition is given its own method. Existing partitions keep their current method until you change them individually, and each of those changes is a rewrite of that partition’s data. If you want a whole partitioned set moved, plan the work partition by partition rather than expecting one statement to convert everything.

Evaluating alternatives

The official documentation explains the interface and the migration semantics. It does not publish a comparative benchmark of table access methods, and the research behind this article did not establish a current, broad catalogue of third-party table methods. If you are choosing between candidates, judge each one on documented facts for your situation:

  • Which PostgreSQL major versions the implementation supports, and whether that matches your upgrade path.
  • Read and write support, and whether it supports indexes and TIDs for the operations you need.
  • Crash-safety design: whether it writes through WAL, how it is replicated, and how it behaves in point-in-time recovery.
  • Transaction behavior, particularly for statements that touch tables using different methods.
  • Extension compatibility with the other extensions and tools in your stack, including backup tools.
  • Migration cost: the rewrite time you measured, the disk space needed, and how you would roll back.

Do not accept a claim that one method is faster or smaller unless it comes with results for a stated PostgreSQL version, hardware, configuration and workload. Such figures are the publisher’s own, and a result on one workload rarely transfers to another.

Common mistakes

  • Expecting CREATE ACCESS METHOD to change existing tables. It only registers the method. Existing tables keep their method until you run ALTER TABLE.
  • Treating index methods as storage engines. Switching a table to GIN or B-tree is not a valid table-storage choice; those are index methods.
  • Skipping the rehearsal. A rewrite on production without a timed test is the usual cause of a missed maintenance window.
  • Forgetting the default. If default_table_access_method is changed, new tables follow the new default, which may not be what you intended.

“

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.