vacuum.c 57.3 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * vacuum.c--
4
 *	  the postgres vacuum cleaner
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.66 1998/07/12 04:37:52 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14
#include <sys/types.h>
15
#include <sys/file.h>
16
#include <string.h>
17 18 19
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
20

B
Bruce Momjian 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include "postgres.h"

#include "access/genam.h"
#include "access/heapam.h"
#include "access/transam.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/catname.h"
#include "catalog/index.h"
#include "catalog/pg_class.h"
#include "catalog/pg_index.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_statistic.h"
#include "catalog/pg_type.h"
#include "commands/vacuum.h"
#include "fmgr.h"
#include "parser/parse_oper.h"
#include "storage/bufmgr.h"
#include "storage/bufpage.h"
V
Vadim B. Mikheev 已提交
40
#include "storage/shmem.h"
B
Bruce Momjian 已提交
41 42 43 44 45 46 47 48
#include "storage/smgr.h"
#include "storage/lmgr.h"
#include "utils/builtins.h"
#include "utils/inval.h"
#include "utils/mcxt.h"
#include "utils/portal.h"
#include "utils/syscache.h"

49
#ifndef HAVE_GETRUSAGE
50 51 52 53 54
#include <rusagestub.h>
#else
#include <sys/time.h>
#include <sys/resource.h>
#endif
55

56
 /* #include <port-protos.h> *//* Why? */
57

58
extern int	BlowawayRelationBuffers(Relation rdesc, BlockNumber block);
59

60
bool		VacuumRunning = false;
61

62
static Portal vc_portal;
63

64
static int	MESSAGE_LEVEL;		/* message level */
V
Vadim B. Mikheev 已提交
65

66 67 68
#define swapLong(a,b)	{long tmp; tmp=a; a=b; b=tmp;}
#define swapInt(a,b)	{int tmp; tmp=a; a=b; b=tmp;}
#define swapDatum(a,b)	{Datum tmp; tmp=a; a=b; b=tmp;}
69 70 71
#define VacAttrStatsEqValid(stats) ( stats->f_cmpeq.fn_addr != NULL )
#define VacAttrStatsLtGtValid(stats) ( stats->f_cmplt.fn_addr != NULL && \
								   stats->f_cmpgt.fn_addr != NULL && \
72 73
								   RegProcedureIsValid(stats->outfunc) )

V
Vadim B. Mikheev 已提交
74

75
/* non-export function prototypes */
76 77
static void vc_init(void);
static void vc_shutdown(void);
78 79 80 81 82 83
static void vc_vacuum(NameData *VacRelP, bool analyze, List *va_cols);
static VRelList vc_getrels(NameData *VacRelP);
static void vc_vacone(Oid relid, bool analyze, List *va_cols);
static void vc_scanheap(VRelStats *vacrelstats, Relation onerel, VPageList Vvpl, VPageList Fvpl);
static void vc_rpfheap(VRelStats *vacrelstats, Relation onerel, VPageList Vvpl, VPageList Fvpl, int nindices, Relation *Irel);
static void vc_vacheap(VRelStats *vacrelstats, Relation onerel, VPageList vpl);
B
Bruce Momjian 已提交
84
static void vc_vacpage(Page page, VPageDescr vpd);
85 86
static void vc_vaconeind(VPageList vpl, Relation indrel, int nhtups);
static void vc_scanoneind(Relation indrel, int nhtups);
87 88 89
static void vc_attrstats(Relation onerel, VRelStats *vacrelstats, HeapTuple htup);
static void vc_bucketcpy(AttributeTupleForm attr, Datum value, Datum *bucket, int16 *bucket_len);
static void vc_updstats(Oid relid, int npages, int ntups, bool hasindex, VRelStats *vacrelstats);
90 91
static void vc_delhilowstats(Oid relid, int attcnt, int *attnums);
static void vc_setpagelock(Relation rel, BlockNumber blkno);
92
static VPageDescr vc_tidreapped(ItemPointer itemptr, VPageList vpl);
93 94 95
static void vc_reappage(VPageList vpl, VPageDescr vpc);
static void vc_vpinsert(VPageList vpl, VPageDescr vpnew);
static void vc_free(VRelList vrl);
96 97 98
static void vc_getindices(Oid relid, int *nindices, Relation **Irel);
static void vc_clsindices(int nindices, Relation *Irel);
static void vc_mkindesc(Relation onerel, int nindices, Relation *Irel, IndDesc **Idesc);
99 100 101 102
static char *vc_find_eq(char *bot, int nelem, int size, char *elm, int (*compar) (char *, char *));
static int	vc_cmp_blk(char *left, char *right);
static int	vc_cmp_offno(char *left, char *right);
static bool vc_enough_space(VPageDescr vpd, Size len);
103 104

void
105
vacuum(char *vacrel, bool verbose, bool analyze, List *va_spec)
106
{
107 108
	char	   *pname;
	MemoryContext old;
109
	PortalVariableMemory pmem;
110 111 112
	NameData	VacRel;
	List	   *le;
	List	   *va_cols = NIL;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

	/*
	 * Create a portal for safe memory across transctions.	We need to
	 * palloc the name space for it because our hash function expects the
	 * name to be on a longword boundary.  CreatePortal copies the name to
	 * safe storage for us.
	 */
	pname = (char *) palloc(strlen(VACPNAME) + 1);
	strcpy(pname, VACPNAME);
	vc_portal = CreatePortal(pname);
	pfree(pname);

	if (verbose)
		MESSAGE_LEVEL = NOTICE;
	else
		MESSAGE_LEVEL = DEBUG;

	/* vacrel gets de-allocated on transaction commit */
	if (vacrel)
		strcpy(VacRel.data, vacrel);

	pmem = PortalGetVariableMemory(vc_portal);
	old = MemoryContextSwitchTo((MemoryContext) pmem);

B
Bruce Momjian 已提交
137
	if (va_spec != NIL && !analyze)
138
		elog(ERROR, "Can't vacuum columns, only tables.  You can 'vacuum analyze' columns.");
B
Bruce Momjian 已提交
139

140 141
	foreach(le, va_spec)
	{
142 143
		char	   *col = (char *) lfirst(le);
		char	   *dest;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

		dest = (char *) palloc(strlen(col) + 1);
		strcpy(dest, col);
		va_cols = lappend(va_cols, dest);
	}
	MemoryContextSwitchTo(old);

	/* initialize vacuum cleaner */
	vc_init();

	/* vacuum the database */
	if (vacrel)
		vc_vacuum(&VacRel, analyze, va_cols);
	else
		vc_vacuum(NULL, analyze, NIL);

	PortalDestroy(&vc_portal);

	/* clean up */
	vc_shutdown();
164 165 166
}

/*
167
 *	vc_init(), vc_shutdown() -- start up and shut down the vacuum cleaner.
168
 *
169 170 171 172
 *		We run exactly one vacuum cleaner at a time.  We use the file system
 *		to guarantee an exclusive lock on vacuuming, since a single vacuum
 *		cleaner instantiation crosses transaction boundaries, and we'd lose
 *		postgres-style locks at the end of every transaction.
173
 *
174 175 176 177 178 179 180 181
 *		The strangeness with committing and starting transactions in the
 *		init and shutdown routines is due to the fact that the vacuum cleaner
 *		is invoked via a sql command, and so is already executing inside
 *		a transaction.	We need to leave ourselves in a predictable state
 *		on entry and exit to the vacuum cleaner.  We commit the transaction
 *		started in PostgresMain() inside vc_init(), and start one in
 *		vc_shutdown() to match the commit waiting for us back in
 *		PostgresMain().
182 183
 */
static void
184
vc_init()
185
{
186
	int			fd;
187

188
	if ((fd = open("pg_vlock", O_CREAT | O_EXCL, 0600)) < 0)
189
		elog(ERROR, "can't create lock file -- another vacuum cleaner running?");
190

191
	close(fd);
192

193 194 195 196 197 198
	/*
	 * By here, exclusive open on the lock file succeeded.	If we abort
	 * for any reason during vacuuming, we need to remove the lock file.
	 * This global variable is checked in the transaction manager on xact
	 * abort, and the routine vc_abort() is called if necessary.
	 */
199

200
	VacuumRunning = true;
201

202 203
	/* matches the StartTransaction in PostgresMain() */
	CommitTransactionCommand();
204 205 206
}

static void
207
vc_shutdown()
208
{
209 210
	/* on entry, not in a transaction */
	if (unlink("pg_vlock") < 0)
211
		elog(ERROR, "vacuum: can't destroy lock file!");
212

213 214
	/* okay, we're done */
	VacuumRunning = false;
215

216 217
	/* matches the CommitTransaction in PostgresMain() */
	StartTransactionCommand();
218

219 220 221 222 223
}

void
vc_abort()
{
224 225
	/* on abort, remove the vacuum cleaner lock file */
	unlink("pg_vlock");
226

227
	VacuumRunning = false;
228 229 230
}

/*
231
 *	vc_vacuum() -- vacuum the database.
232
 *
233 234 235 236
 *		This routine builds a list of relations to vacuum, and then calls
 *		code that vacuums them one at a time.  We are careful to vacuum each
 *		relation in a separate transaction in order to avoid holding too many
 *		locks at one time.
237 238
 */
static void
239
vc_vacuum(NameData *VacRelP, bool analyze, List *va_cols)
240
{
241 242
	VRelList	vrl,
				cur;
243 244 245

	/* get list of relations */
	vrl = vc_getrels(VacRelP);
246

247 248
	if (analyze && VacRelP == NULL && vrl != NULL)
		vc_delhilowstats(InvalidOid, 0, NULL);
249

250 251 252
	/* vacuum each heap relation */
	for (cur = vrl; cur != (VRelList) NULL; cur = cur->vrl_next)
		vc_vacone(cur->vrl_relid, analyze, va_cols);
253

254
	vc_free(vrl);
255 256
}

