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 * - **list_make5() (13)**: existed pre-13, dropped by the PG13 list
20 * rewrite, re-added in PG14; backported for the PG13 gap as a macro
21 * over @c list_make4() + @c lappend().
22 */
23#ifndef COMPATIBILITY_H
24#define COMPATIBILITY_H
25
26#include "postgres.h"
27#include "nodes/pg_list.h"
28
29/**
30 * @brief Version-agnostic wrapper around @c list_delete_cell().
31 *
32 * PostgreSQL 13 changed @c list_delete_cell() to no longer require the
33 * previous cell pointer (because lists became arrays). This inline
34 * helper selects the correct call form at compile time.
35 *
36 * @param list The list to modify.
37 * @param cell The cell to delete.
38 * @param prev The cell immediately before @p cell (ignored on PG ≥ 13).
39 * @return The modified list.
40 */
41static inline List *my_list_delete_cell(List *list, ListCell *cell, ListCell *prev) {
42#if PG_VERSION_NUM >= 130000
43 return list_delete_cell(list, cell);
44#else
45 return list_delete_cell(list, cell, prev);
46#endif
47}
48
49/**
50 * @brief Version-agnostic wrapper around @c lnext().
51 *
52 * PostgreSQL 13 added the list pointer parameter to @c lnext() to
53 * support the array-based list implementation. This inline helper
54 * selects the correct call form at compile time.
55 *
56 * @param l The list (ignored on PG < 13).
57 * @param c The current cell.
58 * @return The next cell, or @c NULL if @p c is the last element.
59 */
60static inline ListCell *my_lnext(const List *l, const ListCell *c)
61{
62#if PG_VERSION_NUM >= 130000
63 return lnext(l, c);
64#else
65 return lnext(c);
66#endif
67}
68
69#if PG_VERSION_NUM < 130000
70/**
71 * @brief Insert @p datum at position @p pos in @p list (PG < 13 backport).
72 *
73 * PostgreSQL 13 introduced @c list_insert_nth() when lists were
74 * reimplemented as arrays. This declaration provides the same function
75 * for older PostgreSQL versions; the implementation lives in
76 * @c compatibility.c.
77 *
78 * @param list The list to insert into (may be @c NIL).
79 * @param pos Zero-based index at which to insert the new element.
80 * @param datum The value to insert.
81 * @return The (possibly reallocated) list.
82 */
83List *list_insert_nth(List *list, int pos, void *datum);
84#endif
85
86#if PG_VERSION_NUM < 140000
87/** @brief OID of @c count(*) / @c count(any) aggregate function (pre-PG 14). */
88#define F_COUNT_ANY 2147
89/** @brief OID of @c count() aggregate function (pre-PG 14). */
90#define F_COUNT_ 2803
91/** @brief OID of @c sum(int4) aggregate function (pre-PG 14). */
92#define F_SUM_INT4 2108
93#endif
94
95#if PG_VERSION_NUM >= 130000 && PG_VERSION_NUM < 140000
96/** @brief Five-element list constructor. The linked-list implementation
97 * (pre-PG 13) had it as a macro; the PG13 array rewrite dropped it; PG14
98 * brought it back. Backport it for the PG13 gap only. */
99#define list_make5(x1, x2, x3, x4, x5) lappend(list_make4(x1, x2, x3, x4), x5)
100#endif
101
102#if PG_VERSION_NUM < 130000
103/** @brief Alignment codes for the array routines (@c construct_array /
104 * @c deconstruct_array). The @c TYPALIGN_* macros (in @c catalog/pg_type.h)
105 * were introduced in PostgreSQL 13; on PG 10-12 the alignment is passed as
106 * the older @c 'i' / @c 'c' characters. */
107#define TYPALIGN_INT 'i'
108#define TYPALIGN_CHAR 'c'
109#endif
110
111#if PG_VERSION_NUM < 140000
112/** @brief OID of the @c array_agg(anynonarray) aggregate (pre-PG 14).
113 * The @c F_ARRAY_AGG_ANYNONARRAY macro only exists since PostgreSQL 14,
114 * when @c fmgroids.h gained overload-disambiguated names for aggregates;
115 * before that, aggregates had no @c fmgroids.h entry at all. Same
116 * stable OID. */
117#define F_ARRAY_AGG_ANYNONARRAY 2335
118#endif
119
120#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).