Skip to content

Commit 970bb21

Browse files
committed
Merge branch 'wip-pg-extension' of https://github.com/sqliteai/sqlite-sync into wip-pg-extension
2 parents 6260dbc + 0656307 commit 970bb21

3 files changed

Lines changed: 59 additions & 11 deletions

File tree

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@
1515
"description": "Enable pretty-printing for gdb",
1616
"text": "-enable-pretty-printing",
1717
"ignoreFailures": true
18+
},
19+
{
20+
"description": "Add PostgreSQL source dir",
21+
"text": "dir /usr/src/postgresql-16/src",
22+
"ignoreFailures": true
23+
},
24+
{
25+
"description": "Map Postgres build paths to source",
26+
"text": "set substitute-path /build/src /usr/src/postgresql-16/src",
27+
"ignoreFailures": true
28+
},
29+
{
30+
"description": "Map Postgres build paths (relative) to source",
31+
"text": "set substitute-path ./build/src /usr/src/postgresql-16/src",
32+
"ignoreFailures": true
1833
}
1934
]
2035
}

docker/postgresql/Dockerfile.debug

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
# PostgreSQL Docker image with CloudSync extension (debug build)
22
FROM postgres:16
33

4-
# Install build dependencies
4+
# Install build dependencies and debug symbols
55
RUN apt-get update && apt-get install -y \
6+
ca-certificates \
7+
gnupg \
8+
wget \
9+
&& . /etc/os-release \
10+
&& echo "deb http://apt.postgresql.org/pub/repos/apt ${VERSION_CODENAME}-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
11+
&& echo "deb-src http://apt.postgresql.org/pub/repos/apt ${VERSION_CODENAME}-pgdg main" > /etc/apt/sources.list.d/pgdg-src.list \
12+
&& wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg \
13+
&& echo "deb http://deb.debian.org/debian-debug ${VERSION_CODENAME}-debug main" > /etc/apt/sources.list.d/debian-debug.list \
14+
&& echo "deb-src http://deb.debian.org/debian ${VERSION_CODENAME} main" > /etc/apt/sources.list.d/debian-src.list \
15+
&& apt-get update && apt-get install -y \
616
build-essential \
17+
dpkg-dev \
718
gdb \
819
postgresql-server-dev-16 \
20+
postgresql-16-dbgsym \
921
git \
1022
make \
23+
&& apt-get source postgresql-16 \
24+
&& mkdir -p /usr/src/postgresql-16 \
25+
&& srcdir="$(find . -maxdepth 1 -type d -name 'postgresql-16*' | head -n 1)" \
26+
&& if [ -n "$srcdir" ]; then cp -a "$srcdir"/. /usr/src/postgresql-16/; fi \
1127
&& rm -rf /var/lib/apt/lists/*
1228

1329
# Create directory for extension source

src/postgresql/cloudsync_postgresql.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,40 @@ void _PG_init(void) {
7272
// Extension initialization
7373
// SPI will be connected per-function call
7474
elog(DEBUG1, "CloudSync extension loading");
75-
75+
7676
// Initialize memory debugger (NOOP in production)
7777
cloudsync_memory_init(1);
78-
78+
7979
// load config, if exists
8080
cloudsync_context *ctx = get_cloudsync_context();
81-
if (cloudsync_config_exists(NULL)) {
82-
if (cloudsync_context_init(ctx) == NULL) {
83-
ereport(ERROR,
81+
82+
int spi_rc = SPI_connect();
83+
if (spi_rc != SPI_OK_CONNECT) {
84+
ereport(ERROR,
8485
(errcode(ERRCODE_INTERNAL_ERROR),
85-
errmsg("An error occurred while trying to initialize context")));
86-
86+
errmsg("SPI_connect failed: %d", spi_rc)));
87+
}
88+
89+
PG_TRY();
90+
{
91+
if (cloudsync_config_exists(ctx)) {
92+
if (cloudsync_context_init(ctx) == NULL) {
93+
ereport(ERROR,
94+
(errcode(ERRCODE_INTERNAL_ERROR),
95+
errmsg("An error occurred while trying to initialize context")));
96+
}
97+
98+
// make sure to update internal version to current version
99+
dbutils_settings_set_key_value(ctx, CLOUDSYNC_KEY_LIBVERSION, CLOUDSYNC_VERSION);
87100
}
88-
89-
// make sure to update internal version to current version
90-
dbutils_settings_set_key_value(ctx, CLOUDSYNC_KEY_LIBVERSION, CLOUDSYNC_VERSION);
101+
SPI_finish();
102+
}
103+
PG_CATCH();
104+
{
105+
SPI_finish();
106+
PG_RE_THROW();
91107
}
108+
PG_END_TRY();
92109
}
93110

94111
void _PG_fini(void) {

0 commit comments

Comments
 (0)