@@ -663,6 +663,24 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
663663/* empty pages are always all-visible */
664664if (!PageIsAllVisible (page ))
665665{
666+ /*
667+ * It's possible that another backend has extended the heap,
668+ * initialized the page, and then failed to WAL-log the page
669+ * due to an ERROR. Since heap extension is not WAL-logged,
670+ * recovery might try to replay our record setting the
671+ * page all-visible and find that the page isn't initialized,
672+ * which will cause a PANIC. To prevent that, check whether
673+ * the page has been previously WAL-logged, and if not, do that
674+ * now.
675+ *
676+ * XXX: It would be nice to use a logging method supporting
677+ * standard buffers here since log_newpage_buffer() will write
678+ * the full block instead of omitting the hole.
679+ */
680+ if (RelationNeedsWAL (onerel )&&
681+ PageGetLSN (page )== InvalidXLogRecPtr )
682+ log_newpage_buffer (buf );
683+
666684PageSetAllVisible (page );
667685MarkBufferDirty (buf );
668686visibilitymap_set (onerel ,blkno ,buf ,InvalidXLogRecPtr ,