Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: support pg_catalog "system attributes" xmin, xmax etc #33296

Open
1 of 6 tasks
knz opened this issue Dec 20, 2018 · 7 comments
Open
1 of 6 tasks

sql: support pg_catalog "system attributes" xmin, xmax etc #33296

knz opened this issue Dec 20, 2018 · 7 comments
Labels
A-sql-pgcatalog A-sql-pgcompat Semantic compatibility with PostgreSQL A-tools-efcore C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-queries SQL Queries Team

Comments

@knz
Copy link
Contributor

knz commented Dec 20, 2018

It appears that PostgreSQL tables, especially tables in the pg_catalog, all support "system attributes".

A system attribute is a virtual column that is selectable in every table, even though it does not show up in its schema description (eg psql \d / SHOW CREATE).

See definitions in pg sources src/backend/catalog/heap.c.

For example it is possible to do select X.xmin from X for every pg_catalog table X.

Of course it is neither reasonable nor correct to simply add these attributes as regular columns:

  • not reasonable because they would cause a lot of data to be instantiated in-memory when the vtables are queried
  • not correct because they should be hidden from introspection

The following attributes seem to be defined:

  • tableoid: the oid of the containing table
  • ctid: the "physical location" of a row within its table
  • xmin: the txn id of the inserting txn
  • xmax: the txn id of the deleting txn or 0
  • cmin: the command id of the inserting command
  • cmax: the command id of the deleting command or 0

Found while working on #31737

Jira issue: CRDB-4698

@knz knz added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) A-sql-pgcompat Semantic compatibility with PostgreSQL labels Dec 20, 2018
@jordanlewis
Copy link
Member

I made a hack for this a long time ago to see if IntelliJ would work if we at least returned something for xmin, and indeed if my recollection is correct it did fix the problem. However this hack is a very hacky hack and wouldn't work anymore with the optimizer.

diff --git a/pkg/sql/select_name_resolution.go b/pkg/sql/select_name_resolution.go
index 21c877e977..519d720a8d 100644
--- a/pkg/sql/select_name_resolution.go
+++ b/pkg/sql/select_name_resolution.go
@@ -115,6 +115,9 @@ func (v *nameResolutionVisitor) VisitPre(expr tree.Expr) (recurse bool, newNode
                return v.VisitPre(vn)

        case *tree.ColumnItem:
+               if t.ColumnName == "xmin" {
+                       return true, tree.NewDInt(tree.DInt(0))
+               }
                srcIdx, colIdx, err := v.sources.findColumn(t)
                if err != nil {
                        v.err = err

@knz
Copy link
Contributor Author

knz commented Dec 21, 2018 via email

@jordanlewis
Copy link
Member

Here's the doc for the system attributes: https://www.postgresql.org/docs/12/ddl-system-columns.html

@michae2
Copy link
Collaborator

michae2 commented Oct 6, 2022

xmin is also used by the .NET Entity Framework as a concurrency token.

@michae2 michae2 added T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) T-sql-queries SQL Queries Team labels Oct 6, 2022
@knz
Copy link
Contributor Author

knz commented Oct 6, 2022

It's unlikely we can implement xmin/xmax in a way that's both compatible with pg and also correct wrt our own concurrency management. For those columns, I would rather go the way of docs for substitute approaches.

@michae2
Copy link
Collaborator

michae2 commented Oct 6, 2022

Note for posterity: see also #7945 and #79591

@mgartner mgartner moved this to Backlog (DO NOT ADD NEW ISSUES) in SQL Queries Jul 24, 2023
@giangpham712
Copy link

giangpham712 commented Jul 31, 2023

lack of support for xmin column is affecting some test in efcore.pg

@rafiss rafiss removed the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label Aug 9, 2023
@mgartner mgartner moved this from Backlog (DO NOT ADD NEW ISSUES) to New Backlog in SQL Queries Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-pgcatalog A-sql-pgcompat Semantic compatibility with PostgreSQL A-tools-efcore C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-queries SQL Queries Team
Projects
Status: Backlog
Development

No branches or pull requests

6 participants