257
static VRelList
258
vc_getrels(NameData *VacRelP)
259
{
260 261 262 263 264
	Relation	pgclass;
	TupleDesc	pgcdesc;
	HeapScanDesc pgcscan;
	HeapTuple	pgctup;
	Buffer		buf;
265
	PortalVariableMemory portalmem;
266 267 268 269 270 271 272 273 274
	MemoryContext old;
	VRelList	vrl,
				cur;
	Datum		d;
	char	   *rname;
	char		rkind;
	bool		n;
	ScanKeyData pgckey;
	bool		found = false;
275 276 277 278 279 280

	StartTransactionCommand();

	if (VacRelP->data)
	{
		ScanKeyEntryInitialize(&pgckey, 0x0, Anum_pg_class_relname,
B
Bruce Momjian 已提交
281
							   F_NAMEEQ,
282 283 284 285 286
							   PointerGetDatum(VacRelP->data));
	}
	else
	{
		ScanKeyEntryInitialize(&pgckey, 0x0, Anum_pg_class_relkind,
B
Bruce Momjian 已提交
287
						  F_CHAREQ, CharGetDatum('r'));
288
	}
289

290 291
	portalmem = PortalGetVariableMemory(vc_portal);
	vrl = cur = (VRelList) NULL;
292

293 294 295
	pgclass = heap_openr(RelationRelationName);
	pgcdesc = RelationGetTupleDescriptor(pgclass);

296
	pgcscan = heap_beginscan(pgclass, false, false, 1, &pgckey);
297

298
	while (HeapTupleIsValid(pgctup = heap_getnext(pgcscan, 0, &buf)))
V
Vadim B. Mikheev 已提交
299 300
	{

301
		found = true;
302

303
		d = heap_getattr(pgctup, Anum_pg_class_relname, pgcdesc, &n);
304
		rname = (char *) d;
305

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
		/*
		 * don't vacuum large objects for now - something breaks when we
		 * do
		 */
		if ((strlen(rname) >= 5) && rname[0] == 'x' &&
			rname[1] == 'i' && rname[2] == 'n' &&
			(rname[3] == 'v' || rname[3] == 'x') &&
			rname[4] >= '0' && rname[4] <= '9')
		{
			elog(NOTICE, "Rel %s: can't vacuum LargeObjects now",
				 rname);
			ReleaseBuffer(buf);
			continue;
		}

321
		d = heap_getattr(pgctup, Anum_pg_class_relkind, pgcdesc, &n);
322 323 324 325 326 327 328 329 330 331

		rkind = DatumGetChar(d);

		/* skip system relations */
		if (rkind != 'r')
		{
			ReleaseBuffer(buf);
			elog(NOTICE, "Vacuum: can not process index and certain system tables");
			continue;
		}
332

333 334 335 336 337 338 339 340 341 342
		/* get a relation list entry for this guy */
		old = MemoryContextSwitchTo((MemoryContext) portalmem);
		if (vrl == (VRelList) NULL)
			vrl = cur = (VRelList) palloc(sizeof(VRelListData));
		else
		{
			cur->vrl_next = (VRelList) palloc(sizeof(VRelListData));
			cur = cur->vrl_next;
		}
		MemoryContextSwitchTo(old);
343

344 345
		cur->vrl_relid = pgctup->t_oid;
		cur->vrl_next = (VRelList) NULL;
346

347 348 349 350 351 352 353 354 355
		/* wei hates it if you forget to do this */
		ReleaseBuffer(buf);
	}
	if (found == false)
		elog(NOTICE, "Vacuum: table not found");


	heap_endscan(pgcscan);
	heap_close(pgclass);
356

357
	CommitTransactionCommand();
358

359
	return (vrl);
360 361 362
}

/*
363
 *	vc_vacone() -- vacuum one heap relation
364
 *
365 366
 *		This routine vacuums a single heap, cleans out its indices, and
 *		updates its statistics npages and ntups statistics.
367
 *
368 369 370 371 372
 *		Doing one heap at a time incurs extra overhead, since we need to
 *		check that the heap exists again just before we vacuum it.	The
 *		reason that we do this is so that vacuuming can be spread across
 *		many small transactions.  Otherwise, two-phase locking would require
 *		us to lock the entire database during one pass of the vacuum cleaner.
373 374
 */
static void
375
vc_vacone(Oid relid, bool analyze, List *va_cols)
376
{
377 378 379 380 381 382 383 384 385
	Relation	pgclass;
	TupleDesc	pgcdesc;
	HeapTuple	pgctup,
				pgttup;
	Buffer		pgcbuf;
	HeapScanDesc pgcscan;
	Relation	onerel;
	ScanKeyData pgckey;
	VPageListData Vvpl;			/* List of pages to vacuum and/or clean
386
								 * indices */
387
	VPageListData Fvpl;			/* List of pages with space enough for
388
								 * re-using */
389 390 391 392 393
	VPageDescr *vpp;
	Relation   *Irel;
	int32		nindices,
				i;
	VRelStats  *vacrelstats;
394 395 396 397

	StartTransactionCommand();

	ScanKeyEntryInitialize(&pgckey, 0x0, ObjectIdAttributeNumber,
B
Bruce Momjian 已提交
398
						   F_OIDEQ,
399 400 401 402
						   ObjectIdGetDatum(relid));

	pgclass = heap_openr(RelationRelationName);
	pgcdesc = RelationGetTupleDescriptor(pgclass);
403
	pgcscan = heap_beginscan(pgclass, false, false, 1, &pgckey);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

	/*
	 * Race condition -- if the pg_class tuple has gone away since the
	 * last time we saw it, we don't need to vacuum it.
	 */

	if (!HeapTupleIsValid(pgctup = heap_getnext(pgcscan, 0, &pgcbuf)))
	{
		heap_endscan(pgcscan);
		heap_close(pgclass);
		CommitTransactionCommand();
		return;
	}

	/* now open the class and vacuum it */
	onerel = heap_open(relid);

	vacrelstats = (VRelStats *) palloc(sizeof(VRelStats));
	vacrelstats->relid = relid;
	vacrelstats->npages = vacrelstats->ntups = 0;
	vacrelstats->hasindex = false;
	if (analyze && !IsSystemRelationName((RelationGetRelationName(onerel))->data))
	{
427 428
		int			attr_cnt,
				   *attnums = NULL;
429 430 431 432 433 434 435
		AttributeTupleForm *attr;

		attr_cnt = onerel->rd_att->natts;
		attr = onerel->rd_att->attrs;

		if (va_cols != NIL)
		{
436 437
			int			tcnt = 0;
			List	   *le;
438 439

			if (length(va_cols) > attr_cnt)
440
				elog(ERROR, "vacuum: too many attributes specified for relation %s",
441 442 443 444
					 (RelationGetRelationName(onerel))->data);
			attnums = (int *) palloc(attr_cnt * sizeof(int));
			foreach(le, va_cols)
			{
445
				char	   *col = (char *) lfirst(le);
446 447 448 449 450 451 452 453 454 455

				for (i = 0; i < attr_cnt; i++)
				{
					if (namestrcmp(&(attr[i]->attname), col) == 0)
						break;
				}
				if (i < attr_cnt)		/* found */
					attnums[tcnt++] = i;
				else
				{
456
					elog(ERROR, "vacuum: there is no attribute %s in %s",
457 458 459 460 461 462 463 464 465 466 467
						 col, (RelationGetRelationName(onerel))->data);
				}
			}
			attr_cnt = tcnt;
		}

		vacrelstats->vacattrstats =
			(VacAttrStats *) palloc(attr_cnt * sizeof(VacAttrStats));

		for (i = 0; i < attr_cnt; i++)
		{
468
			Operator	func_operator;
469
			OperatorTupleForm pgopform;
470
			VacAttrStats *stats;
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486

			stats = &vacrelstats->vacattrstats[i];
			stats->attr = palloc(ATTRIBUTE_TUPLE_SIZE);
			memmove(stats->attr, attr[((attnums) ? attnums[i] : i)], ATTRIBUTE_TUPLE_SIZE);
			stats->best = stats->guess1 = stats->guess2 = 0;
			stats->max = stats->min = 0;
			stats->best_len = stats->guess1_len = stats->guess2_len = 0;
			stats->max_len = stats->min_len = 0;
			stats->initialized = false;
			stats->best_cnt = stats->guess1_cnt = stats->guess1_hits = stats->guess2_hits = 0;
			stats->max_cnt = stats->min_cnt = stats->null_cnt = stats->nonnull_cnt = 0;

			func_operator = oper("=", stats->attr->atttypid, stats->attr->atttypid, true);
			if (func_operator != NULL)
			{
				pgopform = (OperatorTupleForm) GETSTRUCT(func_operator);
487
				fmgr_info(pgopform->oprcode, &(stats->f_cmpeq));
488 489
			}
			else
490
				stats->f_cmpeq.fn_addr = NULL;
491 492 493 494 495

			func_operator = oper("<", stats->attr->atttypid, stats->attr->atttypid, true);
			if (func_operator != NULL)
			{
				pgopform = (OperatorTupleForm) GETSTRUCT(func_operator);
496
				fmgr_info(pgopform->oprcode, &(stats->f_cmplt));
497 498
			}
			else
499
				stats->f_cmplt.fn_addr = NULL;
500 501 502 503 504

			func_operator = oper(">", stats->attr->atttypid, stats->attr->atttypid, true);
			if (func_operator != NULL)
			{
				pgopform = (OperatorTupleForm) GETSTRUCT(func_operator);
505
				fmgr_info(pgopform->oprcode, &(stats->f_cmpgt));
506 507
			}
			else
508
				stats->f_cmpgt.fn_addr = NULL;
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

			pgttup = SearchSysCacheTuple(TYPOID,
								 ObjectIdGetDatum(stats->attr->atttypid),
										 0, 0, 0);
			if (HeapTupleIsValid(pgttup))
				stats->outfunc = ((TypeTupleForm) GETSTRUCT(pgttup))->typoutput;
			else
				stats->outfunc = InvalidOid;
		}
		vacrelstats->va_natts = attr_cnt;
		vc_delhilowstats(relid, ((attnums) ? attr_cnt : 0), attnums);
		if (attnums)
			pfree(attnums);
	}
	else
	{
		vacrelstats->va_natts = 0;
		vacrelstats->vacattrstats = (VacAttrStats *) NULL;
	}

	/* we require the relation to be locked until the indices are cleaned */
	RelationSetLockForWrite(onerel);

	/* scan it */
	Vvpl.vpl_npages = Fvpl.vpl_npages = 0;
	vc_scanheap(vacrelstats, onerel, &Vvpl, &Fvpl);

	/* Now open indices */
	Irel = (Relation *) NULL;
	vc_getindices(vacrelstats->relid, &nindices, &Irel);

	if (nindices > 0)
		vacrelstats->hasindex = true;
	else
		vacrelstats->hasindex = false;

	/* Clean/scan index relation(s) */
	if (Irel != (Relation *) NULL)
	{
		if (Vvpl.vpl_npages > 0)
		{
			for (i = 0; i < nindices; i++)
				vc_vaconeind(&Vvpl, Irel[i], vacrelstats->ntups);
		}
		else
/* just scan indices to update statistic */
		{
			for (i = 0; i < nindices; i++)
				vc_scanoneind(Irel[i], vacrelstats->ntups);
		}
	}

	if (Fvpl.vpl_npages > 0)	/* Try to shrink heap */
		vc_rpfheap(vacrelstats, onerel, &Vvpl, &Fvpl, nindices, Irel);
	else
	{
		if (Irel != (Relation *) NULL)
			vc_clsindices(nindices, Irel);
		if (Vvpl.vpl_npages > 0)/* Clean pages from Vvpl list */
			vc_vacheap(vacrelstats, onerel, &Vvpl);
	}

	/* ok - free Vvpl list of reapped pages */
	if (Vvpl.vpl_npages > 0)
	{
		vpp = Vvpl.vpl_pgdesc;
		for (i = 0; i < Vvpl.vpl_npages; i++, vpp++)
			pfree(*vpp);
		pfree(Vvpl.vpl_pgdesc);
		if (Fvpl.vpl_npages > 0)
			pfree(Fvpl.vpl_pgdesc);
	}

	/* all done with this class */
	heap_close(onerel);
584 585
	heap_endscan(pgcscan);
	heap_close(pgclass);
V
Vadim B. Mikheev 已提交
586

587 588 589 590 591 592 593
	/* update statistics in pg_class */
	vc_updstats(vacrelstats->relid, vacrelstats->npages, vacrelstats->ntups,
				vacrelstats->hasindex, vacrelstats);

	/* next command frees attribute stats */

	CommitTransactionCommand();
594 595 596
}

