Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extensions parameter overrides #76

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ jobs:
mkdir -p ${package_dir}/usr/lib/postgresql/lib
mkdir -p ${package_dir}/var/lib/postgresql/extension
cp *.so ${package_dir}/usr/lib/postgresql/lib
cp *.control ${package_dir}/var/lib/postgresql/extension

# symlinks to Copy files into directory structure
mkdir -p ${package_dir}/usr/lib/postgresql/${{ matrix.postgres }}/lib
Expand All @@ -76,7 +75,6 @@ jobs:

mkdir -p ${package_dir}/usr/share/postgresql/${{ matrix.postgres }}/extension
cd ${package_dir}/usr/share/postgresql/${{ matrix.postgres }}/extension
cp -s ../../../../../var/lib/postgresql/extension/${{ matrix.extension_name }}.control .
cd ../../../../../..

# Create install control file
Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ else
endif

MODULE_big = supautils
OBJS = src/supautils.o src/privileged_extensions.o src/constrained_extensions.o src/utils.o
OBJS = src/supautils.o src/privileged_extensions.o src/constrained_extensions.o src/extensions_parameter_overrides.o src/utils.o

PG_VERSION = $(strip $(shell $(PG_CONFIG) --version | $(GREP) -oP '(?<=PostgreSQL )[0-9]+'))
SYSTEM = $(shell uname -s)

