Skip to content

Commit eeb68c1

Browse files
committed
Avoid catalog lookups in RelationAllowsEarlyPruning().
RelationAllowsEarlyPruning() performed a catalog scan, but is used in two contexts where that was a bad idea: 1. In heap_page_prune_opt(), which runs very frequently in some large scans. This caused major performance problems in a field report that was easy to reproduce. 2. In TestForOldSnapshot(), which runs while we hold a buffer content lock. It's not clear if this was guaranteed to be free of buffer deadlock risk. The check was introduced in commit 2cc41ac and defended against a real problem: 9.6's hash indexes have no page LSN and so we can't allow early pruning (ie the snapshot-too-old feature). We can remove the check from all later releases though: hash indexes are now logged, and there is no way to create UNLOGGED indexes on regular logged tables. If a future release allows such a combination, it might need to put a similar check in place, but it'll need some more thought. Back-patch to 10. Author: Thomas Munro Reviewed-by: Tom Lane, who spotted the second problem Discussion: https://postgr.es/m/CA%2BhUKGKT8oTkp5jw_U4p0S-7UG9zsvtw_M47Y285bER6a2gD%2Bg%40mail.gmail.com Discussion: https://postgr.es/m/CAA4eK1%2BWy%2BN4eE5zPm765h68LrkWc3Biu_8rzzi%2BOYX4j%2BiHRw%40mail.gmail.com
1 parent 19bfa15 commit eeb68c1

File tree

3 files changed

+0
-44
lines changed

3 files changed

+0
-44
lines changed

src/backend/utils/cache/relcache.c

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6068,48 +6068,6 @@ RelationIdIsInInitFile(Oid relationId)
60686068
return RelationSupportsSysCache(relationId);
60696069
}
60706070

6071-
/*
6072-
* Tells whether any index for the relation is unlogged.
6073-
*
6074-
* Note: There doesn't seem to be any way to have an unlogged index attached
6075-
* to a permanent table, but it seems best to keep this general so that it
6076-
* returns sensible results even when they seem obvious (like for an unlogged
6077-
* table) and to handle possible future unlogged indexes on permanent tables.
6078-
*/
6079-
bool
6080-
RelationHasUnloggedIndex(Relation rel)
6081-
{
6082-
List *indexoidlist;
6083-
ListCell *indexoidscan;
6084-
bool result = false;
6085-
6086-
indexoidlist = RelationGetIndexList(rel);
6087-
6088-
foreach(indexoidscan, indexoidlist)
6089-
{
6090-
Oid indexoid = lfirst_oid(indexoidscan);
6091-
HeapTuple tp;
6092-
Form_pg_class reltup;
6093-
6094-
tp = SearchSysCache1(RELOID, ObjectIdGetDatum(indexoid));
6095-
if (!HeapTupleIsValid(tp))
6096-
elog(ERROR, "cache lookup failed for relation %u", indexoid);
6097-
reltup = (Form_pg_class) GETSTRUCT(tp);
6098-
6099-
if (reltup->relpersistence == RELPERSISTENCE_UNLOGGED)
6100-
result = true;
6101-
6102-
ReleaseSysCache(tp);
6103-
6104-
if (result == true)
6105-
break;
6106-
}
6107-
6108-
list_free(indexoidlist);
6109-
6110-
return result;
6111-
}
6112-
61136071
/*
61146072
* Invalidate (remove) the init file during commit of a transaction that
61156073
* changed one or more of the relation cache entries that are kept in the

src/include/utils/rel.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,6 @@ get_partition_col_typmod(PartitionKey key, int col)
638638
/* routines in utils/cache/relcache.c */
639639
extern void RelationIncrementReferenceCount(Relation rel);
640640
extern void RelationDecrementReferenceCount(Relation rel);
641-
extern bool RelationHasUnloggedIndex(Relation rel);
642641
extern List *RelationGetRepsetList(Relation rel);
643642

644643
#endif /* REL_H */

src/include/utils/snapmgr.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
RelationNeedsWAL(rel) \
4141
&& !IsCatalogRelation(rel) \
4242
&& !RelationIsAccessibleInLogicalDecoding(rel) \
43-
&& !RelationHasUnloggedIndex(rel) \
4443
)
4544

4645
#define EarlyPruningEnabled(rel) (old_snapshot_threshold >= 0 && RelationAllowsEarlyPruning(rel))

0 commit comments

Comments
 (0)