/*
597
 *	vc_scanheap() -- scan an open heap relation
598
 *
599 600 601 602 603
 *		This routine sets commit times, constructs Vvpl list of
 *		empty/uninitialized pages and pages with dead tuples and
 *		~LP_USED line pointers, constructs Fvpl list of pages
 *		appropriate for purposes of shrinking and maintains statistics
 *		on the number of live tuples in a heap.
604 605
 */
static void
606
vc_scanheap(VRelStats *vacrelstats, Relation onerel,
V
Vadim B. Mikheev 已提交
607
			VPageList Vvpl, VPageList Fvpl)
608
{
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
	int			nblocks,
				blkno;
	ItemId		itemid;
	ItemPointer itemptr;
	HeapTuple	htup;
	Buffer		buf;
	Page		page,
				tempPage = NULL;
	OffsetNumber offnum,
				maxoff;
	bool		pgchanged,
				tupgone,
				dobufrel,
				notup;
	char	   *relname;
	VPageDescr	vpc,
				vp;
	uint32		nvac,
				ntups,
				nunused,
				ncrash,
				nempg,
				nnepg,
				nchpg,
				nemend;
	Size		frsize,
				frsusf;
	Size		min_tlen = MAXTUPLEN;
	Size		max_tlen = 0;
	int32		i /* , attr_cnt */ ;
	struct rusage ru0,
				ru1;
	bool		do_shrinking = true;
642 643 644 645 646 647 648 649 650 651 652 653 654

	getrusage(RUSAGE_SELF, &ru0);

	nvac = ntups = nunused = ncrash = nempg = nnepg = nchpg = nemend = 0;
	frsize = frsusf = 0;

	relname = (RelationGetRelationName(onerel))->data;

	nblocks = RelationGetNumberOfBlocks(onerel);

	vpc = (VPageDescr) palloc(sizeof(VPageDescrData) + MaxOffsetNumber * sizeof(OffsetNumber));
	vpc->vpd_nusd = 0;

655 656
	elog(MESSAGE_LEVEL, "--Relation %s--", relname);
	
657 658 659 660 661 662
	for (blkno = 0; blkno < nblocks; blkno++)
	{
		buf = ReadBuffer(onerel, blkno);
		page = BufferGetPage(buf);
		vpc->vpd_blkno = blkno;
		vpc->vpd_noff = 0;
663

664 665 666 667 668 669 670 671 672 673 674 675
		if (PageIsNew(page))
		{
			elog(NOTICE, "Rel %s: Uninitialized page %u - fixing",
				 relname, blkno);
			PageInit(page, BufferGetPageSize(buf), 0);
			vpc->vpd_free = ((PageHeader) page)->pd_upper - ((PageHeader) page)->pd_lower;
			frsize += (vpc->vpd_free - sizeof(ItemIdData));
			nnepg++;
			nemend++;
			vc_reappage(Vvpl, vpc);
			WriteBuffer(buf);
			continue;
V
Vadim B. Mikheev 已提交
676
		}
677 678

		if (PageIsEmpty(page))
679
		{
680 681 682 683 684 685 686
			vpc->vpd_free = ((PageHeader) page)->pd_upper - ((PageHeader) page)->pd_lower;
			frsize += (vpc->vpd_free - sizeof(ItemIdData));
			nempg++;
			nemend++;
			vc_reappage(Vvpl, vpc);
			ReleaseBuffer(buf);
			continue;
687 688
		}

689 690 691 692 693 694
		pgchanged = false;
		notup = true;
		maxoff = PageGetMaxOffsetNumber(page);
		for (offnum = FirstOffsetNumber;
			 offnum <= maxoff;
			 offnum = OffsetNumberNext(offnum))
695
		{
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
			itemid = PageGetItemId(page, offnum);

			/*
			 * Collect un-used items too - it's possible to have indices
			 * pointing here after crash.
			 */
			if (!ItemIdIsUsed(itemid))
			{
				vpc->vpd_voff[vpc->vpd_noff++] = offnum;
				nunused++;
				continue;
			}

			htup = (HeapTuple) PageGetItem(page, itemid);
			tupgone = false;

V
Vadim B. Mikheev 已提交
712
			if (!(htup->t_infomask & HEAP_XMIN_COMMITTED))
713
			{
V
Vadim B. Mikheev 已提交
714
				if (htup->t_infomask & HEAP_XMIN_INVALID)
715 716 717
					tupgone = true;
				else
				{
V
Vadim B. Mikheev 已提交
718 719 720 721 722 723 724 725 726
					if (TransactionIdDidAbort(htup->t_xmin))
						tupgone = true;
					else if (TransactionIdDidCommit(htup->t_xmin))
					{
						htup->t_infomask |= HEAP_XMIN_COMMITTED;
						pgchanged = true;
					}
					else if (!TransactionIdIsInProgress(htup->t_xmin))
					{
727

V
Vadim B. Mikheev 已提交
728
						/*
729
						 * Not Aborted, Not Committed, Not in Progress -
V
Vadim B. Mikheev 已提交
730 731 732 733 734 735 736 737 738 739 740
						 * so it's from crashed process. - vadim 11/26/96
						 */
						ncrash++;
						tupgone = true;
					}
					else
					{
						elog(NOTICE, "Rel %s: TID %u/%u: InsertTransactionInProgress %u - can't shrink relation",
							 relname, blkno, offnum, htup->t_xmin);
						do_shrinking = false;
					}
741 742 743
				}
			}

744 745 746
			/*
			 * here we are concerned about tuples with xmin committed and
			 * xmax unknown or committed
V
Vadim B. Mikheev 已提交
747
			 */
748
			if (htup->t_infomask & HEAP_XMIN_COMMITTED &&
V
Vadim B. Mikheev 已提交
749
				!(htup->t_infomask & HEAP_XMAX_INVALID))
750
			{
V
Vadim B. Mikheev 已提交
751 752 753
				if (htup->t_infomask & HEAP_XMAX_COMMITTED)
					tupgone = true;
				else if (TransactionIdDidAbort(htup->t_xmax))
754
				{
V
Vadim B. Mikheev 已提交
755
					htup->t_infomask |= HEAP_XMAX_INVALID;
756 757 758 759 760 761
					pgchanged = true;
				}
				else if (TransactionIdDidCommit(htup->t_xmax))
					tupgone = true;
				else if (!TransactionIdIsInProgress(htup->t_xmax))
				{
762

763 764 765 766
					/*
					 * Not Aborted, Not Committed, Not in Progress - so it
					 * from crashed process. - vadim 06/02/97
					 */
V
Vadim B. Mikheev 已提交
767
					htup->t_infomask |= HEAP_XMAX_INVALID;;
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
					pgchanged = true;
				}
				else
				{
					elog(NOTICE, "Rel %s: TID %u/%u: DeleteTransactionInProgress %u - can't shrink relation",
						 relname, blkno, offnum, htup->t_xmax);
					do_shrinking = false;
				}
			}

			/*
			 * It's possibly! But from where it comes ? And should we fix
			 * it ?  - vadim 11/28/96
			 */
			itemptr = &(htup->t_ctid);
			if (!ItemPointerIsValid(itemptr) ||
				BlockIdGetBlockNumber(&(itemptr->ip_blkid)) != blkno)
			{
				elog(NOTICE, "Rel %s: TID %u/%u: TID IN TUPLEHEADER %u/%u IS NOT THE SAME. TUPGONE %d.",
					 relname, blkno, offnum,
					 BlockIdGetBlockNumber(&(itemptr->ip_blkid)),
					 itemptr->ip_posid, tupgone);
			}

			/*
			 * Other checks...
			 */
			if (htup->t_len != itemid->lp_len)
			{
				elog(NOTICE, "Rel %s: TID %u/%u: TUPLE_LEN IN PAGEHEADER %u IS NOT THE SAME AS IN TUPLEHEADER %u. TUPGONE %d.",
					 relname, blkno, offnum,
					 itemid->lp_len, htup->t_len, tupgone);
			}
			if (!OidIsValid(htup->t_oid))
			{
				elog(NOTICE, "Rel %s: TID %u/%u: OID IS INVALID. TUPGONE %d.",
					 relname, blkno, offnum, tupgone);
			}

			if (tupgone)
			{
809
				ItemId		lpp;
810 811 812

				if (tempPage == (Page) NULL)
				{
813
					Size		pageSize;
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838

					pageSize = PageGetPageSize(page);
					tempPage = (Page) palloc(pageSize);
					memmove(tempPage, page, pageSize);
				}

				lpp = &(((PageHeader) tempPage)->pd_linp[offnum - 1]);

				/* mark it unused */
				lpp->lp_flags &= ~LP_USED;

				vpc->vpd_voff[vpc->vpd_noff++] = offnum;
				nvac++;

			}
			else
			{
				ntups++;
				notup = false;
				if (htup->t_len < min_tlen)
					min_tlen = htup->t_len;
				if (htup->t_len > max_tlen)
					max_tlen = htup->t_len;
				vc_attrstats(onerel, vacrelstats, htup);
			}
839
		}
840 841 842 843 844 845

		if (pgchanged)
		{
			WriteBuffer(buf);
			dobufrel = false;
			nchpg++;
846 847
		}
		else
848 849 850 851 852 853 854 855 856
			dobufrel = true;
		if (tempPage != (Page) NULL)
		{						/* Some tuples are gone */
			PageRepairFragmentation(tempPage);
			vpc->vpd_free = ((PageHeader) tempPage)->pd_upper - ((PageHeader) tempPage)->pd_lower;
			frsize += vpc->vpd_free;
			vc_reappage(Vvpl, vpc);
			pfree(tempPage);
			tempPage = (Page) NULL;
857
		}
858 859 860 861 862
		else if (vpc->vpd_noff > 0)
		{						/* there are only ~LP_USED line pointers */
			vpc->vpd_free = ((PageHeader) page)->pd_upper - ((PageHeader) page)->pd_lower;
			frsize += vpc->vpd_free;
			vc_reappage(Vvpl, vpc);
V
Vadim B. Mikheev 已提交
863
		}
864 865 866 867 868 869
		if (dobufrel)
			ReleaseBuffer(buf);
		if (notup)
			nemend++;
		else
			nemend = 0;
870 871
	}

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
	pfree(vpc);

	/* save stats in the rel list for use later */
	vacrelstats->ntups = ntups;
	vacrelstats->npages = nblocks;
/*	  vacrelstats->natts = attr_cnt;*/
	if (ntups == 0)
		min_tlen = max_tlen = 0;
	vacrelstats->min_tlen = min_tlen;
	vacrelstats->max_tlen = max_tlen;

	Vvpl->vpl_nemend = nemend;
	Fvpl->vpl_nemend = nemend;

	/*
	 * Try to make Fvpl keeping in mind that we can't use free space of
	 * "empty" end-pages and last page if it reapped.
	 */
	if (do_shrinking && Vvpl->vpl_npages - nemend > 0)
	{
892
		int			nusf;		/* blocks usefull for re-using */
893 894 895 896 897 898 899 900 901 902 903 904 905 906

		nusf = Vvpl->vpl_npages - nemend;
		if ((Vvpl->vpl_pgdesc[nusf - 1])->vpd_blkno == nblocks - nemend - 1)
			nusf--;

		for (i = 0; i < nusf; i++)
		{
			vp = Vvpl->vpl_pgdesc[i];
			if (vc_enough_space(vp, min_tlen))
			{
				vc_vpinsert(Fvpl, vp);
				frsusf += vp->vpd_free;
			}
		}
V
Vadim B. Mikheev 已提交
907
	}
908

909 910
	getrusage(RUSAGE_SELF, &ru1);

911
	elog(MESSAGE_LEVEL, "Pages %u: Changed %u, Reapped %u, Empty %u, New %u; \
V
Vadim B. Mikheev 已提交
912
Tup %u: Vac %u, Crash %u, UnUsed %u, MinLen %u, MaxLen %u; Re-using: Free/Avail. Space %u/%u; EndEmpty/Avail. Pages %u/%u. Elapsed %u/%u sec.",
913 914 915 916 917
		 nblocks, nchpg, Vvpl->vpl_npages, nempg, nnepg,
		 ntups, nvac, ncrash, nunused, min_tlen, max_tlen,
		 frsize, frsusf, nemend, Fvpl->vpl_npages,
		 ru1.ru_stime.tv_sec - ru0.ru_stime.tv_sec,
		 ru1.ru_utime.tv_sec - ru0.ru_utime.tv_sec);
918

919
}	/* vc_scanheap */
V
Vadim B. Mikheev 已提交
920