ifneq ($(SYSTEM), Linux)
Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,28 @@ DETAIL: required CPUs: 16
HINT: upgrade to an instance with higher resources
```

This feature is only available from PostgreSQL 13 onwards.
## Extensions Parameter Overrides

You can override `CREATE EXTENSION` parameters like so:

```
supautils.extensions_parameter_overrides = '{ "pg_cron": { "schema": "pg_catalog" } }'
```

Currently, only the `schema` parameter is supported.

These overrides will apply on `CREATE EXTENSION`, e.g.:

```sql
postgres=> create extension pg_cron schema public;
CREATE EXTENSION
postgres=> \dx pg_cron
List of installed extensions
Name | Version | Schema | Description
---------+---------+------------+------------------------------
pg_cron | 1.5 | pg_catalog | Job scheduler for PostgreSQL
(1 row)
```

## Development

Expand Down
32 changes: 32 additions & 0 deletions nix/pg_cron.nix
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest pg_cron nixpkg (1.6.2) breaks on PG14, so we pin the version on 1.5.2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pinned pg_cron breaks on pg 16 though. I'll change the test to use a contrib extension to avoid these issues.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{ lib, stdenv, fetchFromGitHub, postgresql }:

stdenv.mkDerivation rec {
pname = "pg_cron";
version = "1.5.2";

buildInputs = [ postgresql ];

src = fetchFromGitHub {
owner = "citusdata";
repo = pname;
rev = "v${version}";
hash = "sha256-+quVWbKJy6wXpL/zwTk5FF7sYwHA7I97WhWmPO/HSZ4=";
};

installPhase = ''
mkdir -p $out/{lib,share/postgresql/extension}

cp *.so $out/lib
cp *.sql $out/share/postgresql/extension
cp *.control $out/share/postgresql/extension
'';

meta = with lib; {
description = "Run Cron jobs through PostgreSQL";
homepage = "https://github.com/citusdata/pg_cron";
changelog = "https://github.com/citusdata/pg_cron/raw/v${version}/CHANGELOG.md";
maintainers = with maintainers; [ thoughtpolice ];
platforms = postgresql.meta.platforms;
license = licenses.postgresql;
};
}
8 changes: 5 additions & 3 deletions nix/withTmpDb.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ trap 'pg_ctl stop -m i && rm -rf "$tmpdir"' sigint sigterm exit

PGTZ=UTC initdb --no-locale --encoding=UTF8 --nosync -U "$PGUSER"

options="-F -c listen_addresses=\"\" -k $PGDATA -c shared_preload_libraries=\"pg_tle, supautils\" -c wal_level=logical"
options="-F -c listen_addresses=\"\" -k $PGDATA -c shared_preload_libraries=\"pg_cron, pg_tle, supautils\" -c wal_level=logical -c cron.database_name=postgres"

reserved_roles="supabase_storage_admin, anon, reserved_but_not_yet_created, authenticator*"
reserved_memberships="pg_read_server_files, pg_write_server_files, pg_execute_server_program, role_with_reserved_membership"
privileged_extensions="autoinc, citext, hstore, postgres_fdw, pg_tle"
privileged_extensions="autoinc, citext, hstore, pg_cron, pg_tle, postgres_fdw"
privileged_extensions_custom_scripts_path="$tmpdir/privileged_extensions_custom_scripts"
privileged_role="privileged_role"
privileged_role_allowed_configs="session_replication_role, pgrst.*, other.nested.*"
Expand All @@ -25,7 +25,9 @@ placeholder_stuff_options='-c supautils.placeholders="response.headers, another.

cexts_option='-c supautils.constrained_extensions="{\"adminpack\": { \"cpu\": 64}, \"cube\": { \"mem\": \"17 GB\"}, \"lo\": { \"disk\": \"20 GB\"}, \"amcheck\": { \"cpu\": 2, \"mem\": \"100 MB\", \"disk\": \"100 MB\"}}"'

pg_ctl start -o "$options" -o "$reserved_stuff_options" -o "$placeholder_stuff_options" -o "$cexts_option"
epos_option='-c supautils.extensions_parameter_overrides="{\"pg_cron\":{\"schema\":\"pg_catalog\"}}"'

pg_ctl start -o "$options" -o "$reserved_stuff_options" -o "$placeholder_stuff_options" -o "$cexts_option" -o "$epos_option"

# print notice when creating a TLE
mkdir -p "$tmpdir/privileged_extensions_custom_scripts"
Expand Down
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let
];
pgWithExt = { postgresql }: postgresql.withPackages (p: [
(callPackage ./nix/supautils.nix { inherit postgresql; extraMakeFlags = "TEST=1"; })
(callPackage ./nix/pg_cron.nix { inherit postgresql; })
(callPackage ./nix/pg_tle.nix { inherit postgresql; })
]);
pgScriptAll = map (x: callPackage ./nix/pgScript.nix { postgresql = pgWithExt { postgresql = x;}; }) supportedPgVersions;
Expand Down
134 changes: 134 additions & 0 deletions src/extensions_parameter_overrides.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include <postgres.h>

#include <common/jsonapi.h>
#include <miscadmin.h>
#include <tsearch/ts_locale.h>
#include <utils/builtins.h>
#include <utils/json.h>
#include <utils/jsonb.h>
#include <utils/jsonfuncs.h>
#include <utils/memutils.h>

#include "extensions_parameter_overrides.h"

static void json_array_start(void *state) {
json_extension_parameter_overrides_parse_state *parse = state;

parse->state = JEPO_UNEXPECTED_ARRAY;
parse->error_msg = "unexpected array";
}

static void json_object_start(void *state) {
json_extension_parameter_overrides_parse_state *parse = state;

switch (parse->state) {
case JEPO_EXPECT_TOPLEVEL_START:
parse->state = JEPO_EXPECT_TOPLEVEL_FIELD;
break;
case JEPO_EXPECT_SCHEMA:
parse->error_msg = "unexpected object for schema, expected a value";
parse->state = JEPO_UNEXPECTED_OBJECT;
break;
default:
break;
}
}

static void json_object_end(void *state) {
json_extension_parameter_overrides_parse_state *parse = state;

switch (parse->state) {
case JEPO_EXPECT_PARAMETER_OVERRIDES_START:
parse->state = JEPO_EXPECT_TOPLEVEL_FIELD;
(parse->total_epos)++;
break;
default:
break;
}
}

static void json_object_field_start(void *state, char *fname, bool isnull) {
json_extension_parameter_overrides_parse_state *parse = state;
extension_parameter_overrides *x = &parse->epos[parse->total_epos];

switch (parse->state) {
case JEPO_EXPECT_TOPLEVEL_FIELD:
x->name = MemoryContextStrdup(TopMemoryContext, fname);
parse->state = JEPO_EXPECT_PARAMETER_OVERRIDES_START;
break;

case JEPO_EXPECT_PARAMETER_OVERRIDES_START:
if (strcmp(fname, "schema") == 0)
parse->state = JEPO_EXPECT_SCHEMA;
else {
parse->state = JEPO_UNEXPECTED_FIELD;
parse->error_msg = "unexpected field, only schema is allowed";
}
break;

default:
break;
}
}

static void json_scalar(void *state, char *token, JsonTokenType tokentype) {
json_extension_parameter_overrides_parse_state *parse = state;
extension_parameter_overrides *x = &parse->epos[parse->total_epos];

switch (parse->state) {
case JEPO_EXPECT_SCHEMA:
if (tokentype == JSON_TOKEN_STRING) {
x->schema = MemoryContextStrdup(TopMemoryContext, token);
parse->state = JEPO_EXPECT_PARAMETER_OVERRIDES_START;
} else {
parse->state = JEPO_UNEXPECTED_SCHEMA_VALUE;
parse->error_msg = "unexpected schema value, expected a string";
}
break;

case JEPO_EXPECT_TOPLEVEL_START:
parse->state = JEPO_UNEXPECTED_SCALAR;
parse->error_msg = "unexpected scalar, expected an object";
break;

case JEPO_EXPECT_PARAMETER_OVERRIDES_START:
parse->state = JEPO_UNEXPECTED_SCALAR;
parse->error_msg = "unexpected scalar, expected an object";
break;

default:
break;
}
}

json_extension_parameter_overrides_parse_state
parse_extensions_parameter_overrides(const char *str,
extension_parameter_overrides *epos) {
JsonLexContext *lex;
JsonParseErrorType json_error;
JsonSemAction sem;

json_extension_parameter_overrides_parse_state state = {
JEPO_EXPECT_TOPLEVEL_START, NULL, 0, epos};

lex =
makeJsonLexContextCstringLen(pstrdup(str), strlen(str), PG_UTF8, true);

sem.semstate = &state;
sem.object_start = json_object_start;
sem.object_end = json_object_end;
sem.array_start = json_array_start;
sem.array_end = NULL;
sem.object_field_start = json_object_field_start;
sem.object_field_end = NULL;
sem.array_element_start = NULL;
sem.array_element_end = NULL;
sem.scalar = json_scalar;

json_error = pg_parse_json(lex, &sem);

if (json_error != JSON_SUCCESS)
state.error_msg = "invalid json";

return state;
}
34 changes: 34 additions & 0 deletions src/extensions_parameter_overrides.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef EXTENSIONS_PARAMETER_OVERRIDES_H
#define EXTENSIONS_PARAMETER_OVERRIDES_H

#include <postgres.h>

typedef struct {
char *name;
char *schema;
} extension_parameter_overrides;

typedef enum {
JEPO_EXPECT_TOPLEVEL_START,
JEPO_EXPECT_TOPLEVEL_FIELD,
JEPO_EXPECT_PARAMETER_OVERRIDES_START,
JEPO_EXPECT_SCHEMA,
JEPO_UNEXPECTED_FIELD,
JEPO_UNEXPECTED_ARRAY,
JEPO_UNEXPECTED_SCALAR,
JEPO_UNEXPECTED_OBJECT,
JEPO_UNEXPECTED_SCHEMA_VALUE
} json_extension_parameter_overrides_semantic_state;

typedef struct {
json_extension_parameter_overrides_semantic_state state;
char *error_msg;
int total_epos;
extension_parameter_overrides *epos;
} json_extension_parameter_overrides_parse_state;

extern json_extension_parameter_overrides_parse_state
parse_extensions_parameter_overrides(const char *str,
extension_parameter_overrides *epos);

#endif
45 changes: 42 additions & 3 deletions src/privileged_extensions.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <commands/defrem.h>
#include <executor/spi.h>
#include <miscadmin.h>
#include <nodes/makefuncs.h>
#include <nodes/pg_list.h>
#include <storage/fd.h>
#include <utils/acl.h>
Expand All @@ -16,6 +17,7 @@
#include <utils/snapmgr.h>
#include <utils/varlena.h>

#include "extensions_parameter_overrides.h"
#include "privileged_extensions.h"
#include "utils.h"

Expand Down Expand Up @@ -91,7 +93,8 @@ void handle_create_extension(
void (*process_utility_hook)(PROCESS_UTILITY_PARAMS),
PROCESS_UTILITY_PARAMS, const char *privileged_extensions,
const char *privileged_extensions_superuser,
const char *privileged_extensions_custom_scripts_path) {
const char *privileged_extensions_custom_scripts_path,
const extension_parameter_overrides *epos, const size_t total_epos) {
CreateExtensionStmt *stmt = (CreateExtensionStmt *)pstmt->utilityStmt;
char *filename = (char *)palloc(MAXPGPATH);

Expand Down Expand Up @@ -173,9 +176,45 @@ void handle_create_extension(
}
}

// Apply overrides.
for (size_t i = 0; i < total_epos; i++) {
if (strcmp(epos[i].name, stmt->extname) == 0) {
const extension_parameter_overrides *epo = &epos[i];
DefElem *schema_option = NULL;
DefElem *schema_override_option = NULL;
ListCell *option_cell;

if (epo->schema != NULL) {
Node *schema_node = (Node *)makeString(pstrdup(epo->schema));
schema_override_option = makeDefElem("schema", schema_node, -1);
}

foreach (option_cell, stmt->options) {
DefElem *defel = (DefElem *)lfirst(option_cell);

if (strcmp(defel->defname, "schema") == 0) {
if (schema_option != NULL) {
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("conflicting or redundant options")));
}
schema_option = defel;
}
}

if (schema_override_option != NULL) {
if (schema_option != NULL) {
stmt->options =
list_delete_ptr(stmt->options, schema_option);
}
stmt->options = lappend(stmt->options, schema_override_option);
}
}
}

// Run `CREATE EXTENSION`.
if (!superuser() && is_string_in_comma_delimited_string(
stmt->extname, privileged_extensions)) {
if (is_string_in_comma_delimited_string(stmt->extname,
privileged_extensions)) {
bool already_switched_to_superuser = false;
switch_to_superuser(privileged_extensions_superuser,
&already_switched_to_superuser);
Expand Down
13 changes: 7 additions & 6 deletions src/privileged_extensions.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#ifndef PRIVILEGED_EXTENSIONS_H
#define PRIVILEGED_EXTENSIONS_H

#include "extensions_parameter_overrides.h"
#include "utils.h"

extern void
handle_create_extension(void (*process_utility_hook)(PROCESS_UTILITY_PARAMS),
PROCESS_UTILITY_PARAMS,
const char *privileged_extensions,
const char *privileged_extensions_superuser,
const char *privileged_extensions_custom_scripts_path);
extern void handle_create_extension(
void (*process_utility_hook)(PROCESS_UTILITY_PARAMS),
PROCESS_UTILITY_PARAMS, const char *privileged_extensions,
const char *privileged_extensions_superuser,
const char *privileged_extensions_custom_scripts_path,
const extension_parameter_overrides *epos, const size_t total_epos);

extern void
handle_alter_extension(void (*process_utility_hook)(PROCESS_UTILITY_PARAMS),
Expand Down
Loading
Loading