in backend/query/pg_catalog.cc [483:813]
void PGCatalog::FillPGAttributeTable() {
auto pg_attribute = tables_by_name_.at(kPGAttribute).get();
std::vector<std::vector<zetasql::Value>> rows;
for (const Table* table : default_schema_->tables()) {
if (!table->postgresql_oid().has_value()) {
ZETASQL_VLOG(1) << "Table " << table->Name()
<< " does not have a PostgreSQL OID.";
continue;
}
if (!table->primary_key_index_postgresql_oid().has_value()) {
ZETASQL_VLOG(1) << "PK for " << table->Name()
<< " does not have a PostgreSQL OID.";
continue;
}
// Add columns.
int ordinal_position = 1;
for (const Column* column : table->columns()) {
const PostgresTypeMapping* pg_type =
system_catalog_->GetTypeFromReverseMapping(column->GetType());
auto type = pg_type->PostgresTypeOid();
type = type == TEXTOID ? VARCHAROID : type;
type = type == TEXTARRAYOID ? VARCHARARRAYOID : type;
rows.push_back({
// attrelid
CreatePgOidValue(table->postgresql_oid().value()).value(),
// attname
String(column->Name()),
// atttypid
CreatePgOidValue(type).value(),
// attstattarget
NullInt64(),
// attlen
NullInt64(),
// attnum
Int64(ordinal_position),
// attndims
Int64(column->GetType()->IsArray() ? 1 : 0),
// attcacheoff
Int64(-1),
// atttypmod
NullInt64(),
// attbyval
NullBool(),
// attalign
NullString(),
// attstorage
NullString(),
// attcompression
String(std::string{'\0'}),
// attnotnull
Bool(!column->is_nullable()),
// atthasdef
Bool(column->has_default_value()),
// atthasmissing
Bool(false),
// attidentity
String(column->is_identity_column() ? "d" : std::string{'\0'}),
// attgenerated
String(column->is_generated() ? "s" : std::string{'\0'}),
// attisdropped
Bool(false),
// attislocal
Bool(true),
// attinhcount
Int64(0),
// attcollation
NullPgOid(),
// attoptions
NullString(),
// attfdwoptions
NullString(),
});
++ordinal_position;
}
// Add primary key columns.
ordinal_position = 1;
for (const KeyColumn* key_column : table->primary_key()) {
const PostgresTypeMapping* pg_type =
system_catalog_->GetTypeFromReverseMapping(
key_column->column()->GetType());
auto type = pg_type->PostgresTypeOid();
type = type == TEXTOID ? VARCHAROID : type;
type = type == TEXTARRAYOID ? VARCHARARRAYOID : type;
rows.push_back({
// attrelid
CreatePgOidValue(table->primary_key_index_postgresql_oid().value())
.value(),
// attname
String(key_column->column()->Name()),
// atttypid
CreatePgOidValue(type).value(),
// attstattarget
NullInt64(),
// attlen
NullInt64(),
// attnum
Int64(ordinal_position++),
// attndims
Int64(key_column->column()->GetType()->IsArray() ? 1 : 0),
// attcacheoff
Int64(-1),
// atttypmod
NullInt64(),
// attbyval
NullBool(),
// attalign
NullString(),
// attstorage
NullString(),
// attcompression
String(std::string{'\0'}),
// attnotnull
Bool(!key_column->column()->is_nullable()),
// atthasdef
Bool(key_column->column()->has_default_value()),
// atthasmissing
Bool(false),
// attidentity
String(std::string{'\0'}),
// attgenerated
String(key_column->column()->is_generated() ? "s"
: std::string{'\0'}),
// attisdropped
Bool(false),
// attislocal
Bool(true),
// attinhcount
Int64(0),
// attcollation
NullPgOid(),
// attoptions
NullString(),
// attfdwoptions
NullString(),
});
}
for (const Index* index : table->indexes()) {
if (!index->postgresql_oid().has_value()) {
ZETASQL_VLOG(1) << "Index " << index->Name()
<< " does not have a PostgreSQL OID.";
continue;
}
int index_ordinal_position = 1;
for (const KeyColumn* key_column : index->key_columns()) {
const PostgresTypeMapping* pg_type =
system_catalog_->GetTypeFromReverseMapping(
key_column->column()->GetType());
auto type = pg_type->PostgresTypeOid();
type = type == TEXTOID ? VARCHAROID : type;
type = type == TEXTARRAYOID ? VARCHARARRAYOID : type;
rows.push_back({
// attrelid
CreatePgOidValue(index->postgresql_oid().value()).value(),
// attname
String(key_column->column()->Name()),
// atttypid
CreatePgOidValue(type).value(),
// attstattarget
NullInt64(),
// attlen
NullInt64(),
// attnum
Int64(index_ordinal_position++),
// attndims
Int64(key_column->column()->GetType()->IsArray() ? 1 : 0),
// attcacheoff
Int64(-1),
// atttypmod
NullInt64(),
// attbyval
NullBool(),
// attalign
NullString(),
// attstorage
NullString(),
// attcompression
String(std::string{'\0'}),
// attnotnull
Bool(!key_column->column()->is_nullable()),
// atthasdef
Bool(key_column->column()->has_default_value()),
// atthasmissing
Bool(false),
// attidentity
String(std::string{'\0'}),
// attgenerated
String(key_column->column()->is_generated() ? "s"
: std::string{'\0'}),
// attisdropped
Bool(false),
// attislocal
Bool(true),
// attinhcount
Int64(0),
// attcollation
NullPgOid(),
// attoptions
NullString(),
// attfdwoptions
NullString(),
});
}
index_ordinal_position++; // Account for inheritted primary key.
for (const Column* column : index->stored_columns()) {
const PostgresTypeMapping* pg_type =
system_catalog_->GetTypeFromReverseMapping(column->GetType());
auto type = pg_type->PostgresTypeOid();
type = type == TEXTOID ? VARCHAROID : type;
type = type == TEXTARRAYOID ? VARCHARARRAYOID : type;
rows.push_back({
// attrelid
CreatePgOidValue(index->postgresql_oid().value()).value(),
// attname
String(column->Name()),
// atttypid
CreatePgOidValue(type).value(),
// attstattarget
NullInt64(),
// attlen
NullInt64(),
// attnum
Int64(index_ordinal_position++),
// attndims
Int64(column->GetType()->IsArray() ? 1 : 0),
// attcacheoff
Int64(-1),
// atttypmod
NullInt64(),
// attbyval
NullBool(),
// attalign
NullString(),
// attstorage
NullString(),
// attcompression
String(std::string{'\0'}),
// attnotnull
Bool(!column->is_nullable()),
// atthasdef
Bool(column->has_default_value()),
// atthasmissing
Bool(false),
// attidentity
String(std::string{'\0'}),
// attgenerated
String(column->is_generated() ? "s" : std::string{'\0'}),
// attisdropped
Bool(false),
// attislocal
Bool(true),
// attinhcount
Int64(0),
// attcollation
NullPgOid(),
// attoptions
NullString(),
// attfdwoptions
NullString(),
});
}
}
}
for (const View* view : default_schema_->views()) {
if (!view->postgresql_oid().has_value()) {
ZETASQL_VLOG(1) << "View " << view->Name() << " does not have a PostgreSQL OID.";
continue;
}
int ordinal_position = 1;
for (const View::Column& column : view->columns()) {
const PostgresTypeMapping* pg_type =
system_catalog_->GetTypeFromReverseMapping(column.type);
auto type = pg_type->PostgresTypeOid();
type = type == TEXTOID ? VARCHAROID : type;
type = type == TEXTARRAYOID ? VARCHARARRAYOID : type;
rows.push_back({
// attrelid
CreatePgOidValue(view->postgresql_oid().value()).value(),
// attname
String(column.name),
// atttypid
CreatePgOidValue(type).value(),
// attstattarget
NullInt64(),
// attlen
NullInt64(),
// attnum
Int64(ordinal_position++),
// attndims
Int64(column.type->IsArray() ? 1 : 0),
// attcacheoff
Int64(-1),
// atttypmod
NullInt64(),
// attbyval
NullBool(),
// attalign
NullString(),
// attstorage
NullString(),
// attcompression
String(std::string{'\0'}),
// attnotnull
Bool(false),
// atthasdef
Bool(false),
// atthasmissing
Bool(false),
// attidentity
String(std::string{'\0'}),
// attgenerated
String(std::string{'\0'}),
// attisdropped
Bool(false),
// attislocal
Bool(true),
// attinhcount
Int64(0),
// attcollation
NullPgOid(),
// attoptions
NullString(),
// attfdwoptions
NullString(),
});
}
}
pg_attribute->SetContents(rows);
}