921 922

/*
923
 *	vc_rpfheap() -- try to repaire relation' fragmentation
924
 *
925 926 927 928 929 930
 *		This routine marks dead tuples as unused and tries re-use dead space
 *		by moving tuples (and inserting indices if needed). It constructs
 *		Nvpl list of free-ed pages (moved tuples) and clean indices
 *		for them after committing (in hack-manner - without losing locks
 *		and freeing memory!) current transaction. It truncates relation
 *		if some end-blocks are gone away.
931 932
 */
static void
933 934
vc_rpfheap(VRelStats *vacrelstats, Relation onerel,
		   VPageList Vvpl, VPageList Fvpl, int nindices, Relation *Irel)
935
{
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
	TransactionId myXID;
	CommandId	myCID;
	Buffer		buf,
				ToBuf;
	int			nblocks,
				blkno;
	Page		page,
				ToPage = NULL;
	OffsetNumber offnum = 0,
				maxoff = 0,
				newoff,
				moff;
	ItemId		itemid,
				newitemid;
	HeapTuple	htup,
				newtup;
	TupleDesc	tupdesc = NULL;
	Datum	   *idatum = NULL;
	char	   *inulls = NULL;
955
	InsertIndexResult iresult;
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
	VPageListData Nvpl;
	VPageDescr	ToVpd = NULL,
				Fvplast,
				Vvplast,
				vpc,
			   *vpp;
	int			ToVpI = 0;
	IndDesc    *Idesc,
			   *idcur;
	int			Fblklast,
				Vblklast,
				i;
	Size		tlen;
	int			nmoved,
				Fnpages,
				Vnpages;
	int			nchkmvd,
				ntups;
	bool		isempty,
				dowrite;
	struct rusage ru0,
				ru1;
978 979 980 981 982 983 984

	getrusage(RUSAGE_SELF, &ru0);

	myXID = GetCurrentTransactionId();
	myCID = GetCurrentCommandId();

	if (Irel != (Relation *) NULL)		/* preparation for index' inserts */
V
Vadim B. Mikheev 已提交
985
	{
986 987 988 989
		vc_mkindesc(onerel, nindices, Irel, &Idesc);
		tupdesc = RelationGetTupleDescriptor(onerel);
		idatum = (Datum *) palloc(INDEX_MAX_KEYS * sizeof(*idatum));
		inulls = (char *) palloc(INDEX_MAX_KEYS * sizeof(*inulls));
V
Vadim B. Mikheev 已提交
990
	}
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

	Nvpl.vpl_npages = 0;
	Fnpages = Fvpl->vpl_npages;
	Fvplast = Fvpl->vpl_pgdesc[Fnpages - 1];
	Fblklast = Fvplast->vpd_blkno;
	Assert(Vvpl->vpl_npages > Vvpl->vpl_nemend);
	Vnpages = Vvpl->vpl_npages - Vvpl->vpl_nemend;
	Vvplast = Vvpl->vpl_pgdesc[Vnpages - 1];
	Vblklast = Vvplast->vpd_blkno;
	Assert(Vblklast >= Fblklast);
	ToBuf = InvalidBuffer;
	nmoved = 0;

	vpc = (VPageDescr) palloc(sizeof(VPageDescrData) + MaxOffsetNumber * sizeof(OffsetNumber));
	vpc->vpd_nusd = vpc->vpd_noff = 0;

	nblocks = vacrelstats->npages;
	for (blkno = nblocks - Vvpl->vpl_nemend - 1;; blkno--)
V
Vadim B. Mikheev 已提交
1009
	{
1010 1011 1012
		/* if it's reapped page and it was used by me - quit */
		if (blkno == Fblklast && Fvplast->vpd_nusd > 0)
			break;
V
Vadim B. Mikheev 已提交
1013

1014 1015 1016 1017 1018 1019 1020 1021 1022
		buf = ReadBuffer(onerel, blkno);
		page = BufferGetPage(buf);

		vpc->vpd_noff = 0;

		isempty = PageIsEmpty(page);

		dowrite = false;
		if (blkno == Vblklast)	/* it's reapped page */
1023
		{
1024 1025 1026
			if (Vvplast->vpd_noff > 0)	/* there are dead tuples */
			{					/* on this page - clean */
				Assert(!isempty);
B
Bruce Momjian 已提交
1027
				vc_vacpage(page, Vvplast);
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
				dowrite = true;
			}
			else
				Assert(isempty);
			--Vnpages;
			Assert(Vnpages > 0);
			/* get prev reapped page from Vvpl */
			Vvplast = Vvpl->vpl_pgdesc[Vnpages - 1];
			Vblklast = Vvplast->vpd_blkno;
			if (blkno == Fblklast)		/* this page in Fvpl too */
			{
				--Fnpages;
				Assert(Fnpages > 0);
				Assert(Fvplast->vpd_nusd == 0);
				/* get prev reapped page from Fvpl */
				Fvplast = Fvpl->vpl_pgdesc[Fnpages - 1];
				Fblklast = Fvplast->vpd_blkno;
			}
			Assert(Fblklast <= Vblklast);
			if (isempty)
			{
				ReleaseBuffer(buf);
				continue;
			}
		}
		else
			Assert(!isempty);

		vpc->vpd_blkno = blkno;
		maxoff = PageGetMaxOffsetNumber(page);
		for (offnum = FirstOffsetNumber;
			 offnum <= maxoff;
			 offnum = OffsetNumberNext(offnum))
		{
			itemid = PageGetItemId(page, offnum);

			if (!ItemIdIsUsed(itemid))
				continue;

			htup = (HeapTuple) PageGetItem(page, itemid);
			tlen = htup->t_len;

			/* try to find new page for this tuple */
			if (ToBuf == InvalidBuffer ||
				!vc_enough_space(ToVpd, tlen))
			{
				if (ToBuf != InvalidBuffer)
				{
					WriteBuffer(ToBuf);
					ToBuf = InvalidBuffer;

					/*
					 * If no one tuple can't be added to this page -
					 * remove page from Fvpl. - vadim 11/27/96
1082 1083 1084
					 *
					 * But we can't remove last page - this is our
					 * "show-stopper" !!!	- vadim 02/25/98
1085
					 */
1086
					if (ToVpd != Fvplast &&
1087
						!vc_enough_space(ToVpd, vacrelstats->min_tlen))
1088
					{
1089 1090 1091
						Assert(Fnpages > ToVpI + 1);
						memmove(Fvpl->vpl_pgdesc + ToVpI,
								Fvpl->vpl_pgdesc + ToVpI + 1,
1092
						   sizeof(VPageDescr *) * (Fnpages - ToVpI - 1));
1093
						Fnpages--;
1094
						Assert(Fvplast == Fvpl->vpl_pgdesc[Fnpages - 1]);
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
					}
				}
				for (i = 0; i < Fnpages; i++)
				{
					if (vc_enough_space(Fvpl->vpl_pgdesc[i], tlen))
						break;
				}
				if (i == Fnpages)
					break;		/* can't move item anywhere */
				ToVpI = i;
				ToVpd = Fvpl->vpl_pgdesc[ToVpI];
				ToBuf = ReadBuffer(onerel, ToVpd->vpd_blkno);
				ToPage = BufferGetPage(ToBuf);
				/* if this page was not used before - clean it */
				if (!PageIsEmpty(ToPage) && ToVpd->vpd_nusd == 0)
B
Bruce Momjian 已提交
1110
					vc_vacpage(ToPage, ToVpd);
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
			}

			/* copy tuple */
			newtup = (HeapTuple) palloc(tlen);
			memmove((char *) newtup, (char *) htup, tlen);

			/* store transaction information */
			TransactionIdStore(myXID, &(newtup->t_xmin));
			newtup->t_cmin = myCID;
			StoreInvalidTransactionId(&(newtup->t_xmax));
V
Vadim B. Mikheev 已提交
1121 1122 1123
			/* set xmin to unknown and xmax to invalid */
			newtup->t_infomask &= ~(HEAP_XACT_MASK);
			newtup->t_infomask |= HEAP_XMAX_INVALID;
1124 1125 1126 1127 1128 1129

			/* add tuple to the page */
			newoff = PageAddItem(ToPage, (Item) newtup, tlen,
								 InvalidOffsetNumber, LP_USED);
			if (newoff == InvalidOffsetNumber)
			{
1130
				elog(ERROR, "\
V
Vadim B. Mikheev 已提交
1131
failed to add item with len = %u to page %u (free space %u, nusd %u, noff %u)",
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
					 tlen, ToVpd->vpd_blkno, ToVpd->vpd_free,
					 ToVpd->vpd_nusd, ToVpd->vpd_noff);
			}
			newitemid = PageGetItemId(ToPage, newoff);
			pfree(newtup);
			newtup = (HeapTuple) PageGetItem(ToPage, newitemid);
			ItemPointerSet(&(newtup->t_ctid), ToVpd->vpd_blkno, newoff);

			/* now logically delete end-tuple */
			TransactionIdStore(myXID, &(htup->t_xmax));
			htup->t_cmax = myCID;
V
Vadim B. Mikheev 已提交
1143 1144
			/* set xmax to unknown */
			htup->t_infomask &= ~(HEAP_XMAX_INVALID | HEAP_XMAX_COMMITTED);
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157

			ToVpd->vpd_nusd++;
			nmoved++;
			ToVpd->vpd_free = ((PageHeader) ToPage)->pd_upper - ((PageHeader) ToPage)->pd_lower;
			vpc->vpd_voff[vpc->vpd_noff++] = offnum;

			/* insert index' tuples if needed */
			if (Irel != (Relation *) NULL)
			{
				for (i = 0, idcur = Idesc; i < nindices; i++, idcur++)
				{
					FormIndexDatum(
								   idcur->natts,
B
Bruce Momjian 已提交
1158
							   (AttrNumber *) &(idcur->tform->indkey[0]),
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
								   newtup,
								   tupdesc,
								   InvalidBuffer,
								   idatum,
								   inulls,
								   idcur->finfoP);
					iresult = index_insert(
										   Irel[i],
										   idatum,
										   inulls,
										   &(newtup->t_ctid),
										   onerel);
					if (iresult)
						pfree(iresult);
				}
			}

		}						/* walk along page */

		if (vpc->vpd_noff > 0)	/* some tuples were moved */
V
Vadim B. Mikheev 已提交
1179
		{
1180 1181
			vc_reappage(&Nvpl, vpc);
			WriteBuffer(buf);
V
Vadim B. Mikheev 已提交
1182
		}
1183 1184 1185 1186
		else if (dowrite)
			WriteBuffer(buf);
		else
			ReleaseBuffer(buf);
V
Vadim B. Mikheev 已提交
1187

1188 1189 1190 1191 1192 1193 1194 1195
		if (offnum <= maxoff)
			break;				/* some item(s) left */

	}							/* walk along relation */

	blkno++;					/* new number of blocks */

	if (ToBuf != InvalidBuffer)
V
Vadim B. Mikheev 已提交
1196
	{
1197 1198
		Assert(nmoved > 0);
		WriteBuffer(ToBuf);
V
Vadim B. Mikheev 已提交
1199
	}
1200 1201

	if (nmoved > 0)
V
Vadim B. Mikheev 已提交
1202
	{
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212

		/*
		 * We have to commit our tuple' movings before we'll truncate
		 * relation, but we shouldn't lose our locks. And so - quick hack:
		 * flush buffers and record status of current transaction as
		 * committed, and continue. - vadim 11/13/96
		 */
		FlushBufferPool(!TransactionFlushEnabled());
		TransactionIdCommit(myXID);
		FlushBufferPool(!TransactionFlushEnabled());
V
Vadim B. Mikheev 已提交
1213
	}
1214 1215

	/*
V
Vadim B. Mikheev 已提交
1216
	 * Clean uncleaned reapped pages from Vvpl list and set xmin committed
1217 1218 1219 1220
	 * for inserted tuples
	 */
	nchkmvd = 0;
	for (i = 0, vpp = Vvpl->vpl_pgdesc; i < Vnpages; i++, vpp++)
V
Vadim B. Mikheev 已提交
1221
	{
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
		Assert((*vpp)->vpd_blkno < blkno);
		buf = ReadBuffer(onerel, (*vpp)->vpd_blkno);
		page = BufferGetPage(buf);
		if ((*vpp)->vpd_nusd == 0)		/* this page was not used */
		{

			/*
			 * noff == 0 in empty pages only - such pages should be
			 * re-used
			 */
			Assert((*vpp)->vpd_noff > 0);
B
Bruce Momjian 已提交
1233
			vc_vacpage(page, *vpp);
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
		}
		else
/* this page was used */
		{
			ntups = 0;
			moff = PageGetMaxOffsetNumber(page);
			for (newoff = FirstOffsetNumber;
				 newoff <= moff;
				 newoff = OffsetNumberNext(newoff))
			{
				itemid = PageGetItemId(page, newoff);
				if (!ItemIdIsUsed(itemid))
					continue;
				htup = (HeapTuple) PageGetItem(page, itemid);
				if (TransactionIdEquals((TransactionId) htup->t_xmin, myXID))
				{
V
Vadim B. Mikheev 已提交
1250
					htup->t_infomask |= HEAP_XMIN_COMMITTED;
1251 1252 1253 1254 1255 1256 1257
					ntups++;
				}
			}
			Assert((*vpp)->vpd_nusd == ntups);
			nchkmvd += ntups;
		}
		WriteBuffer(buf);
V
Vadim B. Mikheev 已提交
1258
	}
1259 1260 1261
	Assert(nmoved == nchkmvd);

	getrusage(RUSAGE_SELF, &ru1);
1262

1263
	elog(MESSAGE_LEVEL, "Rel %s: Pages: %u --> %u; Tuple(s) moved: %u. \
V
Vadim B. Mikheev 已提交
1264
Elapsed %u/%u sec.",
1265 1266 1267 1268 1269 1270
		 (RelationGetRelationName(onerel))->data,
		 nblocks, blkno, nmoved,
		 ru1.ru_stime.tv_sec - ru0.ru_stime.tv_sec,
		 ru1.ru_utime.tv_sec - ru0.ru_utime.tv_sec);

	if (Nvpl.vpl_npages > 0)
V
Vadim B. Mikheev 已提交
1271
	{
1272 1273 1274
		/* vacuum indices again if needed */
		if (Irel != (Relation *) NULL)
		{
1275 1276 1277
			VPageDescr *vpleft,
					   *vpright,
						vpsave;
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324

			/* re-sort Nvpl.vpl_pgdesc */
			for (vpleft = Nvpl.vpl_pgdesc,
				 vpright = Nvpl.vpl_pgdesc + Nvpl.vpl_npages - 1;
				 vpleft < vpright; vpleft++, vpright--)
			{
				vpsave = *vpleft;
				*vpleft = *vpright;
				*vpright = vpsave;
			}
			for (i = 0; i < nindices; i++)
				vc_vaconeind(&Nvpl, Irel[i], vacrelstats->ntups);
		}

		/*
		 * clean moved tuples from last page in Nvpl list if some tuples
		 * left there
		 */
		if (vpc->vpd_noff > 0 && offnum <= maxoff)
		{
			Assert(vpc->vpd_blkno == blkno - 1);
			buf = ReadBuffer(onerel, vpc->vpd_blkno);
			page = BufferGetPage(buf);
			ntups = 0;
			maxoff = offnum;
			for (offnum = FirstOffsetNumber;
				 offnum < maxoff;
				 offnum = OffsetNumberNext(offnum))
			{
				itemid = PageGetItemId(page, offnum);
				if (!ItemIdIsUsed(itemid))
					continue;
				htup = (HeapTuple) PageGetItem(page, itemid);
				Assert(TransactionIdEquals((TransactionId) htup->t_xmax, myXID));
				itemid->lp_flags &= ~LP_USED;
				ntups++;
			}
			Assert(vpc->vpd_noff == ntups);
			PageRepairFragmentation(page);
			WriteBuffer(buf);
		}

		/* now - free new list of reapped pages */
		vpp = Nvpl.vpl_pgdesc;
		for (i = 0; i < Nvpl.vpl_npages; i++, vpp++)
			pfree(*vpp);
		pfree(Nvpl.vpl_pgdesc);
V
Vadim B. Mikheev 已提交
1325 1326
	}

1327 1328
	/* truncate relation */
	if (blkno < nblocks)
V
Vadim B. Mikheev 已提交
1329
	{
1330 1331
		i = BlowawayRelationBuffers(onerel, blkno);
		if (i < 0)
1332
			elog(FATAL, "VACUUM (vc_rpfheap): BlowawayRelationBuffers returned %d", i);
B
Bruce Momjian 已提交
1333
		blkno = smgrtruncate(DEFAULT_SMGR, onerel, blkno);
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
		Assert(blkno >= 0);
		vacrelstats->npages = blkno;	/* set new number of blocks */
	}

	if (Irel != (Relation *) NULL)		/* pfree index' allocations */
	{
		pfree(Idesc);
		pfree(idatum);
		pfree(inulls);
		vc_clsindices(nindices, Irel);
V
Vadim B. Mikheev 已提交
1344 1345
	}

1346 1347
	pfree(vpc);

1348
}	/* vc_rpfheap */
V
Vadim B. Mikheev 已提交
1349 1350

