|
| 1 | +#include "catalog/postgres/pg_statistic_impl.h" |
| 2 | + |
| 3 | +#include "catalog/database_catalog.h" |
| 4 | +#include "catalog/index_schema.h" |
| 5 | +#include "catalog/postgres/builder.h" |
| 6 | +#include "catalog/postgres/pg_namespace.h" |
| 7 | +#include "catalog/schema.h" |
| 8 | +#include "storage/index/index.h" |
| 9 | +#include "storage/sql_table.h" |
| 10 | + |
| 11 | +namespace noisepage::catalog::postgres { |
| 12 | + |
| 13 | +PgStatisticImpl::PgStatisticImpl(db_oid_t db_oid) : db_oid_(db_oid) {} |
| 14 | + |
| 15 | +void PgStatisticImpl::BootstrapPRIs() { |
| 16 | + const std::vector<col_oid_t> pg_statistic_all_oids{PgStatistic::PG_STATISTIC_ALL_COL_OIDS.cbegin(), |
| 17 | + PgStatistic::PG_STATISTIC_ALL_COL_OIDS.end()}; |
| 18 | + pg_statistic_all_cols_pri_ = statistics_->InitializerForProjectedRow(pg_statistic_all_oids); |
| 19 | + pg_statistic_all_cols_prm_ = statistics_->ProjectionMapForOids(pg_statistic_all_oids); |
| 20 | + const std::vector<col_oid_t> statistic_oid_index_oids{PgStatistic::STARELID.oid_, PgStatistic::STAATTNUM.oid_}; |
| 21 | + statistic_oid_index_pri_ = statistics_->InitializerForProjectedRow(statistic_oid_index_oids); |
| 22 | + statistic_oid_index_prm_ = statistics_->ProjectionMapForOids(statistic_oid_index_oids); |
| 23 | +} |
| 24 | + |
| 25 | +void PgStatisticImpl::Bootstrap(common::ManagedPointer<transaction::TransactionContext> txn, |
| 26 | + common::ManagedPointer<DatabaseCatalog> dbc) { |
| 27 | + dbc->BootstrapTable(txn, PgStatistic::STATISTIC_TABLE_OID, PgNamespace::NAMESPACE_CATALOG_NAMESPACE_OID, |
| 28 | + "pg_statistic", Builder::GetStatisticTableSchema(), statistics_); |
| 29 | + dbc->BootstrapIndex(txn, PgNamespace::NAMESPACE_CATALOG_NAMESPACE_OID, PgStatistic::STATISTIC_TABLE_OID, |
| 30 | + PgStatistic::STATISTIC_OID_INDEX_OID, "pg_statistic_index", |
| 31 | + Builder::GetStatisticOidIndexSchema(db_oid_), statistic_oid_index_); |
| 32 | +} |
| 33 | + |
| 34 | +void PgStatisticImpl::CreateColumnStatistic(const common::ManagedPointer<transaction::TransactionContext> txn, |
| 35 | + const table_oid_t table_oid, const col_oid_t col_oid, |
| 36 | + const Schema::Column &col) { |
| 37 | + auto *const redo = txn->StageWrite(db_oid_, PgStatistic::STATISTIC_TABLE_OID, pg_statistic_all_cols_pri_); |
| 38 | + auto delta = common::ManagedPointer(redo->Delta()); |
| 39 | + auto &pm = pg_statistic_all_cols_prm_; |
| 40 | + |
| 41 | + // Prepare the PR for insertion. |
| 42 | + { |
| 43 | + PgStatistic::STARELID.Set(delta, pm, table_oid); |
| 44 | + PgStatistic::STAATTNUM.Set(delta, pm, col_oid); |
| 45 | + PgStatistic::STA_NULLROWS.Set(delta, pm, 0); |
| 46 | + PgStatistic::STA_NUMROWS.Set(delta, pm, 0); |
| 47 | + } |
| 48 | + const auto tuple_slot = statistics_->Insert(txn, redo); |
| 49 | + |
| 50 | + const auto oid_pri = statistic_oid_index_->GetProjectedRowInitializer(); |
| 51 | + auto oid_prm = statistic_oid_index_->GetKeyOidToOffsetMap(); |
| 52 | + byte *const buffer = common::AllocationUtil::AllocateAligned(oid_pri.ProjectedRowSize()); |
| 53 | + |
| 54 | + // Insert into pg_statistic_index. |
| 55 | + { |
| 56 | + auto *pr = oid_pri.InitializeRow(buffer); |
| 57 | + pr->Set<table_oid_t, false>(oid_prm[indexkeycol_oid_t(1)], table_oid, false); |
| 58 | + pr->Set<col_oid_t, false>(oid_prm[indexkeycol_oid_t(2)], col_oid, false); |
| 59 | + |
| 60 | + bool UNUSED_ATTRIBUTE result = statistic_oid_index_->InsertUnique(txn, *pr, tuple_slot); |
| 61 | + NOISEPAGE_ASSERT(result, "Assigned pg_statistic OIDs failed to be unique."); |
| 62 | + } |
| 63 | + |
| 64 | + delete[] buffer; |
| 65 | +} |
| 66 | + |
| 67 | +bool PgStatisticImpl::DeleteColumnStatistics(const common::ManagedPointer<transaction::TransactionContext> txn, |
| 68 | + const table_oid_t table_oid) { |
| 69 | + const auto &oid_pri = statistic_oid_index_->GetProjectedRowInitializer(); |
| 70 | + const auto &oid_prm = statistic_oid_index_->GetKeyOidToOffsetMap(); |
| 71 | + |
| 72 | + byte *const key_buffer = common::AllocationUtil::AllocateAligned(oid_pri.ProjectedRowSize()); |
| 73 | + byte *const key_buffer_2 = common::AllocationUtil::AllocateAligned(oid_pri.ProjectedRowSize()); |
| 74 | + |
| 75 | + // Look for the column statistic in pg_statistic_index. |
| 76 | + std::vector<storage::TupleSlot> index_results; |
| 77 | + { |
| 78 | + auto *pr_lo = oid_pri.InitializeRow(key_buffer); |
| 79 | + auto *pr_hi = oid_pri.InitializeRow(key_buffer_2); |
| 80 | + |
| 81 | + // Low key (class, min col_oid_t) |
| 82 | + pr_lo->Set<table_oid_t, false>(oid_prm.at(indexkeycol_oid_t(1)), table_oid, false); |
| 83 | + pr_lo->Set<col_oid_t, false>(oid_prm.at(indexkeycol_oid_t(2)), col_oid_t(std::numeric_limits<uint32_t>::min()), |
| 84 | + false); |
| 85 | + |
| 86 | + // High key (class + 1, max col_oid_t) |
| 87 | + pr_hi->Set<table_oid_t, false>(oid_prm.at(indexkeycol_oid_t(1)), table_oid + 1, false); |
| 88 | + pr_hi->Set<col_oid_t, false>(oid_prm.at(indexkeycol_oid_t(2)), col_oid_t(std::numeric_limits<uint32_t>::max()), |
| 89 | + false); |
| 90 | + |
| 91 | + statistic_oid_index_->ScanAscending(*txn, storage::index::ScanType::Closed, 2, pr_lo, pr_hi, 0, &index_results); |
| 92 | + // TODO(WAN): Is there an assertion that we can make here? |
| 93 | + } |
| 94 | + |
| 95 | + // Scan pg_statistic to get the columns. |
| 96 | + if (!index_results.empty()) { |
| 97 | + auto pr = common::ManagedPointer(statistic_oid_index_pri_.InitializeRow(key_buffer)); |
| 98 | + for (const auto &slot : index_results) { |
| 99 | + auto UNUSED_ATTRIBUTE result = statistics_->Select(txn, slot, pr.Get()); |
| 100 | + NOISEPAGE_ASSERT(result, "Index scan did a visibility check, so Select shouldn't fail at this point."); |
| 101 | + |
| 102 | + auto &pm = statistic_oid_index_prm_; |
| 103 | + auto *col_oid = PgStatistic::STAATTNUM.Get(pr, pm); |
| 104 | + NOISEPAGE_ASSERT(col_oid != nullptr, "OID shouldn't be NULL."); |
| 105 | + |
| 106 | + // Delete from pg_statistic. |
| 107 | + { |
| 108 | + txn->StageDelete(db_oid_, PgStatistic::STATISTIC_TABLE_OID, slot); |
| 109 | + if (!statistics_->Delete(txn, slot)) { // Failed to delete some column. Ask to abort. |
| 110 | + delete[] key_buffer; |
| 111 | + delete[] key_buffer_2; |
| 112 | + return false; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Delete from pg_statistic_index. |
| 117 | + { |
| 118 | + auto *key_pr = oid_pri.InitializeRow(key_buffer_2); |
| 119 | + key_pr->Set<table_oid_t, false>(oid_prm.at(indexkeycol_oid_t(1)), table_oid, false); |
| 120 | + key_pr->Set<col_oid_t, false>(oid_prm.at(indexkeycol_oid_t(2)), *col_oid, false); |
| 121 | + statistic_oid_index_->Delete(txn, *key_pr, slot); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + delete[] key_buffer; |
| 127 | + delete[] key_buffer_2; |
| 128 | + return true; |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace noisepage::catalog::postgres |
0 commit comments