Skip to content

Commit 775684e

Browse files
authored
Merge pull request #2597 from sebix/jsonb
PostgreSQL: use JSONB for extra column
2 parents 806d81e + cab6de5 commit 775684e

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Please refer to the [NEWS](NEWS.md) for a list of changes which have an affect o
4141
- `.github/workflows/codespell.yml`, `debian-package.yml`, `regexploit.yml`: Upgrade to `ubuntu-latest` runners (PR#2602 by Sebastian Wagner).
4242

4343
### Tools
44+
- `intelmq.bin.intelmq_psql_initdb`: Use `JSONB` type by default, Postgres supports it since version 9 (PR#2597 by Sebastian Wagner).
4445

4546
### Contrib
4647

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ Please refer to the change log for a full list of changes.
2424
### Libraries
2525

2626
### Postgres databases
27+
To switch to the more efficient data type `jsonb` instead of `json`, use the following SQL statement. Data is preserved. JSONB also has more query and data manipulation features than plain JSON.
28+
```sql
29+
ALTER TABLE events
30+
ALTER COLUMN "extra" SET DATA TYPE jsonb;
31+
```
2732

2833

2934
3.4.0 Feature release (2025-03-14)

intelmq/bin/intelmq_psql_initdb.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2015 Sebastian Wagner, 2023 CERT.at GmbH
1+
# SPDX-FileCopyrightText: 2015-2021 nic.at GmbH, 2022 Sebastian Wagner, 2023 CERT.at GmbH, 2025 Institute for Common Good Technology
22
#
33
# SPDX-License-Identifier: AGPL-3.0-or-later
44

@@ -133,7 +133,7 @@ def _generate_separated_raws_schema(fields: dict, partition_key: str) -> list:
133133

134134
def generate(harmonization_file=HARMONIZATION_CONF_FILE, skip_events=False,
135135
separate_raws=False, partition_key=None, skip_or_replace=False,
136-
use_jsonb=False):
136+
no_jsonb=False):
137137
FIELDS = {}
138138
sql_lines = []
139139

@@ -171,7 +171,7 @@ def generate(harmonization_file=HARMONIZATION_CONF_FILE, skip_events=False,
171171
elif value['type'] == 'UUID':
172172
dbtype = 'UUID'
173173
elif value['type'] in ('JSON', 'JSONDict'):
174-
dbtype = 'jsonb' if use_jsonb else 'json'
174+
dbtype = 'json' if no_jsonb else 'jsonb'
175175
else:
176176
raise ValueError('Unknown type %r.' % value['type'])
177177

@@ -213,8 +213,8 @@ def main():
213213
help="Path to the harmonization file")
214214
parser.add_argument("--skip-or-replace", default=False, action="store_true",
215215
help="Add IF NOT EXISTS or REPLACE directive to created schemas")
216-
parser.add_argument("--jsonb", default=False, action="store_true",
217-
help="Use JSONB type to represent dictionary fields")
216+
parser.add_argument("--no-jsonb", action="store_true",
217+
help="Do not use JSONB but JSON type to represent dictionary fields")
218218
args = parser.parse_args()
219219

220220
OUTPUTFILE = args.outputfile
@@ -232,7 +232,7 @@ def main():
232232
separate_raws=args.separate_raws,
233233
partition_key=args.partition_key,
234234
skip_or_replace=args.skip_or_replace,
235-
use_jsonb=args.jsonb,
235+
no_jsonb=args.no_jsonb,
236236
)
237237
print("INFO - Writing %s file" % OUTPUTFILE)
238238
fp.write(psql)

intelmq/tests/bin/initdb.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ CREATE TABLE events (
3232
"event_description.text" text,
3333
"event_description.url" text,
3434
"event_hash" varchar(40),
35-
"extra" json,
35+
"extra" jsonb,
3636
"feed.accuracy" real,
3737
"feed.code" varchar(100),
3838
"feed.documentation" text,
@@ -46,7 +46,7 @@ CREATE TABLE events (
4646
"malware.version" text,
4747
"misp.attribute_uuid" varchar(36),
4848
"misp.event_uuid" varchar(36),
49-
"output" json,
49+
"output" jsonb,
5050
"protocol.application" varchar(100),
5151
"protocol.transport" varchar(11),
5252
"raw" text,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
SPDX-FileCopyrightText: 2016 Sebastian Wagner
1+
SPDX-FileCopyrightText: 2016 nic.at GmbH
22
SPDX-License-Identifier: AGPL-3.0-or-later

0 commit comments

Comments
 (0)