/*
1351
 *	vc_vacheap() -- free dead tuples
V
Vadim B. Mikheev 已提交
1352
 *
1353 1354
 *		This routine marks dead tuples as unused and truncates relation
 *		if there are "empty" end-blocks.
V
Vadim B. Mikheev 已提交
1355 1356
 */
static void
1357
vc_vacheap(VRelStats *vacrelstats, Relation onerel, VPageList Vvpl)
V
Vadim B. Mikheev 已提交
1358
{
1359 1360 1361 1362 1363
	Buffer		buf;
	Page		page;
	VPageDescr *vpp;
	int			nblocks;
	int			i;
1364 1365

	nblocks = Vvpl->vpl_npages;
1366
	nblocks -= Vvpl->vpl_nemend;/* nothing to do with them */
1367 1368 1369 1370 1371 1372 1373

	for (i = 0, vpp = Vvpl->vpl_pgdesc; i < nblocks; i++, vpp++)
	{
		if ((*vpp)->vpd_noff > 0)
		{
			buf = ReadBuffer(onerel, (*vpp)->vpd_blkno);
			page = BufferGetPage(buf);
B
Bruce Momjian 已提交
1374
			vc_vacpage(page, *vpp);
1375 1376
			WriteBuffer(buf);
		}
V
Vadim B. Mikheev 已提交
1377 1378
	}

1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
	/* truncate relation if there are some empty end-pages */
	if (Vvpl->vpl_nemend > 0)
	{
		Assert(vacrelstats->npages >= Vvpl->vpl_nemend);
		nblocks = vacrelstats->npages - Vvpl->vpl_nemend;
		elog(MESSAGE_LEVEL, "Rel %s: Pages: %u --> %u.",
			 (RelationGetRelationName(onerel))->data,
			 vacrelstats->npages, nblocks);

		/*
		 * we have to flush "empty" end-pages (if changed, but who knows
		 * it) before truncation
		 */
		FlushBufferPool(!TransactionFlushEnabled());
1393

1394 1395
		i = BlowawayRelationBuffers(onerel, nblocks);
		if (i < 0)
1396
			elog(FATAL, "VACUUM (vc_vacheap): BlowawayRelationBuffers returned %d", i);
1397

B
Bruce Momjian 已提交
1398
		nblocks = smgrtruncate(DEFAULT_SMGR, onerel, nblocks);
1399 1400 1401
		Assert(nblocks >= 0);
		vacrelstats->npages = nblocks;	/* set new number of blocks */
	}
V
Vadim B. Mikheev 已提交
1402

1403
}	/* vc_vacheap */
V
Vadim B. Mikheev 已提交
1404 1405

/*
B
Bruce Momjian 已提交
1406
 *	vc_vacpage() -- free dead tuples on a page
1407
 *					 and repaire its fragmentation.
V
Vadim B. Mikheev 已提交
1408 1409
 */
