cockroachdb / cockroach

CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
https://www.cockroachlabs.com
Other
29.87k stars 3.77k forks source link

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

Open knz opened 5 years ago

knz commented 5 years ago

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:

The following attributes seem to be defined:

Found while working on #31737

Jira issue: CRDB-4698

jordanlewis commented 5 years ago

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 commented 5 years ago

Actually the proper implementation is not far from where you did this. It should indeed be in that function or the equivalent code in opt/build/scope.go. however it must kick in as fallback if the column name does not otherwise exist, not upfront. Also we probably want to recognize the other system attributes too. Also the type of the value should be oid not int. But you got the gist right. -- Sent from my Android device with K-9 Mail. Please excuse my brevity.

jordanlewis commented 4 years ago

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

michae2 commented 1 year ago

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

knz commented 1 year ago

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 commented 1 year ago

Note for posterity: see also https://github.com/cockroachdb/cockroach/issues/7945 and https://github.com/cockroachdb/cockroach/issues/79591

giangpham712 commented 1 year ago

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