1 /*-------------------------------------------------------------------------
2  *
3  * version_compat.h
4  *	  Compatibility macros for writing code agnostic to PostgreSQL versions
5  *
6  * Copyright (c) Citus Data, Inc.
7  *
8  *-------------------------------------------------------------------------
9  */
10 
11 #ifndef VERSION_COMPAT_H
12 #define VERSION_COMPAT_H
13 
14 #include "postgres.h"
15 
16 #include "distributed/pg_version_constants.h"
17 
18 #include "access/sdir.h"
19 #include "access/heapam.h"
20 #include "commands/explain.h"
21 #include "catalog/namespace.h"
22 #include "distributed/citus_ruleutils.h"
23 #include "distributed/citus_safe_lib.h"
24 #include "executor/tuptable.h"
25 #include "nodes/parsenodes.h"
26 #include "parser/parse_func.h"
27 #include "optimizer/optimizer.h"
28 
29 #if (PG_VERSION_NUM >= PG_VERSION_13)
30 #include "tcop/tcopprot.h"
31 #endif
32 
33 #if PG_VERSION_NUM >= PG_VERSION_14
34 #define AlterTableStmtObjType_compat(a) ((a)->objtype)
35 #define getObjectTypeDescription_compat(a, b) getObjectTypeDescription(a, b)
36 #define getObjectIdentity_compat(a, b) getObjectIdentity(a, b)
37 
38 /* for MemoryContextMethods->stats */
39 #define stats_compat(a, b, c, d, e) stats(a, b, c, d, e)
40 #define FuncnameGetCandidates_compat(a, b, c, d, e, f, g) \
41 	FuncnameGetCandidates(a, b, c, d, e, f, g)
42 #define expand_function_arguments_compat(a, b, c, d) expand_function_arguments(a, b, c, d)
43 #define BeginCopyFrom_compat(a, b, c, d, e, f, g, h) BeginCopyFrom(a, b, c, d, e, f, g, h)
44 #define standard_ProcessUtility_compat(a, b, c, d, e, f, g, h) \
45 	standard_ProcessUtility(a, b, c, d, e, f, g, h)
46 #define ProcessUtility_compat(a, b, c, d, e, f, g, h) \
47 	ProcessUtility(a, b, c, d, e, f, g, h)
48 #define SetTuplestoreDestReceiverParams_compat(a, b, c, d, e, f) \
49 	SetTuplestoreDestReceiverParams(a, b, c, d, e, f)
50 #define pgproc_statusflags_compat(pgproc) ((pgproc)->statusFlags)
51 #define get_partition_parent_compat(a, b) get_partition_parent(a, b)
52 #define RelationGetPartitionDesc_compat(a, b) RelationGetPartitionDesc(a, b)
53 #define make_simple_restrictinfo_compat(a, b) make_simple_restrictinfo(a, b)
54 #define pull_varnos_compat(a, b) pull_varnos(a, b)
55 #define pg_get_statisticsobj_worker_compat(a, b, c) pg_get_statisticsobj_worker(a, b, c)
56 #else
57 #define AlterTableStmtObjType_compat(a) ((a)->relkind)
58 #define F_NEXTVAL F_NEXTVAL_OID
59 #define ROLE_PG_MONITOR DEFAULT_ROLE_MONITOR
60 #define PROC_WAIT_STATUS_WAITING STATUS_WAITING
61 #define getObjectTypeDescription_compat(a, b) getObjectTypeDescription(a)
62 #define getObjectIdentity_compat(a, b) getObjectIdentity(a)
63 
64 /* for MemoryContextMethods->stats */
65 #define stats_compat(a, b, c, d, e) stats(a, b, c, d)
66 #define FuncnameGetCandidates_compat(a, b, c, d, e, f, g) \
67 	FuncnameGetCandidates(a, b, c, d, e, g)
68 #define expand_function_arguments_compat(a, b, c, d) expand_function_arguments(a, c, d)
69 #define VacOptValue VacOptTernaryValue
70 #define VACOPTVALUE_UNSPECIFIED VACOPT_TERNARY_DEFAULT
71 #define VACOPTVALUE_DISABLED VACOPT_TERNARY_DISABLED
72 #define VACOPTVALUE_ENABLED VACOPT_TERNARY_ENABLED
73 #define CopyFromState CopyState
74 #define BeginCopyFrom_compat(a, b, c, d, e, f, g, h) BeginCopyFrom(a, b, d, e, f, g, h)
75 #define standard_ProcessUtility_compat(a, b, c, d, e, f, g, h) \
76 	standard_ProcessUtility(a, b, d, e, f, g, h)
77 #define ProcessUtility_compat(a, b, c, d, e, f, g, h) ProcessUtility(a, b, d, e, f, g, h)
78 #define COPY_FRONTEND COPY_NEW_FE
79 #define SetTuplestoreDestReceiverParams_compat(a, b, c, d, e, f) \
80 	SetTuplestoreDestReceiverParams(a, b, c, d)
81 #define pgproc_statusflags_compat(pgproc) \
82 	((&ProcGlobal->allPgXact[(pgproc)->pgprocno])->vacuumFlags)
83 #define get_partition_parent_compat(a, b) get_partition_parent(a)
84 #define RelationGetPartitionDesc_compat(a, b) RelationGetPartitionDesc(a)
85 #define PQ_LARGE_MESSAGE_LIMIT 0
86 #define make_simple_restrictinfo_compat(a, b) make_simple_restrictinfo(b)
87 #define pull_varnos_compat(a, b) pull_varnos(b)
88 #define pg_get_statisticsobj_worker_compat(a, b, c) pg_get_statisticsobj_worker(a, c)
89 #endif
90 
91 #if PG_VERSION_NUM >= PG_VERSION_13
92 #define lnext_compat(l, r) lnext(l, r)
93 #define list_delete_cell_compat(l, c, p) list_delete_cell(l, c)
94 #define pg_plan_query_compat(p, q, c, b) pg_plan_query(p, q, c, b)
95 #define planner_compat(p, c, b) planner(p, NULL, c, b)
96 #define standard_planner_compat(a, c, d) standard_planner(a, NULL, c, d)
97 #define GetSequencesOwnedByRelation(a) getOwnedSequences(a)
98 #define GetSequencesOwnedByColumn(a, b) getOwnedSequences_internal(a, b, 0)
99 #define CMDTAG_SELECT_COMPAT CMDTAG_SELECT
100 #define ExplainOnePlanCompat(a, b, c, d, e, f, g, h) \
101 	ExplainOnePlan(a, b, c, d, e, f, g, h)
102 #define SetListCellPtr(a, b) ((a)->ptr_value = (b))
103 #define RangeTableEntryFromNSItem(a) ((a)->p_rte)
104 #define QueryCompletionCompat QueryCompletion
105 #else /* pre PG13 */
106 #define lnext_compat(l, r) lnext(r)
107 #define list_delete_cell_compat(l, c, p) list_delete_cell(l, c, p)
108 #define pg_plan_query_compat(p, q, c, b) pg_plan_query(p, c, b)
109 #define planner_compat(p, c, b) planner(p, c, b)
110 #define standard_planner_compat(a, c, d) standard_planner(a, c, d)
111 #define CMDTAG_SELECT_COMPAT "SELECT"
112 #define GetSequencesOwnedByRelation(a) getOwnedSequences(a, InvalidAttrNumber)
113 #define GetSequencesOwnedByColumn(a, b) getOwnedSequences(a, b)
114 #define ExplainOnePlanCompat(a, b, c, d, e, f, g, h) ExplainOnePlan(a, b, c, d, e, f, g)
115 #define SetListCellPtr(a, b) ((a)->data.ptr_value = (b))
116 #define RangeTableEntryFromNSItem(a) (a)
117 #define QueryCompletionCompat char
118 #define varattnosyn varoattno
119 #define varnosyn varnoold
120 #endif
121 #if PG_VERSION_NUM >= PG_VERSION_12
122 
123 #define CreateTableSlotForRel(rel) table_slot_create(rel, NULL)
124 #define MakeSingleTupleTableSlotCompat MakeSingleTupleTableSlot
125 #define AllocSetContextCreateExtended AllocSetContextCreateInternal
126 #define NextCopyFromCompat NextCopyFrom
127 #define ArrayRef SubscriptingRef
128 #define T_ArrayRef T_SubscriptingRef
129 #define or_clause is_orclause
130 #define GetSysCacheOid1Compat GetSysCacheOid1
131 #define GetSysCacheOid2Compat GetSysCacheOid2
132 #define GetSysCacheOid3Compat GetSysCacheOid3
133 #define GetSysCacheOid4Compat GetSysCacheOid4
134 
135 #define fcGetArgValue(fc, n) ((fc)->args[n].value)
136 #define fcGetArgNull(fc, n) ((fc)->args[n].isnull)
137 #define fcSetArgExt(fc, n, val, is_null) \
138 	(((fc)->args[n].isnull = (is_null)), ((fc)->args[n].value = (val)))
139 
140 typedef struct
141 {
142 	File fd;
143 	off_t offset;
144 } FileCompat;
145 
146 static inline int
FileWriteCompat(FileCompat * file,char * buffer,int amount,uint32 wait_event_info)147 FileWriteCompat(FileCompat *file, char *buffer, int amount, uint32 wait_event_info)
148 {
149 	int count = FileWrite(file->fd, buffer, amount, file->offset, wait_event_info);
150 	if (count > 0)
151 	{
152 		file->offset += count;
153 	}
154 	return count;
155 }
156 
157 
158 static inline int
FileReadCompat(FileCompat * file,char * buffer,int amount,uint32 wait_event_info)159 FileReadCompat(FileCompat *file, char *buffer, int amount, uint32 wait_event_info)
160 {
161 	int count = FileRead(file->fd, buffer, amount, file->offset, wait_event_info);
162 	if (count > 0)
163 	{
164 		file->offset += count;
165 	}
166 	return count;
167 }
168 
169 
170 static inline FileCompat
FileCompatFromFileStart(File fileDesc)171 FileCompatFromFileStart(File fileDesc)
172 {
173 	FileCompat fc;
174 
175 	/* ensure uninitialized padding doesn't escape the function */
176 	memset_struct_0(fc);
177 	fc.fd = fileDesc;
178 	fc.offset = 0;
179 
180 	return fc;
181 }
182 
183 
184 #endif /* PG12 */
185 
186 #define fcSetArg(fc, n, value) fcSetArgExt(fc, n, value, false)
187 #define fcSetArgNull(fc, n) fcSetArgExt(fc, n, (Datum) 0, true)
188 
189 #endif   /* VERSION_COMPAT_H */
190