static void
B
Bruce Momjian 已提交
1410
vc_vacpage(Page page, VPageDescr vpd)
V
Vadim B. Mikheev 已提交
1411
{
1412 1413
	ItemId		itemid;
	int			i;
1414 1415 1416

	Assert(vpd->vpd_nusd == 0);
	for (i = 0; i < vpd->vpd_noff; i++)
V
Vadim B. Mikheev 已提交
1417
	{
1418 1419
		itemid = &(((PageHeader) page)->pd_linp[vpd->vpd_voff[i] - 1]);
		itemid->lp_flags &= ~LP_USED;
V
Vadim B. Mikheev 已提交
1420
	}
1421
	PageRepairFragmentation(page);
V
Vadim B. Mikheev 已提交
1422

1423
}	/* vc_vacpage */
1424

1425
/*
1426
 *	_vc_scanoneind() -- scan one index relation to update statistic.
1427 1428 1429
 *
 */
static void
1430
vc_scanoneind(Relation indrel, int nhtups)
1431
{
1432
	RetrieveIndexResult res;
1433 1434 1435 1436 1437
	IndexScanDesc iscan;
	int			nitups;
	int			nipages;
	struct rusage ru0,
				ru1;
1438

1439
	getrusage(RUSAGE_SELF, &ru0);
1440

1441 1442 1443
	/* walk through the entire index */
	iscan = index_beginscan(indrel, false, 0, (ScanKey) NULL);
	nitups = 0;
1444

1445 1446 1447 1448 1449 1450
	while ((res = index_getnext(iscan, ForwardScanDirection))
		   != (RetrieveIndexResult) NULL)
	{
		nitups++;
		pfree(res);
	}
1451

1452
	index_endscan(iscan);
1453

1454 1455 1456
	/* now update statistics in pg_class */
	nipages = RelationGetNumberOfBlocks(indrel);
	vc_updstats(indrel->rd_id, nipages, nitups, false, NULL);
1457

1458
	getrusage(RUSAGE_SELF, &ru1);
1459

1460 1461 1462 1463
	elog(MESSAGE_LEVEL, "Ind %s: Pages %u; Tuples %u. Elapsed %u/%u sec.",
		 indrel->rd_rel->relname.data, nipages, nitups,
		 ru1.ru_stime.tv_sec - ru0.ru_stime.tv_sec,
		 ru1.ru_utime.tv_sec - ru0.ru_utime.tv_sec);
1464

1465 1466 1467
	if (nitups != nhtups)
		elog(NOTICE, "Ind %s: NUMBER OF INDEX' TUPLES (%u) IS NOT THE SAME AS HEAP' (%u)",
			 indrel->rd_rel->relname.data, nitups, nhtups);
1468

1469
}	/* vc_scanoneind */
1470

1471
/*
1472
 *	vc_vaconeind() -- vacuum one index relation.
1473
 *
1474 1475 1476 1477 1478
 *		Vpl is the VPageList of the heap we're currently vacuuming.
 *		It's locked. Indrel is an index relation on the vacuumed heap.
 *		We don't set locks on the index	relation here, since the indexed
 *		access methods support locking at different granularities.
 *		We let them handle it.
1479
 *
1480 1481
 *		Finally, we arrange to update the index relation's statistics in
 *		pg_class.
1482 1483
 */
static void
1484
vc_vaconeind(VPageList vpl, Relation indrel, int nhtups)
1485
{
1486
	RetrieveIndexResult res;
1487 1488 1489 1490 1491 1492 1493 1494
	IndexScanDesc iscan;
	ItemPointer heapptr;
	int			nvac;
	int			nitups;
	int			nipages;
	VPageDescr	vp;
	struct rusage ru0,
				ru1;
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504

	getrusage(RUSAGE_SELF, &ru0);

	/* walk through the entire index */
	iscan = index_beginscan(indrel, false, 0, (ScanKey) NULL);
	nvac = 0;
	nitups = 0;

	while ((res = index_getnext(iscan, ForwardScanDirection))
		   != (RetrieveIndexResult) NULL)
V
Vadim B. Mikheev 已提交
1505
	{
1506 1507 1508 1509
		heapptr = &res->heap_iptr;

		if ((vp = vc_tidreapped(heapptr, vpl)) != (VPageDescr) NULL)
		{
1510
#if 0
1511 1512 1513 1514 1515
			elog(DEBUG, "<%x,%x> -> <%x,%x>",
				 ItemPointerGetBlockNumber(&(res->index_iptr)),
				 ItemPointerGetOffsetNumber(&(res->index_iptr)),
				 ItemPointerGetBlockNumber(&(res->heap_iptr)),
				 ItemPointerGetOffsetNumber(&(res->heap_iptr)));
1516
#endif
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
			if (vp->vpd_noff == 0)
			{					/* this is EmptyPage !!! */
				elog(NOTICE, "Ind %s: pointer to EmptyPage (blk %u off %u) - fixing",
					 indrel->rd_rel->relname.data,
					 vp->vpd_blkno, ItemPointerGetOffsetNumber(heapptr));
			}
			++nvac;
			index_delete(indrel, &res->index_iptr);
		}
		else
			nitups++;
1528

1529 1530 1531
		/* be tidy */
		pfree(res);
	}
1532

1533
	index_endscan(iscan);
1534

1535 1536 1537
	/* now update statistics in pg_class */
	nipages = RelationGetNumberOfBlocks(indrel);
	vc_updstats(indrel->rd_id, nipages, nitups, false, NULL);
1538

1539
	getrusage(RUSAGE_SELF, &ru1);
1540

1541 1542 1543 1544
	elog(MESSAGE_LEVEL, "Ind %s: Pages %u; Tuples %u: Deleted %u. Elapsed %u/%u sec.",
		 indrel->rd_rel->relname.data, nipages, nitups, nvac,
		 ru1.ru_stime.tv_sec - ru0.ru_stime.tv_sec,
		 ru1.ru_utime.tv_sec - ru0.ru_utime.tv_sec);
1545

1546 1547 1548
	if (nitups != nhtups)
		elog(NOTICE, "Ind %s: NUMBER OF INDEX' TUPLES (%u) IS NOT THE SAME AS HEAP' (%u)",
			 indrel->rd_rel->relname.data, nitups, nhtups);
V
Vadim B. Mikheev 已提交
1549

1550
}	/* vc_vaconeind */
V
Vadim B. Mikheev 已提交
1551 1552

/*
1553
 *	vc_tidreapped() -- is a particular tid reapped?
V
Vadim B. Mikheev 已提交
1554
 *
1555
 *		vpl->VPageDescr_array is sorted in right order.
V
Vadim B. Mikheev 已提交
1556
 */
1557
static VPageDescr
1558
vc_tidreapped(ItemPointer itemptr, VPageList vpl)
V
Vadim B. Mikheev 已提交
1559
{
1560 1561 1562 1563 1564
	OffsetNumber ioffno;
	OffsetNumber *voff;
	VPageDescr	vp,
			   *vpp;
	VPageDescrData vpd;
V
Vadim B. Mikheev 已提交
1565

1566 1567
	vpd.vpd_blkno = ItemPointerGetBlockNumber(itemptr);
	ioffno = ItemPointerGetOffsetNumber(itemptr);
V
Vadim B. Mikheev 已提交
1568

1569 1570 1571 1572
	vp = &vpd;
	vpp = (VPageDescr *) vc_find_eq((char *) (vpl->vpl_pgdesc),
					   vpl->vpl_npages, sizeof(VPageDescr), (char *) &vp,
									vc_cmp_blk);
V
Vadim B. Mikheev 已提交
1573

1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
	if (vpp == (VPageDescr *) NULL)
		return ((VPageDescr) NULL);
	vp = *vpp;

	/* ok - we are on true page */

	if (vp->vpd_noff == 0)
	{							/* this is EmptyPage !!! */
		return (vp);
	}

	voff = (OffsetNumber *) vc_find_eq((char *) (vp->vpd_voff),
					vp->vpd_noff, sizeof(OffsetNumber), (char *) &ioffno,
									   vc_cmp_offno);

	if (voff == (OffsetNumber *) NULL)
		return ((VPageDescr) NULL);

	return (vp);

1594
}	/* vc_tidreapped */
1595 1596

/*
1597
 *	vc_attrstats() -- compute column statistics used by the optimzer
1598
 *
1599 1600 1601 1602
 *	We compute the column min, max, null and non-null counts.
 *	Plus we attempt to find the count of the value that occurs most
 *	frequently in each column
 *	These figures are used to compute the selectivity of the column
1603
 *
1604 1605 1606 1607 1608
 *	We use a three-bucked cache to get the most frequent item
 *	The 'guess' buckets count hits.  A cache miss causes guess1
 *	to get the most hit 'guess' item in the most recent cycle, and
 *	the new item goes into guess2.	Whenever the total count of hits
 *	of a 'guess' entry is larger than 'best', 'guess' becomes 'best'.
1609
 *
1610 1611
 *	This method works perfectly for columns with unique values, and columns
 *	with only two unique values, plus nulls.
1612
 *
1613 1614
 *	It becomes less perfect as the number of unique values increases and
 *	their distribution in the table becomes more random.
1615 1616 1617
 *
 */
static void
1618
vc_attrstats(Relation onerel, VRelStats *vacrelstats, HeapTuple htup)
1619
{
1620 1621 1622 1623 1624 1625
	int			i,
				attr_cnt = vacrelstats->va_natts;
	VacAttrStats *vacattrstats = vacrelstats->vacattrstats;
	TupleDesc	tupDesc = onerel->rd_att;
	Datum		value;
	bool		isnull;
1626 1627 1628

	for (i = 0; i < attr_cnt; i++)
	{
1629 1630
		VacAttrStats *stats = &vacattrstats[i];
		bool		value_hit = true;
1631

1632
		value = heap_getattr(htup,
1633
							 stats->attr->attnum, tupDesc, &isnull);
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659

		if (!VacAttrStatsEqValid(stats))
			continue;

		if (isnull)
			stats->null_cnt++;
		else
		{
			stats->nonnull_cnt++;
			if (stats->initialized == false)
			{
				vc_bucketcpy(stats->attr, value, &stats->best, &stats->best_len);
				/* best_cnt gets incremented later */
				vc_bucketcpy(stats->attr, value, &stats->guess1, &stats->guess1_len);
				stats->guess1_cnt = stats->guess1_hits = 1;
				vc_bucketcpy(stats->attr, value, &stats->guess2, &stats->guess2_len);
				stats->guess2_hits = 1;
				if (VacAttrStatsLtGtValid(stats))
				{
					vc_bucketcpy(stats->attr, value, &stats->max, &stats->max_len);
					vc_bucketcpy(stats->attr, value, &stats->min, &stats->min_len);
				}
				stats->initialized = true;
			}
			if (VacAttrStatsLtGtValid(stats))
			{
1660
				if ((*fmgr_faddr(&stats->f_cmplt)) (value, stats->min))
1661 1662 1663 1664
				{
					vc_bucketcpy(stats->attr, value, &stats->min, &stats->min_len);
					stats->min_cnt = 0;
				}
1665
				if ((*fmgr_faddr(&stats->f_cmpgt)) (value, stats->max))
1666 1667 1668 1669
				{
					vc_bucketcpy(stats->attr, value, &stats->max, &stats->max_len);
					stats->max_cnt = 0;
				}
1670
				if ((*fmgr_faddr(&stats->f_cmpeq)) (value, stats->min))
1671
					stats->min_cnt++;
1672
				else if ((*fmgr_faddr(&stats->f_cmpeq)) (value, stats->max))
1673 1674
					stats->max_cnt++;
			}
1675
			if ((*fmgr_faddr(&stats->f_cmpeq)) (value, stats->best))
1676
				stats->best_cnt++;
1677
			else if ((*fmgr_faddr(&stats->f_cmpeq)) (value, stats->guess1))
1678 1679 1680 1681
			{
				stats->guess1_cnt++;
				stats->guess1_hits++;
			}
1682
			else if ((*fmgr_faddr(&stats->f_cmpeq)) (value, stats->guess2))
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
				stats->guess2_hits++;
			else
				value_hit = false;

			if (stats->guess2_hits > stats->guess1_hits)
			{
				swapDatum(stats->guess1, stats->guess2);
				swapInt(stats->guess1_len, stats->guess2_len);
				stats->guess1_cnt = stats->guess2_hits;
				swapLong(stats->guess1_hits, stats->guess2_hits);
			}
			if (stats->guess1_cnt > stats->best_cnt)
			{
				swapDatum(stats->best, stats->guess1);
				swapInt(stats->best_len, stats->guess1_len);
				swapLong(stats->best_cnt, stats->guess1_cnt);
				stats->guess1_hits = 1;
				stats->guess2_hits = 1;
			}
			if (!value_hit)
			{
				vc_bucketcpy(stats->attr, value, &stats->guess2, &stats->guess2_len);
				stats->guess1_hits = 1;
				stats->guess2_hits = 1;
			}
1708 1709
		}
	}
1710
	return;
1711 1712 1713
}

