Rowids and hidden row identifiers
Under every heap and table there is a physical handle for a row: an address you can select, sometimes select by, and never build an API on, because rows move and ids get reused.
- Family
- UUIDs & identifiers
- Missing in
- SQL Server, MySQL
- Gotchas
- 5 listed
Engine by engine
Oracle exposes ROWID, SQLite exposes rowid, PostgreSQL offers ctid, MySQL hides its clustered key, and SQL Server has no equivalent pseudo-column worth using.
| 引擎 | 类型 | 存储 | 备注 |
|---|---|---|---|
| SQL Server |
none
Workaround
|
不适用 | No rowid pseudo-column. Heaps have an internal RID (visible only through %%physloc%% or sys.dm_db_database_page_allocations), but it is undocumented in use and unstable. |
| PostgreSQL |
ctid (system column)
|
6 bytes | Physical location as (page, item); selectable like any column. Moves on every VACUUM FULL or UPDATE that relocates the tuple; never persist it. |
| MySQL |
none (hidden PK)
Workaround
|
6 bytes | InnoDB is index-organized: the clustered key (your PK, or a hidden 6-byte GEN_CLUST_INDEX row id) is the address, but SQL cannot reference it. |
| SQLite |
ROWID
|
1-8 bytes (varint) | Every ordinary table has a rowid; new rows get max(rowid)+1, so deleted ids can come back unless AUTOINCREMENT is set. 'oid' and '_rowid_' are aliases, unless a real column shadows them. |
| Oracle |
ROWID / UROWID
|
10 bytes (typical) | Encodes file, block and slot; the fastest way to address one row and the fastest way to break a stored pointer, since MOVE and shrink rewrites it. UROWID covers index-organized tables. |
Gotchas
Where the engines disagree. These tend to surface as bugs during a migration or a port.
- Rowids are addresses, not identities: they change on table rewrites (Oracle ALTER TABLE MOVE, PostgreSQL VACUUM FULL, SQLite VACUUM on a table without an INTEGER PRIMARY KEY), and ids of deleted rows can be handed out again.
- SQLite gives you three interchangeable names for the same value: rowid, oid and _rowid_. A user column named 'oid' silently shadows the alias, which surprises anyone porting old code that assumed oid was the rowid.
- PostgreSQL ctid is the classic deduplication tool (DELETE FROM t a USING t b WHERE a.key = b.key AND a.ctid > b.ctid), but every UPDATE writes a new tuple with a new ctid, so a captured ctid goes stale immediately.
- SQL Server's closest thing, the heap RID, exists only when the table has no clustered index; add one and the RID is gone. Code that leaned on it breaks on the first index change.
- SQLite tables declared WITHOUT ROWID have no rowid at all and must declare a PRIMARY KEY; SELECT rowid on them is an error.
Common questions
Can I use ROWID as a primary key?
No. It is an address that changes on rewrites and gets reused after deletes. Oracle explicitly warns against persisting ROWID; anything you need stable should be its own column.
What is ctid used for in PostgreSQL, then?
Short-lived physical tricks: deduplication, and since PostgreSQL 14, batching big updates or deletes by page range (WHERE ctid >= '(1000,0)' AND ctid < '(2000,0)' uses a TID range scan). It is a maintenance handle, not application data.
Why does SQLite have both rowid and oid?
History: SQLite accepted 'oid' as a friendly alias early on. It means the same thing as rowid unless you declare your own column called oid, which shadows it. Avoid naming columns oid.
Map a type to the other four engines
The free type converter maps a type from one engine to its closest equivalent in the others and says where storage, precision or behaviour differ. The desktop app does the same mapping while you edit.