ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
compatibility.h
Go to the documentation of this file.
1/**
2 * @file compatibility.h
3 * @brief PostgreSQL cross-version compatibility shims for ProvSQL.
4 *
5 * ProvSQL supports a range of PostgreSQL major versions. This header
6 * centralises the small API differences between those versions so that
7 * the rest of the codebase can call a single, stable interface.
8 *
9 * Currently handled differences:
10 * - **List API (13+)**: @c list_delete_cell() and @c lnext() gained or
11 * lost a @p prev argument between PostgreSQL 12 and 13. The
12 * @c my_list_delete_cell() and @c my_lnext() wrappers hide this.
13 * - **list_insert_nth() (< 13)**: In PostgreSQL 12 and earlier the
14 * list implementation was a linked list and this helper did not exist;
15 * @c compatibility.c provides a backport.
16 * - **Predefined function OIDs (< 14)**: The @c F_COUNT_ANY,
17 * @c F_COUNT_, and @c F_SUM_INT4 macros were introduced in PostgreSQL
18 * 14. Stable OID values for older releases are defined here.
19 */
20#ifndef COMPATIBILITY_H
21#define COMPATIBILITY_H
22
23#include "postgres.h"
24#include "nodes/pg_list.h"
25
26/**
27 * @brief Version-agnostic wrapper around @c list_delete_cell().
28 *
29 * PostgreSQL 13 changed @c list_delete_cell() to no longer require the
30 * previous cell pointer (because lists became arrays). This inline
31 * helper selects the correct call form at compile time.
32 *
33 * @param list The list to modify.
34 * @param cell The cell to delete.
35 * @param prev The cell immediately before @p cell (ignored on PG ≥ 13).
36 * @return The modified list.
37 */
38static inline List *my_list_delete_cell(List *list, ListCell *cell, ListCell *prev) {
39#if PG_VERSION_NUM >= 130000
40 return list_delete_cell(list, cell);
41#else
42 return list_delete_cell(list, cell, prev);
43#endif
44}
45
46/**
47 * @brief Version-agnostic wrapper around @c lnext().
48 *
49 * PostgreSQL 13 added the list pointer parameter to @c lnext() to
50 * support the array-based list implementation. This inline helper
51 * selects the correct call form at compile time.
52 *
53 * @param l The list (ignored on PG < 13).
54 * @param c The current cell.
55 * @return The next cell, or @c NULL if @p c is the last element.
56 */
57static inline ListCell *my_lnext(const List *l, const ListCell *c)
58{
59#if PG_VERSION_NUM >= 130000
60 return lnext(l, c);
61#else
62 return lnext(c);
63#endif
64}
65
66#if PG_VERSION_NUM < 130000
67/**
68 * @brief Insert @p datum at position @p pos in @p list (PG < 13 backport).
69 *
70 * PostgreSQL 13 introduced @c list_insert_nth() when lists were
71 * reimplemented as arrays. This declaration provides the same function
72 * for older PostgreSQL versions; the implementation lives in
73 * @c compatibility.c.
74 *
75 * @param list The list to insert into (may be @c NIL).
76 * @param pos Zero-based index at which to insert the new element.
77 * @param datum The value to insert.
78 * @return The (possibly reallocated) list.
79 */
80List *list_insert_nth(List *list, int pos, void *datum);
81#endif
82
83#if PG_VERSION_NUM < 140000
84/** @brief OID of @c count(*) / @c count(any) aggregate function (pre-PG 14). */
85#define F_COUNT_ANY 2147
86/** @brief OID of @c count() aggregate function (pre-PG 14). */
87#define F_COUNT_ 2803
88/** @brief OID of @c sum(int4) aggregate function (pre-PG 14). */
89#define F_SUM_INT4 2108
90#endif
91
92#endif /* COMPATIBILITY_H */
static List * my_list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Version-agnostic wrapper around list_delete_cell().
static ListCell * my_lnext(const List *l, const ListCell *c)
Version-agnostic wrapper around lnext().
List * list_insert_nth(List *list, int pos, void *datum)
Insert datum at position pos in list (PG < 13 backport).