Skip to content

Commit

Permalink
To support PostgreSQL v16 (#155)
Browse files Browse the repository at this point in the history
* added nbtsort-16.c
  • Loading branch information
zwyan0 authored Jan 16, 2024
1 parent 90da084 commit 8734300
Show file tree
Hide file tree
Showing 17 changed files with 2,299 additions and 66 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
strategy:
matrix:
PGVERSION: # TODO: build with master branch
- "16"
- "15"
- "14"
- "13"
Expand Down
11 changes: 6 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ jobs:
strategy:
matrix:
PGVERSION: # TODO: build with master branch
- "15.1"
- "14.6"
- "13.9"
- "12.13"
- "11.18"
- "16.1"
- "15.5"
- "14.10"
- "13.13"
- "12.17"
- "11.22"
- "10.23"
- "9.6.24"
env:
Expand Down
24 changes: 18 additions & 6 deletions bin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ sql/init-14.sql:
cp sql/init-extension-v2.sql sql/init-14.sql
sql/init-15.sql:
cp sql/init-extension-v3.sql sql/init-15.sql
sql/init-16.sql:
cp sql/init-extension-v3.sql sql/init-16.sql

sql/load_bin.sql: sql/load_bin-$(MAJORVERSION).sql
cp sql/load_bin-$(MAJORVERSION).sql sql/load_bin.sql
Expand All @@ -89,6 +91,8 @@ sql/load_bin-14.sql:
cp sql/load_bin-v2.sql sql/load_bin-14.sql
sql/load_bin-15.sql:
cp sql/load_bin-v2.sql sql/load_bin-15.sql
sql/load_bin-16.sql:
cp sql/load_bin-v2.sql sql/load_bin-16.sql

sql/load_csv.sql: sql/load_csv-$(MAJORVERSION).sql
cp sql/load_csv-$(MAJORVERSION).sql sql/load_csv.sql
Expand All @@ -112,6 +116,8 @@ sql/load_csv-14.sql:
cp sql/load_csv-v2.sql sql/load_csv-14.sql
sql/load_csv-15.sql:
cp sql/load_csv-v3.sql sql/load_csv-15.sql
sql/load_csv-16.sql:
cp sql/load_csv-v3.sql sql/load_csv-16.sql

sql/load_filter.sql: sql/load_filter-$(MAJORVERSION).sql
cp sql/load_filter-$(MAJORVERSION).sql sql/load_filter.sql
Expand All @@ -135,6 +141,8 @@ sql/load_filter-14.sql:
cp sql/load_filter-v3.sql sql/load_filter-14.sql
sql/load_filter-15.sql:
cp sql/load_filter-v3.sql sql/load_filter-15.sql
sql/load_filter-16.sql:
cp sql/load_filter-v3.sql sql/load_filter-16.sql

sql/load_function.sql: sql/load_function-$(MAJORVERSION).sql
cp sql/load_function-$(MAJORVERSION).sql sql/load_function.sql
Expand All @@ -158,6 +166,8 @@ sql/load_function-14.sql:
cp sql/load_function-v3.sql sql/load_function-14.sql
sql/load_function-15.sql:
cp sql/load_function-v3.sql sql/load_function-15.sql
sql/load_function-16.sql:
cp sql/load_function-v3.sql sql/load_function-16.sql

sql/write_bin.sql: sql/write_bin-$(MAJORVERSION).sql
cp sql/write_bin-$(MAJORVERSION).sql sql/write_bin.sql
Expand All @@ -181,17 +191,19 @@ sql/write_bin-14.sql:
cp sql/write_bin-v2.sql sql/write_bin-14.sql
sql/write_bin-15.sql:
cp sql/write_bin-v2.sql sql/write_bin-15.sql
sql/write_bin-16.sql:
cp sql/write_bin-v2.sql sql/write_bin-16.sql

.PHONY: subclean
clean: subclean

subclean:
rm -f sql/init.sql sql/init-{9.3,9.4,9.5,9.6,10,11,12,13,14,15}.sql
rm -f sql/load_bin.sql sql/load_bin-{9.3,9.4,9.5,9.6,10,11,12,13,14,15}.sql
rm -f sql/load_csv.sql sql/load_csv-{9.3,9.4,9.5,9.6,10,11,12,13,14,15}.sql
rm -f sql/load_filter.sql sql/load_filter-{9.3,9.4,9.5,9.6,10,11,12,13,14,15}.sql
rm -f sql/load_function.sql sql/load_function-{9.3,9.4,9.5,9.6,10,11,12,13,14,15}.sql
rm -f sql/write_bin.sql sql/write_bin-{9.3,9.4,9.5,9.6,10,11,12,13,14,15}.sql
rm -f sql/init.sql sql/init-{9.3,9.4,9.5,9.6,10,11,12,13,14,15,16}.sql
rm -f sql/load_bin.sql sql/load_bin-{9.3,9.4,9.5,9.6,10,11,12,13,14,15,16}.sql
rm -f sql/load_csv.sql sql/load_csv-{9.3,9.4,9.5,9.6,10,11,12,13,14,15,16}.sql
rm -f sql/load_filter.sql sql/load_filter-{9.3,9.4,9.5,9.6,10,11,12,13,14,15,16}.sql
rm -f sql/load_function.sql sql/load_function-{9.3,9.4,9.5,9.6,10,11,12,13,14,15,16}.sql
rm -f sql/write_bin.sql sql/write_bin-{9.3,9.4,9.5,9.6,10,11,12,13,14,15,16}.sql

installcheck: sql/init.sql sql/load_bin.sql sql/load_csv.sql sql/load_function.sql sql/load_filter.sql sql/write_bin.sql

Expand Down
113 changes: 90 additions & 23 deletions bin/recovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ extern char *DataDir;
/* Entry point for the recovery. */
extern int LoaderRecoveryMain(void);

static void GetSegmentPath(char path[MAXPGPATH], RelFileNode rnode, int segno);

static void GetSegmentPath(char path[MAXPGPATH],
#if PG_VERSION_NUM >= 160000
RelFileLocator rLocator,
#else
RelFileNode rnode,
#endif
int segno);

/* Determins if the recovery is necessary, and then overwrites data file pages with the vacant one if needed. */
static void StartLoaderRecovery(void);
Expand All @@ -62,7 +69,13 @@ static DBState GetDBClusterState(const char *fname);
static void GetLoadStatusInfo(const char *lsfpath, LoadStatus * ls);

/* Overwrite data pages with a vacant page. */
static void ClearLoadedPage(RelFileNode rnode,
static void ClearLoadedPage(
#if PG_VERSION_NUM >= 160000
RelFileLocator rLocator,
#else
RelFileNode rnode,
#endif

BlockNumber blkbeg,
BlockNumber blkend);

Expand All @@ -78,7 +91,7 @@ static void LoaderCreateLockFile(const char *filename,
bool isDDLock, const char *refName);

/* Check that the header fields of a page appear valid. */
bool PageHeaderIsValid(PageHeader page);
bool PageHeaderIsValid(Page page);


/**
Expand Down Expand Up @@ -189,7 +202,12 @@ StartLoaderRecovery(void)
/*
* overwrite pages created by the loader by blank pages
*/
ClearLoadedPage(ls.ls.rnode,
ClearLoadedPage(
#if PG_VERSION_NUM >= 160000
ls.ls.rLocator,
#else
ls.ls.rnode,
#endif
ls.ls.exist_cnt,
ls.ls.exist_cnt + ls.ls.create_cnt);

Expand Down Expand Up @@ -392,7 +410,13 @@ GetLoadStatusInfo(const char *lsfpath, LoadStatus * ls)
* @return void
*/
static void
ClearLoadedPage(RelFileNode rnode, BlockNumber blkbeg, BlockNumber blkend)
ClearLoadedPage(
#if PG_VERSION_NUM >= 160000
RelFileLocator rLocator,
#else
RelFileNode rnode,
#endif
BlockNumber blkbeg, BlockNumber blkend)
{
BlockNumber segno; /* data file segment no */
char segpath[MAXPGPATH]; /* data file name to open */
Expand Down Expand Up @@ -423,7 +447,13 @@ ClearLoadedPage(RelFileNode rnode, BlockNumber blkbeg, BlockNumber blkend)
* set proper extension.
*/
segno = blkbeg / RELSEG_SIZE;
GetSegmentPath(segpath, rnode, segno);
GetSegmentPath(segpath,
#if PG_VERSION_NUM >= 160000
rLocator,
#else
rnode,
#endif
segno);

/*
* TODO: consider to use truncate instead of zero-fill to end of file.
Expand Down Expand Up @@ -516,7 +546,13 @@ ClearLoadedPage(RelFileNode rnode, BlockNumber blkbeg, BlockNumber blkend)
segpath, strerror(errno));

++segno;
GetSegmentPath(segpath, rnode, segno);
GetSegmentPath(segpath,
#if PG_VERSION_NUM >= 160000
rLocator,
#else
rnode,
#endif
segno);

fd = open(segpath, O_RDWR | PG_BINARY, S_IRUSR | S_IWUSR);
if (fd == -1)
Expand Down Expand Up @@ -557,7 +593,7 @@ IsPageCreatedByLoader(Page page)
{
PageHeader targetBlock = (PageHeader) page;

if (!PageHeaderIsValid(targetBlock))
if (!PageHeaderIsValid(page))
return true;

if (targetBlock->pd_lsn.xlogid == 0 && targetBlock->pd_lsn.xrecoff == 0)
Expand Down Expand Up @@ -909,28 +945,34 @@ PageInit(Page page, Size pageSize, Size specialSize)
* treat such a page as empty and without free space. Eventually, VACUUM
* will clean up such a page and make it usable.
*/

bool
PageHeaderIsValid(PageHeader page)
PageHeaderIsValid(Page page)
{
char *pagebytes;
int i;

PageHeader phdr = (PageHeader) page;
/*
* Check normal case
*/
if (PageGetPageSize(page) == BLCKSZ &&
PageGetPageLayoutVersion(page) == PG_PAGE_LAYOUT_VERSION &&
page->pd_lower >= SizeOfPageHeaderData &&
page->pd_lower <= page->pd_upper &&
page->pd_upper <= page->pd_special &&
page->pd_special <= BLCKSZ &&
page->pd_special == MAXALIGN(page->pd_special))
if (PageGetPageSize(
#if PG_VERSION_NUM >= 160000
page) == BLCKSZ && PageGetPageLayoutVersion(page
#else
phdr) == BLCKSZ && PageGetPageLayoutVersion(phdr
#endif
) == PG_PAGE_LAYOUT_VERSION &&
phdr->pd_lower >= SizeOfPageHeaderData &&
phdr->pd_lower <= phdr->pd_upper &&
phdr->pd_upper <= phdr->pd_special &&
phdr->pd_special <= BLCKSZ &&
phdr->pd_special == MAXALIGN(phdr->pd_special))
return true;

/*
* Check all-zeroes case
*/
pagebytes = (char *) page;
pagebytes = (char *) phdr;
for (i = 0; i < BLCKSZ; i++)
{
if (pagebytes[i] != 0)
Expand All @@ -940,24 +982,49 @@ PageHeaderIsValid(PageHeader page)
}

static void
GetSegmentPath(char path[MAXPGPATH], RelFileNode rnode, int segno)
GetSegmentPath(char path[MAXPGPATH],
#if PG_VERSION_NUM >= 160000
RelFileLocator rLocator,
#else
RelFileNode rnode,
#endif
int segno)
{
#if PG_VERSION_NUM >= 160000
if (rLocator.spcOid == GLOBALTABLESPACE_OID)
#else
if (rnode.spcNode == GLOBALTABLESPACE_OID)
#endif
{
/* Shared system relations live in {datadir}/global */
#if PG_VERSION_NUM >= 160000
snprintf(path, MAXPGPATH, "global/%u", rLocator.relNumber);
#else
snprintf(path, MAXPGPATH, "global/%u", rnode.relNode);
#endif
}
#if PG_VERSION_NUM >= 160000
else if (rLocator.spcOid == DEFAULTTABLESPACE_OID)
#else
else if (rnode.spcNode == DEFAULTTABLESPACE_OID)
#endif

{
/* The default tablespace is {datadir}/base */
snprintf(path, MAXPGPATH, "base/%u/%u",
rnode.dbNode, rnode.relNode);
#if PG_VERSION_NUM >= 160000
snprintf(path, MAXPGPATH, "base/%u/%u", rLocator.dbOid, rLocator.relNumber);
#else
snprintf(path, MAXPGPATH, "base/%u/%u", rnode.dbNode, rnode.relNode);
#endif
}
else
{
/* All other tablespaces are accessed via symlinks */
snprintf(path, MAXPGPATH, "pg_tblspc/%u/%u/%u",
rnode.spcNode, rnode.dbNode, rnode.relNode);
#if PG_VERSION_NUM >= 160000
snprintf(path, MAXPGPATH, "pg_tblspc/%u/%u/%u", rLocator.spcOid, rLocator.dbOid, rLocator.relNumber);
#else
snprintf(path, MAXPGPATH, "pg_tblspc/%u/%u/%u", rnode.spcNode, rnode.dbNode, rnode.relNode);
#endif
}

if (segno > 0)
Expand Down
16 changes: 15 additions & 1 deletion include/pg_loadstatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
#define LOADSTATUS_H

#include "storage/block.h"
#if PG_VERSION_NUM >= 160000
#include "storage/relfilelocator.h"
#else
#include "storage/relfilenode.h"
#endif

#ifndef MAXPGPATH
#define MAXPGPATH 1024
Expand All @@ -24,11 +28,17 @@
/* typical sector size is 512 byte */
#define BULKLOAD_LSF_BLCKSZ 512

#if PG_VERSION_NUM >= 160000
#define BULKLOAD_LSF_PATH(buffer, ls) \
snprintf((buffer), MAXPGPATH, \
BULKLOAD_LSF_DIR "/%d.%d.loadstatus", \
(ls)->ls.rLocator.dbOid, (ls)->ls.relid)
#else
#define BULKLOAD_LSF_PATH(buffer, ls) \
snprintf((buffer), MAXPGPATH, \
BULKLOAD_LSF_DIR "/%d.%d.loadstatus", \
(ls)->ls.rnode.dbNode, (ls)->ls.relid)

#endif
/**
* @brief Loading status information
*/
Expand All @@ -37,7 +47,11 @@ typedef union LoadStatus
struct
{
Oid relid; /**< target relation oid */
#if PG_VERSION_NUM >= 160000
RelFileLocator rLocator;
#else
RelFileNode rnode; /**< target relation node */
#endif
BlockNumber exist_cnt; /**< number of blocks already existing */
BlockNumber create_cnt; /**< number of blocks pg_bulkload creates */
} ls;
Expand Down
Loading

0 comments on commit 8734300

Please sign in to comment.