From de0f521f083d9358c08ede2f56dfdace0d241595 Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Fri, 8 Feb 2019 14:33:17 +0100 Subject: [PATCH] sql: fix pg_catalog.pg_attribute This uses column IDs directly for the `attnum` column, so that the value remains stable across column drops. This is a sub-set of the changes in #33697. Required by ORMs, requested for by the TypeORM dev looking at crdb compat in TypeORM. Release note (bug fix): the value of the `attnum` column in `pg_catalog.pg_attribute` now remains stable across column drops. --- .../logictest/testdata/logic_test/pg_catalog | 10 +++---- pkg/sql/pg_catalog.go | 26 +++++++------------ 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/pkg/sql/logictest/testdata/logic_test/pg_catalog b/pkg/sql/logictest/testdata/logic_test/pg_catalog index ec2422495c17..720562abc3c6 100644 --- a/pkg/sql/logictest/testdata/logic_test/pg_catalog +++ b/pkg/sql/logictest/testdata/logic_test/pg_catalog @@ -355,18 +355,18 @@ attrelid relname attname atttypid attstattarget attlen attnum attn 4183203597 t1 b 20 0 8 3 0 -1 4183203597 t1 c 20 0 8 4 0 -1 586319997 primary p 701 0 8 1 0 -1 -586319998 t1_a_key a 20 0 8 1 0 -1 -586319999 index_key b 20 0 8 1 0 -1 -586319999 index_key c 20 0 8 2 0 -1 +586319998 t1_a_key a 20 0 8 2 0 -1 +586319999 index_key b 20 0 8 3 0 -1 +586319999 index_key c 20 0 8 4 0 -1 192646233 t2 t1_id 20 0 8 1 0 -1 192646233 t2 rowid 20 0 8 2 0 -1 -2761941313 primary rowid 20 0 8 1 0 -1 +2761941313 primary rowid 20 0 8 2 0 -1 2761941314 t2_t1_id_idx t1_id 20 0 8 1 0 -1 226054345 t3 a 20 0 8 1 0 -1 226054345 t3 b 20 0 8 2 0 -1 226054345 t3 c 25 0 -1 3 0 -1 226054345 t3 rowid 20 0 8 4 0 -1 -4084598993 primary rowid 20 0 8 1 0 -1 +4084598993 primary rowid 20 0 8 4 0 -1 4084598994 t3_a_b_idx a 20 0 8 1 0 -1 4084598994 t3_a_b_idx b 20 0 8 2 0 -1 4252432642 v1 p 701 0 8 1 0 -1 diff --git a/pkg/sql/pg_catalog.go b/pkg/sql/pg_catalog.go index af4c9cbd13f1..58ec79113473 100644 --- a/pkg/sql/pg_catalog.go +++ b/pkg/sql/pg_catalog.go @@ -327,9 +327,7 @@ CREATE TABLE pg_catalog.pg_attrdef ( h := makeOidHasher() return forEachTableDesc(ctx, p, dbContext, virtualMany, func(db *sqlbase.DatabaseDescriptor, scName string, table *sqlbase.TableDescriptor) error { - colNum := 0 return forEachColumnInTable(table, func(column *sqlbase.ColumnDescriptor) error { - colNum++ if column.DefaultExpr == nil { // pg_attrdef only expects rows for columns with default values. return nil @@ -338,7 +336,7 @@ CREATE TABLE pg_catalog.pg_attrdef ( return addRow( h.ColumnOid(db, scName, table, column), // oid h.TableOid(db, scName, table), // adrelid - tree.NewDInt(tree.DInt(colNum)), // adnum + tree.NewDInt(tree.DInt(column.ID)), // adnum defSrc, // adbin defSrc, // adsrc ) @@ -378,15 +376,15 @@ CREATE TABLE pg_catalog.pg_attribute ( h := makeOidHasher() return forEachTableDesc(ctx, p, dbContext, virtualMany, func(db *sqlbase.DatabaseDescriptor, scName string, table *sqlbase.TableDescriptor) error { // addColumn adds adds either a table or a index column to the pg_attribute table. - addColumn := func(column *sqlbase.ColumnDescriptor, attRelID tree.Datum, colNum int) error { + addColumn := func(column *sqlbase.ColumnDescriptor, attRelID tree.Datum) error { colTyp := column.Type.ToDatumType() return addRow( - attRelID, // attrelid - tree.NewDName(column.Name), // attname - typOid(colTyp), // atttypid - zeroVal, // attstattarget - typLen(colTyp), // attlen - tree.NewDInt(tree.DInt(colNum)), // attnum + attRelID, // attrelid + tree.NewDName(column.Name), // attname + typOid(colTyp), // atttypid + zeroVal, // attstattarget + typLen(colTyp), // attlen + tree.NewDInt(tree.DInt(column.ID)), // attnum zeroVal, // attndims negOneVal, // attcacheoff negOneVal, // atttypmod @@ -406,23 +404,19 @@ CREATE TABLE pg_catalog.pg_attribute ( } // Columns for table. - colNum := 0 if err := forEachColumnInTable(table, func(column *sqlbase.ColumnDescriptor) error { - colNum++ tableID := h.TableOid(db, scName, table) - return addColumn(column, tableID, colNum) + return addColumn(column, tableID) }); err != nil { return err } // Columns for each index. return forEachIndexInTable(table, func(index *sqlbase.IndexDescriptor) error { - colNum := 0 return forEachColumnInIndex(table, index, func(column *sqlbase.ColumnDescriptor) error { - colNum++ idxID := h.IndexOid(db, scName, table, index) - return addColumn(column, idxID, colNum) + return addColumn(column, idxID) }, ) })