/*
1714
 *	vc_bucketcpy() -- update pg_class statistics for one relation
1715 1716 1717
 *
 */
static void
1718
vc_bucketcpy(AttributeTupleForm attr, Datum value, Datum *bucket, int16 *bucket_len)
1719
{
1720 1721 1722 1723
	if (attr->attbyval && attr->attlen != -1)
		*bucket = value;
	else
	{
1724
		int			len = (attr->attlen != -1 ? attr->attlen : VARSIZE(value));
1725 1726 1727 1728 1729 1730 1731 1732 1733

		if (len > *bucket_len)
		{
			if (*bucket_len != 0)
				pfree(DatumGetPointer(*bucket));
			*bucket = PointerGetDatum(palloc(len));
			*bucket_len = len;
		}
		memmove(DatumGetPointer(*bucket), DatumGetPointer(value), len);
1734 1735
	}
}
1736 1737

/*
1738
 *	vc_updstats() -- update pg_class statistics for one relation
1739
 *
1740 1741 1742 1743 1744 1745 1746
 *		This routine works for both index and heap relation entries in
 *		pg_class.  We violate no-overwrite semantics here by storing new
 *		values for ntups, npages, and hasindex directly in the pg_class
 *		tuple that's already on the page.  The reason for this is that if
 *		we updated these tuples in the usual way, then every tuple in pg_class
 *		would be replaced every day.  This would make planning and executing
 *		historical queries very expensive.
1747 1748
 */
static void
1749
vc_updstats(Oid relid, int npages, int ntups, bool hasindex, VRelStats *vacrelstats)
1750
{
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
	Relation	rd,
				ad,
				sd;
	HeapScanDesc rsdesc,
				asdesc;
	TupleDesc	sdesc;
	HeapTuple	rtup,
				atup,
				stup;
	Buffer		rbuf,
				abuf;
	Form_pg_class pgcform;
	ScanKeyData rskey,
				askey;
1765 1766 1767 1768 1769 1770
	AttributeTupleForm attp;

	/*
	 * update number of tuples and number of pages in pg_class
	 */
	ScanKeyEntryInitialize(&rskey, 0x0, ObjectIdAttributeNumber,
B
Bruce Momjian 已提交
1771
						   F_OIDEQ,
1772 1773 1774
						   ObjectIdGetDatum(relid));

	rd = heap_openr(RelationRelationName);
1775
	rsdesc = heap_beginscan(rd, false, false, 1, &rskey);
1776 1777

	if (!HeapTupleIsValid(rtup = heap_getnext(rsdesc, 0, &rbuf)))
1778
		elog(ERROR, "pg_class entry for relid %d vanished during vacuuming",
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
			 relid);

	/* overwrite the existing statistics in the tuple */
	vc_setpagelock(rd, BufferGetBlockNumber(rbuf));
	pgcform = (Form_pg_class) GETSTRUCT(rtup);
	pgcform->reltuples = ntups;
	pgcform->relpages = npages;
	pgcform->relhasindex = hasindex;

	if (vacrelstats != NULL && vacrelstats->va_natts > 0)
1789
	{
1790 1791
		VacAttrStats *vacattrstats = vacrelstats->vacattrstats;
		int			natts = vacrelstats->va_natts;
1792 1793 1794 1795 1796 1797

		ad = heap_openr(AttributeRelationName);
		sd = heap_openr(StatisticRelationName);
		ScanKeyEntryInitialize(&askey, 0, Anum_pg_attribute_attrelid,
							   F_INT4EQ, relid);

1798
		asdesc = heap_beginscan(ad, false, false, 1, &askey);
1799 1800

		while (HeapTupleIsValid(atup = heap_getnext(asdesc, 0, &abuf)))
1801
		{
1802 1803
			int			i;
			float32data selratio;		/* average ratio of rows selected
1804
										 * for a random constant */
1805 1806 1807
			VacAttrStats *stats;
			Datum		values[Natts_pg_statistic];
			char		nulls[Natts_pg_statistic];
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833

			attp = (AttributeTupleForm) GETSTRUCT(atup);
			if (attp->attnum <= 0)		/* skip system attributes for now, */
				/* they are unique anyway */
				continue;

			for (i = 0; i < natts; i++)
			{
				if (attp->attnum == vacattrstats[i].attr->attnum)
					break;
			}
			if (i >= natts)
				continue;
			stats = &(vacattrstats[i]);

			/* overwrite the existing statistics in the tuple */
			if (VacAttrStatsEqValid(stats))
			{

				vc_setpagelock(ad, BufferGetBlockNumber(abuf));

				if (stats->nonnull_cnt + stats->null_cnt == 0 ||
					(stats->null_cnt <= 1 && stats->best_cnt == 1))
					selratio = 0;
				else if (VacAttrStatsLtGtValid(stats) && stats->min_cnt + stats->max_cnt == stats->nonnull_cnt)
				{
1834 1835 1836 1837
					double		min_cnt_d = stats->min_cnt,
								max_cnt_d = stats->max_cnt,
								null_cnt_d = stats->null_cnt,
								nonnullcnt_d = stats->nonnull_cnt;		/* prevent overflow */
1838 1839 1840 1841 1842 1843

					selratio = (min_cnt_d * min_cnt_d + max_cnt_d * max_cnt_d + null_cnt_d * null_cnt_d) /
						(nonnullcnt_d + null_cnt_d) / (nonnullcnt_d + null_cnt_d);
				}
				else
				{
1844 1845
					double		most = (double) (stats->best_cnt > stats->null_cnt ? stats->best_cnt : stats->null_cnt);
					double		total = ((double) stats->nonnull_cnt) + ((double) stats->null_cnt);
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865

					/*
					 * we assume count of other values are 20% of best
					 * count in table
					 */
					selratio = (most * most + 0.20 * most * (total - most)) / total / total;
				}
				if (selratio > 1.0)
					selratio = 1.0;
				attp->attdisbursion = selratio;
				WriteNoReleaseBuffer(abuf);

				/* DO PG_STATISTIC INSERTS */

				/*
				 * doing system relations, especially pg_statistic is a
				 * problem
				 */
				if (VacAttrStatsLtGtValid(stats) && stats->initialized	/* &&
																		 * !IsSystemRelationName(
1866
																		 *
B
Bruce Momjian 已提交
1867
					 pgcform->relname.data) */ )
1868
				{
1869
					FmgrInfo	out_function;
1870
					char	   *out_string;
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882

					for (i = 0; i < Natts_pg_statistic; ++i)
						nulls[i] = ' ';

					/* ----------------
					 *	initialize values[]
					 * ----------------
					 */
					i = 0;
					values[i++] = (Datum) relid;		/* 1 */
					values[i++] = (Datum) attp->attnum; /* 2 */
					values[i++] = (Datum) InvalidOid;	/* 3 */
1883 1884
					fmgr_info(stats->outfunc, &out_function);
					out_string = (*fmgr_faddr(&out_function)) (stats->min, stats->attr->atttypid);
B
Bruce Momjian 已提交
1885
					values[i++] = (Datum) fmgr(F_TEXTIN, out_string);
1886
					pfree(out_string);
1887
					out_string = (char *) (*fmgr_faddr(&out_function)) (stats->max, stats->attr->atttypid);
B
Bruce Momjian 已提交
1888
					values[i++] = (Datum) fmgr(F_TEXTIN, out_string);
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
					pfree(out_string);

					sdesc = sd->rd_att;

					stup = heap_formtuple(sdesc, values, nulls);

					/* ----------------
					 *	insert the tuple in the relation and get the tuple's oid.
					 * ----------------
					 */
					heap_insert(sd, stup);
					pfree(DatumGetPointer(values[3]));
					pfree(DatumGetPointer(values[4]));
					pfree(stup);
				}
			}
1905
		}
1906 1907 1908
		heap_endscan(asdesc);
		heap_close(ad);
		heap_close(sd);
1909
	}
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923

	/* XXX -- after write, should invalidate relcache in other backends */
	WriteNoReleaseBuffer(rbuf); /* heap_endscan release scan' buffers ? */

	/*
	 * invalidating system relations confuses the function cache of
	 * pg_operator and pg_opclass
	 */
	if (!IsSystemRelationName(pgcform->relname.data))
		RelationInvalidateHeapTuple(rd, rtup);

	/* that's all, folks */
	heap_endscan(rsdesc);
	heap_close(rd);
1924 1925
}

1926
/*
1927
 *	vc_delhilowstats() -- delete pg_statistics rows
1928 1929 1930
 *
 */
static void
1931
vc_delhilowstats(Oid relid, int attcnt, int *attnums)
1932
{
1933 1934 1935 1936
	Relation	pgstatistic;
	HeapScanDesc pgsscan;
	HeapTuple	pgstup;
	ScanKeyData pgskey;
1937 1938 1939 1940 1941 1942

	pgstatistic = heap_openr(StatisticRelationName);

	if (relid != InvalidOid)
	{
		ScanKeyEntryInitialize(&pgskey, 0x0, Anum_pg_statistic_starelid,
B
Bruce Momjian 已提交
1943
							   F_OIDEQ,
1944
							   ObjectIdGetDatum(relid));
1945
		pgsscan = heap_beginscan(pgstatistic, false, false, 1, &pgskey);
1946 1947
	}
	else
1948
		pgsscan = heap_beginscan(pgstatistic, false, false, 0, NULL);
1949 1950 1951 1952 1953 1954

	while (HeapTupleIsValid(pgstup = heap_getnext(pgsscan, 0, NULL)))
	{
		if (attcnt > 0)
		{
			Form_pg_statistic pgs = (Form_pg_statistic) GETSTRUCT(pgstup);
1955
			int			i;
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969

			for (i = 0; i < attcnt; i++)
			{
				if (pgs->staattnum == attnums[i] + 1)
					break;
			}
			if (i >= attcnt)
				continue;		/* don't delete it */
		}
		heap_delete(pgstatistic, &pgstup->t_ctid);
	}

	heap_endscan(pgsscan);
	heap_close(pgstatistic);
1970 1971
}

1972 1973
static void
vc_setpagelock(Relation rel, BlockNumber blkno)
1974
{
1975
	ItemPointerData itm;
1976

1977
	ItemPointerSet(&itm, blkno, 1);
1978

1979
	RelationSetLockForWritePage(rel, &itm);
1980 1981 1982
}

/*
1983
 *	vc_reappage() -- save a page on the array of reapped pages.
1984
 *
1985 1986 1987
 *		As a side effect of the way that the vacuuming loop for a given
 *		relation works, higher pages come after lower pages in the array
 *		(and highest tid on a page is last).
1988
 */
1989
static void
1990
vc_reappage(VPageList vpl, VPageDescr vpc)
1991
{
1992
	VPageDescr	newvpd;
1993

1994 1995
	/* allocate a VPageDescrData entry */
	newvpd = (VPageDescr) palloc(sizeof(VPageDescrData) + vpc->vpd_noff * sizeof(OffsetNumber));
1996

1997 1998 1999 2000 2001 2002 2003
	/* fill it in */
	if (vpc->vpd_noff > 0)
		memmove(newvpd->vpd_voff, vpc->vpd_voff, vpc->vpd_noff * sizeof(OffsetNumber));
	newvpd->vpd_blkno = vpc->vpd_blkno;
	newvpd->vpd_free = vpc->vpd_free;
	newvpd->vpd_nusd = vpc->vpd_nusd;
	newvpd->vpd_noff = vpc->vpd_noff;
2004

2005 2006 2007
	/* insert this page into vpl list */
	vc_vpinsert(vpl, newvpd);

2008
}	/* vc_reappage */
V
Vadim B. Mikheev 已提交
2009 2010

static void
2011
vc_vpinsert(VPageList vpl, VPageDescr vpnew)
V
Vadim B. Mikheev 已提交
2012 2013
{

2014 2015 2016 2017 2018 2019 2020 2021
	/* allocate a VPageDescr entry if needed */
	if (vpl->vpl_npages == 0)
		vpl->vpl_pgdesc = (VPageDescr *) palloc(100 * sizeof(VPageDescr));
	else if (vpl->vpl_npages % 100 == 0)
		vpl->vpl_pgdesc = (VPageDescr *) repalloc(vpl->vpl_pgdesc, (vpl->vpl_npages + 100) * sizeof(VPageDescr));
	vpl->vpl_pgdesc[vpl->vpl_npages] = vpnew;
	(vpl->vpl_npages)++;

2022 2023 2024
}

static void
2025
vc_free(VRelList vrl)
2026
{
2027 2028
	VRelList	p_vrl;
	MemoryContext old;
2029
	PortalVariableMemory pmem;
2030

2031 2032
	pmem = PortalGetVariableMemory(vc_portal);
	old = MemoryContextSwitchTo((MemoryContext) pmem);
2033

2034 2035
	while (vrl != (VRelList) NULL)
	{
2036

2037 2038 2039 2040 2041
		/* free rel list entry */
		p_vrl = vrl;
		vrl = vrl->vrl_next;
		pfree(p_vrl);
	}
2042

2043
	MemoryContextSwitchTo(old);
2044 2045
}

2046
static char *
2047
vc_find_eq(char *bot, int nelem, int size, char *elm, int (*compar) (char *, char *))
2048
{
2049 2050 2051 2052 2053
	int			res;
	int			last = nelem - 1;
	int			celm = nelem / 2;
	bool		last_move,
				first_move;
2054 2055 2056

	last_move = first_move = true;
	for (;;)
2057
	{
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
		if (first_move == true)
		{
			res = compar(bot, elm);
			if (res > 0)
				return (NULL);
			if (res == 0)
				return (bot);
			first_move = false;
		}
		if (last_move == true)
		{
			res = compar(elm, bot + last * size);
			if (res > 0)
				return (NULL);
			if (res == 0)
				return (bot + last * size);
			last_move = false;
		}
		res = compar(elm, bot + celm * size);
		if (res == 0)
			return (bot + celm * size);
		if (res < 0)
		{
			if (celm == 0)
				return (NULL);
			last = celm - 1;
			celm = celm / 2;
			last_move = true;
			continue;
		}

		if (celm == last)
			return (NULL);

		last = last - celm - 1;
		bot = bot + (celm + 1) * size;
		celm = (last + 1) / 2;
		first_move = true;
2096
	}
2097

2098
}	/* vc_find_eq */
2099 2100 2101

static int
vc_cmp_blk(char *left, char *right)
2102
{
2103 2104
	BlockNumber lblk,
				rblk;
2105

2106 2107
	lblk = (*((VPageDescr *) left))->vpd_blkno;
	rblk = (*((VPageDescr *) right))->vpd_blkno;
2108

2109 2110 2111 2112 2113
	if (lblk < rblk)
		return (-1);
	if (lblk == rblk)
		return (0);
	return (1);
2114

2115
}	/* vc_cmp_blk */
2116

2117 2118
static int
vc_cmp_offno(char *left, char *right)
2119 2120
{

2121 2122 2123 2124 2125
	if (*(OffsetNumber *) left < *(OffsetNumber *) right)
		return (-1);
	if (*(OffsetNumber *) left == *(OffsetNumber *) right)
		return (0);
	return (1);
2126

2127
}	/* vc_cmp_offno */
V
Vadim B. Mikheev 已提交
2128 2129 2130


static void
2131
vc_getindices(Oid relid, int *nindices, Relation **Irel)
V
Vadim B. Mikheev 已提交
2132
{
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
	Relation	pgindex;
	Relation	irel;
	TupleDesc	pgidesc;
	HeapTuple	pgitup;
	HeapScanDesc pgiscan;
	Datum		d;
	int			i,
				k;
	bool		n;
	ScanKeyData pgikey;
	Oid		   *ioid;
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153

	*nindices = i = 0;

	ioid = (Oid *) palloc(10 * sizeof(Oid));

	/* prepare a heap scan on the pg_index relation */
	pgindex = heap_openr(IndexRelationName);
	pgidesc = RelationGetTupleDescriptor(pgindex);

	ScanKeyEntryInitialize(&pgikey, 0x0, Anum_pg_index_indrelid,
B
Bruce Momjian 已提交
2154
						   F_OIDEQ,
2155 2156
						   ObjectIdGetDatum(relid));

2157
	pgiscan = heap_beginscan(pgindex, false, false, 1, &pgikey);
2158 2159

	while (HeapTupleIsValid(pgitup = heap_getnext(pgiscan, 0, NULL)))
V
Vadim B. Mikheev 已提交
2160
	{
2161
		d = heap_getattr(pgitup, Anum_pg_index_indexrelid,
2162
						 pgidesc, &n);
2163 2164 2165 2166
		i++;
		if (i % 10 == 0)
			ioid = (Oid *) repalloc(ioid, (i + 10) * sizeof(Oid));
		ioid[i - 1] = DatumGetObjectId(d);
V
Vadim B. Mikheev 已提交
2167 2168
	}

2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
	heap_endscan(pgiscan);
	heap_close(pgindex);

	if (i == 0)
	{							/* No one index found */
		pfree(ioid);
		return;
	}

	if (Irel != (Relation **) NULL)
		*Irel = (Relation *) palloc(i * sizeof(Relation));

	for (k = 0; i > 0;)
	{
		irel = index_open(ioid[--i]);
		if (irel != (Relation) NULL)
		{
			if (Irel != (Relation **) NULL)
				(*Irel)[k] = irel;
			else
				index_close(irel);
			k++;
		}
		else
			elog(NOTICE, "CAN't OPEN INDEX %u - SKIP IT", ioid[i]);
	}
	*nindices = k;
	pfree(ioid);
V
Vadim B. Mikheev 已提交
2197

2198 2199 2200 2201 2202 2203
	if (Irel != (Relation **) NULL && *nindices == 0)
	{
		pfree(*Irel);
		*Irel = (Relation *) NULL;
	}

2204
}	/* vc_getindices */
V
Vadim B. Mikheev 已提交
2205 2206 2207


static void
2208
vc_clsindices(int nindices, Relation *Irel)
V
Vadim B. Mikheev 已提交
2209 2210
{

2211 2212
	if (Irel == (Relation *) NULL)
		return;
V
Vadim B. Mikheev 已提交
2213

2214 2215 2216
	while (nindices--)
		index_close(Irel[nindices]);
	pfree(Irel);
V
Vadim B. Mikheev 已提交
2217

2218
}	/* vc_clsindices */
V
Vadim B. Mikheev 已提交
2219 2220 2221


static void
2222
vc_mkindesc(Relation onerel, int nindices, Relation *Irel, IndDesc **Idesc)
V
Vadim B. Mikheev 已提交
2223
{
2224 2225 2226 2227 2228
	IndDesc    *idcur;
	HeapTuple	pgIndexTup;
	AttrNumber *attnumP;
	int			natts;
	int			i;
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256

	*Idesc = (IndDesc *) palloc(nindices * sizeof(IndDesc));

	for (i = 0, idcur = *Idesc; i < nindices; i++, idcur++)
	{
		pgIndexTup =
			SearchSysCacheTuple(INDEXRELID,
								ObjectIdGetDatum(Irel[i]->rd_id),
								0, 0, 0);
		Assert(pgIndexTup);
		idcur->tform = (IndexTupleForm) GETSTRUCT(pgIndexTup);
		for (attnumP = &(idcur->tform->indkey[0]), natts = 0;
			 *attnumP != InvalidAttrNumber && natts != INDEX_MAX_KEYS;
			 attnumP++, natts++);
		if (idcur->tform->indproc != InvalidOid)
		{
			idcur->finfoP = &(idcur->finfo);
			FIgetnArgs(idcur->finfoP) = natts;
			natts = 1;
			FIgetProcOid(idcur->finfoP) = idcur->tform->indproc;
			*(FIgetname(idcur->finfoP)) = '\0';
		}
		else
			idcur->finfoP = (FuncIndexInfo *) NULL;

		idcur->natts = natts;
	}

2257
}	/* vc_mkindesc */
2258 2259


2260
static bool
2261
vc_enough_space(VPageDescr vpd, Size len)
V
Vadim B. Mikheev 已提交
2262 2263
{

2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277
	len = DOUBLEALIGN(len);

	if (len > vpd->vpd_free)
		return (false);

	if (vpd->vpd_nusd < vpd->vpd_noff)	/* there are free itemid(s) */
		return (true);			/* and len <= free_space */

	/* ok. noff_usd >= noff_free and so we'll have to allocate new itemid */
	if (len <= vpd->vpd_free - sizeof(ItemIdData))
		return (true);

	return (false);

2278
}	/* vc_enough_space */